Skip to content

Commit b09114c

Browse files
committed
Refactor repository Create to accept a model param
Signed-off-by: Carlos Martín <[email protected]>
1 parent 01f1cbc commit b09114c

4 files changed

Lines changed: 23 additions & 16 deletions

File tree

server/handler/auth.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,13 @@ func OAuthCallback(
5151
}
5252

5353
if user == nil {
54-
user, err = userRepo.Create(ghUser.Login, ghUser.Username, ghUser.AvatarURL, model.Requester)
54+
user = &model.User{
55+
Login: ghUser.Login,
56+
Username: ghUser.Username,
57+
AvatarURL: ghUser.AvatarURL,
58+
Role: model.Requester}
59+
60+
err = userRepo.Create(user)
5561
if err != nil {
5662
logger.Errorf("can't create user: %s", err)
5763
write(w, r, serializer.NewEmptyResponse(), err)

server/repository/assignments.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,19 @@ func (repo *Assignments) Initialize(userID int, experimentID int) ([]*model.Assi
5656
}
5757

5858
// Create stores an Assignment into the DB, and returns that new Assignment
59-
func (repo *Assignments) Create(
60-
userID, pairID, experimentID, answer, duration int) (*model.Assignment, error) {
59+
func (repo *Assignments) Create(as *model.Assignment) error {
6160

6261
_, err := repo.DB.Exec(
6362
`INSERT INTO assignments (user_id, pair_id, experiment_id, answer, duration)
6463
VALUES ($1, $2, $3, $4, $5)`,
65-
userID, pairID, experimentID, answer, duration)
64+
as.UserID, as.PairID, as.ExperimentID, as.Answer, as.Duration)
6665

6766
if err != nil {
68-
return nil, err
67+
return err
6968
}
7069

71-
return repo.Get(userID, pairID)
70+
as, err = repo.Get(as.UserID, as.PairID)
71+
return err
7272
}
7373

7474
// getWithQuery builds a Assignment from the given sql QueryRow. If the

server/repository/experiments.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,21 @@ type Experiments struct {
1313
}
1414

1515
// Create stores an Experiment into the DB, and returns that new Experiment
16-
func (repo *Experiments) Create(name, description string) (*model.Experiment, error) {
16+
func (repo *Experiments) Create(exp *model.Experiment) error {
1717
// TODO: for now this method is not used, but if we allow experiment creation
1818
// the name should be safely escaped
19-
return nil, fmt.Errorf("Not implemented")
19+
return fmt.Errorf("Not implemented")
2020

2121
_, err := repo.DB.Exec(
2222
"INSERT INTO experiments (name, description) VALUES ($1, $2)",
23-
name, description)
23+
exp.Name, exp.Description)
2424

2525
if err != nil {
26-
return nil, err
26+
return err
2727
}
2828

29-
return repo.Get(name)
29+
exp, err = repo.Get(exp.Name)
30+
return err
3031
}
3132

3233
// getWithQuery builds an Experiment from the given sql QueryRow. If the

server/repository/users.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@ type Users struct {
1313
}
1414

1515
// Create stores a User into the DB, and returns that new User
16-
func (repo *Users) Create(
17-
login, username, avatarURL string, role model.Role) (*model.User, error) {
16+
func (repo *Users) Create(user *model.User) error {
1817

1918
_, err := repo.DB.Exec(
2019
"INSERT INTO users (login, username, avatar_url, role) VALUES ($1, $2, $3, $4)",
21-
login, username, avatarURL, role)
20+
user.Login, user.Username, user.AvatarURL, user.Role)
2221

2322
if err != nil {
24-
return nil, err
23+
return err
2524
}
2625

27-
return repo.Get(login)
26+
user, err = repo.Get(user.Login)
27+
return err
2828
}
2929

3030
// getWithQuery builds a User from the given sql QueryRow. If the User does not

0 commit comments

Comments
 (0)