Skip to content

Commit

Permalink
Merge pull request #126 from JimTheCat/CU-8697p10b5_Create-a-matching…
Browse files Browse the repository at this point in the history
…-profile-from-scratch_Kinga-Traczyk

feature: Create a matching profile from scratch
  • Loading branch information
KinTrae authored Jan 28, 2025
2 parents fe28cd6 + 592228d commit 1a2ec7b
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -43,8 +44,13 @@ public ResponseEntity<List<PictureDto>> addMatchingProfilePictures(@RequestPart
}

@PostMapping("/create")
public ResponseEntity<MatchingProfileDto> createMatchingProfileFromScratch(@AuthenticationPrincipal UserDetails userDetails) {
return ResponseEntity.ok(matchingProfileService.createMatchingProfile(userDetails.getUsername()));
public ResponseEntity<MatchingProfileDto> createMatchingProfileBasedOnAccount(@AuthenticationPrincipal UserDetails userDetails) {
return ResponseEntity.ok(matchingProfileService.createMatchingProfileBasedOnAccount(userDetails.getUsername()));
}

@PostMapping("/create-from-scratch")
public ResponseEntity<MatchingProfileDto> createMatchingProfileFromScratch(@RequestBody CreateMatchingProfileRequestDto request, @AuthenticationPrincipal UserDetails userDetails) {
return ResponseEntity.ok(matchingProfileService.createMatchingProfileFromScratch(request, userDetails.getUsername()));
}

@GetMapping("/all")
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -34,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 All @@ -43,6 +49,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) {
Expand All @@ -53,10 +60,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();
Expand All @@ -70,10 +75,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(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"));
}

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);
Expand All @@ -86,24 +113,23 @@ public List<PictureDto> addMatchingProfilePictures(List<MultipartFile> 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()){
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 @@ -204,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 @@ -222,4 +248,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));
}
}
}

0 comments on commit 1a2ec7b

Please sign in to comment.