Skip to content

Commit

Permalink
Merge pull request #92 from LetsCareer-A/refactor/#91
Browse files Browse the repository at this point in the history
#91 [refactor] 커리어 전체 조회 시 어필할 커리어 등록된 커리어 표시되게 수정
  • Loading branch information
oosedus authored Sep 11, 2024
2 parents 0876539 + 5bd2be1 commit 6d86688
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.letscareer.appealCareer.domain.repository;

import com.example.letscareer.appealCareer.domain.model.AppealCareer;
import com.example.letscareer.career.domain.model.Career;
import com.example.letscareer.stage.domain.model.Stage;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
Expand All @@ -10,4 +11,5 @@
@Repository
public interface AppealCareerRepository extends JpaRepository<AppealCareer, Long> {
List<AppealCareer> findByStage(Stage stage);
List<AppealCareer> findByStageAndCareerIn(Stage stage, List<Career> careers);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@

@RestController
@RequiredArgsConstructor
@RequestMapping("/careers")
public class CareerController {

private final UserRepository userRepository;
private final CareerService careerService;

@PostMapping
@PostMapping("/careers")
public ApiResponse saveCareer(@RequestHeader("userId") Long userId,
@RequestBody SaveCareerRequest request) {

try {
careerService.saveCareer(userId, request);
return SuccessNonDataResponse.success(SuccessCode.SAVE_CAREER_SUCCESS);
Expand All @@ -36,7 +34,7 @@ public ApiResponse saveCareer(@RequestHeader("userId") Long userId,
}
}

@GetMapping("/{careerId}")
@GetMapping("/careers/{careerId}")
public ApiResponse getCareerDetail(@RequestHeader("userId") Long userId,
@PathVariable Long careerId) {
try {
Expand All @@ -46,7 +44,7 @@ public ApiResponse getCareerDetail(@RequestHeader("userId") Long userId,
}
}

@GetMapping
@GetMapping("/careers")
public ApiResponse getCareers(@RequestHeader("userId") Long userId,
@RequestParam(value = "page", defaultValue = "1") int page,
@RequestParam(value = "size", defaultValue = "15") int size,
Expand All @@ -58,10 +56,12 @@ public ApiResponse getCareers(@RequestHeader("userId") Long userId,
}
}

@GetMapping("/all")
public ApiResponse getAllCareers(@RequestHeader("userId") Long userId) {
@GetMapping("/schedules/{scheduleId}/stages/{stageId}/careers/all")
public ApiResponse getAllCareers(@RequestHeader("userId") Long userId,
@PathVariable Long scheduleId,
@PathVariable Long stageId) {
try {
return SuccessResponse.success(SuccessCode.GET_ALL_CAREERS_SUCCESS, careerService.getAllCareers(userId));
return SuccessResponse.success(SuccessCode.GET_ALL_CAREERS_SUCCESS, careerService.getAllCareers(userId, scheduleId, stageId));
} catch (NotFoundException | BadRequestException e) {
return ErrorResponse.error(e.getErrorCode());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.letscareer.career.domain.dto;

public record CareerWithAppealDTO(
Long careerId,
String category,
String title,
String summary,
boolean isAppeal
) {
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,38 @@
package com.example.letscareer.career.domain.dto.converter;

import com.example.letscareer.appealCareer.domain.model.AppealCareer;
import com.example.letscareer.career.domain.dto.CareerDTO;
import com.example.letscareer.career.domain.dto.CareerWithAppealDTO;
import com.example.letscareer.career.domain.model.Career;
import org.springframework.data.domain.Page;

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

public class CareerConverter {

// Career 리스트 -> CareerDTO 리스트 변환 로직
public static List<CareerDTO> convertToCareerDTOList(List<Career> careers) {
public static List<CareerWithAppealDTO> convertToCareerWithAppealDTOList(List<Career> careers, List<AppealCareer> appealCareers) {
// Career ID를 기준으로 어필 여부를 확인하는 맵 생성
Map<Long, Boolean> appealMap = appealCareers.stream()
.collect(Collectors.toMap(ac -> ac.getCareer().getCareerId(), ac -> true));

return careers.stream()
.map(CareerConverter::convertToCareerDTO)
.map(career -> convertToCareerWithAppealDTO(career, appealMap.getOrDefault(career.getCareerId(), false)))
.collect(Collectors.toList());
}

public static CareerWithAppealDTO convertToCareerWithAppealDTO(Career career, boolean isAppeal) {
return new CareerWithAppealDTO(
career.getCareerId(),
career.getCategory().getValue(),
career.getTitle(),
toSummary(career.getSituation()),
isAppeal
);
}

// Career 페이지 -> CareerDTO 리스트 변환 로직
public static List<CareerDTO> convertToCareerDTOList(Page<Career> careers) {
return careers.getContent().stream()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.example.letscareer.career.domain.dto.response;

import com.example.letscareer.career.domain.dto.CareerDTO;
import com.example.letscareer.career.domain.dto.CareerWithAppealDTO;

import java.util.List;

public record GetAllCareersResponse(
List<CareerDTO> careers
List<CareerWithAppealDTO> careers
) {
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.example.letscareer.career.service;

import com.example.letscareer.appealCareer.domain.model.AppealCareer;
import com.example.letscareer.appealCareer.domain.repository.AppealCareerRepository;
import com.example.letscareer.career.domain.dto.CareerWithAppealDTO;
import com.example.letscareer.career.domain.dto.converter.CareerConverter;
import com.example.letscareer.career.domain.dto.response.GetAllCareersResponse;
import com.example.letscareer.career.domain.model.Career;
Expand All @@ -11,6 +14,10 @@
import com.example.letscareer.common.exception.enums.ErrorCode;
import com.example.letscareer.common.exception.model.NotFoundException;
import com.example.letscareer.common.exception.model.ValidationException;
import com.example.letscareer.schedule.domain.model.Schedule;
import com.example.letscareer.schedule.domain.repository.ScheduleRepository;
import com.example.letscareer.stage.domain.model.Stage;
import com.example.letscareer.stage.domain.repository.StageRepository;
import com.example.letscareer.user.domain.User;
import com.example.letscareer.user.domain.repository.UserRepository;
import jakarta.transaction.Transactional;
Expand All @@ -31,6 +38,9 @@ public class CareerService {
@Autowired
private final CareerRepository careerRepository;
private final UserRepository userRepository;
private final AppealCareerRepository appealCareerRepository;
private final ScheduleRepository scheduleRepository;
private final StageRepository stageRepository;

@Transactional
public void saveCareer(Long userId, SaveCareerRequest request) {
Expand Down Expand Up @@ -63,10 +73,16 @@ public GetCareersResponse getCareers(Long userId, int page, int size, List<Strin

@Transactional
@Cacheable(value = "allCareersCache", key = "#userId", unless = "#result == null || #result.careers.size() == 0")
public GetAllCareersResponse getAllCareers(Long userId) {
public GetAllCareersResponse getAllCareers(Long userId, Long scheduleId, Long stageId) {
User user = getUser(userId);
Schedule schedule = getSchedule(scheduleId);
Stage stage = getStage(schedule, stageId);

List<Career> careers = careerRepository.findByUser(user);
List<CareerDTO> careerDTOS = CareerConverter.convertToCareerDTOList(careers);

// AppealCareer 테이블에서 해당 커리어들이 핵심으로 등록되었는지 확인
List<AppealCareer> appealCareers = appealCareerRepository.findByStageAndCareerIn(stage, careers);
List<CareerWithAppealDTO> careerDTOS = CareerConverter.convertToCareerWithAppealDTOList(careers, appealCareers);
return new GetAllCareersResponse(careerDTOS);
}

Expand All @@ -79,4 +95,14 @@ private Career getCareer(Long careerId, User user) {
return careerRepository.findByCareerIdAndUser(careerId, user)
.orElseThrow(() -> new NotFoundException(ErrorCode.CAREER_NOT_FOUND_EXCEPTION));
}

private Schedule getSchedule(Long scheduleId) {
return scheduleRepository.findById(scheduleId)
.orElseThrow(() -> new NotFoundException(ErrorCode.SCHEDULE_NOT_FOUND_EXCEPTION));
}

private Stage getStage(Schedule schedule, Long stageId) {
return stageRepository.findByStageIdAndSchedule(stageId, schedule)
.orElseThrow(() -> new NotFoundException(ErrorCode.STAGE_NOT_FOUND_EXCEPTION));
}
}

0 comments on commit 6d86688

Please sign in to comment.