Skip to content

Commit

Permalink
feat: Add introduce patch code
Browse files Browse the repository at this point in the history
  • Loading branch information
nanna29 committed Jul 27, 2024
1 parent 05b423e commit 71cd16b
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
import umc.kkijuk.server.introduce.common.BaseResponse;
import umc.kkijuk.server.introduce.domain.Introduce;
import umc.kkijuk.server.introduce.domain.MasterIntroduce;
import umc.kkijuk.server.introduce.dto.IntroduceReqDto;
import umc.kkijuk.server.introduce.dto.IntroduceResDto;
import umc.kkijuk.server.introduce.dto.MasterIntroduceReqDto;
import umc.kkijuk.server.introduce.dto.MasterIntroduceResDto;
import umc.kkijuk.server.introduce.dto.*;
import umc.kkijuk.server.introduce.error.BaseErrorResponse;
import umc.kkijuk.server.introduce.error.BaseException;
import umc.kkijuk.server.introduce.service.IntroduceService;
Expand Down Expand Up @@ -45,8 +42,8 @@ public ResponseEntity<Object> save(@PathVariable("recruitId") Long recruitId, @R
}
}

@GetMapping("/{introId}")
@Operation(summary = "자기소개서 조회")
@GetMapping("detail/{introId}")
@Operation(summary = "자기소개서 개별 조회")
public ResponseEntity<Object> get(@PathVariable("introId") Long introId){
try {
IntroduceResDto introduceResDto = introduceService.getIntro(introId);
Expand All @@ -64,11 +61,30 @@ public ResponseEntity<Object> get(@PathVariable("introId") Long introId){
}
}

/*@PatchMapping("/{introId}")
@GetMapping("list")
@Operation(summary = "자기소개서 목록 조회")
public ResponseEntity<Object> get(){
try {
List<IntroduceListResDto> introduceListResDtos = introduceService.getIntroList();
return ResponseEntity
.status(HttpStatus.OK)
.body(new BaseResponse<>(HttpStatus.OK.value(), "자기소개서 목록 조회 완료", introduceListResDtos));
} catch (BaseException e) {
return ResponseEntity
.status(e.getCode())
.body(new BaseErrorResponse(e.getCode(), e.getMessage()));
} catch (Exception e) {
return ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(new BaseErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Server error"));
}
}

@PatchMapping("/{introId}")
@Operation(summary = "자기소개서 수정")
public ResponseEntity<Object> update(@PathVariable("introId") Long introId, @RequestBody IntroduceReqDto introduceReqDto){
try {
IntroduceResDto introduceResDto = introduceService.saveIntro(introId, introduceReqDto);
IntroduceResDto introduceResDto = introduceService.updateIntro(introId, introduceReqDto);
return ResponseEntity
.status(HttpStatus.OK)
.body(new BaseResponse<>(HttpStatus.OK.value(), "자기소개서 수정 완료", introduceResDto));
Expand All @@ -79,9 +95,9 @@ public ResponseEntity<Object> update(@PathVariable("introId") Long introId, @Req
} catch (Exception e) {
return ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(new BaseErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Server error"));
.body(new BaseErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Server error"+e.getMessage()+e.getStackTrace()));
}
}*/
}

@DeleteMapping("/{introId}")
@Operation(summary = "자기소개서 삭제")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ public Introduce(RecruitEntity recruit, List<Question> questions, int state) {
setQuestions(questions);
}

public String getUpdated_at() {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
return updated_at != null ? updated_at.format(formatter) : null;
}

