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

This commit is contained in:
Ivan Rezchikov
2026-09-18 00:45:51 +03:00
commit 628dd6384b
149 changed files with 11478 additions and 0 deletions
+76
View File
@@ -0,0 +1,76 @@
package handler
import (
"errors"
"log"
"net/http"
"strings"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/dusiburg/fancy-api/internal/service"
)
type AuthHandler struct {
service *service.AuthService
}
func NewAuthHandler(service *service.AuthService) *AuthHandler {
return &AuthHandler{service: service}
}
func (h *AuthHandler) CreateSession(c *gin.Context) {
accessToken := strings.TrimPrefix(c.GetHeader("Authorization"), "Bearer ")
if accessToken == "" {
c.JSON(http.StatusUnauthorized, gin.H{"error": "access token is required"})
return
}
sessionToken, err := h.service.CreateSession(c, accessToken)
if err != nil {
log.Printf("create session: %v", err)
switch {
case errors.Is(err, service.ErrKotosoftUnauthorized):
c.JSON(http.StatusUnauthorized, gin.H{"error": "invalid access token"})
default:
c.JSON(http.StatusInternalServerError, gin.H{"error": "session creation failed"})
}
return
}
c.JSON(http.StatusOK, gin.H{"session_token": sessionToken})
}
func (h *AuthHandler) Me(c *gin.Context) {
userID, ok := c.Get("user_id")
if !ok {
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
return
}
user, err := h.service.GetUser(c, userID.(uuid.UUID))
if err != nil {
log.Printf("get user %s: %v", userID, err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to fetch user"})
return
}
c.JSON(http.StatusOK, user)
}
func (h *AuthHandler) DeleteSession(c *gin.Context) {
token := strings.TrimPrefix(c.GetHeader("Authorization"), "Bearer ")
if token == "" {
c.JSON(http.StatusUnauthorized, gin.H{"error": "session token is required"})
return
}
if err := h.service.RevokeSession(c, token); err != nil {
log.Printf("revoke session: %v", err)
c.JSON(http.StatusUnauthorized, gin.H{"error": "invalid session token"})
return
}
c.Status(http.StatusNoContent)
}
@@ -0,0 +1,250 @@
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"})
}
}
@@ -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)
}
+15
View File
@@ -0,0 +1,15 @@
package handler
import (
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
func userIDFromContext(c *gin.Context) (uuid.UUID, bool) {
val, exists := c.Get("user_id")
if !exists {
return uuid.UUID{}, false
}
id, ok := val.(uuid.UUID)
return id, ok
}
@@ -0,0 +1,70 @@
package handler
import (
"errors"
"log"
"net/http"
"strings"
"github.com/dusiburg/fancy-api/internal/model"
"github.com/dusiburg/fancy-api/internal/provider/dictionary"
"github.com/dusiburg/fancy-api/internal/provider/llm"
"github.com/dusiburg/fancy-api/internal/service"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
type DictionaryHandler struct {
service *service.DictionaryService
}
func NewDictionaryHandler(svc *service.DictionaryService) *DictionaryHandler {
return &DictionaryHandler{service: svc}
}
func userID(c *gin.Context) (uuid.UUID, bool) {
val, exists := c.Get("user_id")
if !exists {
return uuid.UUID{}, false
}
id, ok := val.(uuid.UUID)
return id, ok
}
func (h *DictionaryHandler) Lookup(c *gin.Context) {
userID, ok := userIDFromContext(c)
if !ok {
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
return
}
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
}
result, err := h.service.Lookup(c.Request.Context(), userID, word, lang)
if err != nil {
log.Printf("lookup %q [%s]: %v", word, lang, err)
switch {
case errors.Is(err, dictionary.ErrWordNotFound):
c.JSON(http.StatusNotFound, gin.H{"error": "word not found"})
case errors.Is(err, dictionary.ErrUnsupportedLanguage):
c.JSON(http.StatusBadRequest, gin.H{"error": "unsupported language"})
case errors.Is(err, llm.ErrQuotaExceeded):
c.JSON(http.StatusTooManyRequests, gin.H{"error": "limit exceeded"})
default:
c.JSON(http.StatusInternalServerError, gin.H{"error": "getting dictionary data failed"})
}
return
}
c.JSON(http.StatusOK, result)
}
@@ -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/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)
}
@@ -0,0 +1,63 @@
package handler
import (
"errors"
"net/http"
"strings"
"github.com/gin-gonic/gin"
"github.com/dusiburg/fancy-api/internal/model"
"github.com/dusiburg/fancy-api/internal/provider/translation"
"github.com/dusiburg/fancy-api/internal/service"
)
type TranslationHandler struct {
service *service.TranslationService
}
func NewTranslationHandler(svc *service.TranslationService) *TranslationHandler {
return &TranslationHandler{service: svc}
}
type translateRequest struct {
Text string `json:"text" binding:"required"`
From string `json:"from"`
To string `json:"to"`
}
func (h *TranslationHandler) Translate(c *gin.Context) {
var req translateRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
from, ok := model.ParseLanguage(req.From)
if !ok {
c.JSON(http.StatusBadRequest, gin.H{"error": "unsupported source language"})
return
}
to, ok := model.ParseLanguage(req.To)
if !ok {
c.JSON(http.StatusBadRequest, gin.H{"error": "unsupported target language"})
return
}
text := strings.TrimSpace(req.Text)
result, err := h.service.Translate(c.Request.Context(), text, from, to)
if err != nil {
switch {
case errors.Is(err, translation.ErrUnsupportedLanguage):
c.JSON(http.StatusBadRequest, gin.H{"error": "unsupported language"})
case errors.Is(err, translation.ErrQuotaExceeded):
c.JSON(http.StatusTooManyRequests, gin.H{"error": "translation quota exceeded"})
default:
c.JSON(http.StatusInternalServerError, gin.H{"error": "translation failed"})
}
return
}
c.JSON(http.StatusOK, result)
}
+34
View File
@@ -0,0 +1,34 @@
package handler
import (
"log"
"net/http"
"github.com/dusiburg/fancy-api/internal/service"
"github.com/gin-gonic/gin"
)
type UsageHandler struct {
service *service.UsageService
}
func NewUsageHandler(svc *service.UsageService) *UsageHandler {
return &UsageHandler{service: svc}
}
func (h *UsageHandler) Get(c *gin.Context) {
userID, ok := userIDFromContext(c)
if !ok {
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
return
}
usage, err := h.service.GetUsage(c.Request.Context(), userID)
if err != nil {
log.Printf("getting usage: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "getting usage failed"})
return
}
c.JSON(http.StatusOK, usage)
}