-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
347 additions
and
336 deletions.
There are no files selected for viewing
224 changes: 11 additions & 213 deletions
224
src/main/java/umc/kkijuk/server/introduce/service/IntroduceService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,220 +1,18 @@ | ||
package umc.kkijuk.server.introduce.service; | ||
|
||
import jakarta.transaction.Transactional; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import umc.kkijuk.server.common.domian.exception.IntroFoundException; | ||
import umc.kkijuk.server.common.domian.exception.IntroOwnerMismatchException; | ||
import umc.kkijuk.server.common.domian.exception.ResourceNotFoundException; | ||
import umc.kkijuk.server.introduce.controller.response.FindIntroduceResponse; | ||
import umc.kkijuk.server.introduce.controller.response.FindMasterIntroduceResponse; | ||
import umc.kkijuk.server.introduce.controller.response.IntroduceListResponse; | ||
import umc.kkijuk.server.introduce.controller.response.IntroduceResponse; | ||
import umc.kkijuk.server.introduce.domain.*; | ||
import umc.kkijuk.server.introduce.dto.*; | ||
import umc.kkijuk.server.introduce.repository.IntroduceRepository; | ||
import umc.kkijuk.server.introduce.repository.MasterIntroduceRepository; | ||
import umc.kkijuk.server.introduce.repository.QuestionRepository; | ||
import umc.kkijuk.server.introduce.controller.response.*; | ||
import umc.kkijuk.server.introduce.dto.IntroduceReqDto; | ||
import umc.kkijuk.server.member.domain.Member; | ||
import umc.kkijuk.server.recruit.infrastructure.RecruitEntity; | ||
import umc.kkijuk.server.recruit.infrastructure.RecruitJpaRepository; | ||
|
||
import java.util.*; | ||
import java.util.stream.Collectors; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class IntroduceService { | ||
|
||
private final IntroduceRepository introduceRepository; | ||
private final RecruitJpaRepository recruitJpaRepository; | ||
private final QuestionRepository questionRepository; | ||
private final MasterIntroduceRepository masterIntroduceRepository; | ||
|
||
@Transactional | ||
public IntroduceResponse saveIntro(Member requestMember, Long recruitId, IntroduceReqDto introduceReqDto){ | ||
RecruitEntity recruit=recruitJpaRepository.findById(recruitId) | ||
.orElseThrow(()-> new ResourceNotFoundException("recruit ", recruitId)); | ||
if (introduceRepository.findByRecruitId(recruitId).isPresent()) { | ||
throw new IntroFoundException("이미 자기소개서가 존재합니다"); | ||
} | ||
|
||
List<Question> questions = introduceReqDto.getQuestionList().stream() | ||
.map(dto -> new Question(dto.getTitle(), dto.getContent(), dto.getNumber())) | ||
.collect(Collectors.toList()); | ||
|
||
Introduce introduce=Introduce.builder() | ||
.memberId(requestMember.getId()) | ||
.recruit(recruit) | ||
.questions(questions) | ||
.state(introduceReqDto.getState()) | ||
.build(); | ||
|
||
introduceRepository.save(introduce); | ||
/*List<String> introduceList=getIntroduceTitles();*/ | ||
return new IntroduceResponse(introduce, introduceReqDto.getQuestionList()/*,introduceList*/); | ||
} | ||
|
||
@Transactional | ||
public IntroduceResponse getIntro(Member requestMember, Long introId){ | ||
Introduce introduce=introduceRepository.findById(introId) | ||
.orElseThrow(()-> new ResourceNotFoundException("introduce ", introId)); | ||
if (!introduce.getMemberId().equals(requestMember.getId())) { | ||
throw new IntroOwnerMismatchException(); | ||
} | ||
|
||
List<QuestionDto> questionList = introduce.getQuestions() | ||
.stream() | ||
.map(question -> new QuestionDto(question.getTitle(), question.getContent(), question.getNumber())) | ||
.collect(Collectors.toList()); | ||
|
||
/*List<String> introduceList=getIntroduceTitles();*/ | ||
|
||
return new IntroduceResponse(introduce, questionList/*, introduceList*/); | ||
} | ||
|
||
@Transactional | ||
public List<IntroduceListResponse> getIntroList(Member requestMember){ | ||
List<Introduce> introduces = introduceRepository.findAllByMemberId(requestMember.getId()) | ||
.orElseThrow(IntroOwnerMismatchException::new); | ||
|
||
return introduces.stream() | ||
.map(IntroduceListResponse::new) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
@Transactional | ||
public IntroduceResponse updateIntro(Member requestMember, Long introId, IntroduceReqDto introduceReqDto) throws Exception{ | ||
Introduce introduce=introduceRepository.findById(introId) | ||
.orElseThrow(()-> new ResourceNotFoundException("introduce ", introId)); | ||
|
||
if (!introduce.getMemberId().equals(requestMember.getId())) { | ||
throw new IntroOwnerMismatchException(); | ||
} | ||
|
||
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.getQuestions().clear(); | ||
introduce.getQuestions().addAll(updatedQuestions); | ||
|
||
introduceRepository.save(introduce); | ||
|
||
List<QuestionDto> responseQuestionList = introduce.getQuestions().stream() | ||
.map(question -> QuestionDto.builder() | ||
.title(question.getTitle()) | ||
.content(question.getContent()) | ||
.number(question.getNumber()) | ||
.build()) | ||
.collect(Collectors.toList()); | ||
|
||
return IntroduceResponse.builder() | ||
.introduce(introduce) | ||
.questionList(responseQuestionList) | ||
.build(); | ||
} | ||
|
||
|
||
@Transactional | ||
public Long deleteIntro(Member requestMember, Long introId){ | ||
Introduce introduce=introduceRepository.findById(introId) | ||
.orElseThrow(()-> new ResourceNotFoundException("introduce ", introId)); | ||
if (!introduce.getMemberId().equals(requestMember.getId())) { | ||
throw new IntroOwnerMismatchException(); | ||
} | ||
|
||
introduceRepository.delete(introduce); | ||
|
||
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 | ||
}*/ | ||
|
||
public Map<String, Object> searchIntroduceAndMasterByKeyword(String keyword, Member requestMember) { | ||
// 자기소개서 검색 | ||
List<FindIntroduceResponse> introduceList = introduceRepository.searchIntroduceByKeywordForMember(keyword, requestMember.getId()) | ||
.stream() | ||
.flatMap(introduce -> introduce.getQuestions().stream() | ||
.filter(q -> q.getContent().contains(keyword)) | ||
.map(q -> FindIntroduceResponse.builder() | ||
.introId(introduce.getId()) | ||
.title(introduce.getRecruit().getTitle()) | ||
.content(q.getContent()) | ||
.createdDate(introduce.getCreatedAt().toLocalDate()) | ||
.build())) | ||
.collect(Collectors.toList()); | ||
|
||
// 마스터 자기소개서 검색 | ||
List<FindMasterIntroduceResponse> masterIntroduceList = masterIntroduceRepository.searchMasterIntroduceByKeywordForMember(keyword, requestMember.getId()) | ||
.stream() | ||
.flatMap(masterIntroduce -> masterIntroduce.getMasterQuestion().stream() | ||
.filter(mq -> mq.getContent().contains(keyword)) | ||
.map(mq -> FindMasterIntroduceResponse.builder() | ||
.masterIntroId(masterIntroduce.getId()) | ||
.title("Master") | ||
.content(mq.getContent()) | ||
.createdDate(masterIntroduce.getCreatedAt().toLocalDate()) | ||
.build())) | ||
.collect(Collectors.toList()); | ||
|
||
List<Object> result = new ArrayList<>(); | ||
result.addAll(masterIntroduceList); | ||
result.addAll(introduceList); | ||
|
||
int count = result.size(); | ||
|
||
Map<String, Object> response = new LinkedHashMap<>(); | ||
response.put("count", count); | ||
response.put("data", result); | ||
|
||
return response; | ||
} | ||
|
||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public interface IntroduceService { | ||
|
||
IntroduceResponse saveIntro(Member requestMember, Long recruitId, IntroduceReqDto introduceReqDto); | ||
IntroduceResponse getIntro(Member requestMember, Long introId); | ||
List<IntroduceListResponse> getIntroList(Member requestMember); | ||
IntroduceResponse updateIntro(Member requestMember, Long introId, IntroduceReqDto introduceReqDto) throws Exception; | ||
Long deleteIntro(Member requestMember, Long introId); | ||
Map<String, Object> searchIntroduceAndMasterByKeyword(String keyword, Member requestMember); | ||
} |
Oops, something went wrong.