229 lines
6.4 KiB
Go
229 lines
6.4 KiB
Go
package controller
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
"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 OLTController struct {
|
|
oltUC usecase.OLTUsecase
|
|
rg *gin.RouterGroup
|
|
}
|
|
|
|
func NewOLTController(oltUC usecase.OLTUsecase, rg *gin.RouterGroup) *OLTController {
|
|
return &OLTController{
|
|
oltUC: oltUC,
|
|
rg: rg,
|
|
}
|
|
}
|
|
|
|
func (c *OLTController) Route() {
|
|
oltGroup := c.rg.Group("/olt")
|
|
{
|
|
oltGroup.POST("", c.createOLT)
|
|
oltGroup.GET("", c.getAllOLTs)
|
|
oltGroup.GET("/:id", c.getOLTByID)
|
|
oltGroup.PUT("/:id", c.updateOLT)
|
|
oltGroup.DELETE("/:id", c.deleteOLT)
|
|
oltGroup.POST("/:id/assign-device", c.assignDeviceToOLT)
|
|
oltGroup.DELETE("/unassign-device/:deviceId", c.unassignDeviceFromOLT)
|
|
oltGroup.GET("/:id/devices", c.getDevicesByOLT)
|
|
}
|
|
}
|
|
|
|
func (c *OLTController) createOLT(ctx *gin.Context) {
|
|
var oltDTO req.OLTDTO
|
|
if err := ctx.ShouldBindJSON(&oltDTO); err != nil {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
err := c.oltUC.CreateOLT(oltDTO)
|
|
if err != nil {
|
|
if err.Error() == "OLT name already exists" {
|
|
common.ErrorResponses(ctx, http.StatusConflict, err.Error())
|
|
return
|
|
}
|
|
common.ErrorResponses(ctx, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
common.SingleResponses(ctx, "OLT created successfully", gin.H{
|
|
"olt_name": oltDTO.OLTName,
|
|
})
|
|
}
|
|
|
|
func (c *OLTController) getAllOLTs(ctx *gin.Context) {
|
|
// Pagination parameters
|
|
page, _ := strconv.Atoi(ctx.DefaultQuery("page", "1"))
|
|
if page < 1 {
|
|
page = 1
|
|
}
|
|
|
|
olts, err := c.oltUC.GetAllOLTs()
|
|
if err != nil {
|
|
common.ErrorResponses(ctx, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
response := gin.H{
|
|
"olts": olts,
|
|
"total": len(olts),
|
|
"page": page,
|
|
}
|
|
|
|
common.SingleResponses(ctx, "OLTs retrieved successfully", response)
|
|
}
|
|
|
|
func (c *OLTController) getOLTByID(ctx *gin.Context) {
|
|
idStr := ctx.Param("id")
|
|
id, err := uuid.Parse(idStr)
|
|
if err != nil {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, "Invalid OLT ID")
|
|
return
|
|
}
|
|
|
|
olt, err := c.oltUC.GetOLTByID(id)
|
|
if err != nil {
|
|
common.ErrorResponses(ctx, http.StatusNotFound, "OLT not found")
|
|
return
|
|
}
|
|
|
|
common.SingleResponses(ctx, "OLT details retrieved successfully", olt)
|
|
}
|
|
|
|
func (c *OLTController) updateOLT(ctx *gin.Context) {
|
|
idStr := ctx.Param("id")
|
|
id, err := uuid.Parse(idStr)
|
|
if err != nil {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, "Invalid OLT ID")
|
|
return
|
|
}
|
|
|
|
var updateDTO req.UpdateOLTDTO
|
|
if err := ctx.ShouldBindJSON(&updateDTO); err != nil {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
err = c.oltUC.UpdateOLT(id, updateDTO)
|
|
if err != nil {
|
|
switch err.Error() {
|
|
case "OLT not found":
|
|
common.ErrorResponses(ctx, http.StatusNotFound, err.Error())
|
|
case "OLT name already exists":
|
|
common.ErrorResponses(ctx, http.StatusConflict, err.Error())
|
|
default:
|
|
common.ErrorResponses(ctx, http.StatusInternalServerError, err.Error())
|
|
}
|
|
return
|
|
}
|
|
|
|
common.SingleResponses(ctx, "OLT updated successfully", gin.H{
|
|
"olt_id": id,
|
|
})
|
|
}
|
|
|
|
func (c *OLTController) deleteOLT(ctx *gin.Context) {
|
|
idStr := ctx.Param("id")
|
|
id, err := uuid.Parse(idStr)
|
|
if err != nil {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, "Invalid OLT ID")
|
|
return
|
|
}
|
|
|
|
err = c.oltUC.DeleteOLT(id)
|
|
if err != nil {
|
|
if err.Error() == "OLT not found" {
|
|
common.ErrorResponses(ctx, http.StatusNotFound, err.Error())
|
|
return
|
|
}
|
|
common.ErrorResponses(ctx, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
common.SingleResponses(ctx, "OLT deleted successfully", gin.H{
|
|
"olt_id": id,
|
|
})
|
|
}
|
|
|
|
func (c *OLTController) assignDeviceToOLT(ctx *gin.Context) {
|
|
oltIDStr := ctx.Param("id")
|
|
oltID, err := uuid.Parse(oltIDStr)
|
|
if err != nil {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, "Invalid OLT ID")
|
|
return
|
|
}
|
|
|
|
var assignDTO req.AssignDeviceToOLTDTO
|
|
if err := ctx.ShouldBindJSON(&assignDTO); err != nil {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
err = c.oltUC.AssignDeviceToOLT(oltID, assignDTO.DeviceID)
|
|
if err != nil {
|
|
if err.Error() == "only ODP devices can be assigned to OLT" {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
common.ErrorResponses(ctx, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
common.SingleResponses(ctx, "Device assigned to OLT successfully", gin.H{
|
|
"olt_id": oltID,
|
|
"device_id": assignDTO.DeviceID,
|
|
})
|
|
}
|
|
|
|
func (c *OLTController) unassignDeviceFromOLT(ctx *gin.Context) {
|
|
deviceIDStr := ctx.Param("deviceId")
|
|
deviceID, err := uuid.Parse(deviceIDStr)
|
|
if err != nil {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, "Invalid device ID")
|
|
return
|
|
}
|
|
|
|
err = c.oltUC.UnassignDeviceFromOLT(deviceID)
|
|
if err != nil {
|
|
common.ErrorResponses(ctx, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
common.SingleResponses(ctx, "Device unassigned from OLT successfully", gin.H{
|
|
"device_id": deviceID,
|
|
})
|
|
}
|
|
|
|
func (c *OLTController) getDevicesByOLT(ctx *gin.Context) {
|
|
idStr := ctx.Param("id")
|
|
id, err := uuid.Parse(idStr)
|
|
if err != nil {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, "Invalid OLT ID")
|
|
return
|
|
}
|
|
|
|
devices, err := c.oltUC.GetDevicesByOLT(id)
|
|
if err != nil {
|
|
if err.Error() == "OLT not found" {
|
|
common.ErrorResponses(ctx, http.StatusNotFound, err.Error())
|
|
return
|
|
}
|
|
common.ErrorResponses(ctx, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
response := gin.H{
|
|
"devices": devices,
|
|
"total": len(devices),
|
|
}
|
|
|
|
common.SingleResponses(ctx, "OLT devices retrieved successfully", response)
|
|
} |