version 1
Build and Deploy / test-backend (push) Successful in 1m11s
Build and Deploy / lint-frontend (push) Successful in 21s
Build and Deploy / build-backend (push) Successful in 58s
Build and Deploy / deploy (push) Canceled after 0s
Build and Deploy / build-frontend (push) Canceled after 5m38s
Build and Deploy / test-backend (push) Successful in 1m11s
Build and Deploy / lint-frontend (push) Successful in 21s
Build and Deploy / build-backend (push) Successful in 58s
Build and Deploy / deploy (push) Canceled after 0s
Build and Deploy / build-frontend (push) Canceled after 5m38s
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/dusiburg/fancy-api/internal/model"
|
||||
"github.com/dusiburg/fancy-api/internal/provider/collocations"
|
||||
"github.com/dusiburg/fancy-api/internal/service"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type CollocationsHandler struct {
|
||||
service *service.CollocationsService
|
||||
}
|
||||
|
||||
func NewCollocationsHandler(service *service.CollocationsService) *CollocationsHandler {
|
||||
return &CollocationsHandler{service: service}
|
||||
}
|
||||
|
||||
func (h *CollocationsHandler) GenerateCollocations(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.GenerateCollocations(c, userID, word, lang)
|
||||
if err != nil {
|
||||
log.Printf("generate collocations %q [%s]: %v", word, lang, err)
|
||||
switch {
|
||||
case errors.Is(err, collocations.ErrUnsupportedLanguage):
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "unsupported language"})
|
||||
case errors.Is(err, collocations.ErrQuotaExceeded):
|
||||
c.JSON(http.StatusTooManyRequests, gin.H{"error": "limit exceeded"})
|
||||
default:
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "collocations generation failed"})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, result)
|
||||
}
|
||||
Reference in New Issue
Block a user