/
Arsinenko
/
CompClubAPI_Go
Обзор
Документация
Войти
/
Arsinenko
/
CompClubAPI_Go
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
internal/handlers/working_space_handler.go
177 строк
5 KB
Arsinenko
Working space update
17 ноя 2025, 17:41
17 ноя 2025, 17:41
d647065
Код
Авторство
О чём код?
package handlers import ( "CompClubAPI/internal/middleware" "CompClubAPI/internal/services" "net/http" "strconv" "github.com/gin-gonic/gin" ) // CreateWorkingSpaceHandler godoc // @Summary Create a new working space // @Description Create a new working space with the given details // @Tags working-spaces // @Accept json // @Produce json // @Param working_space body services.CreateWorkingSpaceRequest true "Working Space" // @Success 201 {object} map[string]interface{} // @Failure 400 {object} map[string]interface{} // @Failure 500 {object} map[string]interface{} // @Router /working-spaces [post] func CreateWorkingSpaceHandler(s services.WorkingSpaceService) gin.HandlerFunc { return func(c *gin.Context) { var request services.CreateWorkingSpaceRequest if err := c.ShouldBindJSON(&request); err != nil { c.JSON(400, gin.H{"error": err.Error()}) return } response, err := s.CreateWorkingSpace(request) if err != nil { if err.Error() == "working space not found" { c.JSON(400, gin.H{"error": err.Error()}) return } c.JSON(500, gin.H{"error": err.Error()}) return } c.JSON(http.StatusCreated, gin.H{"working_space": response}) } } // UpdateWorkingSpaceTariffHandler godoc // @Summary Update working space tariff // @Description Update the tariff of a specific working space by its ID // @Tags working-spaces // @Accept json // @Produce json // @Param id path int true "Working Space ID" // @Param tariff path string true "New Tariff" // @Success 200 {object} map[string]interface{} // @Failure 400 {object} map[string]interface{} // @Failure 500 {object} map[string]interface{} // @Router /working-spaces/{id}/tariff/{tariff} [put] func UpdateWorkingSpaceTariffHandler(s services.WorkingSpaceService) gin.HandlerFunc { return func(c *gin.Context) { id, err := strconv.Atoi(c.Param("id")) if err != nil { c.JSON(400, gin.H{"error": err.Error()}) return } tariff := c.Param("tariff") ws, err := s.UpdateWorkingSpaceTariff(int32(id), tariff) if err != nil { if err.Error() == "working space not found" { c.JSON(400, gin.H{"error": err.Error()}) } c.JSON(500, gin.H{"error": err.Error()}) return } c.JSON(200, gin.H{"working_space": ws}) } } // UpdateWorkingSpaceStatusHandler godoc // @Summary Update working space status // @Description Update the status of a specific working space by its ID // @Tags working-spaces // @Accept json // @Produce json // @Param id path int true "Working Space ID" // @Param status path boolean true "New Status" // @Success 200 {object} map[string]interface{} // @Failure 400 {object} map[string]interface{} // @Failure 500 {object} map[string]interface{} // @Router /working-spaces/{id}/status/{status} [put] func UpdateWorkingSpaceStatusHandler(s services.WorkingSpaceService) gin.HandlerFunc { return func(c *gin.Context) { id, err := strconv.Atoi(c.Param("id")) if err != nil { c.JSON(400, gin.H{"error": err.Error()}) return } status, err := strconv.ParseBool(c.Param("status")) if err != nil { c.JSON(400, gin.H{"error": err.Error()}) return } ws, err := s.UpdateStatus(int32(id), status) if err != nil { if err.Error() == "working space not found" { c.JSON(400, gin.H{"error": err.Error()}) return } c.JSON(500, gin.H{"error": err.Error()}) return } c.JSON(200, gin.H{"working_space": ws}) } } // GetWorkingSpaceHandler godoc // @Summary Get a working space // @Description Get a specific working space by its ID // @Tags working-spaces // @Accept json // @Produce json // @Param id path int true "Working Space ID" // @Success 200 {object} map[string]interface{} // @Failure 400 {object} map[string]interface{} // @Failure 500 {object} map[string]interface{} // @Router /working-spaces/{id} [get] func GetWorkingSpaceHandler(s services.WorkingSpaceService) gin.HandlerFunc { return func(c *gin.Context) { id, err := strconv.Atoi(c.Param("id")) if err != nil { c.JSON(400, gin.H{"error": err.Error()}) return } ws, err := s.GetWorkingSpace(int32(id)) if err != nil { if err.Error() == "working space not found" { c.JSON(400, gin.H{"error": err.Error()}) return } c.JSON(500, gin.H{"error": err.Error()}) return } c.JSON(200, gin.H{"working_space": ws}) } } // GetWorkingSpacesHandler godoc // @Summary Get all working spaces // @Description Get a list of all working spaces // @Tags working-spaces // @Accept json // @Produce json // @Success 200 {object} map[string]interface{} // @Failure 500 {object} map[string]interface{} // @Router /working-spaces [get] func GetWorkingSpacesHandler(s services.WorkingSpaceService) gin.HandlerFunc { return func(c *gin.Context) { wss, err := s.GetWorkingSpaces() if err != nil { if err.Error() == "working spaces not found" { c.JSON(200, gin.H{"working_spaces": nil}) return } c.JSON(500, gin.H{"error": err.Error()}) return } c.JSON(200, gin.H{"working_spaces": wss}) } } func InitWorkingSpaceHandler(r *gin.RouterGroup, s services.WorkingSpaceService) { jwtMiddleware := middleware.NewJwtMiddleware() r.Use(middleware.AdminMiddleware(jwtMiddleware)) r.POST("/", CreateWorkingSpaceHandler(s)) r.PUT("/:id/tariff/:tariff", UpdateWorkingSpaceTariffHandler(s)) r.PUT("/:id/status/:status", UpdateWorkingSpaceStatusHandler(s)) r.GET("/:id", GetWorkingSpaceHandler(s)) r.GET("/", GetWorkingSpacesHandler(s)) }