blob: dd25e847cd307b12c091e33ed658a500c74620a7 [file] [log] [blame]
iomodocf795602021-03-07 23:14:15 +04001package app
2
3import (
iomodo352127d2021-03-26 20:10:32 +04004 "github.com/giolekva/pcloud/core/kg/log"
iomodocf795602021-03-07 23:14:15 +04005 "github.com/giolekva/pcloud/core/kg/model"
6 "github.com/pkg/errors"
7)
8
9// GetUser returns user
10func (a *App) GetUser(userID string) (*model.User, error) {
11 user, err := a.store.User().Get(userID)
12 if err != nil {
13 return nil, errors.Wrap(err, "can't get user from store")
14 }
15 return user, nil
16}
iomodo352127d2021-03-26 20:10:32 +040017
18func (a *App) CreateUser(user *model.User) (*model.User, error) {
19 if !a.isFirstUserAccount() {
20 return nil, errors.New("not a first user")
21 }
22
23 user.HashPassword()
24 updatedUser, err := a.store.User().Save(user)
25 if err != nil {
26 return nil, errors.Wrap(err, "can't save user to the DB")
27 }
28 return updatedUser, nil
29}
30
31func (a *App) isFirstUserAccount() bool {
32 count, err := a.store.User().Count()
33 if err != nil {
34 a.logger.Error("error fetching first user account", log.Err(err))
35 }
36 return count > 0
37}