Skip to content

Commit

Permalink
[refactor] 커리어 관련 DTO converter 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
oosedus committed Sep 4, 2024
1 parent 3a0326a commit 3efb809
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.example.letscareer.career.domain.dto.converter;

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

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

public class CareerConverter {

// Career 리스트 -> CareerDTO 리스트 변환 로직
public static List<CareerDTO> convertToCareerDTOList(List<Career> careers) {
return careers.stream()
.map(CareerConverter::convertToCareerDTO)
.collect(Collectors.toList());
}

// Career 페이지 -> CareerDTO 리스트 변환 로직
public static List<CareerDTO> convertToCareerDTOList(Page<Career> careers) {
return careers.getContent().stream()
.map(CareerConverter::convertToCareerDTO)
.collect(Collectors.toList());
}

// Career -> CareerDTO 변환 로직 메서드
private static CareerDTO convertToCareerDTO(Career career) {
return new CareerDTO(
career.getCareerId(),
career.getCategory().getValue(),
career.getTitle(),
toSummary(career.getSituation())
);
}

private static String toSummary(String content) {
return content.length() > 25 ? content.substring(0, 25) + "..." : content;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.example.letscareer.career.domain.dto.response;

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

import java.util.List;

Expand All @@ -9,4 +11,11 @@ public record GetCareersResponse(
int totalPages,
List<CareerDTO> careers
) {
public static GetCareersResponse from(Page<Career> careers, List<CareerDTO> careerDTOList) {
return new GetCareersResponse(
careers.getNumber() + 1,
careers.getTotalPages(),
careerDTOList
);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.letscareer.career.service;

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;
import com.example.letscareer.career.domain.dto.CareerDTO;
Expand All @@ -8,7 +9,6 @@
import com.example.letscareer.career.domain.dto.response.GetCareersResponse;
import com.example.letscareer.career.domain.repository.CareerRepository;
import com.example.letscareer.common.exception.enums.ErrorCode;
import com.example.letscareer.common.exception.model.BadRequestException;
import com.example.letscareer.common.exception.model.NotFoundException;
import com.example.letscareer.common.exception.model.ValidationException;
import com.example.letscareer.user.domain.User;
Expand All @@ -23,7 +23,6 @@
import org.springframework.stereotype.Service;

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

@Service
@RequiredArgsConstructor
Expand All @@ -35,8 +34,7 @@ public class CareerService {

@Transactional
public void saveCareer(Long userId, SaveCareerRequest request) {
User user = userRepository.findByUserId(userId)
.orElseThrow(() -> new NotFoundException(ErrorCode.USER_NOT_FOUND_EXCEPTION));
User user = getUser(userId);

if(request.title() == null || request.title().isEmpty()) {
throw new ValidationException(ErrorCode.CAREER_TITLE_IS_EMPTY);
Expand All @@ -46,59 +44,39 @@ public void saveCareer(Long userId, SaveCareerRequest request) {
careerRepository.save(career);
}

@Transactional
public GetCareerDetailResponse getCareerDetail(Long userId, Long careerId) {
User user = userRepository.findByUserId(userId)
.orElseThrow(() -> new NotFoundException(ErrorCode.USER_NOT_FOUND_EXCEPTION));

Career career = careerRepository.findByCareerIdAndUser(careerId, user)
.orElseThrow(() -> new NotFoundException(ErrorCode.CAREER_NOT_FOUND_EXCEPTION));

User user = getUser(userId);
Career career = getCareer(careerId, user);
return GetCareerDetailResponse.from(career);
}

@Transactional
public GetCareersResponse getCareers(Long userId, int page, int size) {
User user = userRepository.findByUserId(userId)
.orElseThrow(() -> new NotFoundException(ErrorCode.USER_NOT_FOUND_EXCEPTION));

User user = getUser(userId);
Pageable pageable = PageRequest.of(page - 1, size);

Page<Career> careers = careerRepository.findByUser(user, pageable);
List<CareerDTO> careerDTOS = careers.getContent().stream()
.map(career -> new CareerDTO(
career.getCareerId(),
career.getCategory().getValue(),
career.getTitle(),
toSummary(career.getSituation())
))
.collect(Collectors.toList());
List<CareerDTO> careerDTOS = CareerConverter.convertToCareerDTOList(careers);

return new GetCareersResponse(
careers.getNumber() + 1,
careers.getTotalPages(),
careerDTOS
);
return GetCareersResponse.from(careers, careerDTOS);
}

@Transactional
@Cacheable(value = "allCareersCache", key = "#userId", unless = "#result == null || #result.careers.size() == 0")
public GetAllCareersResponse getAllCareers(Long userId) {
User user = userRepository.findByUserId(userId)
.orElseThrow(() -> new NotFoundException(ErrorCode.USER_NOT_FOUND_EXCEPTION));

User user = getUser(userId);
List<Career> careers = careerRepository.findByUser(user);

List<CareerDTO> careerDTOS = careers.stream()
.map(career -> new CareerDTO(
career.getCareerId(),
career.getCategory().getValue(),
career.getTitle(),
toSummary(career.getSituation())
))
.collect(Collectors.toList());

List<CareerDTO> careerDTOS = CareerConverter.convertToCareerDTOList(careers);
return new GetAllCareersResponse(careerDTOS);
}

private String toSummary(String content) {
return content.length() > 25 ? content.substring(0, 25) + "..." : content;
private User getUser(Long userId) {
return userRepository.findByUserId(userId)
.orElseThrow(() -> new NotFoundException(ErrorCode.USER_NOT_FOUND_EXCEPTION));
}

private Career getCareer(Long careerId, User user) {
return careerRepository.findByCareerIdAndUser(careerId, user)
.orElseThrow(() -> new NotFoundException(ErrorCode.CAREER_NOT_FOUND_EXCEPTION));
}
}

0 comments on commit 3efb809

Please sign in to comment.