reCoreD-UI/server/handlers_domains.go

130 lines
2.8 KiB
Go
Raw Normal View History

2024-04-03 09:05:12 +00:00
package server
import (
"net/http"
2024-04-09 08:28:18 +00:00
"reCoreD-UI/controllers"
2024-04-03 09:05:12 +00:00
"reCoreD-UI/models"
"github.com/gin-gonic/gin"
2024-04-19 04:47:00 +00:00
_ "reCoreD-UI/docs"
2024-04-03 09:05:12 +00:00
)
2024-04-19 04:47:00 +00:00
// GetDomains godoc
//
// @Router /domains/ [get]
// @Summary List all domains
// @Description List all domains
// @Tags domains
// @Accept json
// @Product json
// @Success 200 {object} Response{data=[]models.Domain}
// @Failure 401 {object} Response{data=nil}
// @Failure 500 {object} Response{data=nil}
2024-04-09 13:16:19 +00:00
func getDomains(c *gin.Context) {
2024-04-09 08:28:18 +00:00
domains, err := controllers.GetDomains("")
2024-04-03 09:05:12 +00:00
if err != nil {
errorHandler(c, err)
return
}
c.JSON(http.StatusOK, Response{
Succeed: true,
Data: domains,
})
}
2024-04-19 04:47:00 +00:00
// CreateDomain godoc
//
// @Router /domains/ [post]
// @Summary Create a domain
// @Description Create a domain
// @Tags domains
// @Product json
// @Param object body models.Domain true "content"
// @Success 201 {object} Response{data=models.Domain}
// @Failure 400 {object} Response{data=nil}
// @Failure 401 {object} Response{data=nil}
// @Failure 500 {object} Response{data=nil}
2024-04-09 13:16:19 +00:00
func createDomain(c *gin.Context) {
2024-04-03 09:05:12 +00:00
domain := &models.Domain{}
if err := c.BindJSON(domain); err != nil {
c.JSON(http.StatusBadRequest, Response{
Succeed: false,
Message: err.Error(),
})
return
}
2024-04-09 08:28:18 +00:00
domain, err := controllers.CreateDomain(domain)
2024-04-07 05:08:30 +00:00
if err != nil {
2024-04-03 09:05:12 +00:00
errorHandler(c, err)
return
}
c.JSON(http.StatusCreated, Response{
Succeed: true,
2024-04-07 05:08:30 +00:00
Data: domain,
2024-04-03 09:05:12 +00:00
})
}
2024-04-19 04:47:00 +00:00
// UpdateDomain godoc
//
// @Router /domains/ [put]
// @Summary Update a domain
// @Description Update a domain
// @Tags domains
// @Accept json
// @Product json
// @Param object body models.Domain true "content"
// @Success 200 {object} Response{data=models.Domain}
// @Failure 400 {object} Response{data=nil}
// @Failure 401 {object} Response{data=nil}
// @Failure 404 {object} Response{data=nil}
// @Failure 500 {object} Response{data=nil}
2024-04-09 13:16:19 +00:00
func updateDomain(c *gin.Context) {
2024-04-03 09:05:12 +00:00
domain := &models.Domain{}
if err := c.BindJSON(domain); err != nil {
c.JSON(http.StatusBadRequest, Response{
Succeed: false,
Message: err.Error(),
})
return
}
2024-04-09 08:28:18 +00:00
if err := controllers.UpdateDomain(domain); err != nil {
2024-04-03 09:05:12 +00:00
errorHandler(c, err)
return
}
c.JSON(http.StatusOK, Response{
Succeed: true,
})
}
2024-04-19 04:47:00 +00:00
// DeleteDomain godoc
//
// @Router /domains/{id} [delete]
// @Summary Delete a domain
// @Description Delete a domain
// @Tags domains
// @Product json
// @Param id path int true "Domain ID"
// @Success 204 {object} Response{data=nil}
// @Failure 401 {object} Response{data=nil}
// @Failure 404 {object} Response{data=nil}
// @Failure 500 {object} Response{data=nil}
2024-04-09 13:16:19 +00:00
func deleteDomain(c *gin.Context) {
2024-04-03 09:05:12 +00:00
id := c.Param("id")
2024-04-09 08:28:18 +00:00
if err := controllers.DeleteDomain(id); err != nil {
2024-04-03 09:05:12 +00:00
errorHandler(c, err)
return
}
c.JSON(http.StatusNoContent, Response{
Succeed: true,
})
}