Skip to content

Commit

Permalink
feat: Add introduce List in masterIntroduce
Browse files Browse the repository at this point in the history
  • Loading branch information
nanna29 committed Jul 27, 2024
1 parent 71cd16b commit dd6b208
Show file tree
Hide file tree
Showing 9 changed files with 289 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
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.*;
import umc.kkijuk.server.introduce.error.BaseErrorResponse;
import umc.kkijuk.server.introduce.error.BaseException;
Expand All @@ -20,6 +19,7 @@
@RestController
@RequiredArgsConstructor
@RequestMapping("/history/intro/")
@Slf4j
public class IntroduceController {
private final IntroduceService introduceService;

Expand All @@ -36,6 +36,7 @@ public ResponseEntity<Object> save(@PathVariable("recruitId") Long recruitId, @R
.status(e.getCode())
.body(new BaseErrorResponse(e.getCode(), e.getMessage()));
} catch (Exception e) {
log.info("Exception occurred: ", e);
return ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(new BaseErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Server error"));
Expand All @@ -55,6 +56,7 @@ public ResponseEntity<Object> get(@PathVariable("introId") Long introId){
.status(e.getCode())
.body(new BaseErrorResponse(e.getCode(), e.getMessage()));
} catch (Exception e) {
log.info("Exception occurred: ", e);
return ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(new BaseErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Server error"));
Expand All @@ -63,7 +65,7 @@ public ResponseEntity<Object> get(@PathVariable("introId") Long introId){

@GetMapping("list")
@Operation(summary = "자기소개서 목록 조회")
public ResponseEntity<Object> get(){
public ResponseEntity<Object> getList(){
try {
List<IntroduceListResDto> introduceListResDtos = introduceService.getIntroList();
return ResponseEntity
Expand All @@ -74,6 +76,7 @@ public ResponseEntity<Object> get(){
.status(e.getCode())
.body(new BaseErrorResponse(e.getCode(), e.getMessage()));
} catch (Exception e) {
log.info("Exception occurred: ", e);
return ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(new BaseErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Server error"));
Expand All @@ -93,9 +96,10 @@ public ResponseEntity<Object> update(@PathVariable("introId") Long introId, @Req
.status(e.getCode())
.body(new BaseErrorResponse(e.getCode(), e.getMessage()));
} catch (Exception e) {
log.info("Exception occurred: ", e);
return ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(new BaseErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Server error"+e.getMessage()+e.getStackTrace()));
.body(new BaseErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Server error "+e.getMessage()+e.getCause()));
}
}

Expand All @@ -112,6 +116,7 @@ public ResponseEntity<Object> delete(@PathVariable("introId") Long introId){
.status(e.getCode())
.body(new BaseErrorResponse(e.getCode(), e.getMessage()));
} catch (Exception e) {
log.info("Exception occurred: ", e);
return ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(new BaseErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Server error"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand All @@ -20,6 +21,7 @@
@RestController
@RequiredArgsConstructor
@RequestMapping("/history/intro/master")
@Slf4j
public class MasterIntroduceController {
private final MasterIntroduceService masterIntroduceService;

Expand All @@ -36,6 +38,7 @@ public ResponseEntity<Object> save(@RequestBody MasterIntroduceReqDto masterIntr
.status(e.getCode())
.body(new BaseErrorResponse(e.getCode(), e.getMessage()));
} catch (Exception e) {
log.info("Exception occurred: ", e);
return ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(new BaseErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Server error"));
Expand All @@ -46,7 +49,7 @@ public ResponseEntity<Object> save(@RequestBody MasterIntroduceReqDto masterIntr
@Operation(summary = "마스터 자기소개서 조회")
public ResponseEntity<Object> get(){
try {
List<MasterIntroduce> masterIntroduce = masterIntroduceService.getMasterIntro();
List<MasterIntroduceResDto> masterIntroduce = masterIntroduceService.getMasterIntro();
return ResponseEntity
.status(HttpStatus.OK)
.body(new BaseResponse<>(HttpStatus.OK.value(), "마스터 자기소개서 조회 완료", masterIntroduce));
Expand All @@ -55,6 +58,7 @@ public ResponseEntity<Object> get(){
.status(e.getCode())
.body(new BaseErrorResponse(e.getCode(), e.getMessage()));
} catch (Exception e) {
log.info("Exception occurred: ", e);
return ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(new BaseErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Server error"));
Expand All @@ -74,6 +78,7 @@ public ResponseEntity<Object> update(Long id, @RequestBody MasterIntroduceReqDto
.status(e.getCode())
.body(new BaseErrorResponse(e.getCode(), e.getMessage()));
} catch (Exception e) {
log.info("Exception occurred: ", e);
return ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(new BaseErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Server error"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@
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
Expand Up @@ -9,6 +9,8 @@
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

@Getter
@Setter
public class IntroduceResDto {
Expand All @@ -21,10 +23,11 @@ public class IntroduceResDto {
private String link;
private String updatedAt;
private String timeSinceUpdate;
private List<String> introduceList;
private int state;

@Builder
public IntroduceResDto(Introduce introduce, List<QuestionDto> questionList) {
public IntroduceResDto(Introduce introduce, List<QuestionDto> questionList, List<String> introduceList) {
this.id = introduce.getId();
this.recruitId=introduce.getRecruit().toModel().getId();
this.recruitTitle=introduce.getRecruit().toModel().getTitle();
Expand All @@ -34,6 +37,7 @@ public IntroduceResDto(Introduce introduce, List<QuestionDto> questionList) {
this.link=introduce.getRecruit().toModel().getLink();
this.updatedAt = formatUpdatedAt(introduce.getUpdated_at());
this.timeSinceUpdate = calculateTimeUntilDeadline(introduce.getUpdated_at());
this.introduceList = introduceList;
this.state=introduce.getState();
}
private String formatUpdatedAt(LocalDateTime updatedAt) {
Expand All @@ -46,4 +50,5 @@ private String calculateTimeUntilDeadline(LocalDateTime deadline) {
long days = duration.toDays();
return days > 0 ? "D-" + days : "공고 기한 마감";
}

}
Original file line number Diff line number Diff line change
@@ -1,26 +1,40 @@
package umc.kkijuk.server.introduce.dto;

import jakarta.transaction.Transactional;
import lombok.*;
import umc.kkijuk.server.introduce.domain.Introduce;
import umc.kkijuk.server.introduce.domain.IntroduceRepository;
import umc.kkijuk.server.introduce.domain.MasterIntroduce;
import umc.kkijuk.server.introduce.domain.MasterIntroduceRepository;
import umc.kkijuk.server.recruit.infrastructure.RecruitJpaRepository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Getter
@Setter
@Builder
@AllArgsConstructor
public class MasterIntroduceResDto {
private Long id;
private String oneLiner;
private String introduction;
private String motive;
private String prosAndCons;
private String updatedAt;
private List<String> introduceList;

@Builder
public MasterIntroduceResDto(MasterIntroduce masterIntroduce) {
public MasterIntroduceResDto(MasterIntroduce masterIntroduce, List<String> introduceList) {
this.id = masterIntroduce.getId();
this.oneLiner = masterIntroduce.getOneLiner();
this.introduction = masterIntroduce.getIntroduction();
this.motive = masterIntroduce.getMotive();
this.prosAndCons = masterIntroduce.getProsAndCons();
this.updatedAt = masterIntroduce.getUpdated_at();
this.introduceList = introduceList;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public IntroduceResDto saveIntro(Long recruitId, IntroduceReqDto introduceReqDto
.build();

introduceRepository.save(introduce);

return new IntroduceResDto(introduce, introduceReqDto.getQuestionList());
List<String> introduceList=getIntroduceTitles();
return new IntroduceResDto(introduce, introduceReqDto.getQuestionList(),introduceList);
}

@Transactional
Expand All @@ -54,7 +54,9 @@ public IntroduceResDto getIntro(Long introId){
.map(question -> new QuestionDto(question.getTitle(), question.getContent(), question.getNumber()))
.collect(Collectors.toList());

return new IntroduceResDto(introduce, questionList);
List<String> introduceList=getIntroduceTitles();

return new IntroduceResDto(introduce, questionList, introduceList);
}

@Transactional
Expand All @@ -66,7 +68,7 @@ public List<IntroduceListResDto> getIntroList(){
}

@Transactional
public IntroduceResDto updateIntro(Long introId, IntroduceReqDto introduceReqDto) {
public IntroduceResDto updateIntro(Long introId, IntroduceReqDto introduceReqDto) throws Exception{
Introduce introduce = introduceRepository.findById(introId)
.orElseThrow(() -> new BaseException(HttpStatus.NOT_FOUND.value(), "해당 자기소개서를 찾을 수 없습니다"));

Expand Down Expand Up @@ -135,4 +137,17 @@ public Long deleteIntro(Long introId){

return introduce.getId();
}

@Transactional
public List<String> getIntroduceTitles() {
// Fetch all Introduce entities
List<Introduce> introduces = introduceRepository.findAll();

// Map Introduce entities to Recruit titles
return introduces.stream()
.map(introduce -> recruitJpaRepository.findById(introduce.getRecruit().toModel().getId())) // Get the Recruit entity
.filter(Optional::isPresent) // Filter out any empty results
.map(opt -> opt.get().toModel().getTitle()) // Get the title of the Recruit
.collect(Collectors.toList()); // Collect titles into a List
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
package umc.kkijuk.server.introduce.service;

import jakarta.transaction.Transactional;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import umc.kkijuk.server.introduce.domain.Introduce;
import umc.kkijuk.server.introduce.domain.IntroduceRepository;
import umc.kkijuk.server.introduce.domain.MasterIntroduce;
import umc.kkijuk.server.introduce.domain.MasterIntroduceRepository;
import umc.kkijuk.server.introduce.dto.IntroduceListResDto;
import umc.kkijuk.server.introduce.dto.MasterIntroduceReqDto;
import umc.kkijuk.server.introduce.dto.MasterIntroduceResDto;
import umc.kkijuk.server.introduce.error.BaseException;
import umc.kkijuk.server.recruit.infrastructure.RecruitJpaRepository;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Service
public class MasterIntroduceService{
private final MasterIntroduceRepository masterIntroduceRepository;
private final IntroduceRepository introduceRepository;
private final RecruitJpaRepository recruitJpaRepository;

@Transactional
public MasterIntroduceResDto saveMasterIntro(MasterIntroduceReqDto masterIntroduceReqDto) throws Exception{
Expand All @@ -32,12 +42,24 @@ public MasterIntroduceResDto saveMasterIntro(MasterIntroduceReqDto masterIntrodu

masterIntroduceRepository.save(masterIntroduce);

return new MasterIntroduceResDto(masterIntroduce);
return new MasterIntroduceResDto(masterIntroduce, null);
}

@Transactional
public List<MasterIntroduce> getMasterIntro(){
return masterIntroduceRepository.findAll();
public List<MasterIntroduceResDto> getMasterIntro(){
List<MasterIntroduce> masterIntroduces= masterIntroduceRepository.findAll();
List<String> introduceList=getIntroduceTitles();
return masterIntroduces.stream()
.map(masterIntroduce -> MasterIntroduceResDto.builder()
.id(masterIntroduce.getId())
.oneLiner(masterIntroduce.getOneLiner())
.introduction(masterIntroduce.getIntroduction())
.motive(masterIntroduce.getMotive())
.prosAndCons(masterIntroduce.getProsAndCons())
.updatedAt(masterIntroduce.getUpdated_at())
.introduceList(introduceList)
.build())
.collect(Collectors.toList());
}

@Transactional
Expand All @@ -50,7 +72,20 @@ public MasterIntroduceResDto updateMasterIntro(Long id, MasterIntroduceReqDto ma
masterIntroduceReqDto.getIntroduction(),
masterIntroduceReqDto.getMotive(),
masterIntroduceReqDto.getProsAndCons());
List<String> introduceList=getIntroduceTitles();
return new MasterIntroduceResDto(masterIntroduce, introduceList);
}

@Transactional
public List<String> getIntroduceTitles() {
// Fetch all Introduce entities
List<Introduce> introduces = introduceRepository.findAll();

return new MasterIntroduceResDto(masterIntroduce);
// Map Introduce entities to Recruit titles
return introduces.stream()
.map(introduce -> recruitJpaRepository.findById(introduce.getRecruit().toModel().getId())) // Get the Recruit entity
.filter(Optional::isPresent) // Filter out any empty results
.map(opt -> opt.get().toModel().getTitle()) // Get the title of the Recruit
.collect(Collectors.toList()); // Collect titles into a List
}
}
Loading

0 comments on commit dd6b208

Please sign in to comment.