-
Notifications
You must be signed in to change notification settings - Fork 0
Implement Member Update Endpoint #73
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
Allimonae
wants to merge
5
commits into
main
Choose a base branch
from
updatemember2
base: main
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
73a6032
MemberSqlRepo updateMember and getMemberById
Allimonae 64d5565
Ran mvn spotless:apply
Allimonae c7fd0e2
Fixed test errors and added dependency to handle optional<string>
Allimonae b8a550d
Addressed auto code analysis - refactored MemberService
Allimonae e2829d8
Removed comment in UpdateMemberRequest
Allimonae 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,9 @@ | ||
| package org.patinanetwork.patchats.api.member; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.UUID; | ||
| import java.util.function.Consumer; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.patinanetwork.patchats.api.member.db.models.Member; | ||
| import org.patinanetwork.patchats.api.member.db.repos.MemberRepo; | ||
|
|
@@ -10,6 +12,7 @@ | |
| import org.patinanetwork.patchats.api.member.dto.UpdateMemberRequest; | ||
| import org.patinanetwork.patchats.common.web.exception.MemberDuplicateException; | ||
| import org.patinanetwork.patchats.common.web.exception.MemberNotFoundException; | ||
| import org.patinanetwork.patchats.common.web.exception.ValidationException; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| @Service | ||
|
|
@@ -48,40 +51,53 @@ public List<MemberDto> getMembers() { | |
| public MemberDto updateMember(UpdateMemberRequest request, UUID id) { | ||
| Member member = memberRepo.getMemberById(id).orElseThrow(() -> new MemberNotFoundException(id)); | ||
|
|
||
| if (request.firstName() != null) { | ||
| member.setFirstName(request.firstName()); | ||
| } | ||
| if (request.lastName() != null) { | ||
| member.setLastName(request.lastName()); | ||
| } | ||
| if (request.email() != null) { | ||
| member.setEmail(request.email()); | ||
| } | ||
| if (request.linkedInUrl() != null) { | ||
| member.setLinkedInUrl(request.linkedInUrl()); | ||
| } | ||
| if (request.introduction() != null) { | ||
| member.setIntroduction(request.introduction()); | ||
| } | ||
| if (request.matchPref() != null) { | ||
| member.setMatchPref(request.matchPref()); | ||
| } | ||
| if (request.industryPref() != null) { | ||
| member.setIndustryPref(request.industryPref()); | ||
| } | ||
| if (request.rolePref() != null) { | ||
| member.setRolePref(request.rolePref()); | ||
| } | ||
| if (request.topics() != null) { | ||
| member.setTopics(request.topics()); | ||
| } | ||
| if (request.extraNotes() != null) { | ||
| member.setExtraNotes(request.extraNotes()); | ||
| } | ||
| validateAndUpdateRequired(member, request); | ||
| updateOptional(member, request); | ||
|
|
||
| Member updatedMember = memberRepo.updateMember(member).orElseThrow(() -> new MemberNotFoundException(id)); | ||
| return MemberDto.from(updatedMember); | ||
| } | ||
|
|
||
| private void validateAndUpdateRequired(Member member, UpdateMemberRequest request) { | ||
| validateAndUpdate(request.firstName(), member::setFirstName, "firstName"); | ||
| validateAndUpdate(request.lastName(), member::setLastName, "lastName"); | ||
| validateAndUpdateEmail(member, request); | ||
| validateAndUpdate(request.introduction(), member::setIntroduction, "introduction"); | ||
| } | ||
|
|
||
| private void validateAndUpdate(Optional<String> field, Consumer<String> setter, String fieldName) { | ||
| if (field.isPresent()) { | ||
| String value = field.get(); | ||
| if (value.isBlank()) { | ||
| throw new ValidationException(fieldName + " cannot be empty"); | ||
| } | ||
| setter.accept(value); | ||
| } | ||
| } | ||
|
|
||
| private void validateAndUpdateEmail(Member member, UpdateMemberRequest request) { | ||
| if (request.email().isPresent()) { | ||
| String email = request.email().get(); | ||
| if (email.isBlank()) { | ||
| throw new ValidationException("email cannot be empty"); | ||
| } | ||
| if (!email.equals(member.getEmail()) | ||
| && memberRepo.getMemberByEmail(email).isPresent()) { | ||
| throw new MemberDuplicateException(email); | ||
| } | ||
| member.setEmail(email); | ||
| } | ||
| } | ||
|
|
||
| private void updateOptional(Member member, UpdateMemberRequest request) { | ||
| request.linkedInUrl().ifPresent(member::setLinkedInUrl); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the only difference here between this and the validate function that you throw an exception? I feel like it'd be easier to have a mandatory flag and then you can use the helper for these as well. |
||
| request.matchPref().ifPresent(member::setMatchPref); | ||
| request.industryPref().ifPresent(member::setIndustryPref); | ||
| request.rolePref().ifPresent(member::setRolePref); | ||
| request.topics().ifPresent(member::setTopics); | ||
| request.extraNotes().ifPresent(member::setExtraNotes); | ||
| } | ||
|
|
||
| // TODO: Implement these methods after createMember and updateMember is fully functional and tested | ||
| // public MemberDto getMemberById(UUID id) { | ||
| // return memberRepo | ||
|
|
||
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
22 changes: 11 additions & 11 deletions
22
src/main/java/org/patinanetwork/patchats/api/member/dto/UpdateMemberRequest.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 |
|---|---|---|
| @@ -1,16 +1,16 @@ | ||
| package org.patinanetwork.patchats.api.member.dto; | ||
|
|
||
| import jakarta.validation.constraints.Email; | ||
| import java.util.Optional; | ||
|
|
||
| public record UpdateMemberRequest( | ||
| String firstName, | ||
| String lastName, | ||
| // TODO: Changes to email require verification after authentication is implemented | ||
| @Email String email, | ||
| String linkedInUrl, | ||
| String introduction, | ||
| String matchPref, | ||
| String industryPref, | ||
| String rolePref, | ||
| String topics, | ||
| String extraNotes) {} | ||
| Optional<String> firstName, | ||
| Optional<String> lastName, | ||
| Optional<@Email String> email, | ||
| Optional<String> linkedInUrl, | ||
| Optional<String> introduction, | ||
| Optional<String> matchPref, | ||
| Optional<String> industryPref, | ||
| Optional<String> rolePref, | ||
| Optional<String> topics, | ||
| Optional<String> extraNotes) {} |
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
11 changes: 11 additions & 0 deletions
11
src/main/java/org/patinanetwork/patchats/common/web/exception/ValidationException.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,11 @@ | ||
| package org.patinanetwork.patchats.common.web.exception; | ||
|
|
||
| public class ValidationException extends RuntimeException { | ||
| public ValidationException(String message) { | ||
| super(message); | ||
| } | ||
|
|
||
| public ValidationException(String message, Throwable cause) { | ||
| super(message, cause); | ||
| } | ||
| } |
Oops, something went wrong.
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.
Shouldn't this field already be unique in the DB?
I feel like it's better to let the DB handle it rather than do a full scan ourselves.
The only thing may be, I'm not sure if the JDBC exception lets us see which column violated the unique constraint. It is nicer that we have a clear exception that tells the user that the email is the culprit.
Regardless, adding onto my other comment about returning everything, I think this should be at least replaced with just a true/false return.