189 lines
5.3 KiB
Go
189 lines
5.3 KiB
Go
package controller
|
|
|
|
import (
|
|
"net/http"
|
|
"users_management/m/config"
|
|
"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
|
|
cfg *config.Config
|
|
}
|
|
|
|
func NewFishboneController(fu usecase.FishboneUseCase, rg *gin.RouterGroup, cfg *config.Config) *FishboneController {
|
|
return &FishboneController{
|
|
fu: fu,
|
|
rg: rg,
|
|
cfg: cfg,
|
|
}
|
|
}
|
|
|
|
func (fc *FishboneController) Route() {
|
|
rg := fc.rg.Group("/fishbone")
|
|
rg.Use(middleware.ConditionalRequireAnyRole(fc.cfg,"Teknisi", "Admin", "Super Admin"))
|
|
// Apply middleware to all routes
|
|
{
|
|
rg.GET("", fc.GetFishbone())
|
|
rg.POST("", fc.CreateFishbone())
|
|
rg.GET("/:uuid", fc.GetFishboneByID())
|
|
rg.PUT("/:uuid", fc.UpdateFishbone())
|
|
rg.GET("/backbone/:backbone_id", fc.GetFishboneByBackboneID())
|
|
}
|
|
}
|
|
|
|
|
|
func (fc *FishboneController) GetFishboneByBackboneID() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
backboneIDStr := c.Param("backbone_id")
|
|
backboneID, err := uuid.Parse(backboneIDStr)
|
|
if err != nil {
|
|
common.ErrorResponses(c, http.StatusBadRequest, "Invalid backbone ID format")
|
|
return
|
|
}
|
|
|
|
fishbones, err := fc.fu.GetByBackboneID(backboneID)
|
|
if err != nil {
|
|
common.ErrorResponses(c, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
if len(fishbones) == 0 {
|
|
common.SingleResponses(c, "No fishbones found for this backbone", []interface{}{})
|
|
return
|
|
}
|
|
|
|
common.SingleResponses(c, "Success", fishbones)
|
|
}
|
|
}
|
|
|
|
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 successfully", nil)
|
|
}
|
|
}
|
|
|
|
func (fc *FishboneController) GetFishboneByID() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
id := c.Param("uuid")
|
|
fishboneUUID, err := uuid.Parse(id)
|
|
if err != nil {
|
|
common.ErrorResponses(c, http.StatusBadRequest, "Invalid UUID format")
|
|
return
|
|
}
|
|
|
|
fishbone, err := fc.fu.GetByID(fishboneUUID)
|
|
if err != nil {
|
|
if err.Error() == "fishbone not found" {
|
|
common.ErrorResponses(c, http.StatusNotFound, err.Error())
|
|
return
|
|
}
|
|
common.ErrorResponses(c, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
common.SingleResponses(c, "Success", fishbone)
|
|
}
|
|
}
|
|
|
|
func (fc *FishboneController) UpdateFishbone() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
id := c.Param("uuid")
|
|
fishboneUUID, err := uuid.Parse(id)
|
|
if err != nil {
|
|
common.ErrorResponses(c, http.StatusBadRequest, "Invalid UUID format")
|
|
return
|
|
}
|
|
|
|
var fishboneDTO req.UpdateFishboneDTO
|
|
err = c.ShouldBindJSON(&fishboneDTO)
|
|
|
|
if err != nil {
|
|
common.ErrorResponses(c, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
err = fc.fu.UpdateFishbone(fishboneUUID, fishboneDTO)
|
|
|
|
if err != nil {
|
|
if err.Error() == "fishbone not found" {
|
|
common.ErrorResponses(c, http.StatusNotFound, err.Error())
|
|
return
|
|
}
|
|
common.ErrorResponses(c, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
common.SingleResponses(c, "Fishbone has been updated successfully", nil)
|
|
}
|
|
}
|
|
|
|
func (fc *FishboneController) DeleteFishbone() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
id := c.Param("uuid")
|
|
fishboneUUID, err := uuid.Parse(id)
|
|
if err != nil {
|
|
common.ErrorResponses(c, http.StatusBadRequest, "Invalid UUID format")
|
|
return
|
|
}
|
|
|
|
err = fc.fu.DeleteFishbone(fishboneUUID)
|
|
if err != nil {
|
|
if err.Error() == "fishbone not found" {
|
|
common.ErrorResponses(c, http.StatusNotFound, err.Error())
|
|
return
|
|
}
|
|
common.ErrorResponses(c, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
common.SingleResponses(c, "Fishbone has been deleted successfully", nil)
|
|
}
|
|
}
|
|
|
|
func (fc *FishboneController) GetFishboneStats() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
stats, err := fc.fu.GetFishboneStats()
|
|
if err != nil {
|
|
common.ErrorResponses(c, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
common.SingleResponses(c, "Success", stats)
|
|
}
|
|
} |