Skip to content

Commit

Permalink
Merge pull request #31 from OnAndOff-UMC/feat/#30
Browse files Browse the repository at this point in the history
[feat]: 요일 관련 API 구현
  • Loading branch information
kimjm9841 authored Jan 30, 2024
2 parents c542859 + 47fb076 commit 46b3418
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.onnoff.onnoff.domain.weekday.controller;

import com.onnoff.onnoff.apiPayload.ApiResponse;
import com.onnoff.onnoff.domain.weekday.dto.WeekdayResponseDTO;
import com.onnoff.onnoff.domain.weekday.service.WeekdayService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;

@RestController
@RequiredArgsConstructor
public class WeekdayController {

private final WeekdayService weekdayService;

@GetMapping("/weekdays/init")
@Operation(summary = "초기 요일 조회 API", description = "오늘 날짜를 기준으로 일주일을 조회하는 API입니다.")
public ApiResponse<WeekdayResponseDTO.WeekdayResultDTO> getInitWeekday() {
return ApiResponse.onSuccess(weekdayService.getWeekday(LocalDate.now()));
}

@GetMapping("/weekdays/prev")
@Operation(summary = "이전 주 요일 조회 API", description = "입력된 날짜를 기준으로 이전 일주일을 조회하는 API입니다. Query String으로 날짜를 입력해 주세요.")
public ApiResponse<WeekdayResponseDTO.WeekdayResultDTO> getPrevWeekday(@RequestParam(name = "date") LocalDate date) {
return ApiResponse.onSuccess(weekdayService.getWeekday(date.minusDays(7)));
}

@GetMapping("/weekdays/next")
@Operation(summary = "다음 주 요일 조회 API", description = "입력된 날짜를 기준으로 다음 일주일을 조회하는 API입니다. Query String으로 날짜를 입력해 주세요.")
public ApiResponse<WeekdayResponseDTO.WeekdayResultDTO> getNextWeekday(@RequestParam(name = "date") LocalDate date) {
return ApiResponse.onSuccess(weekdayService.getWeekday(date.plusDays(7)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.onnoff.onnoff.domain.weekday.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDate;

public class WeekdayResponseDTO {

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class WeekdayResultDTO {
LocalDate monday;
LocalDate tuesday;
LocalDate wednesday;
LocalDate thursday;
LocalDate friday;
LocalDate saturday;
LocalDate sunday;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.onnoff.onnoff.domain.weekday.service;

import com.onnoff.onnoff.domain.weekday.dto.WeekdayResponseDTO;

import java.time.LocalDate;

public interface WeekdayService {

WeekdayResponseDTO.WeekdayResultDTO getWeekday(LocalDate date);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.onnoff.onnoff.domain.weekday.service;

import com.onnoff.onnoff.domain.weekday.dto.WeekdayResponseDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

@Service
@RequiredArgsConstructor
public class WeekdayServiceImpl implements WeekdayService {

@Override
public WeekdayResponseDTO.WeekdayResultDTO getWeekday(LocalDate date) {
LocalDate monday = date.minusDays(date.getDayOfWeek().getValue() - 1);

return WeekdayResponseDTO.WeekdayResultDTO.builder()
.monday(monday)
.tuesday(monday.plusDays(1))
.wednesday(monday.plusDays(2))
.thursday(monday.plusDays(3))
.friday(monday.plusDays(4))
.saturday(monday.plusDays(5))
.sunday(monday.plusDays(6))
.build();
}
}

0 comments on commit 46b3418

Please sign in to comment.