Skip to content

Commit

Permalink
Merge pull request #70 from LetsCareer-A/feat/#69
Browse files Browse the repository at this point in the history
#69 [refactor] 커리어 관련 코드 리팩토링
  • Loading branch information
pkl0912 authored Sep 5, 2024
2 parents b8e3bf1 + 3efb809 commit 0bf04ba
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 70 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,5 +1,6 @@
package com.example.letscareer.career.domain.dto.response;

import com.example.letscareer.career.domain.model.Career;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;

Expand All @@ -13,4 +14,15 @@ public record GetCareerDetailResponse(
String action,
String result
) {
public static GetCareerDetailResponse from(Career career) {
return new GetCareerDetailResponse(
career.getCareerId(),
career.getCategory().getValue(),
career.getTitle(),
career.getSituation(),
career.getTask(),
career.getAction(),
career.getResult()
);
}
}
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,6 +1,7 @@
package com.example.letscareer.career.domain.model;


import com.example.letscareer.career.domain.dto.request.SaveCareerRequest;
import com.example.letscareer.user.domain.User;
import jakarta.persistence.*;
import lombok.*;
Expand All @@ -9,10 +10,9 @@

@Entity
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class Career {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand All @@ -36,4 +36,16 @@ public class Career {
private String action;
@Lob
private String result;

public static Career of(User user, SaveCareerRequest request) {
return Career.builder()
.user(user)
.title(request.title())
.category(request.category())
.situation(request.situation())
.task(request.task())
.action(request.action())
.result(request.result())
.build();
}
}
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,92 +34,49 @@ 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);
}

try {
Career career = Career.builder()
.user(user)
.category(request.category())
.title(request.title())
.situation(request.situation())
.task(request.task())
.action(request.action())
.result(request.result())
.build();

careerRepository.save(career);
} catch (Exception e) {
throw new BadRequestException(ErrorCode.INTERNAL_SERVER_EXCEPTION);
}
Career career = Career.of(user, 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));

return new GetCareerDetailResponse(
career.getCareerId(),
career.getCategory().getValue(),
career.getTitle(),
career.getSituation(),
career.getTask(),
career.getAction(),
career.getResult()
);
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 0bf04ba

Please sign in to comment.