From 1b005e9a61a28d1ec4c1c544febb5a69e8f000e0 Mon Sep 17 00:00:00 2001 From: nanna29 Date: Sun, 4 Aug 2024 07:56:22 +0900 Subject: [PATCH] feat: Add Education Service & Controller --- .../record/controller/RecordController.java | 17 ++++--- .../server/record/domain/Education.java | 9 ++-- .../kkijuk/server/record/domain/Record.java | 7 +-- .../server/record/dto/EducationReqDto.java | 25 +++++++++++ ...EducationDto.java => EducationResDto.java} | 17 ++++--- .../server/record/dto/RecordResDto.java | 7 ++- .../server/record/service/RecordService.java | 44 ++++++++++++++++--- .../resources/application-prod.properties | 2 +- 8 files changed, 100 insertions(+), 28 deletions(-) create mode 100644 src/main/java/umc/kkijuk/server/record/dto/EducationReqDto.java rename src/main/java/umc/kkijuk/server/record/dto/{EducationDto.java => EducationResDto.java} (62%) diff --git a/src/main/java/umc/kkijuk/server/record/controller/RecordController.java b/src/main/java/umc/kkijuk/server/record/controller/RecordController.java index e7379758..2f1d7349 100644 --- a/src/main/java/umc/kkijuk/server/record/controller/RecordController.java +++ b/src/main/java/umc/kkijuk/server/record/controller/RecordController.java @@ -8,11 +8,9 @@ import org.springframework.web.bind.annotation.*; import umc.kkijuk.server.common.LoginUser; import umc.kkijuk.server.introduce.common.BaseResponse; -import umc.kkijuk.server.introduce.dto.IntroduceReqDto; -import umc.kkijuk.server.introduce.dto.IntroduceResDto; import umc.kkijuk.server.member.domain.Member; -import umc.kkijuk.server.record.domain.Record; -import umc.kkijuk.server.record.dto.RecordListResDto; +import umc.kkijuk.server.record.dto.EducationReqDto; +import umc.kkijuk.server.record.dto.EducationResDto; import umc.kkijuk.server.record.dto.RecordReqDto; import umc.kkijuk.server.record.dto.RecordResDto; import umc.kkijuk.server.record.service.RecordService; @@ -34,7 +32,7 @@ public ResponseEntity save(@RequestBody RecordReqDto recordReqDto){ Long id = recordService.saveRecord(requestMember, recordReqDto); return ResponseEntity .status(HttpStatus.OK) - .body(new BaseResponse<>(HttpStatus.OK.value(), "이력서 생성 완료", id)); + .body(new BaseResponse<>(HttpStatus.OK.value(), "이력서 생성 완료","id: "+id)); } @GetMapping @@ -54,4 +52,13 @@ public ResponseEntity update(Long recordId, @RequestBody RecordReqDto re .status(HttpStatus.OK) .body(new BaseResponse<>(HttpStatus.OK.value(), "이력서 수정 완료", recordResDto)); } + + @PostMapping("/education") + @Operation(summary = "학력 생성") + public ResponseEntity saveEducation(Long recordId, @RequestBody EducationReqDto educationReqDto){ + Long id = recordService.saveEducation(recordId, educationReqDto); + return ResponseEntity + .status(HttpStatus.OK) + .body(new BaseResponse<>(HttpStatus.OK.value(), "학력 생성 완료", "id: "+id)); + } } diff --git a/src/main/java/umc/kkijuk/server/record/domain/Education.java b/src/main/java/umc/kkijuk/server/record/domain/Education.java index 2878d7b4..e2b1077e 100644 --- a/src/main/java/umc/kkijuk/server/record/domain/Education.java +++ b/src/main/java/umc/kkijuk/server/record/domain/Education.java @@ -8,6 +8,7 @@ import umc.kkijuk.server.introduce.domain.Introduce; import java.time.LocalDateTime; +import java.time.YearMonth; @Entity @Table(name="education") @@ -26,12 +27,12 @@ public class Education extends BaseEntity { private String schoolName; private String major; private String state; - private LocalDateTime admissionDate; - private LocalDateTime graduationDate; + private YearMonth admissionDate; + private YearMonth graduationDate; @Builder public Education(String category, String schoolName, String major - , String state, LocalDateTime admissionDate, LocalDateTime graduationDate) { + , String state, YearMonth admissionDate, YearMonth graduationDate) { this.category = category; this.schoolName = schoolName; this.major = major; @@ -45,7 +46,7 @@ public void setRecord(Record record) { } public void update(String category, String schoolName, String major - , String state, LocalDateTime admissionDate, LocalDateTime graduationDate) { + , String state, YearMonth admissionDate, YearMonth graduationDate) { this.category = category; this.schoolName = schoolName; this.major = major; diff --git a/src/main/java/umc/kkijuk/server/record/domain/Record.java b/src/main/java/umc/kkijuk/server/record/domain/Record.java index c056ca20..e3d5b1e7 100644 --- a/src/main/java/umc/kkijuk/server/record/domain/Record.java +++ b/src/main/java/umc/kkijuk/server/record/domain/Record.java @@ -23,7 +23,6 @@ public class Record extends BaseEntity { private String address; private String profileImageUrl; - @NotNull @OneToMany(mappedBy = "record", cascade = CascadeType.ALL, orphanRemoval = true) private List educations; @@ -33,11 +32,13 @@ public Record(Long memberId, String address, String profileImageUrl, List educations) { - this.educations= educations; + this.educations = educations; for (Education education : educations) { education.setRecord(this); } diff --git a/src/main/java/umc/kkijuk/server/record/dto/EducationReqDto.java b/src/main/java/umc/kkijuk/server/record/dto/EducationReqDto.java new file mode 100644 index 00000000..df7248aa --- /dev/null +++ b/src/main/java/umc/kkijuk/server/record/dto/EducationReqDto.java @@ -0,0 +1,25 @@ +package umc.kkijuk.server.record.dto; + +import com.fasterxml.jackson.annotation.JsonFormat; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import org.springframework.format.annotation.DateTimeFormat; + +import java.time.LocalDateTime; +import java.time.YearMonth; + +@Builder +@AllArgsConstructor +@Getter +public class EducationReqDto { + private String category; + private String schoolName; + private String major; + private String state; + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM") + private YearMonth admissionDate; + + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM") + private YearMonth graduationDate; +} diff --git a/src/main/java/umc/kkijuk/server/record/dto/EducationDto.java b/src/main/java/umc/kkijuk/server/record/dto/EducationResDto.java similarity index 62% rename from src/main/java/umc/kkijuk/server/record/dto/EducationDto.java rename to src/main/java/umc/kkijuk/server/record/dto/EducationResDto.java index 0e937176..7e122d74 100644 --- a/src/main/java/umc/kkijuk/server/record/dto/EducationDto.java +++ b/src/main/java/umc/kkijuk/server/record/dto/EducationResDto.java @@ -4,29 +4,36 @@ import lombok.Builder; import lombok.Getter; import lombok.Setter; -import umc.kkijuk.server.introduce.domain.Question; import umc.kkijuk.server.record.domain.Education; import java.time.LocalDateTime; +import java.time.YearMonth; @Getter @Setter @AllArgsConstructor @Builder -public class EducationDto { +public class EducationResDto { private String category; private String schoolName; private String major; private String state; - private LocalDateTime admissionDate; - private LocalDateTime graduationDate; + private YearMonth admissionDate; + private YearMonth graduationDate; + private Boolean isCurrent; - public EducationDto(Education education) { + @Builder + public EducationResDto(Education education) { this.category = education.getCategory(); this.schoolName = education.getSchoolName(); this.major = education.getMajor(); this.state = education.getState(); this.admissionDate = education.getAdmissionDate(); this.graduationDate = education.getGraduationDate(); + this.isCurrent=determineIsCurrent(graduationDate); + } + + private static Boolean determineIsCurrent(YearMonth graduationDate) { + return graduationDate.isAfter(YearMonth.now()); } } diff --git a/src/main/java/umc/kkijuk/server/record/dto/RecordResDto.java b/src/main/java/umc/kkijuk/server/record/dto/RecordResDto.java index fe75de46..c25684d0 100644 --- a/src/main/java/umc/kkijuk/server/record/dto/RecordResDto.java +++ b/src/main/java/umc/kkijuk/server/record/dto/RecordResDto.java @@ -4,8 +4,6 @@ import lombok.Builder; import lombok.Getter; import lombok.Setter; -import umc.kkijuk.server.introduce.domain.Introduce; -import umc.kkijuk.server.introduce.dto.QuestionDto; import umc.kkijuk.server.member.domain.Member; import umc.kkijuk.server.record.domain.Record; @@ -13,7 +11,6 @@ import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.List; -import java.util.Optional; @Getter @Setter @@ -23,6 +20,7 @@ public class RecordResDto { private String address; private String profileImageUrl; private String updatedAt; + private List educationList; private List activitiesAndExperiences; private List jobs; private String name; @@ -45,11 +43,12 @@ public RecordResDto(Member member, // 이력서 있을 때 @Builder - public RecordResDto(Record record, Member member, + public RecordResDto(Record record, Member member, List educationList, List activitiesAndExperiences, List jobs) { this.address = record.getAddress(); this.profileImageUrl=record.getProfileImageUrl(); + this.educationList=educationList; this.activitiesAndExperiences=activitiesAndExperiences; this.jobs=jobs; this.name = member.getName(); diff --git a/src/main/java/umc/kkijuk/server/record/service/RecordService.java b/src/main/java/umc/kkijuk/server/record/service/RecordService.java index 36994e6a..2a6f364a 100644 --- a/src/main/java/umc/kkijuk/server/record/service/RecordService.java +++ b/src/main/java/umc/kkijuk/server/record/service/RecordService.java @@ -10,11 +10,11 @@ import umc.kkijuk.server.common.domian.exception.ResourceNotFoundException; import umc.kkijuk.server.member.domain.Member; import umc.kkijuk.server.member.repository.MemberJpaRepository; +import umc.kkijuk.server.record.domain.Education; +import umc.kkijuk.server.record.domain.EducationRepository; import umc.kkijuk.server.record.domain.Record; import umc.kkijuk.server.record.domain.RecordRepository; -import umc.kkijuk.server.record.dto.RecordListResDto; -import umc.kkijuk.server.record.dto.RecordReqDto; -import umc.kkijuk.server.record.dto.RecordResDto; +import umc.kkijuk.server.record.dto.*; import java.util.*; import java.util.stream.Collectors; @@ -25,6 +25,7 @@ public class RecordService { private final CareerRepository careerRepository; private final RecordRepository recordRepository; private final MemberJpaRepository memberJpaRepository; + private final EducationRepository educationRepository; @Transactional public Long saveRecord(Member requestMember, RecordReqDto recordReqDto){ @@ -43,6 +44,27 @@ public Long saveRecord(Member requestMember, RecordReqDto recordReqDto){ return record.getId(); } + @Transactional + public Long saveEducation(Long recordId, EducationReqDto educationReqDto){ + Record record = recordRepository.findById(recordId) + .orElseThrow(() -> new ResourceNotFoundException("Record", recordId)); + + Education education=Education.builder() + .category(educationReqDto.getCategory()) + .schoolName(educationReqDto.getSchoolName()) + .major(educationReqDto.getMajor()) + .state(educationReqDto.getState()) + .admissionDate(educationReqDto.getAdmissionDate()) + .graduationDate(educationReqDto.getGraduationDate()) + .build(); + + education.setRecord(record); + + educationRepository.save(education); + + return education.getId(); + } + @Transactional public RecordResDto getRecord(Member requestMember) { Member member=memberJpaRepository.findById(requestMember.getId()) @@ -67,11 +89,15 @@ public RecordResDto getRecord(Member requestMember) { .sorted(Comparator.comparing(RecordListResDto::getEndDate).reversed()) .collect(Collectors.toList()); - // 이력서 있을 때 if (!records.isEmpty()) { Record record = records.get(0); - return new RecordResDto(record, member, activitiesAndExperiences, jobs); + List educationList = record.getEducations() + .stream() + .map(education -> new EducationResDto(education)) + .collect(Collectors.toList()); + + return new RecordResDto(record, member, educationList, activitiesAndExperiences, jobs); } // 이력서 없을 때 @@ -112,7 +138,13 @@ public RecordResDto updateRecord(Member requestMember, Long recordId, RecordReqD recordReqDto.getAddress(), recordReqDto.getProfileImageUrl()); - return new RecordResDto(record, member, activitiesAndExperiences, jobs); + List educationList = record.getEducations() + .stream() + .map(education -> new EducationResDto(education)) + .collect(Collectors.toList()); + + return new RecordResDto(record, member, educationList, activitiesAndExperiences, jobs); } + } diff --git a/src/main/resources/application-prod.properties b/src/main/resources/application-prod.properties index e21b881c..ff1100ed 100644 --- a/src/main/resources/application-prod.properties +++ b/src/main/resources/application-prod.properties @@ -7,6 +7,6 @@ spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver # spring jpa spring.jpa.database=mysql spring.jpa.show-sql=true -spring.jpa.hibernate.ddl-auto=create +spring.jpa.hibernate.ddl-auto=update spring.jpa.properties.hibernate.format_sql=true