Skip to content

Commit

Permalink
get some profile controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Gorzala committed Dec 17, 2023
1 parent e072872 commit 8e7426b
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 60 deletions.
17 changes: 13 additions & 4 deletions src/main/java/net/dancier/dancer/core/ProfileController.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package net.dancier.dancer.core;

import lombok.RequiredArgsConstructor;
import net.dancier.dancer.core.dto.ProfileDto;
import net.dancier.dancer.core.dto.PublicProfileDto;
import net.dancier.dancer.core.dto.ProfileOfCurrentUserDto;
import net.dancier.dancer.core.dto.UsernameAvailableDto;
import net.dancier.dancer.core.exception.NotFoundException;
import net.dancier.dancer.core.exception.UnresolvableZipCode;
Expand All @@ -15,6 +16,7 @@
import javax.validation.Valid;

import java.util.Map;
import java.util.UUID;

import static net.dancier.dancer.authentication.Constants.ROLE_USER;

Expand All @@ -37,16 +39,23 @@ public ResponseEntity<?> checkUsernameAvailability(@PathVariable String dancerNa

@Secured(ROLE_USER)
@GetMapping
public ResponseEntity<ProfileDto> get(@CurrentUser AuthenticatedUser authenticatedUser) {
public ResponseEntity<ProfileOfCurrentUserDto> get(@CurrentUser AuthenticatedUser authenticatedUser) {
return ResponseEntity.ok(
profileService.getProfileByUserId(authenticatedUser.getUserId())
);
}

@Secured(ROLE_USER)
@GetMapping("/{dancerId}")
public ResponseEntity<PublicProfileDto> get(@PathVariable UUID dancerId) {
PublicProfileDto publicProfileDto = profileService.getProfileByDancerId(dancerId);
return ResponseEntity.ok(publicProfileDto);
}

@Secured(ROLE_USER)
@PutMapping
public ResponseEntity put(@CurrentUser AuthenticatedUser authenticatedUser, @Valid @RequestBody ProfileDto profileDto) {
profileService.updateProfileForUserId(authenticatedUser.getUserId(), profileDto);
public ResponseEntity put(@CurrentUser AuthenticatedUser authenticatedUser, @Valid @RequestBody ProfileOfCurrentUserDto profileOfCurrentUserDto) {
profileService.updateProfileForUserId(authenticatedUser.getUserId(), profileOfCurrentUserDto);
return ResponseEntity.ok().build();
}

Expand Down
50 changes: 28 additions & 22 deletions src/main/java/net/dancier/dancer/core/ProfileService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import net.dancier.dancer.authentication.model.User;
import net.dancier.dancer.authentication.repository.UserRepository;
import net.dancier.dancer.core.dto.DanceProfileDto;
import net.dancier.dancer.core.dto.ProfileDto;
import net.dancier.dancer.core.dto.ProfileOfCurrentUserDto;
import net.dancier.dancer.core.dto.PublicProfileDto;
import net.dancier.dancer.core.events.ProfileUpdatedEvent;
import net.dancier.dancer.core.exception.BusinessException;
import net.dancier.dancer.core.exception.NotFoundException;
Expand Down Expand Up @@ -42,7 +43,12 @@ public class ProfileService {

private final ApplicationEventPublisher applicationEventPublisher;

public ProfileDto getProfileByUserId(UUID userId) {
public PublicProfileDto getProfileByDancerId(UUID dancerId) {
Dancer dancer = dancerRepository.findById(dancerId).orElseThrow(() -> new NotFoundException("No such Dancer"));
return PublicProfileDto.of(dancer);
}

public ProfileOfCurrentUserDto getProfileByUserId(UUID userId) {
User user = userRepository.findById(userId).orElseThrow(
() -> new NotFoundException("User not found for id: " + userId));
Dancer dancer = dancerRepository.findByUserId(userId).orElseGet(() -> new Dancer());
Expand All @@ -51,7 +57,7 @@ public ProfileDto getProfileByUserId(UUID userId) {
}

@Transactional
public void updateProfileForUserId(UUID userId, ProfileDto profileDto) {
public void updateProfileForUserId(UUID userId, ProfileOfCurrentUserDto profileOfCurrentUserDto) {
Dancer dancer = dancerRepository
.findByUserId(userId)
.orElseGet(
Expand All @@ -61,31 +67,31 @@ public void updateProfileForUserId(UUID userId, ProfileDto profileDto) {
d.setVersion(0);
return d;
});
dancer.setGender(profileDto.getGender());
dancer.setBirthDate(profileDto.getBirthDate());
dancer.setSize(profileDto.getSize());
dancer.setZipCode(profileDto.getZipCode());
dancer.setProfileImageHash(profileDto.getProfileImageHash());
dancer.setAboutMe(profileDto.getAboutMe());
if (dancer.getDancerName() == null && profileDto.getDancerName() != null) {
checkDancerNameRules(profileDto.getDancerName());
dancer.setDancerName(profileDto.getDancerName());
dancer.setGender(profileOfCurrentUserDto.getGender());
dancer.setBirthDate(profileOfCurrentUserDto.getBirthDate());
dancer.setSize(profileOfCurrentUserDto.getSize());
dancer.setZipCode(profileOfCurrentUserDto.getZipCode());
dancer.setProfileImageHash(profileOfCurrentUserDto.getProfileImageHash());
dancer.setAboutMe(profileOfCurrentUserDto.getAboutMe());
if (dancer.getDancerName() == null && profileOfCurrentUserDto.getDancerName() != null) {
checkDancerNameRules(profileOfCurrentUserDto.getDancerName());
dancer.setDancerName(profileOfCurrentUserDto.getDancerName());
}
ZipCode zipCode = zipCodeRepository.findByCountryAndZipCode(profileDto.getCountry(), profileDto.getZipCode());
ZipCode zipCode = zipCodeRepository.findByCountryAndZipCode(profileOfCurrentUserDto.getCountry(), profileOfCurrentUserDto.getZipCode());
if (zipCode != null) {
dancer.setCity(zipCode.getCity());
dancer.setLatitude(zipCode.getLatitude());
dancer.setLongitude(zipCode.getLongitude());
dancer.setCountry(Country.valueOf(zipCode.getCountry()));
} else if (StringUtils.hasText(profileDto.getZipCode())) {
} else if (StringUtils.hasText(profileOfCurrentUserDto.getZipCode())) {
throw new UnresolvableZipCode("Zip Code could not be resolved.");
} else {
dancer.setCity(null);
dancer.setLatitude(null);
dancer.setLongitude(null);
dancer.setCountry(null);
}
handleDancerProfiles(dancer, profileDto);
handleDancerProfiles(dancer, profileOfCurrentUserDto);
dancer.setUpdatedAt(Instant.now());
if (dancer.getVersion()!=null) {
dancer.setVersion(dancer.getVersion() + 1);
Expand All @@ -109,13 +115,13 @@ private void checkDancerNameRules(String dancerName) {
}
}

private void handleDancerProfiles(Dancer dancer, ProfileDto profileDto) {
Set<Dance> allDances = getNeededDances(profileDto);
private void handleDancerProfiles(Dancer dancer, ProfileOfCurrentUserDto profileOfCurrentUserDto) {
Set<Dance> allDances = getNeededDances(profileOfCurrentUserDto);
dancer.setWantsTo(handleDancerProfileInternal(
dancer.getWantsTo(), profileDto.getWantsTo(), allDances
dancer.getWantsTo(), profileOfCurrentUserDto.getWantsTo(), allDances
));
dancer.setAbleTo(handleDancerProfileInternal(
dancer.getAbleTo(), profileDto.getAbleTo(), allDances
dancer.getAbleTo(), profileOfCurrentUserDto.getAbleTo(), allDances
));
}

Expand Down Expand Up @@ -153,9 +159,9 @@ public boolean existsByDancerName(String dancerName) {
return this.dancerRepository.existsByDancerName(dancerName);
}

Set<Dance> getNeededDances(ProfileDto profileDto) {
Set<DanceProfileDto> allRequestedDanceProfilesDto = new HashSet<>(profileDto.getWantsTo());
allRequestedDanceProfilesDto.addAll(new HashSet<>(profileDto.getAbleTo()));
Set<Dance> getNeededDances(ProfileOfCurrentUserDto profileOfCurrentUserDto) {
Set<DanceProfileDto> allRequestedDanceProfilesDto = new HashSet<>(profileOfCurrentUserDto.getWantsTo());
allRequestedDanceProfilesDto.addAll(new HashSet<>(profileOfCurrentUserDto.getAbleTo()));
Set<String> allRequestedDanceNames = allRequestedDanceProfilesDto
.stream()
.map(dp -> dp.getDance())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
JavaTimeModule javaTimeModule=new JavaTimeModule();
javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ISO_DATE_TIME));

mapper.registerModule(javaTimeModule);
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import java.util.UUID;

@Data
public class ProfileDto {
public class ProfileOfCurrentUserDto {

private UUID id;

Expand Down
54 changes: 54 additions & 0 deletions src/main/java/net/dancier/dancer/core/dto/PublicProfileDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package net.dancier.dancer.core.dto;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import net.dancier.dancer.authentication.model.User;
import net.dancier.dancer.core.model.Dancer;
import net.dancier.dancer.core.model.Gender;

import java.util.Date;
import java.util.Set;
import java.util.UUID;

@Data
public class PublicProfileDto {

public static PublicProfileDto of(Dancer dancer) {
PublicProfileDto publicProfileDto = new PublicProfileDto();
publicProfileDto.setId(dancer.getId());
publicProfileDto.setSize(dancer.getSize());
publicProfileDto.setGender(dancer.getGender());
publicProfileDto.setDancerName(dancer.getDancerName());
publicProfileDto.setBirthDate(dancer.getBirthDate());
//publicProfileDto.setAbleTo(dancer.getAbleTo());
//publicProfileDto.setWantsTo(dancer.getWantsTo());
publicProfileDto.setCity(dancer.getCity());
//publicProfileDto.setCountry(dancer.getCountry());
publicProfileDto.setProfileImageHash(dancer.getProfileImageHash());
publicProfileDto.setAboutMe(dancer.getAboutMe());
return publicProfileDto;
}

private UUID id;

private Integer size;

private Gender gender;

private String dancerName;

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
private Date birthDate;

private Set<DanceProfileDto> ableTo;

private Set<DanceProfileDto> wantsTo;

private String city;

private String country;

private String profileImageHash;

private String aboutMe;
}
34 changes: 17 additions & 17 deletions src/main/java/net/dancier/dancer/core/util/ModelMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,39 @@

import net.dancier.dancer.authentication.model.User;
import net.dancier.dancer.core.dto.DanceProfileDto;
import net.dancier.dancer.core.dto.ProfileDto;
import net.dancier.dancer.core.dto.ProfileOfCurrentUserDto;
import net.dancier.dancer.core.model.DanceProfile;
import net.dancier.dancer.core.model.Dancer;

import java.util.stream.Collectors;

public class ModelMapper {

public static ProfileDto dancerAndUserToProfile(Dancer dancer, User user) {
ProfileDto profileDto = new ProfileDto();
profileDto.setGender(dancer.getGender());
profileDto.setId(dancer.getId());
profileDto.setBirthDate(dancer.getBirthDate());
profileDto.setSize(dancer.getSize());
profileDto.setZipCode(dancer.getZipCode());
profileDto.setDancerName(dancer.getDancerName());
profileDto.setProfileImageHash(dancer.getProfileImageHash());
public static ProfileOfCurrentUserDto dancerAndUserToProfile(Dancer dancer, User user) {
ProfileOfCurrentUserDto profileOfCurrentUserDto = new ProfileOfCurrentUserDto();
profileOfCurrentUserDto.setGender(dancer.getGender());
profileOfCurrentUserDto.setId(dancer.getId());
profileOfCurrentUserDto.setBirthDate(dancer.getBirthDate());
profileOfCurrentUserDto.setSize(dancer.getSize());
profileOfCurrentUserDto.setZipCode(dancer.getZipCode());
profileOfCurrentUserDto.setDancerName(dancer.getDancerName());
profileOfCurrentUserDto.setProfileImageHash(dancer.getProfileImageHash());
if (dancer.getCountry()!=null) {
profileDto.setCountry(dancer.getCountry().name());
profileOfCurrentUserDto.setCountry(dancer.getCountry().name());
}
profileDto.setCity(dancer.getCity());
profileDto.setAbleTo(dancer
profileOfCurrentUserDto.setCity(dancer.getCity());
profileOfCurrentUserDto.setAbleTo(dancer
.getAbleTo()
.stream()
.map(ModelMapper::danceProfile2danceProfileDto).collect(Collectors.toSet()));
profileDto.setWantsTo(dancer
profileOfCurrentUserDto.setWantsTo(dancer
.getWantsTo()
.stream()
.map(ModelMapper::danceProfile2danceProfileDto).collect(Collectors.toSet())
);
profileDto.setEmail(user.getEmail());
profileDto.setAboutMe(dancer.getAboutMe());
return profileDto;
profileOfCurrentUserDto.setEmail(user.getEmail());
profileOfCurrentUserDto.setAboutMe(dancer.getAboutMe());
return profileOfCurrentUserDto;
}

public static DanceProfileDto danceProfile2danceProfileDto(DanceProfile danceProfile) {
Expand Down
20 changes: 10 additions & 10 deletions src/test/java/net/dancier/dancer/core/EndToEndProfileTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import net.dancier.dancer.AbstractPostgreSQLEnabledTest;
import net.dancier.dancer.core.dto.DanceProfileDto;
import net.dancier.dancer.core.dto.ProfileDto;
import net.dancier.dancer.core.dto.ProfileOfCurrentUserDto;
import net.dancier.dancer.core.model.*;
import net.dancier.dancer.location.ZipCode;
import net.dancier.dancer.location.ZipCodeRepository;
Expand Down Expand Up @@ -52,30 +52,30 @@ void fromVirginProfileToPopulatedProfile() throws Exception {
.andExpect(jsonPath("$.size").isEmpty())
.andExpect(jsonPath("$.email").isNotEmpty());

ProfileDto profileDto = objectMapper.readValue(
ProfileOfCurrentUserDto profileOfCurrentUserDto = objectMapper.readValue(
initialGetOfProfile
.andReturn()
.getResponse()
.getContentAsString(),
ProfileDto.class);
ProfileOfCurrentUserDto.class);

DanceProfileDto danceProfileDto = new DanceProfileDto();
danceProfileDto.setDance("Tango");
danceProfileDto.setLevel(Level.BASIC);
danceProfileDto.setLeading(Leading.FOLLOW);


profileDto.setGender(Gender.DIVERS);
profileDto.setBirthDate(new Date());
profileDto.setAbleTo(Set.of(danceProfileDto));
profileDto.setWantsTo(Set.of(danceProfileDto));
profileDto.setZipCode("44339");
profileDto.setCountry("GER");
profileOfCurrentUserDto.setGender(Gender.DIVERS);
profileOfCurrentUserDto.setBirthDate(new Date());
profileOfCurrentUserDto.setAbleTo(Set.of(danceProfileDto));
profileOfCurrentUserDto.setWantsTo(Set.of(danceProfileDto));
profileOfCurrentUserDto.setZipCode("44339");
profileOfCurrentUserDto.setCountry("GER");

ResultActions changeDaProfile = mockMvc
.perform(put("/profile")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsBytes(profileDto))
.content(objectMapper.writeValueAsBytes(profileOfCurrentUserDto))
);

changeDaProfile.andExpect(status().isOk());
Expand Down
12 changes: 6 additions & 6 deletions src/test/java/net/dancier/dancer/core/ProfileTestFactory.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package net.dancier.dancer.core;

import net.dancier.dancer.core.dto.ProfileDto;
import net.dancier.dancer.core.dto.ProfileOfCurrentUserDto;

import java.util.Set;

public class ProfileTestFactory {

public static ProfileDto profileDto() {
ProfileDto profileDto = new ProfileDto();
profileDto.setWantsTo(Set.of());
profileDto.setAbleTo(Set.of());
return profileDto;
public static ProfileOfCurrentUserDto profileDto() {
ProfileOfCurrentUserDto profileOfCurrentUserDto = new ProfileOfCurrentUserDto();
profileOfCurrentUserDto.setWantsTo(Set.of());
profileOfCurrentUserDto.setAbleTo(Set.of());
return profileOfCurrentUserDto;
}
}

0 comments on commit 8e7426b

Please sign in to comment.