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

Change /groups/{group id}/users route #115

Merged
merged 17 commits into from
May 12, 2024

Conversation

zsiggg
Copy link
Contributor

@zsiggg zsiggg commented Feb 12, 2024

Editing existing routes and adding routes for stories user and add stories user panels on the frontend

Corresponding frontend PR: source-academy/frontend#2763

a userId could have multiple roles, if it is present in the usergroups table in multiple rows
(with different groupId).

however, since we specify the groupId in the /group/{groupId}/users route, it makes sense that we
only retrieve users that have that groupId.

- in controller, get groupId from context
- for each user obtained from the users table, call GetUserRoleByID function from usergroups model
- store the roles obtained in an array
- pass array to updated ListFrom function from users view, which now include a role attribute
in its JSON response
@zsiggg zsiggg self-assigned this Feb 12, 2024
@zsiggg zsiggg marked this pull request as draft February 12, 2024 12:37
@coveralls
Copy link

coveralls commented Feb 12, 2024

Coverage Status

coverage: 47.43% (-2.2%) from 49.653%
when pulling 35970d4 on change-/groups/{groupId}/users-route
into 7288818 on main.

previously, the DeleteUser function was not working as it chained
`.Delete(&user)` after `.First(&user)`. this resulted in a query as
shown, and its corresponding error:

* `ERROR: table name "users" specified more than once (SQLSTATE 42712)
[8.059ms] [rows:1] UPDATE "users" SET "deleted_at"='2024-02-12
20:52:41.02' FROM "users" WHERE id = 4 AND "users"."deleted_at" IS NULL
AND "users"."id" = 4`

the intent of `.First(&user)` was likely to store the user to be deleted
first in the `user` variable with the use of a `SELECT` SQL statement.
however, chaining another method at the end of a finisher method likely
led to some errors (see chaining
[here](https://gorm.io/docs/method_chaining.html)).

thus, this commit attempts to separate the two statements, preserving
the initial intent of storing the user to be deleted before deleting,
and then returning this user.
missing validation for parameters, but seemed unnecessary since there is
only one route with parameter validation (creating user)
@zsiggg zsiggg marked this pull request as ready for review February 18, 2024 17:35
@zsiggg zsiggg requested a review from RichDom2185 February 18, 2024 17:35
@RichDom2185
Copy link
Member

Relevant frontend PR: source-academy/frontend#2763

view/users/list.go Outdated Show resolved Hide resolved
Comment on lines +48 to +54
for i, user := range users {
roles[i], err = model.GetUserRoleByID(db, user.ID, *groupId)
if err != nil {
logrus.Error(err)
return err
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

N + 1 query problem

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filed as an issue: #121.

We can just merge first as it's not critical at the moment.

Copy link
Member

@RichDom2185 RichDom2185 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, sorry for the delayed review, LGTM, thanks! Just waiting for the frontend PR to also pass review before merging at the same time.

@RichDom2185 RichDom2185 merged commit 840e79d into main May 12, 2024
4 checks passed
@RichDom2185 RichDom2185 deleted the change-/groups/{groupId}/users-route branch May 12, 2024 09:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants