Skip to content

Commit

Permalink
Merge pull request #8 from wiemanboy/feature/create-profile
Browse files Browse the repository at this point in the history
Can now create and update a profile
  • Loading branch information
wiemanboy authored Oct 19, 2024
2 parents 360a2b9 + 58ea1da commit abad3a1
Show file tree
Hide file tree
Showing 17 changed files with 251 additions and 172 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</parent>
<groupId>com.wiemanboy</groupId>
<artifactId>WiemanApi</artifactId>
<version>0.1.1</version>
<version>1.0.0</version>
<name>WiemanApi</name>
<description>WiemanApi</description>
<url/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package com.wiemanboy.wiemanapi.application;

import com.wiemanboy.wiemanapi.data.ProfileRepository;
import com.wiemanboy.wiemanapi.domain.Profile;
import com.wiemanboy.wiemanapi.domain.*;
import com.wiemanboy.wiemanapi.domain.exceptions.ProfileNotFoundException;
import com.wiemanboy.wiemanapi.presentation.dto.response.DescriptionDto;
import com.wiemanboy.wiemanapi.presentation.dto.response.SkillSectionDto;
import com.wiemanboy.wiemanapi.presentation.dto.response.SocialDto;
import jakarta.transaction.Transactional;
import org.springframework.stereotype.Service;

import java.net.URI;
import java.util.List;

