API package refactor

This commit is contained in:
Manuel Forcén Muñoz 2025-02-12 19:29:00 +01:00
parent a7cd86962e
commit bc6b57bc54
14 changed files with 474 additions and 416 deletions

26
apis/extract.go Normal file
View file

@ -0,0 +1,26 @@
package apis
import (
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"net/http"
"planner/core"
)
func extractUser(orm *gorm.DB, c *gin.Context) *core.User {
username, _, ok := c.Request.BasicAuth()
if !ok {
c.Status(http.StatusUnauthorized)
return nil
}
u := core.User{
Username: username,
}
result := orm.Take(&u)
if result.Error != nil {
c.String(http.StatusNotFound, "Unable to find user "+username)
return nil
}
return &u
}