117 lines
2.6 KiB
Go
117 lines
2.6 KiB
Go
package controller
|
|
|
|
import (
|
|
"net/http"
|
|
"users_management/m/middleware"
|
|
"users_management/m/model/dto/req"
|
|
"users_management/m/usecase"
|
|
"users_management/m/utils/common"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type FishboneController struct {
|
|
fu usecase.FishboneUseCase
|
|
rg *gin.RouterGroup
|
|
}
|
|
|
|
func (fc *FishboneController) Route() {
|
|
rg := fc.rg.Group("/fishbone")
|
|
rg.Use(middleware.AuthMiddleware())
|
|
rg.Use(middleware.RateLimitMiddleware())
|
|
{
|
|
rg.GET("", fc.GetFishbone())
|
|
rg.POST("", fc.CreateFishbone())
|
|
rg.GET("/:uuid", fc.GetFishboneByID())
|
|
rg.PUT("/:uuid", fc.UpdateFishbone())
|
|
}
|
|
}
|
|
|
|
func NewFishboneController(fu usecase.FishboneUseCase, rg *gin.RouterGroup) *FishboneController {
|
|
return &FishboneController{
|
|
fu: fu,
|
|
rg: rg,
|
|
}
|
|
}
|
|
|
|
func (fc *FishboneController) GetFishbone() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
fishbones, err := fc.fu.GetAllFishbone()
|
|
|
|
if err != nil {
|
|
common.ErrorResponses(c, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
common.SingleResponses(c, "Success", fishbones)
|
|
}
|
|
}
|
|
|
|
func (fc *FishboneController) CreateFishbone() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
var fishboneDTO req.FishboneDTO
|
|
err := c.ShouldBindJSON(&fishboneDTO)
|
|
|
|
if err != nil {
|
|
common.ErrorResponses(c, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
err = fc.fu.CreateFishbone(fishboneDTO)
|
|
|
|
if err != nil {
|
|
common.ErrorResponses(c, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
common.SingleResponses(c, "Fishbone has been created", nil)
|
|
}
|
|
}
|
|
|
|
func (fc *FishboneController) GetFishboneByID() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
id := c.Param("uuid")
|
|
uuid, err := uuid.Parse(id)
|
|
if err != nil{
|
|
common.ErrorResponses(c, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
fishbone, err := fc.fu.GetByID(uuid)
|
|
|
|
if err != nil {
|
|
common.ErrorResponses(c, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
common.SingleResponses(c, "Success", fishbone)
|
|
}
|
|
}
|
|
|
|
func (fc *FishboneController) UpdateFishbone() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
id := c.Param("uuid")
|
|
uuid, err := uuid.Parse(id)
|
|
if err != nil {
|
|
common.ErrorResponses(c, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
var fishboneDTO req.UpdateFishboneDTO
|
|
err = c.ShouldBindJSON(&fishboneDTO)
|
|
|
|
if err != nil {
|
|
common.ErrorResponses(c, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
err = fc.fu.UpdateFishbone(uuid, fishboneDTO)
|
|
|
|
if err != nil {
|
|
common.ErrorResponses(c, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
common.SingleResponses(c, "Fishbone has been updated", nil)
|
|
}
|
|
} |