Skip to content

Commit

Permalink
refactor: on API 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
threeony committed Feb 2, 2024
1 parent a59b205 commit b91ed0a
Show file tree
Hide file tree
Showing 21 changed files with 62 additions and 69 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/onnoff/onnoff/auth/config/WebConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ public void addInterceptors(InterceptorRegistry registry) {

registry.addInterceptor(new UserInterceptor(userService, jwtUtil))
.addPathPatterns("/**") // 스프링 경로는 /*와 /**이 다름
.excludePathPatterns("/swagger-ui/**", "/v3/api-docs/**", "/oauth2/**", "/on/**", "/health", "/token/**" , "/message/**");
.excludePathPatterns("/swagger-ui/**", "/v3/api-docs/**", "/oauth2/**", "/health", "/token/**" , "/message/**");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
@RequiredArgsConstructor
public class JwtAuthFilter extends OncePerRequestFilter {
private final JwtTokenProvider jwtTokenProvider;
private final static String[] ignorePrefix = {"/swagger-ui", "/v3/api-docs", "/oauth2", "/on", "/health", "/token/validate" , "/message"};
private final static String[] ignorePrefix = {"/swagger-ui", "/v3/api-docs", "/oauth2", "/health", "/token/validate" , "/message"};
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
log.info("url ={}", request.getRequestURI());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,30 @@ public class ResolutionController {

@GetMapping("/")
@Operation(summary = "오늘의 다짐 조회 API")
public ApiResponse<ResolutionResponse.ResolutionViewDTO> getResolutions(@RequestParam(name = "userId") Long userId,
@RequestParam(name = "date") LocalDate date){
List<Resolution> resolutionList = resolutionService.getAll(userId, date);
return ApiResponse.onSuccess(ResolutionConverter.toResolutionViewDTO(userId, date, resolutionList));
public ApiResponse<ResolutionResponse.ResolutionViewDTO> getResolutions(@RequestParam(name = "date") LocalDate date){
List<Resolution> resolutionList = resolutionService.getAll(date);
return ApiResponse.onSuccess(ResolutionConverter.toResolutionViewDTO(date, resolutionList));
}

@PostMapping("/")
@Operation(summary = "오늘의 다짐 추가 API")
public ApiResponse<ResolutionResponse.AddResultDTO> addResolution(@RequestParam(name = "userId") Long userId,
@RequestBody @Valid ResolutionRequest.AddResolutionDTO request){
Resolution resolution = resolutionService.addResolution(userId, request);
public ApiResponse<ResolutionResponse.AddResultDTO> addResolution(@RequestBody @Valid ResolutionRequest.AddResolutionDTO request){
Resolution resolution = resolutionService.addResolution(request);
return ApiResponse.onSuccess(ResolutionConverter.toAddResolutionResultDTO(resolution));
}

@PutMapping("/")
@Operation(summary = "오늘의 다짐 수정 API")
public ApiResponse<ResolutionResponse.ResolutionViewDTO> modifyResolution(@RequestParam(name = "userId") Long userId,
@RequestBody @Valid ResolutionRequest.ModifyResolutionDTO request){
resolutionService.modifyResolution(userId, request);
return ApiResponse.onSuccess(null);
public ApiResponse<ResolutionResponse.ResolutionViewDTO> modifyResolution(@RequestBody @Valid ResolutionRequest.ModifyResolutionDTO request){
List<Resolution> modifiedResolutionList = resolutionService.modifyResolution(request);
return ApiResponse.onSuccess(ResolutionConverter.toResolutionViewDTO(request.getDate(), modifiedResolutionList));
}

@DeleteMapping("/{resolutionId}")
@Operation(summary = "오늘의 다짐 삭제 API")
public ApiResponse<ResolutionResponse.AddResultDTO> deleteResolution(@RequestParam(name = "userId") Long userId,
@RequestParam(name = "date") LocalDate date,
@PathVariable(name = "resolutionId") Long resolutionId){
resolutionService.deleteResolution(userId, date, resolutionId);
public ApiResponse<Long> deleteResolution(@RequestParam(name = "date") LocalDate date,
@PathVariable(name = "resolutionId") Long resolutionId){
resolutionService.deleteResolution(date, resolutionId);
return ApiResponse.onSuccess(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ public static Resolution toAddResolution(Long order, ResolutionRequest.AddResolu
public static ResolutionResponse.AddResultDTO toAddResolutionResultDTO(Resolution resolution){
return ResolutionResponse.AddResultDTO.builder()
.resolutionId(resolution.getId())
.order(resolution.getOrder())
.content(resolution.getContent())
.createdAt(resolution.getCreatedAt())
.build();
}

public static ResolutionResponse.ResolutionViewDTO toResolutionViewDTO(Long userId, LocalDate date, List<Resolution> resolutionList){
public static ResolutionResponse.ResolutionViewDTO toResolutionViewDTO(LocalDate date, List<Resolution> resolutionList){
List<ResolutionResponse.ResolutionDTO> resolutionDTOList = resolutionList.stream()
.map(resolution -> ResolutionResponse.ResolutionDTO.builder()
.resolutionId(resolution.getId())
Expand All @@ -38,7 +40,6 @@ public static ResolutionResponse.ResolutionViewDTO toResolutionViewDTO(Long user
.collect(Collectors.toList());

return ResolutionResponse.ResolutionViewDTO.builder()
.userId(userId)
.date(date)
.resolutionDTOList(resolutionDTOList)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import jakarta.validation.constraints.*;
import lombok.Getter;

import java.time.LocalDate;
Expand All @@ -14,7 +11,7 @@
public class ResolutionRequest {
@Getter
public static class AddResolutionDTO{
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
@PastOrPresent
LocalDate date;
@NotBlank
@Size(max = 30)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class ResolutionResponse {
@AllArgsConstructor
public static class AddResultDTO{
Long resolutionId;
Integer order;
String content;
LocalDateTime createdAt;
}

Expand All @@ -24,7 +26,6 @@ public static class AddResultDTO{
@NoArgsConstructor
@AllArgsConstructor
public static class ResolutionViewDTO{
Long userId;
LocalDate date;
List<ResolutionDTO> resolutionDTOList;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.util.List;

public interface ResolutionRepository extends JpaRepository<Resolution, Long> {
List<Resolution> findAllByUserAndDate(User user, LocalDate date);
List<Resolution> findAllByUserAndDateOrderByOrder(User user, LocalDate date);

Long countByUserAndDate(User user, LocalDate date);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

public interface ResolutionService {

List<Resolution> getAll(Long userId, LocalDate date);
List<Resolution> getAll(LocalDate date);

Resolution addResolution(Long userId, ResolutionRequest.AddResolutionDTO request);
Resolution addResolution(ResolutionRequest.AddResolutionDTO request);

void modifyResolution(Long userId, ResolutionRequest.ModifyResolutionDTO request);
List<Resolution> modifyResolution(ResolutionRequest.ModifyResolutionDTO request);

void deleteResolution(Long userId, LocalDate date, Long resolutionId);
void deleteResolution(LocalDate date, Long resolutionId);
}
Original file line number Diff line number Diff line change
@@ -1,41 +1,38 @@
package com.onnoff.onnoff.domain.on.resolution.service;

import com.onnoff.onnoff.apiPayload.code.status.ErrorStatus;
import com.onnoff.onnoff.apiPayload.exception.GeneralException;
import com.onnoff.onnoff.apiPayload.exception.handler.ResolutionHandler;
import com.onnoff.onnoff.auth.UserContext;
import com.onnoff.onnoff.domain.on.resolution.converter.ResolutionConverter;
import com.onnoff.onnoff.domain.on.resolution.dto.ResolutionRequest;
import com.onnoff.onnoff.domain.on.resolution.entity.Resolution;
import com.onnoff.onnoff.domain.on.resolution.repository.ResolutionRepository;
import com.onnoff.onnoff.domain.user.User;
import com.onnoff.onnoff.domain.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
@RequiredArgsConstructor
public class ResolutionServiceImpl implements ResolutionService{
private final UserRepository userRepository;
private final ResolutionRepository resolutionRepository;

@Override
public List<Resolution> getAll(Long userId, LocalDate date){
User user = userRepository.findById(userId)
.orElseThrow(() -> new GeneralException(ErrorStatus.USER_NOT_FOUND));
public List<Resolution> getAll(LocalDate date){
User user = UserContext.getUser();

return resolutionRepository.findAllByUserAndDate(user, date).stream().toList();
return resolutionRepository.findAllByUserAndDateOrderByOrder(user, date).stream().toList();
}

@Override
@Transactional
public Resolution addResolution(Long userId, ResolutionRequest.AddResolutionDTO request){
User user = userRepository.findById(userId)
.orElseThrow(() -> new GeneralException(ErrorStatus.USER_NOT_FOUND));
public Resolution addResolution(ResolutionRequest.AddResolutionDTO request){
User user = UserContext.getUser();

Long order = resolutionRepository.countByUserAndDate(user, request.getDate());

Expand All @@ -47,33 +44,34 @@ public Resolution addResolution(Long userId, ResolutionRequest.AddResolutionDTO

@Override
@Transactional
public void modifyResolution(Long userId, ResolutionRequest.ModifyResolutionDTO requestList){
public List<Resolution> modifyResolution(ResolutionRequest.ModifyResolutionDTO requestList){
List<Resolution> modifiedResolutionList = new ArrayList<>();
for(ResolutionRequest.ResolutionDTO request : requestList.getResolutionDTOList()){

Resolution resolutionToModify = resolutionRepository.findById(request.getResolutionId())
.orElseThrow(() -> new ResolutionHandler(ErrorStatus.RESOLUTION_NOT_FOUND));
resolutionToModify.modifyResolution(request.getOrder(), request.getContent());
modifiedResolutionList.add(resolutionToModify);
}
return modifiedResolutionList;
}

@Override
@Transactional
public void deleteResolution(Long userId, LocalDate date, Long resolutionId){
public void deleteResolution(LocalDate date, Long resolutionId){
Resolution resolutionToDelete = resolutionRepository.findById(resolutionId)
.orElseThrow(() -> new ResolutionHandler(ErrorStatus.RESOLUTION_NOT_FOUND));

//해당 resolution 아래 객체들 순서 당겨주기
User user = userRepository.findById(userId)
.orElseThrow(() -> new GeneralException(ErrorStatus.USER_NOT_FOUND));
List<Resolution> resolutionList = resolutionRepository.findAllByUserAndDate(user, date).stream().toList();
User user = UserContext.getUser();
List<Resolution> resolutionList = resolutionRepository.findAllByUserAndDateOrderByOrder(user, date).stream().toList();

for(Resolution resolution : resolutionList){
if(resolution.getOrder() > resolutionToDelete.getOrder()){
resolution.setOrder(resolution.getOrder() - 1);
}
}


resolutionRepository.delete(resolutionToDelete);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ public class OnController {

@GetMapping("/")
@Operation(summary = "ON 화면 조회")
public ApiResponse<OnResponse.OnViewDTO> getOn(@RequestParam(name = "userId") Long userId,
@RequestParam(name = "date", required = false) LocalDate date){
return ApiResponse.onSuccess(onService.getOn(userId, date));
public ApiResponse<OnResponse.OnViewDTO> getOn(@RequestParam(name = "date", required = false) LocalDate date){
return ApiResponse.onSuccess(onService.getOn(date));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ public class WorklogController {

@PostMapping("/")
@Operation(summary = "업무일지 추가 API")
public ApiResponse<WorklogResponse.AddResultDTO> addWorklog(@RequestParam(name = "userId") Long userId,
@RequestBody @Valid WorklogRequest.AddWorklogDTO request){
Worklog worklog = worklogService.addWorklog(userId, request);
public ApiResponse<WorklogResponse.AddResultDTO> addWorklog(@RequestBody @Valid WorklogRequest.AddWorklogDTO request){
Worklog worklog = worklogService.addWorklog(request);
return ApiResponse.onSuccess(WorklogConverter.toAddWorklogResultDTO(worklog));
}

Expand Down Expand Up @@ -54,7 +53,7 @@ public ApiResponse<WorklogResponse.ModifyResultDTO> modifyWorklogIsChecked(@Path

@DeleteMapping("/{worklogId}")
@Operation(summary = "업무일지 삭제 API")
public ApiResponse<WorklogResponse.AddResultDTO> deleteWorklog(@PathVariable(name = "worklogId") Long worklogId){
public ApiResponse<Long> deleteWorklog(@PathVariable(name = "worklogId") Long worklogId){
worklogService.deleteWorklog(worklogId);
return ApiResponse.onSuccess(null);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.onnoff.onnoff.domain.on.worklog.converter;

import com.onnoff.onnoff.domain.on.resolution.converter.ResolutionConverter;
import com.onnoff.onnoff.domain.on.resolution.dto.ResolutionResponse;
import com.onnoff.onnoff.domain.on.resolution.entity.Resolution;
import com.onnoff.onnoff.domain.on.worklog.dto.OnResponse;
Expand All @@ -9,7 +8,6 @@

import java.time.LocalDate;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class OnConverter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public static Worklog toAddWorklog(WorklogRequest.AddWorklogDTO request){
public static WorklogResponse.AddResultDTO toAddWorklogResultDTO(Worklog worklog){
return WorklogResponse.AddResultDTO.builder()
.worklogId(worklog.getId())
.content(worklog.getContent())
.isChecked(worklog.getIsChecked())
.createdAt(worklog.getCreatedAt())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.onnoff.onnoff.domain.on.worklog.dto;

import com.onnoff.onnoff.domain.on.resolution.dto.ResolutionRequest;
import com.onnoff.onnoff.domain.on.resolution.dto.ResolutionResponse;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.PastOrPresent;
import jakarta.validation.constraints.Size;
import lombok.Getter;

Expand All @@ -10,7 +11,7 @@
public class WorklogRequest {
@Getter
public static class AddWorklogDTO{
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
@PastOrPresent
LocalDate date;
@NotBlank
@Size(max = 30)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class WorklogResponse {
@AllArgsConstructor
public static class AddResultDTO{
Long worklogId;
String content;
Boolean isChecked;
LocalDateTime createdAt;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
import java.util.List;

public interface WorklogRepository extends JpaRepository<Worklog, Long> {
List<Worklog> findAllByUserAndDate(User user, LocalDate date);
List<Worklog> findAllByUserAndDateOrderByCreatedAt(User user, LocalDate date);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
import java.time.LocalDate;

public interface OnService {
public OnResponse.OnViewDTO getOn(Long userId, LocalDate date);
public OnResponse.OnViewDTO getOn(LocalDate date);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.onnoff.onnoff.apiPayload.code.status.ErrorStatus;
import com.onnoff.onnoff.apiPayload.exception.GeneralException;
import com.onnoff.onnoff.auth.UserContext;
import com.onnoff.onnoff.domain.on.resolution.entity.Resolution;
import com.onnoff.onnoff.domain.on.resolution.repository.ResolutionRepository;
import com.onnoff.onnoff.domain.on.worklog.converter.OnConverter;
Expand All @@ -27,17 +28,16 @@ public class OnServiceImpl implements OnService{
private final WorklogRepository worklogRepository;

@Override
public OnResponse.OnViewDTO getOn(Long userId, LocalDate date){
public OnResponse.OnViewDTO getOn(LocalDate date){
LocalDate localDate = Objects.requireNonNullElseGet(date, LocalDate::now);

User user = userRepository.findById(userId)
.orElseThrow(() -> new GeneralException(ErrorStatus.USER_NOT_FOUND));
User user = UserContext.getUser();

List<Resolution> resolutionList = resolutionRepository.findAllByUserAndDate(user, localDate);
List<Resolution> resolutionList = resolutionRepository.findAllByUserAndDateOrderByOrder(user, localDate);

List<Worklog> worklogList = worklogRepository.findAllByUserAndDate(user, localDate);
List<Worklog> worklogList = worklogRepository.findAllByUserAndDateOrderByCreatedAt(user, localDate);


return OnConverter.getOnDTO(userId, localDate, resolutionList, worklogList);
return OnConverter.getOnDTO(user.getId(), localDate, resolutionList, worklogList);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

public interface WorklogService {

Worklog addWorklog(Long userId, WorklogRequest.AddWorklogDTO request);
Worklog addWorklog(WorklogRequest.AddWorklogDTO request);

Worklog modifyContent(Long worklogId, WorklogRequest.ModifyWorklogDTO request);

Expand Down
Loading

0 comments on commit b91ed0a

Please sign in to comment.