Skip to content

Commit

Permalink
Merge branch 'develop' into feature/recruit
Browse files Browse the repository at this point in the history
  • Loading branch information
tape4 authored Dec 4, 2024
2 parents ce81956 + ce56eba commit 7930c78
Show file tree
Hide file tree
Showing 19 changed files with 439 additions and 310 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import umc.kkijuk.server.dashboard.controller.response.IntroduceRemindResponse;
import umc.kkijuk.server.dashboard.controller.response.RecruitRemindResponse;
import umc.kkijuk.server.introduce.domain.Introduce;
import umc.kkijuk.server.introduce.domain.IntroduceRepository;
import umc.kkijuk.server.introduce.repository.IntroduceRepository;
import umc.kkijuk.server.member.domain.Member;
import umc.kkijuk.server.recruit.controller.port.RecruitService;
import umc.kkijuk.server.recruit.domain.Recruit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,26 @@
import umc.kkijuk.server.introduce.common.BaseResponse;
import umc.kkijuk.server.introduce.controller.response.IntroduceListResponse;
import umc.kkijuk.server.introduce.controller.response.IntroduceResponse;
import umc.kkijuk.server.introduce.controller.response.MasterIntroduceResponse;
import umc.kkijuk.server.introduce.dto.*;
import umc.kkijuk.server.introduce.service.IntroduceService;
import umc.kkijuk.server.introduce.service.MasterIntroduceService;
import umc.kkijuk.server.login.argumentresolver.Login;
import umc.kkijuk.server.login.controller.dto.LoginInfo;
import umc.kkijuk.server.member.domain.Member;
import umc.kkijuk.server.member.service.MemberService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Tag(name = "introduce", description = "자기소개서 API")
@RestController
@RequiredArgsConstructor
@RequestMapping("/history/intro/")
public class IntroduceController {
private final IntroduceService introduceService;
private final MasterIntroduceService masterIntroduceService;
private final MemberService memberService;

private final Member requestMember = Member.builder()
Expand Down Expand Up @@ -90,4 +95,14 @@ public ResponseEntity<Object> delete(
.body(new BaseResponse<>(HttpStatus.OK.value(), "자기소개서 삭제 완료", intro_Id));
}

@GetMapping("/search")
@Operation(summary = "키워드로 자기소개서 문단 검색")
public ResponseEntity<Map<String, Object>> searchIntroduceByKeyword(@Login LoginInfo loginInfo, @RequestParam String keyword) {
Member requestMember = memberService.getById(loginInfo.getMemberId());
Map<String, Object> response = introduceService.searchIntroduceAndMasterByKeyword(keyword, requestMember);
return ResponseEntity.status(HttpStatus.OK).body(response);
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package umc.kkijuk.server.introduce.controller.response;

import lombok.*;

import java.time.LocalDate;
import java.time.LocalDateTime;

@Data
@Getter
@Builder
@AllArgsConstructor
public class FindIntroduceResponse {
private Long introId;
private String title;
private String content;
private LocalDate createdDate;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package umc.kkijuk.server.introduce.controller.response;

import lombok.*;

import java.time.LocalDate;
import java.time.LocalDateTime;

@Data
@Getter
@Builder
@AllArgsConstructor
public class FindMasterIntroduceResponse {
private Long masterIntroId;
private String title;
private String content;
private LocalDate createdDate;
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package umc.kkijuk.server.introduce.domain;
package umc.kkijuk.server.introduce.repository;

import org.springframework.data.domain.Page;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import org.springframework.data.domain.Pageable;
import umc.kkijuk.server.introduce.domain.Introduce;

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

Expand All @@ -16,4 +18,10 @@ public interface IntroduceRepository extends JpaRepository<Introduce, Long> {
@Query("SELECT i FROM Introduce i WHERE i.memberId = :memberId AND i.state = :state ORDER BY i.recruit.endTime ASC")
Page<Introduce> findByMemberIdAndStateOrderByEndTimeAsc(@Param("memberId") Long memberId, @Param("state") int state, Pageable pageable);

@Query("SELECT i FROM Introduce i " +
"JOIN i.questions q " +
"WHERE i.memberId = :memberId AND q.content LIKE %:keyword%")
List<Introduce> searchIntroduceByKeywordForMember(String keyword, Long memberId);


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package umc.kkijuk.server.introduce.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import umc.kkijuk.server.introduce.domain.MasterIntroduce;

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

public interface MasterIntroduceRepository extends JpaRepository<MasterIntroduce, Long> {
Optional<MasterIntroduce> findByMemberId(Long memberId);
@Query("SELECT m FROM MasterIntroduce m " +
"JOIN m.masterQuestion mq " +
"WHERE m.memberId = :memberId AND mq.content LIKE %:keyword%")
List<MasterIntroduce> searchMasterIntroduceByKeywordForMember(String keyword, Long memberId);

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package umc.kkijuk.server.introduce.domain;
package umc.kkijuk.server.introduce.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import umc.kkijuk.server.introduce.domain.MasterQuestion;

public interface MasterQuestionRepository extends JpaRepository<MasterQuestion, Long> {
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package umc.kkijuk.server.introduce.domain;
package umc.kkijuk.server.introduce.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import umc.kkijuk.server.introduce.domain.Question;

import java.util.List;
import java.util.Optional;
Expand Down
176 changes: 11 additions & 165 deletions src/main/java/umc/kkijuk/server/introduce/service/IntroduceService.java
Original file line number Diff line number Diff line change
@@ -1,172 +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.RecruitOwnerMismatchException;
import umc.kkijuk.server.common.domian.exception.ResourceNotFoundException;
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.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.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
import java.util.List;
import java.util.Map;

@RequiredArgsConstructor
@Service
public class IntroduceService {
public interface IntroduceService {

private final IntroduceRepository introduceRepository;
private final RecruitJpaRepository recruitJpaRepository;
private final QuestionRepository questionRepository;

@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
}*/
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);
}
Loading

0 comments on commit 7930c78

Please sign in to comment.