Skip to content

Commit

Permalink
feat: Add Education Service & Controller
Browse files Browse the repository at this point in the history
  • Loading branch information
nanna29 committed Aug 3, 2024
1 parent 43795a9 commit 1b005e9
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -34,7 +32,7 @@ public ResponseEntity<Object> 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
Expand All @@ -54,4 +52,13 @@ public ResponseEntity<Object> update(Long recordId, @RequestBody RecordReqDto re
.status(HttpStatus.OK)
.body(new BaseResponse<>(HttpStatus.OK.value(), "이력서 수정 완료", recordResDto));
}

@PostMapping("/education")
@Operation(summary = "학력 생성")
public ResponseEntity<Object> 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));
}
}
9 changes: 5 additions & 4 deletions src/main/java/umc/kkijuk/server/record/domain/Education.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import umc.kkijuk.server.introduce.domain.Introduce;

import java.time.LocalDateTime;
import java.time.YearMonth;

@Entity
@Table(name="education")
Expand All @@ -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;
Expand All @@ -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;
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/umc/kkijuk/server/record/domain/Record.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Education> educations;

Expand All @@ -33,11 +32,13 @@ public Record(Long memberId, String address, String profileImageUrl, List<Educat
this.address = address;
this.profileImageUrl = profileImageUrl;
this.educations = educations;
setEducations(educations);
if (educations != null) {
setEducations(educations);
}
}

public void setEducations(List<Education> educations) {
this.educations= educations;
this.educations = educations;
for (Education education : educations) {
education.setRecord(this);
}
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/umc/kkijuk/server/record/dto/EducationReqDto.java
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
7 changes: 3 additions & 4 deletions src/main/java/umc/kkijuk/server/record/dto/RecordResDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@
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;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Optional;

@Getter
@Setter
Expand All @@ -23,6 +20,7 @@ public class RecordResDto {
private String address;
private String profileImageUrl;
private String updatedAt;
private List<EducationResDto> educationList;
private List<RecordListResDto> activitiesAndExperiences;
private List<RecordListResDto> jobs;
private String name;
Expand All @@ -45,11 +43,12 @@ public RecordResDto(Member member,

// 이력서 있을 때
@Builder
public RecordResDto(Record record, Member member,
public RecordResDto(Record record, Member member, List<EducationResDto> educationList,
List<RecordListResDto> activitiesAndExperiences,
List<RecordListResDto> jobs) {
this.address = record.getAddress();
this.profileImageUrl=record.getProfileImageUrl();
this.educationList=educationList;
this.activitiesAndExperiences=activitiesAndExperiences;
this.jobs=jobs;
this.name = member.getName();
Expand Down
44 changes: 38 additions & 6 deletions src/main/java/umc/kkijuk/server/record/service/RecordService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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){
Expand All @@ -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())
Expand All @@ -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<EducationResDto> educationList = record.getEducations()
.stream()
.map(education -> new EducationResDto(education))
.collect(Collectors.toList());

return new RecordResDto(record, member, educationList, activitiesAndExperiences, jobs);
}

// 이력서 없을 때
Expand Down Expand Up @@ -112,7 +138,13 @@ public RecordResDto updateRecord(Member requestMember, Long recordId, RecordReqD
recordReqDto.getAddress(),
recordReqDto.getProfileImageUrl());

return new RecordResDto(record, member, activitiesAndExperiences, jobs);
List<EducationResDto> educationList = record.getEducations()
.stream()
.map(education -> new EducationResDto(education))
.collect(Collectors.toList());

return new RecordResDto(record, member, educationList, activitiesAndExperiences, jobs);
}


}
2 changes: 1 addition & 1 deletion src/main/resources/application-prod.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 1b005e9

Please sign in to comment.