reCoreD-UI/server/handlers_records.go

129 lines
2.4 KiB
Go
Raw Normal View History

2024-04-03 09:05:12 +00:00
package server
2024-04-03 14:37:15 +00:00
import (
2024-04-09 08:28:18 +00:00
"fmt"
2024-04-03 14:37:15 +00:00
"net/http"
2024-04-09 08:28:18 +00:00
"reCoreD-UI/controllers"
2024-04-03 14:37:15 +00:00
"reCoreD-UI/models"
"github.com/gin-gonic/gin"
)
2024-04-09 13:16:19 +00:00
func getRecords(c *gin.Context) {
query := models.Record[models.RecordContentDefault]{}
2024-04-03 14:37:15 +00:00
if err := c.BindQuery(&query); err != nil {
c.JSON(http.StatusBadRequest, Response{
Succeed: false,
Message: err.Error(),
})
return
}
domain := c.Param("domain")
2024-04-09 08:28:18 +00:00
query.Zone = fmt.Sprintf("%s.", domain)
2024-04-03 14:37:15 +00:00
2024-04-09 13:16:19 +00:00
records, err := controllers.GetRecords(&query)
2024-04-03 14:37:15 +00:00
if err != nil {
errorHandler(c, err)
return
}
c.JSON(http.StatusOK, Response{
Succeed: true,
Data: records,
})
}
2024-04-09 13:16:19 +00:00
func createRecord(c *gin.Context) {
record := &models.Record[models.RecordContentDefault]{}
2024-04-03 14:37:15 +00:00
if err := c.BindJSON(record); err != nil {
c.JSON(http.StatusBadRequest, Response{
Succeed: false,
Message: err.Error(),
})
return
}
domain := c.Param("domain")
2024-04-09 13:16:19 +00:00
if domain != record.WithOutDotTail() {
2024-04-03 14:37:15 +00:00
c.JSON(http.StatusBadRequest, Response{
Succeed: false,
Message: "request body doesn't match URI",
})
return
}
2024-04-09 13:16:19 +00:00
irecord, err := controllers.CreateRecord(record)
2024-04-09 08:28:18 +00:00
if err != nil {
2024-04-03 14:37:15 +00:00
errorHandler(c, err)
return
}
c.JSON(http.StatusCreated, Response{
Succeed: true,
2024-04-09 13:16:19 +00:00
Data: irecord,
2024-04-03 14:37:15 +00:00
})
}
2024-04-09 13:16:19 +00:00
func createRecords(c *gin.Context) {
var records []models.IRecord
2024-04-03 14:37:15 +00:00
if err := c.BindJSON(&records); err != nil {
c.JSON(http.StatusBadRequest, Response{
Succeed: false,
Message: err.Error(),
})
return
}
2024-04-09 08:28:18 +00:00
if err := controllers.CreateRecords(records); err != nil {
2024-04-03 14:37:15 +00:00
errorHandler(c, err)
return
}
c.JSON(http.StatusCreated, Response{
Succeed: true,
})
}
2024-04-09 13:16:19 +00:00
func updateRecord(c *gin.Context) {
record := &models.Record[models.RecordContentDefault]{}
2024-04-03 14:37:15 +00:00
if err := c.BindJSON(record); err != nil {
c.JSON(http.StatusBadRequest, Response{
Succeed: false,
Message: err.Error(),
})
return
}
domain := c.Param("domain")
if domain != record.Zone {
c.JSON(http.StatusBadRequest, Response{
Succeed: false,
Message: "request body doesn't match URI",
})
return
}
2024-04-09 08:28:18 +00:00
if err := controllers.UpdateRecord(record); err != nil {
2024-04-03 14:37:15 +00:00
errorHandler(c, err)
return
}
c.JSON(http.StatusOK, Response{
Succeed: true,
})
}
2024-04-09 13:16:19 +00:00
func deleteRecord(c *gin.Context) {
2024-04-03 14:37:15 +00:00
domain := c.Param("domain")
id := c.Param("id")
2024-04-09 08:28:18 +00:00
if err := controllers.DeleteRecord(domain, id); err != nil {
2024-04-03 14:37:15 +00:00
errorHandler(c, err)
return
}
c.JSON(http.StatusNoContent, Response{
Succeed: true,
})
}