Skip to content

Commit

Permalink
Merge pull request #116 from humpose/feat/validation
Browse files Browse the repository at this point in the history
feat: validation
  • Loading branch information
jjh4450 authored Nov 15, 2024
2 parents 06b1cfe + 5d556f6 commit 7971ba6
Show file tree
Hide file tree
Showing 20 changed files with 84 additions and 43 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ dependencies {
implementation ("io.netty:netty-resolver-dns-native-macos:4.1.70.Final:osx-aarch_64")
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation ("org.springframework.boot:spring-boot-starter-validation")
compileOnly("org.projectlombok:lombok")
developmentOnly("org.springframework.boot:spring-boot-devtools")
runtimeOnly("com.h2database:h2")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package jeje.work.aeatbe.controller;

import jakarta.validation.Valid;
import jeje.work.aeatbe.dto.article.ArticleDTO;
import jeje.work.aeatbe.dto.article.ArticleResponseDTO;
import jeje.work.aeatbe.service.ArticleService;
Expand All @@ -26,7 +27,7 @@ public class ArticleController {
* @return 생성된 칼럼의 DTO와 상태 코드 201 (Created)
*/
@PostMapping
public ResponseEntity<ArticleDTO> createArticle(@RequestBody ArticleDTO articleDTO) {
public ResponseEntity<ArticleDTO> createArticle(@RequestBody @Valid ArticleDTO articleDTO) {
ArticleDTO createdArticle = articleService.createArticle(articleDTO);
return ResponseEntity.status(HttpStatus.CREATED).body(createdArticle);
}
Expand Down Expand Up @@ -71,7 +72,7 @@ public ResponseEntity<ArticleResponseDTO> getArticleById(@PathVariable Long id)
* @return 업데이트된 칼럼의 DTO와 상태 코드 200 (OK)
*/
@PatchMapping("/{id}")
public ResponseEntity<ArticleDTO> updateArticle(@PathVariable Long id, @RequestBody ArticleDTO articleDTO) {
public ResponseEntity<ArticleDTO> updateArticle(@PathVariable Long id, @RequestBody @Valid ArticleDTO articleDTO) {
ArticleDTO updatedArticle = articleService.updateArticle(id, articleDTO);
return ResponseEntity.ok(updatedArticle);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package jeje.work.aeatbe.controller;


import jakarta.validation.Valid;
import jeje.work.aeatbe.dto.product.ProductDTO;
import jeje.work.aeatbe.dto.product.ProductResponseDTO;
import jeje.work.aeatbe.service.ProductService;
Expand Down Expand Up @@ -79,7 +80,7 @@ public ResponseEntity<ProductResponseDTO> getProductsById(@PathVariable Long id)
*/
@PostMapping
public ResponseEntity<?> postProducts(
@RequestBody ProductDTO productDTO,
@RequestBody @Valid ProductDTO productDTO,
@RequestParam List<String> allergies,
@RequestParam List<String> freeFroms
// ,@LoginUser Long userId
Expand All @@ -100,7 +101,7 @@ public ResponseEntity<?> postProducts(
*/
@PatchMapping("/{id}")
public ResponseEntity<ProductResponseDTO> updateProducts(@PathVariable Long id,
@RequestBody ProductDTO productDTO,
@RequestBody @Valid ProductDTO productDTO,
@RequestParam List<String> allergies,
@RequestParam List<String> freeFroms
// ,@LoginUser Long userId
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package jeje.work.aeatbe.controller;

import jakarta.validation.Valid;
import jeje.work.aeatbe.annotation.LoginUser;
import jeje.work.aeatbe.dto.review.ReviewRequestDTO;
import jeje.work.aeatbe.dto.review.ReviewResponseDTO;
Expand Down Expand Up @@ -62,7 +63,7 @@ public ResponseEntity<?> getReivewsByUser(@LoginUser LoginUserInfo loginUserInfo
* @return 201 created 응답 코드
*/
@PostMapping
public ResponseEntity<?> postReviews(@RequestBody ReviewRequestDTO reviewDTO,
public ResponseEntity<?> postReviews(@RequestBody @Valid ReviewRequestDTO reviewDTO,
@LoginUser LoginUserInfo loginUserInfo) {
reviewService.createReview(reviewDTO, loginUserInfo.userId());
return ResponseEntity.status(HttpStatus.CREATED).build();
Expand All @@ -79,7 +80,7 @@ public ResponseEntity<?> postReviews(@RequestBody ReviewRequestDTO reviewDTO,
*/
@PatchMapping("/{id}")
public ResponseEntity<?> updateReviews(@PathVariable Long id,
@RequestBody ReviewRequestDTO reviewDTO,
@RequestBody @Valid ReviewRequestDTO reviewDTO,
@LoginUser LoginUserInfo loginUserInfo) {
reviewService.updateReviews(id, reviewDTO, loginUserInfo.userId());
return ResponseEntity.ok().build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package jeje.work.aeatbe.controller;

import jakarta.validation.Valid;
import jeje.work.aeatbe.annotation.LoginUser;
import jeje.work.aeatbe.dto.user.LoginUserInfo;
import jeje.work.aeatbe.dto.user.UserInfoResponseDTO;
Expand Down Expand Up @@ -36,7 +37,7 @@ public ResponseEntity<UserInfoResponseDTO> getUserInfo(@LoginUser LoginUserInfo
* @return 업데이트 된 정보
*/
@PatchMapping("/info/update")
public ResponseEntity<UserInfoResponseDTO> updateUserInfo(@RequestBody UserInfoUpdateReqeustDTO userInfoUpdateReqeustDto,
public ResponseEntity<UserInfoResponseDTO> updateUserInfo(@RequestBody @Valid UserInfoUpdateReqeustDTO userInfoUpdateReqeustDto,
@LoginUser LoginUserInfo loginUserInfo) {
userService.updateUserInfo(userInfoUpdateReqeustDto, loginUserInfo.userId());
UserInfoResponseDTO userInfoResponseDto = userService.getUserInfo(loginUserInfo.userId());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package jeje.work.aeatbe.dto.allergyCategory;

import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.Builder;

/**
Expand All @@ -12,6 +14,8 @@
@Builder
public record AllergyCategoryDTO(
Long id,
@NotNull(message = "입력값이 존재해야 합니다.")
@Size(max = 25, message = "알러지 타입이 25자를 초과할 수 없습니다.")
String allergyType
) {
}
10 changes: 10 additions & 0 deletions src/main/java/jeje/work/aeatbe/dto/article/ArticleDTO.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
package jeje.work.aeatbe.dto.article;

import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.Builder;

import java.sql.Timestamp;
import org.hibernate.validator.constraints.URL;

@Builder
public record ArticleDTO(
Long id,
@NotNull(message = "제목이 존재해야 합니다.")
@Size(max = 100, message = "제목이 100자를 초과할 수 없습니다.")
String title,
String subTitle,
Timestamp date,
@NotNull(message = "저자가 존재해야 합니다.")
@Size(max = 50, message = "저자의 이름이 50자를 초과할 수 없습니다.")
String author,
@Size(max = 100, message = "태그 내용이 100자를 초과할 수 없습니다.")
String tags,
@NotNull(message = "내용이 존재해야 합니다.")
String content,
int likes,
@URL(message = "유효하지 않는 URL 형식입니다.")
String thumbnailUrl

) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
package jeje.work.aeatbe.dto.article;

import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.Builder;

import java.sql.Timestamp;
import java.util.List;
import org.hibernate.validator.constraints.URL;

/**
* 특정 칼럼 반환할 때 사용하는 형식입니다.
*/
@Builder
public record ArticleResponseDTO(
Long id,
@NotNull(message = "제목이 존재해야 합니다.")
@Size(max = 100, message = "제목이 100자를 초과할 수 없습니다.")
String title,
@URL(message = "유효하지 않는 URL 형식입니다.")
String imgurl,
Timestamp createdAt,
String auth,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package jeje.work.aeatbe.dto.freeFromCategory;

import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.Builder;

@Builder
public record FreeFromCategoryDTO(
Long id,
@NotNull(message = "입력값이 존재해야 합니다.")
@Size(max = 25, message = "프리프롬 타입은 25자를 초과할 수 없습니다.")
String freeFromType
) {
}
12 changes: 12 additions & 0 deletions src/main/java/jeje/work/aeatbe/dto/product/ProductDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.Builder;

import java.util.List;
import org.hibernate.validator.constraints.URL;

@Builder
@JsonIgnoreProperties(ignoreUnknown = true)
Expand All @@ -16,17 +19,26 @@ public record ProductDTO(
@JsonProperty("allergy")
@JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY) List<ProductAllergyDTO> allergy,
@JsonProperty("nutrient") String nutritionalInfo, // 영양성분
@URL(message = "유효하지 않는 URL 형식입니다.")
@JsonProperty("imgurl1") String productImageUrl, // 상품 이미지
@URL(message = "유효하지 않는 URL 형식입니다.")
@JsonProperty("imgurl2") String metaImageUrl, // 상품 상세 설명 이미지
@Size(max = 200, message = "제품 유형명이 200자를 초과할 수 없습니다.")
@JsonProperty("prdkind") String typeName, // 제품 종류(유형명)
@Size(max = 200, message = "제조사 이름이 200자를 초과할 수 없습니다.")
@JsonProperty("manufacture") String manufacturer, // 제조사
String seller, // todo: 판매 링크(추후에 사용 될 예정)
@Size(max = 100, message = "용량이 100을 초과할 수 없습니다.")
@JsonProperty("capacity") String capacity, // 용량
@NotNull(message = "상품명이 존재해야 합니다.")
@Size(max = 50, message = "상품명은 50자를 초과할 수 없습니다.")
@JsonProperty("prdlstNm") String productName, // 상품명
@Size(min = 0, message = "원재료 이름이 존재해야 합니다.")
@JsonProperty("rawmtrl") String ingredients, // 원재료
Long price, // 가격(추후에 사용 될 예정)
@JsonProperty("barcode") String productBarcode, // 상품 바코드(현재 미사용중)
String tag,
@Size(max = 20, message = "상품 판매처 이름은 20자 이상이어야 합니다.")
String mallName // 상품 판매처 이름
) {
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package jeje.work.aeatbe.dto.product;

import jakarta.validation.constraints.Size;
import lombok.Builder;
import org.hibernate.validator.constraints.URL;

@Builder
public record ProductResponseDTO(
Long id,
String name,
@Size(min = 0, message = "가격이 0원 이상이어야 합니다.")
Long price,
@URL(message = "유효하지 않는 URL 형식입니다.")
String imgUrl,
@Size(min = 0, max = 5, message = "평점이 0점이상, 5점이하여야 합니다.")
Double rating,
@URL(message = "유효하지 않는 URL 형식입니다.")
String ProductUrl,
String description,
String[] freeFrom,
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/jeje/work/aeatbe/dto/review/ReviewDTO.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package jeje.work.aeatbe.dto.review;

import jakarta.annotation.Nullable;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.time.LocalDateTime;
import lombok.Builder;

@Builder
public record ReviewDTO(
Long id,
@Size(min = 0, max = 5, message = "평점이 0점이상, 5점이하여야 합니다.")
@NotNull(message = "평점을 입력해주셔야 합니다.")
Long rate,
String content,
@NotNull(message = "입력값이 존재해야 합니다.")
Long userId,
@NotNull(message = "입력값이 존재해야 합니다.")
Long productId,
LocalDateTime date,
@Nullable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package jeje.work.aeatbe.dto.review;

import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.Builder;

@Builder
public record ReviewRequestDTO(
@Size(min = 0, max = 5, message = "평점이 0점이상, 5점이하여야 합니다.")
@NotNull(message = "평점을 입력해주셔야 합니다.")
Long rate,
String content,
@NotNull(message = "입력값이 존재해야 합니다.")
Long productId
) {
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package jeje.work.aeatbe.dto.review;

import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.time.LocalDateTime;
import jeje.work.aeatbe.dto.user.UserInfoResponseDTO;
import lombok.Builder;
import org.hibernate.validator.constraints.URL;

@Builder
public record ReviewResponseDTO(
Long id,
@Size(min = 0, max = 5, message = "평점이 0점이상, 5점이하여야 합니다.")
@NotNull(message = "평점을 입력해주셔야 합니다.")
Long rate,
String content,
UserInfoResponseDTO user,
@NotNull(message = "입력값이 존재해야 합니다.")
Long productId,
@URL(message = "유효하지 않는 URL 형식입니다.")
String productImgUrl,
String productName,
LocalDateTime date
Expand Down
12 changes: 0 additions & 12 deletions src/main/java/jeje/work/aeatbe/dto/user/UserAllergyDTO.java

This file was deleted.

13 changes: 0 additions & 13 deletions src/main/java/jeje/work/aeatbe/dto/user/UserDTO.java

This file was deleted.

11 changes: 0 additions & 11 deletions src/main/java/jeje/work/aeatbe/dto/user/UserFreeFromDTO.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package jeje.work.aeatbe.dto.user;

import jakarta.validation.constraints.Size;
import lombok.Builder;

import java.util.List;
import org.hibernate.validator.constraints.URL;

@Builder
public record UserInfoResponseDTO(
Long id,
@Size(max = 15, message = "유저이름이 15자를 초과할 수 없습니다.")
String userName,
@URL(message = "유효하지 않는 URL 형식입니다.")
String userImageUrl,
List<String> allergies,
List<String> freefrom
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package jeje.work.aeatbe.dto.user;

import jakarta.validation.constraints.Size;
import lombok.Builder;

import java.util.List;
import org.hibernate.validator.constraints.URL;

@Builder
public record UserInfoUpdateReqeustDTO(
@Size(max = 15, message = "유저 이름이 15자를 초과할 수 없습니다.")
String userName,
@URL(message = "유효하지 않는 URL 형식입니다.")
String userImageUrl,
List<String> allergies,
List<String> freefrom
Expand Down
Loading

0 comments on commit 7971ba6

Please sign in to comment.