package apis import ( "clortho/lib/systems" "github.com/gin-gonic/gin" "net/http" "strconv" ) type systemRequest struct { Name string `json:"name" binding:"required"` } func InitSystemsEndpoints(r *gin.RouterGroup) { group := r.Group("/systems") group.Use(LoggedInMiddleware()) group.Use(AdminMiddleware()) group.GET("/", getSystems) group.GET("/:id", getSystem) group.POST("/", createSystem) group.PUT("/:id", updateSystem) group.DELETE("/:id", deleteSystem) } func getSystems(c *gin.Context) { systemList := systems.GetSystems() c.JSON(http.StatusOK, &systemList) } func getSystem(c *gin.Context) { id, err := strconv.ParseUint(c.Param("id"), 10, 32) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid ID format"}) return } system, err := systems.GetSystem(uint(id)) if err != nil { c.JSON(http.StatusNotFound, gin.H{"error": "System not found"}) return } c.JSON(http.StatusOK, system) } func createSystem(c *gin.Context) { var req systemRequest if err := c.BindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } system, err := systems.CreateSystem(req.Name) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusCreated, system) } func updateSystem(c *gin.Context) { id, err := strconv.ParseUint(c.Param("id"), 10, 32) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid ID format"}) return } var req systemRequest if err := c.BindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } system, err := systems.UpdateSystem(uint(id), req.Name) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, system) } func deleteSystem(c *gin.Context) { id, err := strconv.ParseUint(c.Param("id"), 10, 32) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid ID format"}) return } err = systems.DeleteSystem(uint(id)) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"message": "System deleted successfully"}) }