Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: createUser to saveUser, and added update function to model #90

Merged
merged 1 commit into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions backend/cmd/api/internal/controller/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ func GetUser(ctx context.Context, userid graphql.ID) (*model.User, error) {
return model.FindUserById(ctx, userid)
}

func CreateUser(ctx context.Context, email, fullname, role string) (*model.User, error) {
currentUser := auth.UserFromContext(ctx)
func SaveUser(ctx context.Context, userid *graphql.ID, email, fullname, role string) (*model.User, error) {
authenticatedUser := auth.UserFromContext(ctx)

if !currentUser.IsAdmin() {
if !authenticatedUser.IsAdmin() {
return nil, &ForbiddenError{}
}

Expand All @@ -43,8 +43,10 @@ func CreateUser(ctx context.Context, email, fullname, role string) (*model.User,
return nil, err
}

if userid != nil {
return model.UpdateUser(ctx, *userid, email, fullname, role)
}
return model.NewUser(ctx, email, fullname, role)

}

func SaveUserFismaSystems(ctx context.Context, userid string, fismasystemids []int32) (*model.User, error) {
Expand Down
2 changes: 1 addition & 1 deletion backend/cmd/api/internal/graph/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type User {
}

type Mutation {
createUser(email: String!, fullname: String!, role: String!): UserMutationResponse!
saveUser(userid: ID, email: String!, fullname: String!, role: String!): UserMutationResponse!
saveFunctionScore(scoreid: ID, fismasystemid: Int!, functionid: Int!, score: Float!, notes: String): FunctionScoreMutationReponse!
assignFismaSystems(userid: String!, fismasystemids: [Int!]!): UserMutationResponse!
unassignFismaSystems(userid: String!, fismasystemids: [Int!]!): UserMutationResponse!
Expand Down
12 changes: 9 additions & 3 deletions backend/cmd/api/internal/graph/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,21 @@ func (r *RootResolver) User(ctx context.Context, args struct{ Userid graphql.ID
return controller.GetUser(ctx, args.Userid)
}

func (r *RootResolver) CreateUser(ctx context.Context, args struct {
func (r *RootResolver) SaveUser(ctx context.Context, args struct {
Userid *graphql.ID
Email string
Fullname string
Role string
}) *UserMutationResponse {
res := UserMutationResponse{}
user, err := controller.CreateUser(ctx, args.Email, args.Fullname, args.Role)
res.SetCreated().SetError(err)
user, err := controller.SaveUser(ctx, args.Userid, args.Email, args.Fullname, args.Role)
res.User = user
if args.Userid == nil {
res.SetCreated()
} else {
res.SetOK()
}
res.SetError(err)
return &res
}

Expand Down
28 changes: 16 additions & 12 deletions backend/cmd/api/internal/model/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ func NewUser(ctx context.Context, email, fullname, role string) (*User, error) {
return FindUserByEmail(ctx, email)
}

func UpdateUser(ctx context.Context, userid graphql.ID, email, fullname, role string) (*User, error) {
sql := "UPDATE public.users SET email=$2, fullname=$3, role=$4 WHERE userid=$1"
_, err := exec(ctx, sql, userid, email, fullname, role)
if err != nil {
return nil, err
}

return FindUserById(ctx, userid)
}

func (u *User) IsAdmin() bool {
return u.Role == "ADMIN"
}
Expand Down Expand Up @@ -60,25 +70,19 @@ func FindUsers(ctx context.Context) ([]*User, error) {

// FindUserByIf queries the database for a User with the given ID and returns *User or error
func FindUserById(ctx context.Context, userid graphql.ID) (*User, error) {
sql := `SELECT users.userid, email, fullname, role, ARRAY_AGG(fismasystemid) AS fismasystems FROM users
LEFT JOIN users_fismasystems on users_fismasystems.userid = users.userid
WHERE users.userid=$1
GROUP BY users.userid
`
return findUser(ctx, sql, []any{userid})
return findUser(ctx, "users.userid=$1", []any{userid})
}

// FindUserByEmail queries the database for a User with the given email address and returns *User or error
func FindUserByEmail(ctx context.Context, email string) (*User, error) {
return findUser(ctx, "users.email=$1", []any{email})
}

func findUser(ctx context.Context, where string, args []any) (*User, error) {
sql := `SELECT users.userid, email, fullname, role, ARRAY_AGG(fismasystemid) AS fismasystems FROM users
LEFT JOIN users_fismasystems on users_fismasystems.userid = users.userid
WHERE users.email=$1
GROUP BY users.userid
WHERE ` + where + ` GROUP BY users.userid
`
return findUser(ctx, sql, []any{email})
}

func findUser(ctx context.Context, sql string, args []any) (*User, error) {
row, err := queryRow(ctx, sql, args...)
if err != nil {
return nil, err
Expand Down