-
Notifications
You must be signed in to change notification settings - Fork 63
Class selection on /profile corresponds to CSA/CSP/CSSE/CSH group #173
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
Open
adikatre
wants to merge
5
commits into
Open-Coding-Society:master
Choose a base branch
from
adikatre:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
77dca4a
Fix group creation on spring
adikatre 54ec15c
class corresponds to group name when selected on profile page
adikatre 3a99cfa
class group membership test
adikatre c0ec563
Update AGENTS.md
adikatre d30828c
default class-based groups upon db init
adikatre 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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
116 changes: 116 additions & 0 deletions
116
src/main/java/com/open/spring/mvc/groups/ClassGroupMembershipService.java
This file contains hidden or 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,116 @@ | ||
| package com.open.spring.mvc.groups; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.LinkedHashSet; | ||
| import java.util.List; | ||
| import java.util.Locale; | ||
| import java.util.Map; | ||
| import java.util.NoSuchElementException; | ||
| import java.util.Set; | ||
|
|
||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| import com.open.spring.mvc.person.Person; | ||
| import com.open.spring.mvc.person.PersonJpaRepository; | ||
|
|
||
| @Service | ||
| public class ClassGroupMembershipService { | ||
| static final List<String> CLASS_GROUP_NAMES = List.of("CSA", "CSP", "CSH", "CSSE"); | ||
|
|
||
| private final GroupsJpaRepository groupsRepository; | ||
| private final PersonJpaRepository personRepository; | ||
|
|
||
| public ClassGroupMembershipService( | ||
| GroupsJpaRepository groupsRepository, | ||
| PersonJpaRepository personRepository) { | ||
| this.groupsRepository = groupsRepository; | ||
| this.personRepository = personRepository; | ||
| } | ||
|
|
||
| /** | ||
| * Makes the authenticated person's course-group memberships match their | ||
| * profile classes. Memberships in unrelated groups are left untouched. | ||
| */ | ||
| @Transactional | ||
| public List<String> syncMemberships(String uid, List<String> classes) { | ||
| if (uid == null || uid.isBlank()) { | ||
| throw new IllegalArgumentException("A user id is required"); | ||
| } | ||
|
|
||
| Person person = personRepository.findByUid(uid); | ||
| if (person == null) { | ||
| throw new NoSuchElementException("Authenticated user was not found"); | ||
| } | ||
|
|
||
| Set<String> requestedGroups = normalizeClasses(classes); | ||
| Map<String, Groups> courseGroups = loadCourseGroups(); | ||
|
|
||
| for (String requestedGroup : requestedGroups) { | ||
| if (courseGroups.get(requestedGroup) == null) { | ||
| throw new NoSuchElementException( | ||
| "Course group '" + requestedGroup + "' was not found" | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| List<Groups> changedGroups = new ArrayList<>(); | ||
| for (String groupName : CLASS_GROUP_NAMES) { | ||
| Groups group = courseGroups.get(groupName); | ||
| if (group == null) { | ||
| continue; | ||
| } | ||
|
|
||
| boolean shouldBeMember = requestedGroups.contains(groupName); | ||
| boolean isMember = group.getGroupMembers().contains(person); | ||
|
|
||
| if (shouldBeMember && !isMember) { | ||
| group.addPerson(person); | ||
| changedGroups.add(group); | ||
| } else if (!shouldBeMember && isMember) { | ||
| group.removePerson(person); | ||
| changedGroups.add(group); | ||
| } | ||
| } | ||
|
|
||
| if (!changedGroups.isEmpty()) { | ||
| groupsRepository.saveAll(changedGroups); | ||
| } | ||
|
|
||
| return CLASS_GROUP_NAMES.stream() | ||
| .filter(requestedGroups::contains) | ||
| .toList(); | ||
| } | ||
|
|
||
| private Set<String> normalizeClasses(List<String> classes) { | ||
| Set<String> normalizedClasses = new LinkedHashSet<>(); | ||
| if (classes == null) { | ||
| return normalizedClasses; | ||
| } | ||
|
|
||
| for (String className : classes) { | ||
| if (className == null || className.isBlank()) { | ||
| throw new IllegalArgumentException("Class names cannot be blank"); | ||
| } | ||
|
|
||
| String normalizedClass = className.trim().toUpperCase(Locale.ROOT); | ||
| if (!CLASS_GROUP_NAMES.contains(normalizedClass)) { | ||
| throw new IllegalArgumentException( | ||
| "Unsupported class '" + className + "'" | ||
| ); | ||
| } | ||
| normalizedClasses.add(normalizedClass); | ||
| } | ||
|
|
||
| return normalizedClasses; | ||
| } | ||
|
|
||
| private Map<String, Groups> loadCourseGroups() { | ||
| Map<String, Groups> courseGroups = new LinkedHashMap<>(); | ||
| for (String groupName : CLASS_GROUP_NAMES) { | ||
| courseGroups.put(groupName, groupsRepository.findByName(groupName).orElse(null)); | ||
| } | ||
| return courseGroups; | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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.
When group creation succeeds, this page's Bootstrap 5.0.2 bundle does not provide
Modal.getOrCreateInstance, so this line throws beforelocation.reload(). The catch block then reports an error even though the server already created the group, and retrying produces a duplicate-name conflict; use the existing 5.0-compatible modal instance API or reload directly.Useful? React with 👍 / 👎.