@Service
@Transactional
public class ProfileService {
Expand All @@ -24,12 +30,42 @@ public Profile getProfileByName(String name) {
return profileRepository.findByFullNameOrUsername(name).orElseThrow(() -> new ProfileNotFoundException(null, name));
}

public Profile createProfile(String firstName, String lastName, String username) {
Profile profile = new Profile(firstName, lastName, username);
public Profile createProfile(String firstName, String lastName, String username, List<DescriptionDto> descriptionDtos, List<SocialDto> socialDtos, List<SkillSectionDto> skillSectionDtos) {
Profile profile = new Profile(
firstName,
lastName,
username,
descriptionDtos.stream().map(
descriptionDto -> Description.builder()
.title(descriptionDto.title())
.locale(descriptionDto.locale())
.content(descriptionDto.content())
.build()
).toList(),
socialDtos.stream().map(
socialDto -> Social.builder()
.platform(socialDto.platform())
.url(URI.create(socialDto.url()))
.build()
).toList(),
skillSectionDtos.stream().map(
skillSectionDto -> SkillSection.builder()
.title(skillSectionDto.title())
.skills(skillSectionDto.skills().stream()
.map(
skillDto -> Skill.builder()
.name(skillDto.name())
.level(skillDto.level())
.build()
).toList()
).build(
)
).toList()
);
return profileRepository.save(profile);
}

public Profile patchProfile(String id, String firstName, String lastName, String username) {
public Profile updateProfile(String id, String firstName, String lastName, String username, List<DescriptionDto> descriptionDtos, List<SocialDto> socialDtos, List<SkillSectionDto> skillSectionDtos) {
Profile profile = profileRepository.findById(id).orElseThrow(() -> new ProfileNotFoundException(id, null));

if (firstName != null) {
Expand All @@ -41,6 +77,44 @@ public Profile patchProfile(String id, String firstName, String lastName, String
if (username != null) {
profile.setUsername(username);
}
if (descriptionDtos != null) {
profile.setDescriptions(
descriptionDtos.stream().map(
descriptionDto -> Description.builder()
.title(descriptionDto.title())
.locale(descriptionDto.locale())
.content(descriptionDto.content())
.build()
).toList()
);
}
if (socialDtos != null) {
profile.setSocials(
socialDtos.stream().map(
socialDto -> Social.builder()
.platform(socialDto.platform())
.url(URI.create(socialDto.url()))
.build()
).toList()
);
}
if (skillSectionDtos != null) {
profile.setSkillSections(
skillSectionDtos.stream().map(
skillSectionDto -> SkillSection.builder()
.title(skillSectionDto.title())
.skills(skillSectionDto.skills().stream()
.map(
skillDto -> Skill.builder()
.name(skillDto.name())
.level(skillDto.level())
.build()
).toList()
).build(
)
).toList()
);
}

return profileRepository.save(profile);
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/wiemanboy/wiemanapi/domain/Description.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.wiemanboy.wiemanapi.domain;

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
@Builder
public class Description {
private String locale;
private String title;
Expand Down
53 changes: 18 additions & 35 deletions src/main/java/com/wiemanboy/wiemanapi/domain/Profile.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.wiemanboy.wiemanapi.domain;

import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
Expand All @@ -11,28 +12,34 @@
@Getter
@EqualsAndHashCode
@Document
@Setter
@Builder
public class Profile {
private String id;
private List<Description> descriptions = new ArrayList<>();
private List<Social> socials = new ArrayList<>();
private List<SkillSection> skillSections = new ArrayList<>();
@Setter
private String firstName;
@Setter
private String lastName;
@Setter
private String username;
@Builder.Default
private List<Description> descriptions = new ArrayList<>();
@Builder.Default
private List<Social> socials = new ArrayList<>();
@Builder.Default
private List<SkillSection> skillSections = new ArrayList<>();

protected Profile() {
}

public Profile(String firstName, String lastName, String username) {
protected Profile(String id, String firstName, String lastName, String username, List<Description> descriptions, List<Social> socials, List<SkillSection> skillSections) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.username = username;
this.descriptions = descriptions;
this.socials = socials;
this.skillSections = skillSections;
}

protected Profile() {
}

protected Profile(List<Description> descriptions, List<Social> socials, List<SkillSection> skillSections, String firstName, String lastName, String username) {
public Profile(String firstName, String lastName, String username, List<Description> descriptions, List<Social> socials, List<SkillSection> skillSections) {
this.descriptions = descriptions;
this.socials = socials;
this.skillSections = skillSections;
Expand All @@ -41,40 +48,16 @@ protected Profile(List<Description> descriptions, List<Social> socials, List<Ski
this.username = username;
}

public void addDescription(Description description) {
this.descriptions.add(description);
}

public Description getDescription(String locale) {
return descriptions.stream()
.filter(description -> description.getLocale().equals(locale))
.findFirst()
.orElse(null);
}

public void removeDescription(Description description) {
this.descriptions.remove(description);
}

public void addSocial(Social social) {
socials.add(social);
}

public void removeSocial(Social social) {
socials.remove(social);
}

public void addSkillSection(SkillSection skillSection) {
skillSections.add(skillSection);
}

public List<SkillSection> getSkillSections(String locale) {
return skillSections.stream()
.filter(skillSection -> skillSection.getLocale().equals(locale))
.toList();
}

public void removeSkillSection(SkillSection skillSection) {
skillSections.remove(skillSection);
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/wiemanboy/wiemanapi/domain/Skill.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.wiemanboy.wiemanapi.domain;

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Builder
public class Skill {
private String name;
private String level;
Expand Down
17 changes: 6 additions & 11 deletions src/main/java/com/wiemanboy/wiemanapi/domain/SkillSection.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
package com.wiemanboy.wiemanapi.domain;

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;

@Setter
@Getter
@Builder
public class SkillSection {
@Setter
private String locale;
@Setter
private String title;
@Builder.Default
List<Skill> skills = new ArrayList<>();

public SkillSection(String locale, String title) {
public SkillSection(String locale, String title, List<Skill> skills) {
this.locale = locale;
this.title = title;
}

public void addSkill(Skill skill) {
this.skills.add(skill);
}

public void removeSkill(Skill skill) {
this.skills.remove(skill);
this.skills = skills;
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/wiemanboy/wiemanapi/domain/Social.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.wiemanboy.wiemanapi.domain;

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

import java.net.URI;

@Getter
@Setter
@Builder
public class Social {
private String username;
private String platform;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.wiemanboy.wiemanapi.presentation;

import com.wiemanboy.wiemanapi.application.ProfileService;
import com.wiemanboy.wiemanapi.presentation.dto.request.CreateProfileDto;
import com.wiemanboy.wiemanapi.presentation.dto.response.ProfileDto;
import com.wiemanboy.wiemanapi.presentation.dto.response.ProfileLocaleDto;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/profiles")
Expand All @@ -27,4 +26,30 @@ public ProfileDto getProfile(@PathVariable String id) {
public ProfileLocaleDto getProfileByName(@PathVariable String name, @PathVariable String locale) {
return ProfileLocaleDto.from(profileService.getProfileByName(name), locale);
}

@PostMapping("/")
@ResponseStatus(HttpStatus.CREATED)
public ProfileDto createProfile(@RequestBody CreateProfileDto createProfileDto) {
return ProfileDto.from(profileService.createProfile(
createProfileDto.firstName(),
createProfileDto.lastName(),
createProfileDto.username(),
createProfileDto.descriptions(),
createProfileDto.socials(),
createProfileDto.skillSections()
));
}

@PutMapping("/{id}")
public ProfileDto updateProfile(@PathVariable String id, @RequestBody CreateProfileDto createProfileDto) {
return ProfileDto.from(profileService.updateProfile(
id,
createProfileDto.firstName(),
createProfileDto.lastName(),
createProfileDto.username(),
createProfileDto.descriptions(),
createProfileDto.socials(),
createProfileDto.skillSections()
));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.wiemanboy.wiemanapi.presentation.dto.request;

import com.wiemanboy.wiemanapi.presentation.dto.response.DescriptionDto;
import com.wiemanboy.wiemanapi.presentation.dto.response.SkillSectionDto;
import com.wiemanboy.wiemanapi.presentation.dto.response.SocialDto;

import java.util.List;

public record CreateProfileDto(
String id,
String firstName,
String lastName,
String username,
List<DescriptionDto> descriptions,
List<SkillSectionDto> skillSections,
List<SocialDto> socials
) {
}
3 changes: 2 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ debug=true
spring.application.name=WiemanApi
management.endpoints.web.exposure.include=health
management.endpoints.web.base-path=/services/profiles/actuator/
springdoc.swagger-ui.path=/services/profiles/docs/
springdoc.api-docs.path=/services/profiles/api-docs
springdoc.swagger-ui.path=/services/profiles/docs
springdoc.show-actuator=true
spring.data.mongodb.host=${MONGO_HOST:localhost}
spring.data.mongodb.port=${MONGO_PORT:27017}
Expand Down
Loading

0 comments on commit abad3a1

Please sign in to comment.