package handler import ( "errors" "log" "net/http" "strings" "github.com/dusiburg/fancy-api/internal/model" "github.com/dusiburg/fancy-api/internal/provider/examples" "github.com/dusiburg/fancy-api/internal/service" "github.com/gin-gonic/gin" ) type ExamplesHandler struct { service *service.ExamplesService } func NewExamplesHandler(service *service.ExamplesService) *ExamplesHandler { return &ExamplesHandler{service: service} } func (h *ExamplesHandler) GenerateExamples(c *gin.Context) { word := strings.ToLower(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 } userID, ok := userIDFromContext(c) if !ok { c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"}) return } result, err := h.service.GenerateExamples(c, userID, word, lang) if err != nil { log.Printf("generate examples %q [%s]: %v", word, lang, err) switch { case errors.Is(err, examples.ErrUnsupportedLanguage): c.JSON(http.StatusBadRequest, gin.H{"error": "unsupported language"}) case errors.Is(err, examples.ErrQuotaExceeded): c.JSON(http.StatusTooManyRequests, gin.H{"error": "limit exceeded"}) default: c.JSON(http.StatusInternalServerError, gin.H{"error": "examples generation failed"}) } return } c.JSON(http.StatusOK, result) }