-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
#1[refactor] 1.1 api 기능개발
- Loading branch information
Showing
10 changed files
with
220 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/main/java/com/example/letscareer/schedule/controller/ScheduleController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/java/com/example/letscareer/schedule/dto/response/ScheduleResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
) { | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/example/letscareer/schedule/dto/response/StageDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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){ | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/example/letscareer/schedule/repository/ScheduleRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { | ||
|
||
} |
106 changes: 106 additions & 0 deletions
106
src/main/java/com/example/letscareer/schedule/service/ScheduleService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} | ||
|
20 changes: 20 additions & 0 deletions
20
src/main/java/com/example/letscareer/stage/repository/StageRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/example/letscareer/user/repository/UserRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |