package controllers import ( "net/http" "clapclap/internal/apperr" "clapclap/internal/ginctx" "clapclap/internal/models" "clapclap/internal/utils" "github.com/gin-gonic/gin" ) // GetAllGenres godoc // @Summary Get all genres // @Description Retrieves a list of all music genres. Use the "tree" query parameter to get a nested structure. // @Tags genres // @Produce json // @Param tree query bool false "Set to true to return a nested tree structure of genres and sub-genres" // @Param preload query string false "Relation to preload" Enums(Parent, SubGenres, Tracks) // @Success 200 {array} models.Genre // @Failure 500 {object} swaggError500 // @Router /genres [get] func GetAllGenres(c *gin.Context) { repo, _ := ginctx.GetRepo(c, "repo") var genres []models.Genre isTree := c.Query("tree") preloads := utils.GetReqPreloads(c.Request, utils.GetGenrePreloads()) var err error if isTree == "true" { err = repo.GetGenreRoots(&genres, preloads) } else { err = repo.GetAllGenres(&genres, preloads) } if err != nil { apperr.AbortWithError(c, apperr.NewDatabaseError(err, "Failed to fetch genres")) return } c.JSON(http.StatusOK, genres) } // GetGenreByID godoc // @Summary Get a genre by ID // @Description Retrieves a specific genre's details, including its sub-genres, using its UUID // @Tags genres // @Produce json // @Param id path string true "Genre UUID" // @Param preload query string false "Relation to preload" Enums(Parent, SubGenres, Tracks) // @Success 200 {object} models.Genre // @Failure 404 {object} swaggError404 // @Failure 500 {object} swaggError500 // @Router /genres/{id} [get] func GetGenreByID(c *gin.Context) { repo, _ := ginctx.GetRepo(c, "repo") id := c.Param("id") preloads := utils.GetReqPreloads(c.Request, utils.GetGenrePreloads()) genre, err := repo.GetGenreByID(id, preloads) if err != nil { apperr.AbortWithError(c, apperr.NewDatabaseError(err, "Genre not found")) return } c.JSON(http.StatusOK, genre) }