package handler import ( "errors" "log" "net/http" "strings" "github.com/dusiburg/fancy-api/internal/model" "github.com/dusiburg/fancy-api/internal/repository" "github.com/dusiburg/fancy-api/internal/service" "github.com/gin-gonic/gin" "github.com/google/uuid" ) type CollectionHandler struct { service *service.CollectionService } func CreateCollectionHandler(service *service.CollectionService) *CollectionHandler { return &CollectionHandler{service: service} } func (h *CollectionHandler) GetCollections(c *gin.Context) { userID, ok := userIDFromContext(c) if !ok { c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"}) return } collections, err := h.service.GetCollections(c.Request.Context(), userID) if err != nil { h.handleError(c, err) return } if len(collections) == 0 { c.JSON(http.StatusNotFound, gin.H{"error": "collections not found"}) return } c.JSON(http.StatusOK, collections) } func (h *CollectionHandler) GetCollectionByID(c *gin.Context) { userID, ok := userIDFromContext(c) if !ok { c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"}) return } id, err := uuid.Parse(c.Param("id")) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "invalid id"}) return } collection, err := h.service.GetCollectionByID(c.Request.Context(), userID, id) if err != nil { h.handleError(c, err) return } c.JSON(http.StatusOK, collection) } func (h *CollectionHandler) GetCollectionsByWord(c *gin.Context) { userID, ok := userIDFromContext(c) if !ok { c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"}) return } word := strings.TrimSpace(c.Param("word")) if word == "" { c.JSON(http.StatusBadRequest, gin.H{"error": "word is required"}) return } lang, ok := model.ParseLanguage(c.Param("lang")) if !ok { c.JSON(http.StatusBadRequest, gin.H{"error": "unsupported language"}) return } collections, err := h.service.GetCollectionsByWord(c.Request.Context(), userID, lang, word) if err != nil { h.handleError(c, err) return } c.JSON(http.StatusOK, collections) } type updateCollectionRequest struct { Name string `json:"name" binding:"required"` } func (h *CollectionHandler) UpdateCollectionByID(c *gin.Context) { userID, ok := userIDFromContext(c) if !ok { c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"}) return } id, err := uuid.Parse(c.Param("id")) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "invalid id"}) return } var req updateCollectionRequest if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } collection, err := h.service.UpdateCollectionDetails(c.Request.Context(), userID, id, req.Name) if err != nil { h.handleError(c, err) return } c.JSON(http.StatusOK, collection) } type addWordRequest struct { Words []string `json:"words" binding:"required"` } func (h *CollectionHandler) AddWords(c *gin.Context) { userID, ok := userIDFromContext(c) if !ok { c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"}) return } id, err := uuid.Parse(c.Param("id")) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "invalid id"}) return } var req addWordRequest if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } collection, err := h.service.AddWords(c.Request.Context(), userID, id, req.Words) if err != nil { h.handleError(c, err) return } c.JSON(http.StatusOK, collection) } func (h *CollectionHandler) DeleteWord(c *gin.Context) { userID, ok := userIDFromContext(c) if !ok { c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"}) return } id, err := uuid.Parse(c.Param("id")) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "invalid id"}) return } word := strings.TrimSpace(c.Query("word")) if word == "" { c.JSON(http.StatusBadRequest, gin.H{"error": "word is required"}) return } collection, err := h.service.DeleteWord(c.Request.Context(), userID, id, word) if err != nil { h.handleError(c, err) return } c.JSON(http.StatusOK, collection) } type createCollectionRequest struct { Name string `json:"name" binding:"required"` Lang model.Language `json:"lang" binding:"required"` } func (h *CollectionHandler) CreateCollection(c *gin.Context) { userID, ok := userIDFromContext(c) if !ok { c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"}) return } var req createCollectionRequest if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } collection, err := h.service.CreateCollection(c.Request.Context(), userID, req.Name, req.Lang) if err != nil { h.handleError(c, err) return } c.JSON(http.StatusCreated, collection) } func (h *CollectionHandler) DeleteCollection(c *gin.Context) { userID, ok := userIDFromContext(c) if !ok { c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"}) return } id, err := uuid.Parse(c.Param("id")) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "invalid id"}) return } err = h.service.DeleteCollection(c.Request.Context(), userID, id) if err != nil { h.handleError(c, err) return } c.Status(http.StatusOK) } func (h *CollectionHandler) handleError(c *gin.Context, err error) { switch { case errors.Is(err, repository.ErrCollectionNotFound): c.JSON(http.StatusNotFound, gin.H{"error": "collection not found"}) case errors.Is(err, service.ErrWordNotFound): c.JSON(http.StatusNotFound, gin.H{"error": "word not found"}) case errors.Is(err, service.ErrWordExists): c.JSON(http.StatusConflict, gin.H{"error": "word already exists"}) case errors.Is(err, service.ErrCollectionNotOwned): c.JSON(http.StatusForbidden, gin.H{"error": "you are not allowed to modify this collection"}) default: log.Printf("unhandled error: %v", err) c.JSON(http.StatusInternalServerError, gin.H{"error": "internal error"}) } }