public void setQuestions(List<Question> questions) {
this.questions = questions;
for (Question question : questions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Entity
@Table(name="question")
@Getter
@Setter
@NoArgsConstructor
public class Question {
@Id
Expand All @@ -20,13 +22,16 @@ public class Question {
private Introduce introduce;

private String title;

private String content;

private int number;

@Builder
public Question(String title, String content) {
this.introduce = introduce;
public Question(String title, String content, int number) {
this.title = title;
this.content = content;
this.number = number;
}

public void setIntroduce(Introduce introduce) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,10 @@

import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.Optional;

public interface QuestionRepository extends JpaRepository<Question, Long> {
List<Question> findByNumber(int number);
List<Question> findByIntroduceId(Long number);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package umc.kkijuk.server.introduce.dto;

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import umc.kkijuk.server.introduce.domain.Introduce;

import java.time.LocalDateTime;
import java.time.Duration;
import java.time.format.DateTimeFormatter;

@Getter
@Setter
public class IntroduceListResDto {
private Long id;
private Long recruitId;
private String recruitTitle;
private String deadline;
private String updatedAt;
private String timeSinceUpdate;
private int state;

@Builder
public IntroduceListResDto(Introduce introduce) {
this.id = introduce.getId();
this.recruitId=introduce.getRecruit().toModel().getId();
this.recruitTitle=introduce.getRecruit().toModel().getTitle();
this.deadline=formatUpdatedAt(introduce.getRecruit().toModel().getEndTime());
this.updatedAt = formatUpdatedAt(introduce.getUpdated_at());
this.timeSinceUpdate = calculateTimeUntilDeadline(introduce.getUpdated_at());
this.state=introduce.getState();
}

private String formatUpdatedAt(LocalDateTime updatedAt) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
return updatedAt != null ? updatedAt.format(formatter) : null;
}

private String calculateTimeUntilDeadline(LocalDateTime deadline) {
Duration duration = Duration.between(LocalDateTime.now(), deadline);
long days = duration.toDays();
return days > 0 ? "D-" + days : "공고 기한 마감";
}
}
20 changes: 17 additions & 3 deletions src/main/java/umc/kkijuk/server/introduce/dto/IntroduceResDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import lombok.Setter;
import umc.kkijuk.server.introduce.domain.Introduce;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
@Getter
@Setter
Expand All @@ -14,10 +16,11 @@ public class IntroduceResDto {
private Long recruitId;
private String recruitTitle;
private List<QuestionDto> questionList;
private LocalDateTime deadline;
private String deadline;
private List<String> tags;
private String link;
private String updatedAt;
private String timeSinceUpdate;
private int state;

@Builder
Expand All @@ -26,10 +29,21 @@ public IntroduceResDto(Introduce introduce, List<QuestionDto> questionList) {
this.recruitId=introduce.getRecruit().toModel().getId();
this.recruitTitle=introduce.getRecruit().toModel().getTitle();
this.questionList = questionList;
this.deadline=introduce.getRecruit().toModel().getEndTime();
this.deadline=formatUpdatedAt(introduce.getRecruit().toModel().getEndTime());
this.tags=introduce.getRecruit().toModel().getTags();
this.link=introduce.getRecruit().toModel().getLink();
this.updatedAt = introduce.getUpdated_at();
this.updatedAt = formatUpdatedAt(introduce.getUpdated_at());
this.timeSinceUpdate = calculateTimeUntilDeadline(introduce.getUpdated_at());
this.state=introduce.getState();
}
private String formatUpdatedAt(LocalDateTime updatedAt) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
return updatedAt != null ? updatedAt.format(formatter) : null;
}

private String calculateTimeUntilDeadline(LocalDateTime deadline) {
Duration duration = Duration.between(LocalDateTime.now(), deadline);
long days = duration.toDays();
return days > 0 ? "D-" + days : "공고 기한 마감";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
@Getter
@Setter
@AllArgsConstructor
@Builder
public class QuestionDto {
private String title;
private String content;
private int number;

@Builder
public QuestionDto(Question question) {
this.title = question.getTitle();
this.content = question.getContent();
this.number = question.getNumber();
}
}
15 changes: 0 additions & 15 deletions src/main/java/umc/kkijuk/server/introduce/dto/QuestionReqDto.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import umc.kkijuk.server.recruit.infrastructure.RecruitJpaRepository;


import java.util.List;
import java.util.*;
import java.util.stream.Collectors;

@RequiredArgsConstructor
Expand All @@ -30,7 +30,7 @@ public IntroduceResDto saveIntro(Long recruitId, IntroduceReqDto introduceReqDto
throw new BaseException(HttpStatus.CONFLICT.value(), "이미 자기소개서가 존재합니다");
}
List<Question> questions = introduceReqDto.getQuestionList().stream()
.map(dto -> new Question(dto.getTitle(), dto.getContent()))
.map(dto -> new Question(dto.getTitle(), dto.getContent(), dto.getNumber()))
.collect(Collectors.toList());

Introduce introduce=Introduce.builder()
Expand All @@ -51,51 +51,80 @@ public IntroduceResDto getIntro(Long introId){

List<QuestionDto> questionList = introduce.getQuestions()
.stream()
.map(question -> new QuestionDto(question.getTitle(), question.getContent()))
.map(question -> new QuestionDto(question.getTitle(), question.getContent(), question.getNumber()))
.collect(Collectors.toList());

return new IntroduceResDto(introduce, questionList);
}

/*@Transactional
public IntroduceResDto updateIntro(Long introId, IntroduceReqDto introduceReqDto){
Introduce introduce=introduceRepository.findById(introId)
.orElseThrow(()-> new BaseException(HttpStatus.NOT_FOUND.value(), "해당 자기소개서를 찾을 수 없습니다"));
List<Question> updatedQuestions = introduceReqDto.getQuestionList().stream()
.map(questionDto -> {
if (questionDto.getId() != null) {
// 기존 질문을 찾아서 업데이트
Question question = questionRepository.findById(questionDto.getId())
.orElseThrow(() -> new BaseException(HttpStatus.NOT_FOUND.value(), "해당 질문을 찾을 수 없습니다"));
question.update(questionDto.getTitle(), questionDto.getContent());
return question;
} else {
// 새 질문 생성
Question newQuestion = new Question(questionDto.getTitle(), questionDto.getContent());
newQuestion.setIntroduce(introduce); // 자기소개서와 연관 설정
return newQuestion;
}
})
@Transactional
public List<IntroduceListResDto> getIntroList(){
List<Introduce> introduces = introduceRepository.findAll();
return introduces.stream()
.map(IntroduceListResDto::new)
.collect(Collectors.toList());
}

introduce.getQuestions().forEach(question -> {
if (updatedQuestions.stream().noneMatch(updatedQuestion -> updatedQuestion.getId().equals(question.getId()))) {
questionRepository.delete(question);
@Transactional
public IntroduceResDto updateIntro(Long introId, IntroduceReqDto introduceReqDto) {
Introduce introduce = introduceRepository.findById(introId)
.orElseThrow(() -> new BaseException(HttpStatus.NOT_FOUND.value(), "해당 자기소개서를 찾을 수 없습니다"));

introduce.update(introduceReqDto.getState());

List<Question> existingQuestions = introduce.getQuestions();
Map<Integer, Question> existingQuestionsMap = existingQuestions.stream()
.collect(Collectors.toMap(Question::getNumber, q -> q));

List<QuestionDto> questionDtos = introduceReqDto.getQuestionList();
List<Question> updatedQuestions = new ArrayList<>();

for (QuestionDto questionDto : questionDtos) {
Integer number = questionDto.getNumber();
Question existingQuestion = existingQuestionsMap.get(number);

if (existingQuestion != null) {
existingQuestion.update(questionDto.getTitle(), questionDto.getContent());
updatedQuestions.add(existingQuestion);
} else {
Question newQuestion = new Question();
newQuestion.setTitle(questionDto.getTitle());
newQuestion.setContent(questionDto.getContent());
newQuestion.setNumber(questionDto.getNumber());
newQuestion.setIntroduce(introduce);
updatedQuestions.add(newQuestion);
}
}

List<Question> toRemove = existingQuestions.stream()
.filter(q -> !questionDtos.stream()
.anyMatch(dto -> dto.getNumber() == q.getNumber()))
.collect(Collectors.toList());

toRemove.forEach(question -> {
introduce.getQuestions().remove(question);
questionRepository.delete(question);
});

introduce.setQuestions(updatedQuestions);
introduce.getQuestions().clear();
introduce.getQuestions().addAll(updatedQuestions);

introduceRepository.save(introduce);

List<QuestionDto> questionList = introduce.getQuestions()
.stream()
.map(question -> new QuestionDto(question.getId(), question.getTitle(), question.getContent()))
List<QuestionDto> responseQuestionList = introduce.getQuestions().stream()
.map(question -> QuestionDto.builder()
.title(question.getTitle())
.content(question.getContent())
.number(question.getNumber())
.build())
.collect(Collectors.toList());

return new IntroduceResDto(introduce, questionList);
}*/
return IntroduceResDto.builder()
.introduce(introduce)
.questionList(responseQuestionList)
.build();
}


@Transactional
public Long deleteIntro(Long introId){
Expand Down

0 comments on commit 71cd16b

Please sign in to comment.