update data contract for users

This commit is contained in:
Hayden 2022-08-30 16:44:49 -08:00
parent a9f53a4671
commit 1107904f47
2 changed files with 11 additions and 16 deletions

View file

@ -23,11 +23,11 @@ type UserService struct {
repos *repo.AllRepos repos *repo.AllRepos
} }
func (UserService) toOutUser(user *ent.User, err error) (types.UserOut, error) { func (UserService) toOutUser(user *ent.User, err error) (*types.UserOut, error) {
if err != nil { if err != nil {
return types.UserOut{}, err return &types.UserOut{}, err
} }
return types.UserOut{ return &types.UserOut{
ID: user.ID, ID: user.ID,
Name: user.Name, Name: user.Name,
Email: user.Email, Email: user.Email,
@ -37,10 +37,10 @@ func (UserService) toOutUser(user *ent.User, err error) (types.UserOut, error) {
}, nil }, nil
} }
func (svc *UserService) RegisterUser(ctx context.Context, data types.UserRegistration) (*ent.User, error) { func (svc *UserService) RegisterUser(ctx context.Context, data types.UserRegistration) (*types.UserOut, error) {
group, err := svc.repos.Groups.Create(ctx, data.GroupName) group, err := svc.repos.Groups.Create(ctx, data.GroupName)
if err != nil { if err != nil {
return &ent.User{}, err return &types.UserOut{}, err
} }
hashed, _ := hasher.HashPassword(data.User.Password) hashed, _ := hasher.HashPassword(data.User.Password)
@ -53,28 +53,23 @@ func (svc *UserService) RegisterUser(ctx context.Context, data types.UserRegistr
GroupID: group.ID, GroupID: group.ID,
} }
usr, err := svc.repos.Users.Create(ctx, usrCreate) return svc.toOutUser(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 // 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) (*types.UserOut, error) {
hash := hasher.HashToken(requestToken) hash := hasher.HashToken(requestToken)
return svc.toOutUser(svc.repos.AuthTokens.GetUserFromToken(ctx, hash)) return svc.toOutUser(svc.repos.AuthTokens.GetUserFromToken(ctx, hash))
} }
func (svc *UserService) UpdateSelf(ctx context.Context, ID uuid.UUID, data types.UserUpdate) (*ent.User, error) { func (svc *UserService) UpdateSelf(ctx context.Context, ID uuid.UUID, data types.UserUpdate) (*types.UserOut, error) {
err := svc.repos.Users.Update(ctx, ID, data) err := svc.repos.Users.Update(ctx, ID, data)
if err != nil { if err != nil {
return &ent.User{}, err return &types.UserOut{}, err
} }
return svc.repos.Users.GetOneId(ctx, ID) return svc.toOutUser(svc.repos.Users.GetOneId(ctx, ID))
} }
// ============================================================================ // ============================================================================

View file

@ -30,7 +30,7 @@ type UserCreate struct {
GroupID uuid.UUID `json:"groupID"` GroupID uuid.UUID `json:"groupID"`
} }
func (u *UserCreate) Validate() error { func (u UserCreate) Validate() error {
if u.Name == "" { if u.Name == "" {
return ErrNameEmpty return ErrNameEmpty
} }