Skip to content

Commit

Permalink
feature: Create a matching profile from scratch - after review
Browse files Browse the repository at this point in the history
task:  8697p10b5
  • Loading branch information
KinTrae committed Jan 28, 2025
1 parent 36e04bd commit 592228d
Showing 1 changed file with 11 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"));
}

Expand Down Expand Up @@ -114,20 +116,20 @@ public List<PictureDto> addMatchingProfilePictures(List<MultipartFile> 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<MatchingProfilePicture> 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"));
}
}
Expand Down Expand Up @@ -228,7 +230,7 @@ public void deleteMatchingProfile(String login) {
@Override
@Transactional
public void deleteMatchingProfilePicturesForUser(List<String> profilePictureIds, String login) {
if(profilePictureIds.isEmpty()) {
if (profilePictureIds.isEmpty()) {
throw new IllegalArgumentException(AlertConstants.VALUE_REQUIRED_TITLE);
}

Expand All @@ -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));
}
Expand Down

0 comments on commit 592228d

Please sign in to comment.