Skip to content

Commit

Permalink
Merge pull request #2 from LetsCareer-A/feat/#1
Browse files Browse the repository at this point in the history
#1[refactor] 1.1 api 기능개발
  • Loading branch information
oosedus authored Aug 30, 2024
2 parents 3765e7d + 7023197 commit 15aa225
Show file tree
Hide file tree
Showing 10 changed files with 220 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
@Getter
@ToString
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class SuccessResponse extends ApiResponse{
public class SuccessResponse<T> extends ApiResponse{
private final int code;
private final String message;
private final T data;

public static SuccessResponse success(SuccessCode successCode) {
return new SuccessResponse(successCode.getHttpStatus().value(), successCode.getMessage());
public static <T> SuccessResponse<T> success(SuccessCode successCode, T data) {
return new SuccessResponse(successCode.getHttpStatus().value(), successCode.getMessage(), data);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
public enum SuccessCode {


LOGOUT_SUCCESS(HttpStatus.OK, "로그아웃 성공입니다."); //예


SCHEDULE_SUCCESS(HttpStatus.OK, "일정 찾기 성공입니다.");

private final HttpStatus httpStatus;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.example.letscareer.schedule.controller;

import com.example.letscareer.common.dto.ApiResponse;
import com.example.letscareer.common.dto.ErrorResponse;
import com.example.letscareer.common.dto.SuccessResponse;
import com.example.letscareer.common.exception.enums.ErrorCode;
import com.example.letscareer.common.exception.enums.SuccessCode;
import com.example.letscareer.common.exception.model.NotFoundException;
import com.example.letscareer.schedule.dto.response.ScheduleResponse;
import com.example.letscareer.schedule.service.ScheduleService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/schedules")
public class ScheduleController {
private final ScheduleService scheduleService;

@GetMapping
public ApiResponse getSchedules(
@RequestHeader("userId") Long userId,
@RequestParam("month") int month,
@RequestParam("page") int page,
@RequestParam("size") int size) {

ScheduleResponse scheduleResponse = scheduleService.getSchedules(userId, month, page, size);
return SuccessResponse.success(SuccessCode.SCHEDULE_SUCCESS, scheduleResponse);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import lombok.*;
import org.antlr.v4.runtime.misc.NotNull;

import java.util.Date;

@Entity
@Getter
@Setter
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.example.letscareer.schedule.dto.response;

import java.util.List;

public record ScheduleResponse(
Integer page,
Integer size,
Integer docCount,
Integer midCount,
Integer interviewCount,
List<StageDTO> schedules

) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.example.letscareer.schedule.dto.response;

import com.example.letscareer.schedule.domain.Progress;

import java.util.Date;

public record StageDTO(
Long scheduleId,
Long stageId,
String company,
String department,
String type,
Date deadline,
int dday,
Progress progress){
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.example.letscareer.schedule.repository;

import com.example.letscareer.schedule.domain.Schedule;
import io.lettuce.core.dynamic.annotation.Param;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

@Repository
public interface ScheduleRepository extends JpaRepository<Schedule, Long> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package com.example.letscareer.schedule.service;

import com.example.letscareer.common.exception.model.NotFoundException;
import com.example.letscareer.schedule.domain.Schedule;
import com.example.letscareer.schedule.dto.response.StageDTO;
import com.example.letscareer.schedule.dto.response.ScheduleResponse;
import com.example.letscareer.schedule.repository.ScheduleRepository;
import com.example.letscareer.stage.domain.Stage;
import com.example.letscareer.stage.repository.StageRepository;
import com.example.letscareer.user.domain.User;
import com.example.letscareer.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
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.time.LocalDate;
import java.time.Period;
import java.time.ZoneId;
import java.util.*;

import static com.example.letscareer.common.exception.enums.ErrorCode.USER_NOT_FOUND_EXCEPTION;

@Service
@RequiredArgsConstructor
public class ScheduleService {
private final UserRepository userRepository;
private final StageRepository stageRepository;
private final ScheduleRepository scheduleRepository;

public ScheduleResponse getSchedules(final Long userId, final int month, final int page, final int size) {

Optional<User> user = userRepository.findByUserId(userId);
if (user.isEmpty()) {
throw new NotFoundException(USER_NOT_FOUND_EXCEPTION);
}

Pageable pageable = PageRequest.of(page - 1, size);

// user, month 로 stage 찾기
Page<Stage> stagePage = stageRepository.findAllByUserIdAndMonth(userId, month, pageable);

int docCount = 0;
int midCount = 0;
int interviewCount = 0;

List<StageDTO> schedules = new ArrayList<>();

for (Stage stage : stagePage) {
Schedule schedule = stage.getSchedule();
if (schedule != null) {
Long scheduleId = schedule.getScheduleId();
Long stageId = stage.getStageId();
String type = stage.getType().getValue();
Date deadline = stage.getDate();

switch (stage.getType()) {
case DOC:
docCount++;
break;
case MID:
midCount++;
break;
case INT:
interviewCount++;
break;
}
Integer dday = (deadline != null) ? calculateDday(deadline) : null;

schedules.add(new StageDTO(
scheduleId,
stageId,
schedule.getCompany(),
schedule.getDepartment(),
type,
deadline,
dday,
schedule.getProgress()
));
}
}

//sort -1, -3, +1
schedules.sort(
Comparator.<StageDTO>comparingInt(dto -> (dto.dday() < 0 ? -1 : 1)) // 음수 dday 우선 정렬
.thenComparingInt(dto -> Math.abs(dto.dday())) // 절대값 기준 정렬
);
return new ScheduleResponse(
page,
size,
docCount,
midCount,
interviewCount,
schedules
);
}

private int calculateDday(Date deadline) {
LocalDate deadlineDate = deadline.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
int dday = Period.between(LocalDate.now(), deadlineDate).getDays();
return dday;
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.example.letscareer.stage.repository;

import com.example.letscareer.schedule.domain.Schedule;
import com.example.letscareer.stage.domain.Stage;
import io.lettuce.core.dynamic.annotation.Param;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface StageRepository extends JpaRepository<Stage, Long> {

@Query("SELECT st FROM Stage st WHERE st.schedule.user.userId = :userId AND FUNCTION('MONTH', st.date) = :month")
Page<Stage> findAllByUserIdAndMonth(@Param("userId") Long userId, @Param("month") int month, Pageable pageable);
List<Stage>findAllByScheduleScheduleId(Long scheduleId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example.letscareer.user.repository;

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

import java.util.Optional;

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
public Optional<User> findByUserId(Long userId);
}

0 comments on commit 15aa225

Please sign in to comment.