-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from 11 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
7b74ea8
fix: inject user id into context in development env
zsiggg 7d28577
feat: add ID column to list view of Users
zsiggg 335ef1e
feat: add role attribute to /groups/{groupId}/users response
zsiggg 1db8c90
Merge branch 'main' into change-/groups/{groupId}/users-route
zsiggg 62d5baf
fix: DeleteUser in users model
zsiggg adecceb
feat: function to update row in users model
zsiggg 0fa8e0b
feat: function to update row in usergroups model
zsiggg fa80f01
feat: add /users/{userID}/role put route
zsiggg f708b88
fix: package name for update role's params
zsiggg 1eee2bc
Merge branch 'main' into change-/groups/{groupId}/users-route
zsiggg 3c33471
Merge branch 'main' into change-/groups/{groupId}/users-route
RichDom2185 089952e
Use range iterator for users list view
RichDom2185 48f0768
Remove unused UpdateUser function
RichDom2185 f8e1ab3
Prevent self-update of roles
RichDom2185 553fff1
Fix error message
RichDom2185 cd0df84
Add `UpdateRole` param validation
RichDom2185 35970d4
Refactor logic
RichDom2185 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package usergroups | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"strconv" | ||
|
||
"github.com/go-chi/chi/v5" | ||
"github.com/sirupsen/logrus" | ||
"github.com/source-academy/stories-backend/controller" | ||
"github.com/source-academy/stories-backend/internal/auth" | ||
"github.com/source-academy/stories-backend/internal/database" | ||
apierrors "github.com/source-academy/stories-backend/internal/errors" | ||
userpermissiongroups "github.com/source-academy/stories-backend/internal/permissiongroups/users" | ||
"github.com/source-academy/stories-backend/model" | ||
usergroupparams "github.com/source-academy/stories-backend/params/usergroups" | ||
userviews "github.com/source-academy/stories-backend/view/users" | ||
) | ||
|
||
func HandleUpdateRole(w http.ResponseWriter, r *http.Request) error { | ||
userIDStr := chi.URLParam(r, "userID") | ||
userID, err := strconv.Atoi(userIDStr) | ||
if err != nil { | ||
return apierrors.ClientBadRequestError{ | ||
Message: fmt.Sprintf("Invalid userID: %v", err), | ||
} | ||
} | ||
|
||
err = auth.CheckPermissions(r, userpermissiongroups.Update(uint(userID))) | ||
if err != nil { | ||
logrus.Error(err) | ||
return apierrors.ClientForbiddenError{ | ||
Message: fmt.Sprintf("Error batch creating users: %v", err), | ||
} | ||
} | ||
|
||
var params usergroupparams.UpdateRole | ||
if err := json.NewDecoder(r.Body).Decode(¶ms); err != nil { | ||
e, ok := err.(*json.UnmarshalTypeError) | ||
if !ok { | ||
logrus.Error(err) | ||
return apierrors.ClientBadRequestError{ | ||
Message: fmt.Sprintf("Bad JSON parsing: %v", err), | ||
} | ||
} | ||
|
||
// TODO: Investigate if we should use errors.Wrap instead | ||
return apierrors.ClientUnprocessableEntityError{ | ||
Message: fmt.Sprintf("Invalid JSON format: %s should be a %s.", e.Field, e.Type), | ||
} | ||
} | ||
|
||
// TODO: add validation for updating user? | ||
// err = params.Validate() | ||
// if err != nil { | ||
// logrus.Error(err) | ||
// return apierrors.ClientUnprocessableEntityError{ | ||
// Message: fmt.Sprintf("JSON validation failed: %v", err), | ||
// } | ||
// } | ||
|
||
userGroupModel := *params.ToModel() | ||
|
||
// Get DB instance | ||
db, err := database.GetDBFrom(r) | ||
if err != nil { | ||
logrus.Error(err) | ||
return err | ||
} | ||
|
||
userGroup, err := model.UpdateUserGroupByUserID(db, uint(userID), &userGroupModel) | ||
if err != nil { | ||
logrus.Error(err) | ||
return err | ||
} | ||
|
||
user, err := model.GetUserByID(db, userID) | ||
if err != nil { | ||
logrus.Error(err) | ||
return err | ||
} | ||
|
||
controller.EncodeJSONResponse(w, userviews.SummaryFrom(user, userGroup)) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package usergroupparams | ||
|
||
import ( | ||
groupenums "github.com/source-academy/stories-backend/internal/enums/groups" | ||
"github.com/source-academy/stories-backend/model" | ||
) | ||
|
||
type UpdateRole struct { | ||
Role groupenums.Role `json:"role"` | ||
} | ||
|
||
func (params *UpdateRole) ToModel() *model.UserGroup { | ||
return &model.UserGroup{ | ||
Role: params.Role, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
N + 1 query problem
There was a problem hiding this comment.
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.