118 lines
2.7 KiB
Go
118 lines
2.7 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 DevicePortController struct {
|
|
du usecase.DevicePortUseCase
|
|
rg *gin.RouterGroup
|
|
}
|
|
|
|
func (dc *DevicePortController) Route() {
|
|
rg := dc.rg.Group("/device-port")
|
|
rg.Use(middleware.AuthMiddleware())
|
|
rg.Use(middleware.RateLimitMiddleware())
|
|
rg.Use(middleware.CORSMiddleware())
|
|
{
|
|
rg.GET("", dc.GetDevicePort())
|
|
rg.POST("", dc.CreateDevicePort())
|
|
rg.GET("/:uuid", dc.GetDevicePortByID())
|
|
rg.PUT("/:uuid", dc.UpdateDevicePort())
|
|
}
|
|
}
|
|
|
|
func NewDevicePortController(du usecase.DevicePortUseCase, rg *gin.RouterGroup) *DevicePortController {
|
|
return &DevicePortController{
|
|
du: du,
|
|
rg: rg,
|
|
}
|
|
}
|
|
|
|
func (dc *DevicePortController) GetDevicePort() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
devicePorts, err := dc.du.GetAllDevicePort()
|
|
|
|
if err != nil {
|
|
common.ErrorResponses(c, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
common.SingleResponses(c, "Success", devicePorts)
|
|
}
|
|
}
|
|
|
|
func (dc *DevicePortController) CreateDevicePort() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
var devicePortDTO req.DevicePort
|
|
err := c.ShouldBindJSON(&devicePortDTO)
|
|
|
|
if err != nil {
|
|
common.ErrorResponses(c, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
err = dc.du.CreateDevicePort(devicePortDTO)
|
|
|
|
if err != nil {
|
|
common.ErrorResponses(c, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
common.SingleResponses(c, "Device Port has been created", nil)
|
|
}
|
|
}
|
|
|
|
func (dc *DevicePortController) GetDevicePortByID() 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
|
|
}
|
|
devicePort, err := dc.du.GetByID(uuid)
|
|
|
|
if err != nil {
|
|
common.ErrorResponses(c, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
common.SingleResponses(c, "Success", devicePort)
|
|
}
|
|
}
|
|
|
|
func (dc *DevicePortController) UpdateDevicePort() 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 devicePortDTO req.UpdateDevicePort
|
|
err = c.ShouldBindJSON(&devicePortDTO)
|
|
|
|
if err != nil {
|
|
common.ErrorResponses(c, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
err = dc.du.UpdateDevicePort(uuid, devicePortDTO)
|
|
|
|
if err != nil {
|
|
common.ErrorResponses(c, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
common.SingleResponses(c, "Device Port has been updated", nil)
|
|
}
|
|
} |