357 lines
11 KiB
Go
357 lines
11 KiB
Go
package controller
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"strconv"
|
|
"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 CableConnectionController struct {
|
|
cableConnectionUC usecase.CableConnectionUseCase
|
|
rg *gin.RouterGroup
|
|
cfg *config.Config
|
|
}
|
|
|
|
func NewCableConnectionController(cableConnectionUC usecase.CableConnectionUseCase, rg *gin.RouterGroup, cfg *config.Config) *CableConnectionController {
|
|
return &CableConnectionController{
|
|
cableConnectionUC: cableConnectionUC,
|
|
rg: rg,
|
|
cfg: cfg,
|
|
}
|
|
}
|
|
|
|
func (c *CableConnectionController) Route() {
|
|
cableConnections := c.rg.Group("/cable-connections")
|
|
cableConnections.Use(middleware.ConditionalRequireAnyRole(c.cfg, "Teknisi", "Admin", "Super Admin"))
|
|
{
|
|
// Basic CRUD operations
|
|
cableConnections.POST("/search", c.searchCableConnections)
|
|
cableConnections.GET("/:id", c.getCableConnectionByID)
|
|
cableConnections.POST("", c.createCableConnection)
|
|
cableConnections.PUT("/:id", c.updateCableConnection)
|
|
cableConnections.DELETE("/:id", c.deleteCableConnection)
|
|
|
|
// Bulk operations
|
|
cableConnections.POST("/bulk/create", c.bulkCreateCableConnections)
|
|
cableConnections.PUT("/bulk/update", c.bulkUpdateCableConnections)
|
|
cableConnections.DELETE("/bulk/delete", c.bulkDeleteCableConnections)
|
|
|
|
// Analysis and reporting endpoints
|
|
cableConnections.GET("/device/:deviceId", c.getCableConnectionsByDevice)
|
|
cableConnections.GET("/analytics/length-distribution", c.getCableLengthDistribution)
|
|
cableConnections.GET("/analytics/cable-types", c.getCableTypeAnalytics)
|
|
cableConnections.POST("/calculate-route", c.calculateOptimalRoute)
|
|
|
|
// Maintenance and monitoring
|
|
cableConnections.GET("/status/summary", c.getCableStatusSummary)
|
|
cableConnections.PUT("/:id/status", c.updateCableStatus)
|
|
cableConnections.GET("/maintenance/due", c.getMaintenanceDue)
|
|
|
|
// Network path analysis
|
|
cableConnections.POST("/path/trace", c.traceCablePath)
|
|
cableConnections.GET("/network-map", c.getNetworkMap)
|
|
}
|
|
}
|
|
|
|
func (c *CableConnectionController) searchCableConnections(ctx *gin.Context) {
|
|
var request req.CableConnectionSearchDTO
|
|
if err := ctx.ShouldBindJSON(&request); err != nil {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
connections, total, err := c.cableConnectionUC.SearchCableConnections(request)
|
|
if err != nil {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
response := gin.H{
|
|
"connections": connections,
|
|
"total": total,
|
|
"page": request.Page,
|
|
"per_page": request.PerPage,
|
|
"search_params": gin.H{
|
|
"cable_type": request.CableType,
|
|
"status": request.Status,
|
|
"device_id": request.DeviceID,
|
|
"min_length": request.MinLength,
|
|
"max_length": request.MaxLength,
|
|
"branching_type": request.BranchingType,
|
|
},
|
|
}
|
|
|
|
common.SingleResponses(ctx, "Cable connections retrieved successfully", response)
|
|
}
|
|
|
|
func (c *CableConnectionController) getCableConnectionByID(ctx *gin.Context) {
|
|
id := ctx.Param("id")
|
|
connectionID, err := uuid.Parse(id)
|
|
if err != nil {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, "Invalid cable connection ID")
|
|
return
|
|
}
|
|
|
|
connection, err := c.cableConnectionUC.GetCableConnectionByID(connectionID)
|
|
if err != nil {
|
|
common.ErrorResponses(ctx, http.StatusNotFound, err.Error())
|
|
return
|
|
}
|
|
|
|
common.SingleResponses(ctx, "Cable connection retrieved successfully", connection)
|
|
}
|
|
|
|
func (c *CableConnectionController) createCableConnection(ctx *gin.Context) {
|
|
var request req.CreateCableConnectionDTO
|
|
if err := ctx.ShouldBindJSON(&request); err != nil {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
connection, err := c.cableConnectionUC.CreateCableConnection(request)
|
|
if err != nil {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
common.SingleResponses(ctx, "Cable connection created successfully", connection)
|
|
}
|
|
|
|
func (c *CableConnectionController) updateCableConnection(ctx *gin.Context) {
|
|
id := ctx.Param("id")
|
|
connectionID, err := uuid.Parse(id)
|
|
if err != nil {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, "Invalid cable connection ID")
|
|
return
|
|
}
|
|
|
|
var request req.UpdateCableConnectionDTO
|
|
if err := ctx.ShouldBindJSON(&request); err != nil {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
err = c.cableConnectionUC.UpdateCableConnection(connectionID, request)
|
|
if err != nil {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
common.SingleResponses(ctx, "Cable connection updated successfully", nil)
|
|
}
|
|
|
|
func (c *CableConnectionController) deleteCableConnection(ctx *gin.Context) {
|
|
id := ctx.Param("id")
|
|
connectionID, err := uuid.Parse(id)
|
|
if err != nil {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, "Invalid cable connection ID")
|
|
return
|
|
}
|
|
|
|
err = c.cableConnectionUC.DeleteCableConnection(connectionID)
|
|
if err != nil {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
common.SingleResponses(ctx, "Cable connection deleted successfully", nil)
|
|
}
|
|
|
|
func (c *CableConnectionController) bulkCreateCableConnections(ctx *gin.Context) {
|
|
var request req.BulkCreateCableConnectionDTO
|
|
if err := ctx.ShouldBindJSON(&request); err != nil {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
result, err := c.cableConnectionUC.BulkCreateCableConnections(request)
|
|
if err != nil {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
message := fmt.Sprintf("Bulk create completed: %d successful, %d failed out of %d requested",
|
|
result.Successful, result.Failed, result.TotalRequested)
|
|
common.SingleResponses(ctx, message, result)
|
|
}
|
|
|
|
func (c *CableConnectionController) bulkUpdateCableConnections(ctx *gin.Context) {
|
|
var request req.BulkUpdateCableConnectionDTO
|
|
if err := ctx.ShouldBindJSON(&request); err != nil {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
result, err := c.cableConnectionUC.BulkUpdateCableConnections(request)
|
|
if err != nil {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
message := fmt.Sprintf("Bulk update completed: %d successful, %d failed out of %d requested",
|
|
result.Successful, result.Failed, result.TotalRequested)
|
|
common.SingleResponses(ctx, message, result)
|
|
}
|
|
|
|
func (c *CableConnectionController) bulkDeleteCableConnections(ctx *gin.Context) {
|
|
var request req.BulkDeleteCableConnectionDTO
|
|
if err := ctx.ShouldBindJSON(&request); err != nil {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
result, err := c.cableConnectionUC.BulkDeleteCableConnections(request)
|
|
if err != nil {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
message := fmt.Sprintf("Bulk delete completed: %d successful, %d failed out of %d requested",
|
|
result.Successful, result.Failed, result.TotalRequested)
|
|
common.SingleResponses(ctx, message, result)
|
|
}
|
|
|
|
func (c *CableConnectionController) getCableConnectionsByDevice(ctx *gin.Context) {
|
|
deviceID := ctx.Param("deviceId")
|
|
deviceUUID, err := uuid.Parse(deviceID)
|
|
if err != nil {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, "Invalid device ID")
|
|
return
|
|
}
|
|
|
|
connections, err := c.cableConnectionUC.GetCableConnectionsByDevice(deviceUUID)
|
|
if err != nil {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
common.SingleResponses(ctx, "Cable connections retrieved successfully", connections)
|
|
}
|
|
|
|
func (c *CableConnectionController) getCableLengthDistribution(ctx *gin.Context) {
|
|
cableType := ctx.Query("cable_type")
|
|
|
|
distribution, err := c.cableConnectionUC.GetCableLengthDistribution(cableType)
|
|
if err != nil {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
common.SingleResponses(ctx, "Cable length distribution retrieved successfully", distribution)
|
|
}
|
|
|
|
func (c *CableConnectionController) getCableTypeAnalytics(ctx *gin.Context) {
|
|
analytics, err := c.cableConnectionUC.GetCableTypeAnalytics()
|
|
if err != nil {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
common.SingleResponses(ctx, "Cable type analytics retrieved successfully", analytics)
|
|
}
|
|
|
|
func (c *CableConnectionController) calculateOptimalRoute(ctx *gin.Context) {
|
|
var request req.OptimalRouteRequestDTO
|
|
if err := ctx.ShouldBindJSON(&request); err != nil {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
route, err := c.cableConnectionUC.CalculateOptimalRoute(request)
|
|
if err != nil {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
common.SingleResponses(ctx, "Optimal route calculated successfully", route)
|
|
}
|
|
|
|
func (c *CableConnectionController) getCableStatusSummary(ctx *gin.Context) {
|
|
summary, err := c.cableConnectionUC.GetCableStatusSummary()
|
|
if err != nil {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
common.SingleResponses(ctx, "Cable status summary retrieved successfully", summary)
|
|
}
|
|
|
|
func (c *CableConnectionController) updateCableStatus(ctx *gin.Context) {
|
|
id := ctx.Param("id")
|
|
connectionID, err := uuid.Parse(id)
|
|
if err != nil {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, "Invalid cable connection ID")
|
|
return
|
|
}
|
|
|
|
var request req.UpdateCableStatusDTO
|
|
if err := ctx.ShouldBindJSON(&request); err != nil {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
err = c.cableConnectionUC.UpdateCableStatus(connectionID, request)
|
|
if err != nil {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
common.SingleResponses(ctx, "Cable status updated successfully", nil)
|
|
}
|
|
|
|
func (c *CableConnectionController) getMaintenanceDue(ctx *gin.Context) {
|
|
daysStr := ctx.DefaultQuery("days", "30")
|
|
days, err := strconv.Atoi(daysStr)
|
|
if err != nil {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, "Invalid days parameter")
|
|
return
|
|
}
|
|
|
|
maintenanceList, err := c.cableConnectionUC.GetMaintenanceDue(days)
|
|
if err != nil {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
common.SingleResponses(ctx, "Maintenance due list retrieved successfully", maintenanceList)
|
|
}
|
|
|
|
func (c *CableConnectionController) traceCablePath(ctx *gin.Context) {
|
|
var request req.TraceCablePathDTO
|
|
if err := ctx.ShouldBindJSON(&request); err != nil {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
path, err := c.cableConnectionUC.TraceCablePath(request)
|
|
if err != nil {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
common.SingleResponses(ctx, "Cable path traced successfully", path)
|
|
}
|
|
|
|
func (c *CableConnectionController) getNetworkMap(ctx *gin.Context) {
|
|
// Get query parameters for filtering
|
|
deviceType := ctx.Query("device_type")
|
|
cableType := ctx.Query("cable_type")
|
|
region := ctx.Query("region")
|
|
|
|
networkMap, err := c.cableConnectionUC.GetNetworkMap(deviceType, cableType, region)
|
|
if err != nil {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
common.SingleResponses(ctx, "Network map retrieved successfully", networkMap)
|
|
}
|