Refactor api code: split between packages
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
package appcontext
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/nicksnyder/go-i18n/v2/i18n"
|
||||
"gorm.io/gorm"
|
||||
@@ -11,3 +14,13 @@ type AppContext struct {
|
||||
Db *gorm.DB
|
||||
I18n *i18n.Bundle
|
||||
}
|
||||
|
||||
func (ac AppContext) GetAuthenticatedUser() (model.User, error) {
|
||||
var user model.User
|
||||
username, userIsInContext := ac.C.Get("user")
|
||||
if !userIsInContext {
|
||||
return user, errors.New("User not found in context")
|
||||
}
|
||||
res := ac.Db.Where("name = ?", username).First(&user)
|
||||
return user, res.Error
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user