Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[refactor]: on API 수정 #45

Merged
merged 3 commits into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -5,7 +5,6 @@
import com.onnoff.onnoff.domain.on.resolution.entity.Resolution;

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

Expand All @@ -24,11 +23,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 +39,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
@@ -1,7 +1,7 @@
package com.onnoff.onnoff.domain.on.worklog.dto;

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 +10,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
@@ -1,6 +1,5 @@
package com.onnoff.onnoff.domain.on.worklog.repository;

import com.onnoff.onnoff.domain.on.resolution.entity.Resolution;
import com.onnoff.onnoff.domain.on.worklog.entity.Worklog;
import com.onnoff.onnoff.domain.user.User;
import org.springframework.data.jpa.repository.JpaRepository;
Expand All @@ -9,5 +8,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);
OnResponse.OnViewDTO getOn(LocalDate date);
}
Loading
Loading