Skip to content

Commit

Permalink
Merge pull request #173 from PlanIt-Project/BE_fix#172
Browse files Browse the repository at this point in the history
Be fix#172
  • Loading branch information
JuhyunPark1831 authored Apr 8, 2024
2 parents c717f23 + 0758457 commit 0893fd9
Show file tree
Hide file tree
Showing 25 changed files with 174 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,18 @@ public ApiResponse(int code, String message, T data) {

public static <T> ApiResponse<T> ok(T data) {return new ApiResponse<>(HttpStatus.OK.value(), HttpStatus.OK.name(), data);}

public static ApiResponse<?> error(ErrorCode errorCode) {return new ApiResponse<>(errorCode.getStatus(), errorCode.getMessage(),null);}
public static ApiResponse<?> error(ErrorCode errorCode) {
return new ApiResponse<>(
errorCode.getStatus(),
errorCode.getMessage(),
null);
}

public static ApiResponse<?> error(int code, String message) {
return new ApiResponse<>(
code,
message,
null
);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package com.sideProject.PlanIT.common.response;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.UnexpectedTypeException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;
import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver;

@RestControllerAdvice
@Slf4j
Expand All @@ -17,8 +22,24 @@ protected ResponseEntity<ApiResponse<?>> handleCustomException(CustomException e
}

@ExceptionHandler(value = HttpMessageNotReadableException.class)
protected ResponseEntity<ApiResponse<?>> handleHttpMessageException(HttpMessageNotReadableException ex) {
protected ResponseEntity<ApiResponse<?>> handleHttpException(HttpMessageNotReadableException ex) {
return ResponseEntity.status(400)
.body(ApiResponse.error(ErrorCode.INVALID_INPUT));
}

@ExceptionHandler(value = BindException.class)
protected ResponseEntity<ApiResponse<?>> handleHttpException(BindException ex) {
return ResponseEntity.status(400)
.body(
ApiResponse.error(
400,
ex.getBindingResult().getAllErrors().get(0).getDefaultMessage()));
}

@ExceptionHandler(NoHandlerFoundException.class)
protected ResponseEntity<ApiResponse<?>> handleNoHandlerFoundException(NoHandlerFoundException e,
HttpServletRequest request) {
return ResponseEntity.status(404)
.body(ApiResponse.error(404, "잘못된 페이지 입니다."));
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
package com.sideProject.PlanIT.common.security;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sideProject.PlanIT.common.response.ApiResponse;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Slf4j
public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint {
private ObjectMapper mapper = new ObjectMapper();

@Override
public void commence(HttpServletRequest request, HttpServletResponse response,
AuthenticationException authException) throws IOException {
String accept = request.getHeader("Accept");
log.info(accept);

response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
ApiResponse<?> error = ApiResponse.error(401," Authentication information not found.");

String result = mapper.writeValueAsString(error);

response.setStatus(401);
response.getWriter().write(result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public class BannerAdminController {
@PostMapping
public ApiResponse<String> createBanner(@ModelAttribute BannerRequestDto request
) {
log.info(request.getTitle());
return ApiResponse.ok(bannerService.createBanner(request));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.sideProject.PlanIT.domain.post.dto.request.NoticeRequestDto;
import com.sideProject.PlanIT.domain.post.dto.response.NoticeResponseDto;
import com.sideProject.PlanIT.domain.post.service.NoticeService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand All @@ -17,7 +18,7 @@ public class NoticeAdminController {
private final NoticeService noticeService;

@PostMapping
public ApiResponse<String> createNotice(@ModelAttribute NoticeRequestDto request) {
public ApiResponse<String> createNotice(@Valid @ModelAttribute NoticeRequestDto request) {
return ApiResponse.ok(noticeService.createNotice(request));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.sideProject.PlanIT.domain.post.dto.request;

import jakarta.validation.constraints.NotNull;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -10,8 +11,11 @@
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class NoticeRequestDto {
@NotNull(message = "제목이 업습니다.")
private String title;
@NotNull(message = "시작 날짜가 업습니다.")
private LocalDate startAt;
@NotNull(message = "종료 날짜가 업습니다.")
private LocalDate endAt;
private MultipartFile attachment;
private MultipartFile image;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.sideProject.PlanIT.domain.product.dto.request.ProductRequestDto;
import com.sideProject.PlanIT.domain.product.entity.Product;
import com.sideProject.PlanIT.domain.product.service.ProductService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

Expand All @@ -14,7 +15,7 @@ public class ProductAdminController {
private final ProductService productService;

@PostMapping
public ApiResponse<Product> createProduct(@RequestBody ProductRequestDto request) {
public ApiResponse<Product> createProduct(@Valid @RequestBody ProductRequestDto request) {
return ApiResponse.ok(productService.createProduct(request));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.sideProject.PlanIT.domain.product.entity.enums.ProductSellingType;
import com.sideProject.PlanIT.domain.product.entity.enums.ProductType;
import jakarta.validation.constraints.NotNull;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -12,11 +13,15 @@
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class ProductRequestDto {
@NotNull(message = "상품명이 없습니다.")
private String name;
private Period period;
private int number;
@NotNull(message = "금액이 없습니다.")
private int price;
@NotNull(message = "상품 타입이 없습니다.")
private ProductType type;
@NotNull(message = "판매 상태가 없습니다.")
private ProductSellingType sellingType;

@Builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.sideProject.PlanIT.domain.program.entity.enums.ProgramSearchStatus;
import com.sideProject.PlanIT.domain.program.entity.enums.RegistrationSearchStatus;
import com.sideProject.PlanIT.domain.program.service.ProgramService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
Expand Down Expand Up @@ -82,7 +83,7 @@ public ApiResponse<String> modify(
}

@PostMapping("/approve/{id}")
public ApiResponse<Long> approve(@PathVariable("id") Long id, @RequestBody ApproveRequestDto request) {
public ApiResponse<Long> approve(@PathVariable("id") Long id, @Valid @RequestBody ApproveRequestDto request) {
LocalDateTime now = LocalDateTime.now();
return ApiResponse.ok(
programService.approve(id, request.getTrainer(), now)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.sideProject.PlanIT.domain.program.entity.enums.ProgramSearchStatus;
import com.sideProject.PlanIT.domain.program.entity.enums.RegistrationSearchStatus;
import com.sideProject.PlanIT.domain.program.service.ProgramService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
Expand All @@ -27,7 +28,7 @@ public class ProgramController {
private final ProgramService programService;

@PostMapping("/registration")
public ApiResponse<?> registration(@RequestBody RegistrationRequestDto request, Principal principal){
public ApiResponse<?> registration(@Valid @RequestBody RegistrationRequestDto request, Principal principal){
LocalDateTime now = LocalDateTime.now();
Long id = Long.parseLong(principal.getName());
return ApiResponse.ok(programService.registration(request, id, now));
Expand All @@ -48,8 +49,7 @@ public ApiResponse<Page<ProgramResponseDto>> find(
public ApiResponse<ProgramResponseDto> findById(
@PathVariable("id") Long id,
Principal principal) {
//todo : spring security 개발 후 토큰에서 userID를 전달해 줘야함.
Long userId = Long.parseLong(principal.getName());
long userId = Long.parseLong(principal.getName());
return ApiResponse.ok(
programService.findByProgramId(id, userId)
);
Expand All @@ -60,7 +60,7 @@ public ApiResponse<Page<FindRegistrationResponseDto>> findRegistration(
@RequestParam(value = "option", required = false, defaultValue = "ALL") RegistrationSearchStatus option,
@PageableDefault(size = 10, sort = "id", direction = Sort.Direction.DESC) Pageable pageable,
Principal principal) {
Long id = Long.parseLong(principal.getName());
long id = Long.parseLong(principal.getName());

return ApiResponse.ok(
programService.findRegistrationsByUser(id,option, pageable)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package com.sideProject.PlanIT.domain.program.dto.request;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import static io.lettuce.core.pubsub.PubSubOutput.Type.message;

@Getter
@NoArgsConstructor
public class ApproveRequestDto {
@NotNull(message="트레이너 아이디가 없습니다.")
Long trainer;

@Builder
public ApproveRequestDto(Long trainer) {
this.trainer = trainer;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.sideProject.PlanIT.domain.program.dto.request;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.Builder;
import lombok.Getter;

@Getter
public class ProgramModifyRequestDto {

@NotNull
String startTime;
String endTime;
Long memberId;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package com.sideProject.PlanIT.domain.program.dto.request;

import jakarta.validation.constraints.NotNull;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;

import java.time.LocalDate;

@Getter
@NoArgsConstructor
public class RegistrationRequestDto {
@NotNull(message = "상품 아이디가 없습니다")
Long productId;
Long trainerId;
@NotNull(message = "등록 날짜가 없습니다")
@DateTimeFormat(pattern = "yyyy-MM-dd")
LocalDate registrationAt;
Long trainerId;

@Builder
public RegistrationRequestDto(Long productId, Long trainerId, LocalDate registrationAt) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.sideProject.PlanIT.domain.program.dto.request;
import com.sideProject.PlanIT.domain.product.entity.Product;
import com.sideProject.PlanIT.domain.user.entity.Member;
import com.sideProject.PlanIT.domain.program.entity.enums.RegistrationStatus;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.time.LocalDateTime;

@Getter
@AllArgsConstructor
public class programRegistrationRequestDto {

private LocalDateTime registrationAt;
private LocalDateTime paymentAt;
private LocalDateTime refundAt;
private RegistrationStatus status;
private int discount;
private int totalPrice;
private Member member;
private Product product;

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.sideProject.PlanIT.domain.product.dto.response.ProductResponseDto;
import com.sideProject.PlanIT.domain.program.entity.Registration;
import com.sideProject.PlanIT.domain.program.entity.enums.RegistrationStatus;
import com.sideProject.PlanIT.domain.user.dto.member.response.EmployeeSemiResponseDto;
import com.sideProject.PlanIT.domain.user.dto.member.response.MemberSemiResponseDto;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -19,10 +20,10 @@ public class FindRegistrationResponseDto {
private int totalPrice;
private ProductResponseDto product;
private MemberSemiResponseDto member;
private Long trainerId;
private EmployeeSemiResponseDto trainer;

@Builder
public FindRegistrationResponseDto(Long id, String registrationAt, String refundAt, RegistrationStatus status, int discount, int totalPrice, ProductResponseDto product, MemberSemiResponseDto member, Long trainerId) {
public FindRegistrationResponseDto(Long id, String registrationAt, String refundAt, RegistrationStatus status, int discount, int totalPrice, ProductResponseDto product, MemberSemiResponseDto member, EmployeeSemiResponseDto trainer) {
this.id = id;
this.registrationAt = registrationAt;
this.refundAt = refundAt;
Expand All @@ -31,7 +32,11 @@ public FindRegistrationResponseDto(Long id, String registrationAt, String refund
this.totalPrice = totalPrice;
this.product = product;
this.member = member;
this.trainerId = trainerId;
this.trainer = trainer;
}

public void setTrainer(EmployeeSemiResponseDto trainer) {
this.trainer = trainer;
}

public static FindRegistrationResponseDto of(Registration registration){
Expand All @@ -50,7 +55,7 @@ public static FindRegistrationResponseDto of(Registration registration){
.totalPrice(registration.getTotalPrice())
.product(ProductResponseDto.of(registration.getProduct()))
.member(MemberSemiResponseDto.of(registration.getMember()))
.trainerId(registration.getTrainerId())
.trainer(null)
.build();
}
}
Loading

0 comments on commit 0893fd9

Please sign in to comment.