mirror of
https://github.com/hay-kot/homebox.git
synced 2025-07-03 17:18:36 +00:00
align types with new db schema
This commit is contained in:
parent
63cfeffc4d
commit
b83505104a
30 changed files with 1491 additions and 263 deletions
|
@ -3,7 +3,7 @@ package services
|
|||
import (
|
||||
"context"
|
||||
|
||||
"github.com/hay-kot/content/backend/internal/types"
|
||||
"github.com/hay-kot/content/backend/ent"
|
||||
)
|
||||
|
||||
type contextKeys struct {
|
||||
|
@ -17,16 +17,16 @@ var (
|
|||
|
||||
// SetUserCtx is a helper function that sets the ContextUser and ContextUserToken
|
||||
// values within the context of a web request (or any context).
|
||||
func SetUserCtx(ctx context.Context, user *types.UserOut, token string) context.Context {
|
||||
func SetUserCtx(ctx context.Context, user *ent.User, token string) context.Context {
|
||||
ctx = context.WithValue(ctx, ContextUser, user)
|
||||
ctx = context.WithValue(ctx, ContextUserToken, token)
|
||||
return ctx
|
||||
}
|
||||
|
||||
// UseUserCtx is a helper function that returns the user from the context.
|
||||
func UseUserCtx(ctx context.Context) *types.UserOut {
|
||||
func UseUserCtx(ctx context.Context) *ent.User {
|
||||
if val := ctx.Value(ContextUser); val != nil {
|
||||
return val.(*types.UserOut)
|
||||
return val.(*ent.User)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -5,12 +5,12 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/hay-kot/content/backend/internal/types"
|
||||
"github.com/hay-kot/content/backend/ent"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_SetAuthContext(t *testing.T) {
|
||||
user := &types.UserOut{
|
||||
user := &ent.User{
|
||||
ID: uuid.New(),
|
||||
}
|
||||
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"context"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/hay-kot/content/backend/ent"
|
||||
"github.com/hay-kot/content/backend/internal/repo"
|
||||
"github.com/hay-kot/content/backend/internal/types"
|
||||
)
|
||||
|
@ -12,27 +13,27 @@ type AdminService struct {
|
|||
repos *repo.AllRepos
|
||||
}
|
||||
|
||||
func (svc *AdminService) Create(ctx context.Context, usr types.UserCreate) (types.UserOut, error) {
|
||||
func (svc *AdminService) Create(ctx context.Context, usr types.UserCreate) (*ent.User, error) {
|
||||
return svc.repos.Users.Create(ctx, usr)
|
||||
}
|
||||
|
||||
func (svc *AdminService) GetAll(ctx context.Context) ([]types.UserOut, error) {
|
||||
func (svc *AdminService) GetAll(ctx context.Context) ([]*ent.User, error) {
|
||||
return svc.repos.Users.GetAll(ctx)
|
||||
}
|
||||
|
||||
func (svc *AdminService) GetByID(ctx context.Context, id uuid.UUID) (types.UserOut, error) {
|
||||
func (svc *AdminService) GetByID(ctx context.Context, id uuid.UUID) (*ent.User, error) {
|
||||
return svc.repos.Users.GetOneId(ctx, id)
|
||||
}
|
||||
|
||||
func (svc *AdminService) GetByEmail(ctx context.Context, email string) (types.UserOut, error) {
|
||||
func (svc *AdminService) GetByEmail(ctx context.Context, email string) (*ent.User, error) {
|
||||
return svc.repos.Users.GetOneEmail(ctx, email)
|
||||
}
|
||||
|
||||
func (svc *AdminService) UpdateProperties(ctx context.Context, ID uuid.UUID, data types.UserUpdate) (types.UserOut, error) {
|
||||
func (svc *AdminService) UpdateProperties(ctx context.Context, ID uuid.UUID, data types.UserUpdate) (*ent.User, error) {
|
||||
err := svc.repos.Users.Update(ctx, ID, data)
|
||||
|
||||
if err != nil {
|
||||
return types.UserOut{}, err
|
||||
return &ent.User{}, err
|
||||
}
|
||||
|
||||
return svc.repos.Users.GetOneId(ctx, ID)
|
||||
|
|
|
@ -6,6 +6,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/hay-kot/content/backend/ent"
|
||||
"github.com/hay-kot/content/backend/internal/repo"
|
||||
"github.com/hay-kot/content/backend/internal/types"
|
||||
"github.com/hay-kot/content/backend/pkgs/hasher"
|
||||
|
@ -22,17 +23,41 @@ type UserService struct {
|
|||
repos *repo.AllRepos
|
||||
}
|
||||
|
||||
func (svc *UserService) RegisterUser(ctx context.Context, data types.UserRegistration) (*ent.User, error) {
|
||||
group, err := svc.repos.Groups.Create(ctx, data.GroupName)
|
||||
if err != nil {
|
||||
return &ent.User{}, err
|
||||
}
|
||||
|
||||
hashed, _ := hasher.HashPassword(data.User.Password)
|
||||
|
||||
usrCreate := types.UserCreate{
|
||||
Name: data.User.Name,
|
||||
Email: data.User.Email,
|
||||
Password: hashed,
|
||||
IsSuperuser: false,
|
||||
GroupID: group.ID,
|
||||
}
|
||||
|
||||
usr, err := svc.repos.Users.Create(ctx, usrCreate)
|
||||
if err != nil {
|
||||
return &ent.User{}, err
|
||||
}
|
||||
|
||||
return usr, nil
|
||||
}
|
||||
|
||||
// GetSelf returns the user that is currently logged in based of the token provided within
|
||||
func (svc *UserService) GetSelf(ctx context.Context, requestToken string) (types.UserOut, error) {
|
||||
func (svc *UserService) GetSelf(ctx context.Context, requestToken string) (*ent.User, error) {
|
||||
hash := hasher.HashToken(requestToken)
|
||||
return svc.repos.AuthTokens.GetUserFromToken(ctx, hash)
|
||||
}
|
||||
|
||||
func (svc *UserService) UpdateSelf(ctx context.Context, ID uuid.UUID, data types.UserUpdate) (types.UserOut, error) {
|
||||
func (svc *UserService) UpdateSelf(ctx context.Context, ID uuid.UUID, data types.UserUpdate) (*ent.User, error) {
|
||||
err := svc.repos.Users.Update(ctx, ID, data)
|
||||
|
||||
if err != nil {
|
||||
return types.UserOut{}, err
|
||||
return &ent.User{}, err
|
||||
}
|
||||
|
||||
return svc.repos.Users.GetOneId(ctx, ID)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue