Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…into feat/#12
  • Loading branch information
pkl0912 committed Aug 30, 2024
2 parents 8bedf1b + d2a9536 commit deec22d
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,15 @@ public ApiResponse getCareerDetail(@RequestHeader("userId") Long userId,
return ErrorResponse.error(e.getErrorCode());
}
}

@GetMapping
public ApiResponse getCareers(@RequestHeader("userId") Long userId,
@RequestParam(value = "page", defaultValue = "1") int page,
@RequestParam(value = "size", defaultValue = "15") int size) {
try {
return SuccessResponse.success(SuccessCode.GET_CAREER_DETAIL_SUCCESS, careerService.getCareers(userId, page, size));
} catch (NotFoundException | BadRequestException e) {
return ErrorResponse.error(e.getErrorCode());
}
}
}
13 changes: 13 additions & 0 deletions src/main/java/com/example/letscareer/career/dto/CareerDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.letscareer.career.dto;

import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;

public record CareerDTO(
Long careerId,
@Enumerated(EnumType.STRING)
String category,
String title,
String summary
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example.letscareer.career.dto.response;

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

import java.util.List;

public record GetCareersResponse(
int currentPage,
int totalPages,
List<CareerDTO> careers
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.example.letscareer.career.domain.Career;
import com.example.letscareer.user.domain.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

Expand All @@ -10,4 +12,6 @@
@Repository
public interface CareerRepository extends JpaRepository<Career, Long> {
public Optional<Career> findByCareerIdAndUser(Long careerId, User user);

public Page<Career> findByUser(User user, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.example.letscareer.career.service;

import com.example.letscareer.career.domain.Career;
import com.example.letscareer.career.dto.CareerDTO;
import com.example.letscareer.career.dto.request.SaveCareerRequest;
import com.example.letscareer.career.dto.response.GetCareerDetailResponse;
import com.example.letscareer.career.dto.response.GetCareersResponse;
import com.example.letscareer.career.repository.CareerRepository;
import com.example.letscareer.common.exception.enums.ErrorCode;
import com.example.letscareer.common.exception.model.BadRequestException;
Expand All @@ -12,8 +14,14 @@
import com.example.letscareer.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

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

@Service
@RequiredArgsConstructor
public class CareerService {
Expand Down Expand Up @@ -65,4 +73,31 @@ public GetCareerDetailResponse getCareerDetail(Long userId, Long careerId) {
career.getResult()
);
}

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

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());

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

private String toSummary(String content) {
return content.length() > 25 ? content.substring(0, 25) + "..." : content;
}
}

0 comments on commit deec22d

Please sign in to comment.