forked from mirrors/homebox
31b34241e0
* change /content/ -> /homebox/ * add cache to code generators * update env variables to set data storage * update env variables * set env variables in prod container * implement attachment post route (WIP) * get attachment endpoint * attachment download * implement string utilities lib * implement generic drop zone * use explicit truncate * remove clean dir * drop strings composable for lib * update item types and add attachments * add attachment API * implement service context * consolidate API code * implement editing attachments * implement upload limit configuration * improve error handling * add docs for max upload size * fix test cases
83 lines
1.8 KiB
Go
83 lines
1.8 KiB
Go
package repo
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/hay-kot/homebox/backend/ent"
|
|
"github.com/hay-kot/homebox/backend/ent/user"
|
|
"github.com/hay-kot/homebox/backend/internal/types"
|
|
)
|
|
|
|
type UserRepository struct {
|
|
db *ent.Client
|
|
}
|
|
|
|
func (e *UserRepository) GetOneId(ctx context.Context, id uuid.UUID) (*ent.User, error) {
|
|
return e.db.User.Query().
|
|
Where(user.ID(id)).
|
|
WithGroup().
|
|
Only(ctx)
|
|
}
|
|
|
|
func (e *UserRepository) GetOneEmail(ctx context.Context, email string) (*ent.User, error) {
|
|
return e.db.User.Query().
|
|
Where(user.Email(email)).
|
|
WithGroup().
|
|
Only(ctx)
|
|
}
|
|
|
|
func (e *UserRepository) GetAll(ctx context.Context) ([]*ent.User, error) {
|
|
return e.db.User.Query().WithGroup().All(ctx)
|
|
}
|
|
|
|
func (e *UserRepository) Create(ctx context.Context, usr types.UserCreate) (*ent.User, error) {
|
|
err := usr.Validate()
|
|
if err != nil {
|
|
return &ent.User{}, err
|
|
}
|
|
|
|
entUser, err := e.db.User.
|
|
Create().
|
|
SetName(usr.Name).
|
|
SetEmail(usr.Email).
|
|
SetPassword(usr.Password).
|
|
SetIsSuperuser(usr.IsSuperuser).
|
|
SetGroupID(usr.GroupID).
|
|
Save(ctx)
|
|
if err != nil {
|
|
return entUser, err
|
|
}
|
|
|
|
return e.GetOneId(ctx, entUser.ID)
|
|
}
|
|
|
|
func (e *UserRepository) Update(ctx context.Context, ID uuid.UUID, data types.UserUpdate) error {
|
|
q := e.db.User.Update().
|
|
Where(user.ID(ID)).
|
|
SetName(data.Name).
|
|
SetEmail(data.Email)
|
|
|
|
_, err := q.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
func (e *UserRepository) Delete(ctx context.Context, id uuid.UUID) error {
|
|
_, err := e.db.User.Delete().Where(user.ID(id)).Exec(ctx)
|
|
return err
|
|
}
|
|
|
|
func (e *UserRepository) DeleteAll(ctx context.Context) error {
|
|
_, err := e.db.User.Delete().Exec(ctx)
|
|
return err
|
|
}
|
|
|
|
func (e *UserRepository) GetSuperusers(ctx context.Context) ([]*ent.User, error) {
|
|
users, err := e.db.User.Query().Where(user.IsSuperuser(true)).All(ctx)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return users, nil
|
|
}
|