From 36e04bda2378a384afc3e03b2aefb4634210b2a3 Mon Sep 17 00:00:00 2001 From: KinTrae Date: Sun, 26 Jan 2025 10:53:18 +0100 Subject: [PATCH 1/2] feature: Create a matching profile from scratch task: 8697p10b5 --- .../MatchingProfileController.java | 10 +++- .../dtos/CreateMatchingProfileRequestDto.java | 15 ++++++ .../services/MatchingProfileService.java | 5 +- .../impl/MatchingProfileServiceImpl.java | 53 +++++++++++++++---- 4 files changed, 71 insertions(+), 12 deletions(-) create mode 100644 backend/src/main/java/meowhub/backend/matching/dtos/CreateMatchingProfileRequestDto.java diff --git a/backend/src/main/java/meowhub/backend/matching/controllers/MatchingProfileController.java b/backend/src/main/java/meowhub/backend/matching/controllers/MatchingProfileController.java index 89c1779..75de06b 100644 --- a/backend/src/main/java/meowhub/backend/matching/controllers/MatchingProfileController.java +++ b/backend/src/main/java/meowhub/backend/matching/controllers/MatchingProfileController.java @@ -1,6 +1,7 @@ package meowhub.backend.matching.controllers; import lombok.AllArgsConstructor; +import meowhub.backend.matching.dtos.CreateMatchingProfileRequestDto; import meowhub.backend.matching.dtos.UpdateMatchingProfileRequestDto; import meowhub.backend.matching.dtos.MatchingProfileDto; import meowhub.backend.matching.services.MatchingProfileService; @@ -43,8 +44,13 @@ public ResponseEntity> addMatchingProfilePictures(@RequestPart } @PostMapping("/create") - public ResponseEntity createMatchingProfileFromScratch(@AuthenticationPrincipal UserDetails userDetails) { - return ResponseEntity.ok(matchingProfileService.createMatchingProfile(userDetails.getUsername())); + public ResponseEntity createMatchingProfileBasedOnAccount(@AuthenticationPrincipal UserDetails userDetails) { + return ResponseEntity.ok(matchingProfileService.createMatchingProfileBasedOnAccount(userDetails.getUsername())); + } + + @PostMapping("/create-from-scratch") + public ResponseEntity createMatchingProfileFromScratch(@RequestBody CreateMatchingProfileRequestDto request, @AuthenticationPrincipal UserDetails userDetails) { + return ResponseEntity.ok(matchingProfileService.createMatchingProfileFromScratch(request, userDetails.getUsername())); } @GetMapping("/all") diff --git a/backend/src/main/java/meowhub/backend/matching/dtos/CreateMatchingProfileRequestDto.java b/backend/src/main/java/meowhub/backend/matching/dtos/CreateMatchingProfileRequestDto.java new file mode 100644 index 0000000..89f664d --- /dev/null +++ b/backend/src/main/java/meowhub/backend/matching/dtos/CreateMatchingProfileRequestDto.java @@ -0,0 +1,15 @@ +package meowhub.backend.matching.dtos; + +import lombok.Getter; +import lombok.Setter; +import meowhub.backend.constants.Genders; + +import java.time.LocalDate; + +@Getter +@Setter +public class CreateMatchingProfileRequestDto { + private String name; + private LocalDate birthdate; + private Genders gender; +} diff --git a/backend/src/main/java/meowhub/backend/matching/services/MatchingProfileService.java b/backend/src/main/java/meowhub/backend/matching/services/MatchingProfileService.java index 85ad7b6..7505205 100644 --- a/backend/src/main/java/meowhub/backend/matching/services/MatchingProfileService.java +++ b/backend/src/main/java/meowhub/backend/matching/services/MatchingProfileService.java @@ -1,5 +1,6 @@ package meowhub.backend.matching.services; +import meowhub.backend.matching.dtos.CreateMatchingProfileRequestDto; import meowhub.backend.matching.dtos.UpdateMatchingProfileRequestDto; import meowhub.backend.matching.dtos.MatchingProfileDto; import meowhub.backend.shared.dtos.PictureDto; @@ -14,7 +15,9 @@ public interface MatchingProfileService { MatchingProfileDto getMyProfile(String login); - MatchingProfileDto createMatchingProfile(String login); + MatchingProfileDto createMatchingProfileBasedOnAccount(String login); + + MatchingProfileDto createMatchingProfileFromScratch(CreateMatchingProfileRequestDto request, String login); MatchingProfileDto updateMatchingProfile(UpdateMatchingProfileRequestDto matchingProfileDto, String login); diff --git a/backend/src/main/java/meowhub/backend/matching/services/impl/MatchingProfileServiceImpl.java b/backend/src/main/java/meowhub/backend/matching/services/impl/MatchingProfileServiceImpl.java index 284bd0d..79a03a8 100644 --- a/backend/src/main/java/meowhub/backend/matching/services/impl/MatchingProfileServiceImpl.java +++ b/backend/src/main/java/meowhub/backend/matching/services/impl/MatchingProfileServiceImpl.java @@ -2,6 +2,7 @@ import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; +import meowhub.backend.matching.dtos.CreateMatchingProfileRequestDto; import meowhub.backend.matching.dtos.UpdateMatchingProfileRequestDto; import meowhub.backend.matching.dtos.MatchingProfileDto; import meowhub.backend.matching.models.MatchingProfile; @@ -19,13 +20,16 @@ import meowhub.backend.shared.dtos.PictureDto; import meowhub.backend.shared.utils.PictureUtils; import meowhub.backend.users.facades.UserMatchingServiceFacade; +import meowhub.backend.users.models.Gender; import meowhub.backend.users.models.User; +import meowhub.backend.users.repositories.GenderRepository; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.util.Pair; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; +import java.time.LocalDate; import java.util.ArrayList; import java.util.List; import java.util.Objects; @@ -43,6 +47,7 @@ public class MatchingProfileServiceImpl implements MatchingProfileService { private final HowOftenRepository howOftenRepository; private final EducationRepository educationRepository; private final PictureUtils pictureUtils; + private final GenderRepository genderRepository; @Override public MatchingProfileDto getMyProfile(String login) { @@ -53,10 +58,8 @@ public MatchingProfileDto getMyProfile(String login) { } @Override - public MatchingProfileDto createMatchingProfile(String login) { - if (matchingProfileRepository.existsByUserLogin(login)) { - throw new IllegalArgumentException(String.format(AlertConstants.ALREADY_EXISTS, "Matching profile", login)); - } + public MatchingProfileDto createMatchingProfileBasedOnAccount(String login) { + validateIfMatchingProfileAlreadyExists(login); User user = userMatchingServiceFacade.findUserByLogin(login); MatchingProfile matchingProfile = new MatchingProfile(); @@ -70,10 +73,32 @@ public MatchingProfileDto createMatchingProfile(String login) { } @Override - public MatchingProfileDto updateMatchingProfile(UpdateMatchingProfileRequestDto matchingProfileDto, String login) { - MatchingProfile matchingProfile = matchingProfileRepository.findByUserLogin(login) - .orElseThrow(() -> new IllegalArgumentException(String.format(AlertConstants.RESOURCE_NOT_FOUND, "Matching profile", "login", login))); + public MatchingProfileDto createMatchingProfileFromScratch(CreateMatchingProfileRequestDto request, String login) { + validateIfMatchingProfileAlreadyExists(login); + + if(request.getBirthdate() == null || request.getBirthdate().isAfter(LocalDate.now().minusYears(18))){ + throw new IllegalArgumentException(String.format(AlertConstants.ILLEGAL_ARGUMENT, "birthdate", " null and must be at least 18 years old")); + } else if(request.getName() == null || request.getName().isEmpty()){ + throw new IllegalArgumentException(String.format(AlertConstants.VALUE_REQUIRED, "name")); + } + User user = userMatchingServiceFacade.findUserByLogin(login); + Gender gender = genderRepository.findByCode(request.getGender().name()) + .orElseThrow(() -> new IllegalArgumentException(String.format(AlertConstants.RESOURCE_NOT_FOUND, "Gender", "code", request.getGender()))); + + MatchingProfile matchingProfile = new MatchingProfile(); + matchingProfile.setUser(user); + matchingProfile.setBirthdate(request.getBirthdate()); + matchingProfile.setGender(gender); + matchingProfile.setName(request.getName()); + matchingProfile = matchingProfileRepository.save(matchingProfile); + + return MatchingProfileDto.createFromMatchingProfile(matchingProfile); + } + + @Override + public MatchingProfileDto updateMatchingProfile(UpdateMatchingProfileRequestDto matchingProfileDto, String login) { + MatchingProfile matchingProfile = findMatchingProfileByLoginOrThrow(login); updateMatchingProfile(matchingProfile, matchingProfileDto); matchingProfileRepository.save(matchingProfile); return MatchingProfileDto.createFromMatchingProfile(matchingProfile); @@ -86,8 +111,7 @@ public List addMatchingProfilePictures(List pictures, throw new IllegalArgumentException(AlertConstants.VALUE_REQUIRED_TITLE); } - MatchingProfile matchingProfile = matchingProfileRepository.findByUserLogin(login) - .orElseThrow(() -> new IllegalArgumentException(String.format(AlertConstants.RESOURCE_NOT_FOUND, "Matching profile", "login", login))); + MatchingProfile matchingProfile = findMatchingProfileByLoginOrThrow(login); int currentlyPictures = matchingProfilePictureRepository.countAllByMatchingProfileId(matchingProfile.getId()); if(profilePictureName!= null && !profilePictureName.isEmpty()){ @@ -222,4 +246,15 @@ private void deleteMatchingProfilePicture(String pictureId) { pictureUtils.deletePictureFromOCI(picture.getOciName()); //delete from OCI matchingProfilePictureRepository.deleteById(pictureId); //delete from db } + + private MatchingProfile findMatchingProfileByLoginOrThrow(String login){ + return matchingProfileRepository.findByUserLogin(login) + .orElseThrow(() -> new IllegalArgumentException(String.format(AlertConstants.RESOURCE_NOT_FOUND, "Matching profile", "login", login))); + } + + private void validateIfMatchingProfileAlreadyExists(String login){ + if (matchingProfileRepository.existsByUserLogin(login)) { + throw new IllegalArgumentException(String.format(AlertConstants.ALREADY_EXISTS, "Matching profile", login)); + } + } } From 592228dc06c31f8d484f073749b25b93fc338773 Mon Sep 17 00:00:00 2001 From: KinTrae Date: Tue, 28 Jan 2025 18:03:56 +0100 Subject: [PATCH 2/2] feature: Create a matching profile from scratch - after review task: 8697p10b5 --- .../impl/MatchingProfileServiceImpl.java | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/backend/src/main/java/meowhub/backend/matching/services/impl/MatchingProfileServiceImpl.java b/backend/src/main/java/meowhub/backend/matching/services/impl/MatchingProfileServiceImpl.java index 79a03a8..6600158 100644 --- a/backend/src/main/java/meowhub/backend/matching/services/impl/MatchingProfileServiceImpl.java +++ b/backend/src/main/java/meowhub/backend/matching/services/impl/MatchingProfileServiceImpl.java @@ -38,6 +38,8 @@ @Service @RequiredArgsConstructor public class MatchingProfileServiceImpl implements MatchingProfileService { + private static final int MINIMUM_AGE = 16; + private final MatchingProfileRepository matchingProfileRepository; private final MatchingProfilePictureRepository matchingProfilePictureRepository; private final UserMatchingServiceFacade userMatchingServiceFacade; @@ -76,9 +78,9 @@ public MatchingProfileDto createMatchingProfileBasedOnAccount(String login) { public MatchingProfileDto createMatchingProfileFromScratch(CreateMatchingProfileRequestDto request, String login) { validateIfMatchingProfileAlreadyExists(login); - if(request.getBirthdate() == null || request.getBirthdate().isAfter(LocalDate.now().minusYears(18))){ - throw new IllegalArgumentException(String.format(AlertConstants.ILLEGAL_ARGUMENT, "birthdate", " null and must be at least 18 years old")); - } else if(request.getName() == null || request.getName().isEmpty()){ + if (request.getBirthdate() == null || request.getBirthdate().isAfter(LocalDate.now().minusYears(MINIMUM_AGE))) { + throw new IllegalArgumentException(String.format(AlertConstants.ILLEGAL_ARGUMENT, "birthdate", " null and must be at least " + MINIMUM_AGE + " years old")); + } else if (request.getName() == null || request.getName().isEmpty()) { throw new IllegalArgumentException(String.format(AlertConstants.VALUE_REQUIRED, "name")); } @@ -114,20 +116,20 @@ public List addMatchingProfilePictures(List pictures, MatchingProfile matchingProfile = findMatchingProfileByLoginOrThrow(login); int currentlyPictures = matchingProfilePictureRepository.countAllByMatchingProfileId(matchingProfile.getId()); - if(profilePictureName!= null && !profilePictureName.isEmpty()){ + if (profilePictureName != null && !profilePictureName.isEmpty()) { //checking if given profile picture name (that is to be set as a new profile picture) exists in the list of pictures pictures.stream().filter(picture -> Objects.equals(picture.getOriginalFilename(), profilePictureName)).findFirst() .orElseThrow(() -> new IllegalArgumentException(String.format(AlertConstants.RESOURCE_NOT_FOUND, "Matching profile picture", "name", profilePictureName))); //find current profile picture and set it to false if exists Optional currentProfilePicture = matchingProfilePictureRepository.findByMatchingProfileUserLoginAndIsCurrentProfilePictureTrue(login); - if(currentProfilePicture.isPresent()){ + if (currentProfilePicture.isPresent()) { MatchingProfilePicture current = currentProfilePicture.get(); current.setIsCurrentProfilePicture(false); matchingProfilePictureRepository.save(current); } } else { - if(currentlyPictures == 0){ + if (currentlyPictures == 0) { throw new NullPointerException(String.format(AlertConstants.VALUE_REQUIRED, "profilePictureName, when there is no profile picture")); } } @@ -228,7 +230,7 @@ public void deleteMatchingProfile(String login) { @Override @Transactional public void deleteMatchingProfilePicturesForUser(List profilePictureIds, String login) { - if(profilePictureIds.isEmpty()) { + if (profilePictureIds.isEmpty()) { throw new IllegalArgumentException(AlertConstants.VALUE_REQUIRED_TITLE); } @@ -247,12 +249,12 @@ private void deleteMatchingProfilePicture(String pictureId) { matchingProfilePictureRepository.deleteById(pictureId); //delete from db } - private MatchingProfile findMatchingProfileByLoginOrThrow(String login){ + private MatchingProfile findMatchingProfileByLoginOrThrow(String login) { return matchingProfileRepository.findByUserLogin(login) .orElseThrow(() -> new IllegalArgumentException(String.format(AlertConstants.RESOURCE_NOT_FOUND, "Matching profile", "login", login))); } - private void validateIfMatchingProfileAlreadyExists(String login){ + private void validateIfMatchingProfileAlreadyExists(String login) { if (matchingProfileRepository.existsByUserLogin(login)) { throw new IllegalArgumentException(String.format(AlertConstants.ALREADY_EXISTS, "Matching profile", login)); }