From 2bafaff527ec48c2e762b4c614d424755fbf4f1d Mon Sep 17 00:00:00 2001 From: canyos <4581974@naver.com> Date: Mon, 22 Jul 2024 11:42:41 +0900 Subject: [PATCH 01/14] =?UTF-8?q?feat:=204=EC=A3=BC=EC=B0=A8=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=9D=B4=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 7 ++ src/main/java/gift/Application.java | 4 + src/main/java/gift/Util/JWTUtil.java | 54 ++++++++ .../gift/controller/CategoryController.java | 46 +++++++ .../gift/controller/OptionController.java | 46 +++++++ .../controller/ProductAdminController.java | 47 +++++++ .../gift/controller/ProductController.java | 66 ++++++++++ .../java/gift/controller/UserController.java | 54 ++++++++ .../gift/controller/WishListController.java | 45 +++++++ .../java/gift/dto/category/CategoryDTO.java | 4 + .../gift/dto/option/OptionQuantityDTO.java | 4 + .../java/gift/dto/option/SaveOptionDTO.java | 4 + .../java/gift/dto/option/UpdateOptionDTO.java | 5 + .../gift/dto/product/ModifyProductDTO.java | 4 + .../dto/product/ProductWithOptionDTO.java | 5 + .../java/gift/dto/product/SaveProductDTO.java | 5 + .../java/gift/dto/product/ShowProductDTO.java | 5 + src/main/java/gift/dto/user/LoginDTO.java | 5 + src/main/java/gift/dto/user/SignUpDTO.java | 4 + src/main/java/gift/dto/user/Token.java | 5 + src/main/java/gift/entity/Category.java | 55 +++++++++ src/main/java/gift/entity/Option.java | 65 ++++++++++ src/main/java/gift/entity/Product.java | 100 +++++++++++++++ src/main/java/gift/entity/User.java | 48 ++++++++ src/main/java/gift/entity/WishList.java | 41 +++++++ .../gift/exception/MyExceptionHandler.java | 66 ++++++++++ .../exception/BadRequestException.java | 7 ++ .../exception/NotFoundException.java | 7 ++ .../exception/ServerInternalException.java | 7 ++ .../exception/exception/UnAuthException.java | 7 ++ .../gift/repository/CategoryRepository.java | 22 ++++ .../gift/repository/OptionRepository.java | 22 ++++ .../gift/repository/ProductRepository.java | 21 ++++ .../java/gift/repository/UserRepository.java | 23 ++++ .../gift/repository/WishListRepository.java | 26 ++++ .../java/gift/service/CategoryService.java | 46 +++++++ src/main/java/gift/service/OptionService.java | 62 ++++++++++ .../java/gift/service/ProductService.java | 115 ++++++++++++++++++ src/main/java/gift/service/UserService.java | 43 +++++++ .../java/gift/service/WishListService.java | 70 +++++++++++ src/main/resources/application.properties | 11 ++ src/main/resources/application.yml | 10 ++ src/main/resources/db/data.sql | 6 + src/main/resources/db/schema.sql | 13 ++ src/main/resources/templates/admin/add.html | 65 ++++++++++ .../resources/templates/admin/delete.html | 59 +++++++++ .../resources/templates/admin/modify.html | 63 ++++++++++ .../resources/templates/admin/products.html | 32 +++++ src/main/resources/templates/user/signin.html | 51 ++++++++ src/main/resources/templates/user/signup.html | 51 ++++++++ 50 files changed, 1633 insertions(+) create mode 100644 src/main/java/gift/Util/JWTUtil.java create mode 100644 src/main/java/gift/controller/CategoryController.java create mode 100644 src/main/java/gift/controller/OptionController.java create mode 100644 src/main/java/gift/controller/ProductAdminController.java create mode 100644 src/main/java/gift/controller/ProductController.java create mode 100644 src/main/java/gift/controller/UserController.java create mode 100644 src/main/java/gift/controller/WishListController.java create mode 100644 src/main/java/gift/dto/category/CategoryDTO.java create mode 100644 src/main/java/gift/dto/option/OptionQuantityDTO.java create mode 100644 src/main/java/gift/dto/option/SaveOptionDTO.java create mode 100644 src/main/java/gift/dto/option/UpdateOptionDTO.java create mode 100644 src/main/java/gift/dto/product/ModifyProductDTO.java create mode 100644 src/main/java/gift/dto/product/ProductWithOptionDTO.java create mode 100644 src/main/java/gift/dto/product/SaveProductDTO.java create mode 100644 src/main/java/gift/dto/product/ShowProductDTO.java create mode 100644 src/main/java/gift/dto/user/LoginDTO.java create mode 100644 src/main/java/gift/dto/user/SignUpDTO.java create mode 100644 src/main/java/gift/dto/user/Token.java create mode 100644 src/main/java/gift/entity/Category.java create mode 100644 src/main/java/gift/entity/Option.java create mode 100644 src/main/java/gift/entity/Product.java create mode 100644 src/main/java/gift/entity/User.java create mode 100644 src/main/java/gift/entity/WishList.java create mode 100644 src/main/java/gift/exception/MyExceptionHandler.java create mode 100644 src/main/java/gift/exception/exception/BadRequestException.java create mode 100644 src/main/java/gift/exception/exception/NotFoundException.java create mode 100644 src/main/java/gift/exception/exception/ServerInternalException.java create mode 100644 src/main/java/gift/exception/exception/UnAuthException.java create mode 100644 src/main/java/gift/repository/CategoryRepository.java create mode 100644 src/main/java/gift/repository/OptionRepository.java create mode 100644 src/main/java/gift/repository/ProductRepository.java create mode 100644 src/main/java/gift/repository/UserRepository.java create mode 100644 src/main/java/gift/repository/WishListRepository.java create mode 100644 src/main/java/gift/service/CategoryService.java create mode 100644 src/main/java/gift/service/OptionService.java create mode 100644 src/main/java/gift/service/ProductService.java create mode 100644 src/main/java/gift/service/UserService.java create mode 100644 src/main/java/gift/service/WishListService.java create mode 100644 src/main/resources/application.yml create mode 100644 src/main/resources/db/data.sql create mode 100644 src/main/resources/db/schema.sql create mode 100644 src/main/resources/templates/admin/add.html create mode 100644 src/main/resources/templates/admin/delete.html create mode 100644 src/main/resources/templates/admin/modify.html create mode 100644 src/main/resources/templates/admin/products.html create mode 100644 src/main/resources/templates/user/signin.html create mode 100644 src/main/resources/templates/user/signup.html diff --git a/build.gradle b/build.gradle index df7db9334..2e506ad67 100644 --- a/build.gradle +++ b/build.gradle @@ -18,9 +18,16 @@ repositories { } dependencies { + implementation 'io.jsonwebtoken:jjwt-api:0.12.3' + implementation 'io.jsonwebtoken:jjwt-impl:0.12.3' + implementation 'io.jsonwebtoken:jjwt-jackson:0.12.3' + implementation 'org.springframework.security:spring-security-crypto:5.7.1' + implementation 'org.thymeleaf:thymeleaf:3.1.2.RELEASE' + implementation 'org.springframework.boot:spring-boot-starter-validation:2.7.0' implementation 'org.springframework.boot:spring-boot-starter-jdbc' implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' implementation 'org.springframework.boot:spring-boot-starter-web' + implementation 'org.springframework.boot:spring-boot-starter-data-jpa' runtimeOnly 'com.h2database:h2' testImplementation 'org.springframework.boot:spring-boot-starter-test' testRuntimeOnly 'org.junit.platform:junit-platform-launcher' diff --git a/src/main/java/gift/Application.java b/src/main/java/gift/Application.java index 61603cca0..ae7b59e50 100644 --- a/src/main/java/gift/Application.java +++ b/src/main/java/gift/Application.java @@ -1,7 +1,11 @@ package gift; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.jdbc.core.JdbcTemplate; + @SpringBootApplication public class Application { diff --git a/src/main/java/gift/Util/JWTUtil.java b/src/main/java/gift/Util/JWTUtil.java new file mode 100644 index 000000000..d97525529 --- /dev/null +++ b/src/main/java/gift/Util/JWTUtil.java @@ -0,0 +1,54 @@ +package gift.Util; + +import gift.entity.User; + +import io.jsonwebtoken.*; +import io.jsonwebtoken.security.Keys; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import javax.crypto.SecretKey; +import java.util.Date; + + +@Component +public class JWTUtil { + + private final SecretKey key; + private final int expirationMs; + + public JWTUtil(@Value("${jwt.secretKey}") String jwtSecret, + @Value("${jwt.expiredMs}") int jwtExpirationMs) { + this.key = Keys.hmacShaKeyFor(jwtSecret.getBytes()); + this.expirationMs = jwtExpirationMs; + } + + public String generateToken(User user) { + Date date = new Date(); + Date expire = new Date(date.getTime() + expirationMs); + return Jwts.builder() + .subject(Integer.toString(user.getId())) + .issuedAt(date) + .expiration(expire) + .signWith(key) + .compact(); + } + + public boolean validateToken(String token) throws JwtException { + try { + Jwts.parser().verifyWith(key).build().parseSignedClaims(token); + } catch (JwtException e) { + return false; + } + return true; + } + + public Integer getUserIdFromToken(String token) { + return Integer.parseInt(Jwts.parser() + .verifyWith(key) + .build() + .parseSignedClaims(token) + .getPayload() + .getSubject()); + } +} diff --git a/src/main/java/gift/controller/CategoryController.java b/src/main/java/gift/controller/CategoryController.java new file mode 100644 index 000000000..713158ef5 --- /dev/null +++ b/src/main/java/gift/controller/CategoryController.java @@ -0,0 +1,46 @@ +package gift.controller; + + +import gift.dto.category.CategoryDTO; +import gift.entity.Category; +import gift.service.CategoryService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.*; + +@Controller +public class CategoryController { + @Autowired + CategoryService categoryService; + + @GetMapping("/api/category") + @ResponseBody + public Page getCategory(@RequestParam(value = "page", defaultValue = "0") int pageNum) { + Pageable pageable = PageRequest.of(pageNum, 2, Sort.by(Sort.Direction.ASC, "id")); + return categoryService.getCategory(pageable); + } + + @PostMapping("/api/category") + @ResponseStatus(HttpStatus.CREATED) + public void addCategory(@RequestBody Category category) { + categoryService.save(category); + } + + @PutMapping("/api/category") + @ResponseStatus(HttpStatus.OK) + public void updateCategory(@RequestBody CategoryDTO categoryDTO) { + categoryService.update(categoryDTO); + } + + @DeleteMapping("/api/category/{id}") + public String deleteCategory(@PathVariable("id") int id) { + categoryService.delete(id); + return "redirect:/api/category"; + } + +} diff --git a/src/main/java/gift/controller/OptionController.java b/src/main/java/gift/controller/OptionController.java new file mode 100644 index 000000000..658a62fd7 --- /dev/null +++ b/src/main/java/gift/controller/OptionController.java @@ -0,0 +1,46 @@ +package gift.controller; + +import gift.dto.option.OptionQuantityDTO; +import gift.dto.option.SaveOptionDTO; +import gift.dto.option.UpdateOptionDTO; +import gift.service.OptionService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.*; + +@Controller +public class OptionController { + @Autowired + OptionService optionService; + + @PostMapping("/api/option") + @ResponseStatus(HttpStatus.CREATED) + public void addOption(@RequestBody SaveOptionDTO saveOptionDTO) { + optionService.add(saveOptionDTO); + } + + @DeleteMapping("/api/option/{id}") + @ResponseStatus(HttpStatus.CREATED) + public void deleteOption(@PathVariable int id) { + optionService.delete(id); + } + + @PutMapping("/api/option") + @ResponseStatus(HttpStatus.OK) + public void updateOption(@RequestBody UpdateOptionDTO updateOptionDTO) { + optionService.update(updateOptionDTO); + } + + @PostMapping("/api/option/refill") + @ResponseStatus(HttpStatus.OK) + public void refill(@RequestBody OptionQuantityDTO optionQuantityDTO) { + optionService.refill(optionQuantityDTO); + } + + @PostMapping("/api/option/order") + @ResponseStatus(HttpStatus.OK) + public void order(@RequestBody OptionQuantityDTO optionQuantityDTO) { + optionService.order(optionQuantityDTO); + } +} diff --git a/src/main/java/gift/controller/ProductAdminController.java b/src/main/java/gift/controller/ProductAdminController.java new file mode 100644 index 000000000..f2a00984c --- /dev/null +++ b/src/main/java/gift/controller/ProductAdminController.java @@ -0,0 +1,47 @@ +package gift.controller; + + +import gift.dto.product.ProductWithOptionDTO; +import gift.service.ProductService; +import jakarta.servlet.http.HttpServletRequest; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.servlet.ModelAndView; + + +@Controller +public class ProductAdminController { + @Autowired + private ProductService productService; + + @GetMapping("/admin/products") + public String adminProducts(HttpServletRequest request, @RequestParam(value = "page", defaultValue = "0") int pageNum) { + Pageable pageable = PageRequest.of(pageNum, 2, Sort.by(Sort.Direction.ASC, "id")); + Page products = productService.getAllProductsWithOption(pageable); + request.setAttribute("products", products); + return "admin/products"; + } + + @GetMapping("/admin/add") + public String adminProductsAdd() { + return "admin/add"; + } + + @GetMapping("/admin/modify") + public String adminProductsModify() { + return "admin/modify"; + } + + @GetMapping("/admin/delete") + public String adminProductsDelete() { + return "admin/delete"; + } +} diff --git a/src/main/java/gift/controller/ProductController.java b/src/main/java/gift/controller/ProductController.java new file mode 100644 index 000000000..92b3a479a --- /dev/null +++ b/src/main/java/gift/controller/ProductController.java @@ -0,0 +1,66 @@ +package gift.controller; + + +import gift.dto.product.ModifyProductDTO; +import gift.dto.product.SaveProductDTO; +import gift.dto.product.ShowProductDTO; +import gift.entity.Product; +import gift.service.ProductService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; +import org.springframework.data.web.PageableDefault; + +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.*; + + +@Controller +public class ProductController { + @Autowired + private ProductService productService; + + + @ResponseStatus(HttpStatus.OK) + @ResponseBody + @GetMapping("/api/products") + public Page getProducts(@RequestParam(value = "page", defaultValue = "0") int pageNum) { + Pageable pageable = PageRequest.of(pageNum, 2, Sort.by(Sort.Direction.ASC, "id")); + return productService.getAllProducts(pageable); + + } + + @PostMapping("/api/products") + @ResponseStatus(HttpStatus.CREATED) + public String addProduct(@RequestBody SaveProductDTO product) { + productService.saveProduct(product); + return "redirect:api/products"; + } + + @ResponseStatus(HttpStatus.OK) + @DeleteMapping("/api/products/{Id}") + public String deleteProduct(@PathVariable int Id) { + productService.deleteProduct(Id); + return "redirect:api/products"; + } + + @ResponseStatus(HttpStatus.OK) + @PutMapping("/api/products") + public String modifyProduct(@RequestBody ModifyProductDTO product) { + productService.modifyProduct(product); + return "redirect:api/products"; + } + + + @ResponseStatus(HttpStatus.OK) + @ResponseBody + @GetMapping("/api/product/{id}") + public String getProduct(@PathVariable int Id) { + String product = productService.getProductByID(Id); + return product; + } +} + diff --git a/src/main/java/gift/controller/UserController.java b/src/main/java/gift/controller/UserController.java new file mode 100644 index 000000000..3abe615d7 --- /dev/null +++ b/src/main/java/gift/controller/UserController.java @@ -0,0 +1,54 @@ +package gift.controller; + + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +import gift.dto.user.LoginDTO; +import gift.dto.user.SignUpDTO; +import gift.dto.user.Token; + +import gift.service.UserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.*; + +@Controller +public class UserController { + @Autowired + UserService userService; + + @Autowired + ObjectMapper objectMapper; + + @ResponseStatus(HttpStatus.OK) + @GetMapping("/signup") + public String signUp() { + return "user/signup"; + } + + @ResponseStatus(HttpStatus.CREATED) + @PostMapping("/api/signup") + public String SignUp(@RequestBody SignUpDTO signUpDTO) { + userService.signUp(signUpDTO); + return "redirect:/signin"; + } + + @ResponseStatus(HttpStatus.OK) + @GetMapping("/signin") + public String signIn() { + return "user/signin"; + } + + + @ResponseStatus(HttpStatus.CREATED) + @PostMapping("/api/signin") + @ResponseBody + public String signIn(@RequestBody LoginDTO loginDTO) throws JsonProcessingException { + Token token = userService.signIn(loginDTO); + return objectMapper.writeValueAsString(token); + } + + +} diff --git a/src/main/java/gift/controller/WishListController.java b/src/main/java/gift/controller/WishListController.java new file mode 100644 index 000000000..c229f6cf5 --- /dev/null +++ b/src/main/java/gift/controller/WishListController.java @@ -0,0 +1,45 @@ +package gift.controller; + +import com.fasterxml.jackson.core.JsonProcessingException; + +import gift.dto.product.ShowProductDTO; +import gift.service.WishListService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; + +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.*; + + +@Controller +public class WishListController { + @Autowired + WishListService wishListService; + + @ResponseStatus(HttpStatus.CREATED) + @PostMapping("/api/wishlist/{product_id}") + public String addWishList(@RequestHeader("Authorization") String token, @PathVariable int product_id) { + wishListService.add(token, product_id); + return "redirect:/wishlist"; + } + + @ResponseStatus(HttpStatus.OK) + @GetMapping("/api/wishlist") + @ResponseBody + public Page getWishList(@RequestHeader("Authorization") String token, @RequestParam(value = "page", defaultValue = "0") int pageNum) throws JsonProcessingException { + Pageable pageable = PageRequest.of(pageNum, 2, Sort.by(Sort.Direction.ASC, "id")); + return wishListService.getWishList(token, pageable); + } + + @ResponseStatus(HttpStatus.OK) + @DeleteMapping("/api/wishlist/{product_id}") + public String deleteWishList(@RequestHeader("Authorization") String token, @PathVariable int product_id) { + wishListService.deleteWishList(token, product_id); + return "redirect:/wishlist"; + } + +} diff --git a/src/main/java/gift/dto/category/CategoryDTO.java b/src/main/java/gift/dto/category/CategoryDTO.java new file mode 100644 index 000000000..ee829dc7a --- /dev/null +++ b/src/main/java/gift/dto/category/CategoryDTO.java @@ -0,0 +1,4 @@ +package gift.dto.category; + +public record CategoryDTO(int id, String name) { +} diff --git a/src/main/java/gift/dto/option/OptionQuantityDTO.java b/src/main/java/gift/dto/option/OptionQuantityDTO.java new file mode 100644 index 000000000..de5ad9572 --- /dev/null +++ b/src/main/java/gift/dto/option/OptionQuantityDTO.java @@ -0,0 +1,4 @@ +package gift.dto.option; + +public record OptionQuantityDTO(int id, int quantity) { +} diff --git a/src/main/java/gift/dto/option/SaveOptionDTO.java b/src/main/java/gift/dto/option/SaveOptionDTO.java new file mode 100644 index 000000000..441e1762e --- /dev/null +++ b/src/main/java/gift/dto/option/SaveOptionDTO.java @@ -0,0 +1,4 @@ +package gift.dto.option; + +public record SaveOptionDTO(int product_id, String option) { +} diff --git a/src/main/java/gift/dto/option/UpdateOptionDTO.java b/src/main/java/gift/dto/option/UpdateOptionDTO.java new file mode 100644 index 000000000..39e2a0659 --- /dev/null +++ b/src/main/java/gift/dto/option/UpdateOptionDTO.java @@ -0,0 +1,5 @@ +package gift.dto.option; + +public record UpdateOptionDTO(int id, String option) { + +} diff --git a/src/main/java/gift/dto/product/ModifyProductDTO.java b/src/main/java/gift/dto/product/ModifyProductDTO.java new file mode 100644 index 000000000..039073bfa --- /dev/null +++ b/src/main/java/gift/dto/product/ModifyProductDTO.java @@ -0,0 +1,4 @@ +package gift.dto.product; + +public record ModifyProductDTO(int id, String name, int price, String imageUrl) { +} diff --git a/src/main/java/gift/dto/product/ProductWithOptionDTO.java b/src/main/java/gift/dto/product/ProductWithOptionDTO.java new file mode 100644 index 000000000..4e3636e70 --- /dev/null +++ b/src/main/java/gift/dto/product/ProductWithOptionDTO.java @@ -0,0 +1,5 @@ +package gift.dto.product; + +public record ProductWithOptionDTO(Integer id, String name, Integer price, String imageUrl, String option, + String categoryName) { +} \ No newline at end of file diff --git a/src/main/java/gift/dto/product/SaveProductDTO.java b/src/main/java/gift/dto/product/SaveProductDTO.java new file mode 100644 index 000000000..1c7264a54 --- /dev/null +++ b/src/main/java/gift/dto/product/SaveProductDTO.java @@ -0,0 +1,5 @@ +package gift.dto.product; + +public record SaveProductDTO(String name, int price, String imageUrl, String option, int categoryId) { + +} diff --git a/src/main/java/gift/dto/product/ShowProductDTO.java b/src/main/java/gift/dto/product/ShowProductDTO.java new file mode 100644 index 000000000..3257dd13e --- /dev/null +++ b/src/main/java/gift/dto/product/ShowProductDTO.java @@ -0,0 +1,5 @@ +package gift.dto.product; + +public record ShowProductDTO(Integer id, String name, int price, String imageUrl, String categoryName) { + +} diff --git a/src/main/java/gift/dto/user/LoginDTO.java b/src/main/java/gift/dto/user/LoginDTO.java new file mode 100644 index 000000000..5300b3f6d --- /dev/null +++ b/src/main/java/gift/dto/user/LoginDTO.java @@ -0,0 +1,5 @@ +package gift.dto.user; + +public record LoginDTO(String email, String password) { + +} \ No newline at end of file diff --git a/src/main/java/gift/dto/user/SignUpDTO.java b/src/main/java/gift/dto/user/SignUpDTO.java new file mode 100644 index 000000000..3cba2d77a --- /dev/null +++ b/src/main/java/gift/dto/user/SignUpDTO.java @@ -0,0 +1,4 @@ +package gift.dto.user; + +public record SignUpDTO(String email, String password) { +} diff --git a/src/main/java/gift/dto/user/Token.java b/src/main/java/gift/dto/user/Token.java new file mode 100644 index 000000000..1f15ddb86 --- /dev/null +++ b/src/main/java/gift/dto/user/Token.java @@ -0,0 +1,5 @@ +package gift.dto.user; + +public record Token(String token) { + +} \ No newline at end of file diff --git a/src/main/java/gift/entity/Category.java b/src/main/java/gift/entity/Category.java new file mode 100644 index 000000000..4613035b3 --- /dev/null +++ b/src/main/java/gift/entity/Category.java @@ -0,0 +1,55 @@ +package gift.entity; + +import jakarta.persistence.*; + +import java.util.Collection; +import java.util.List; + +@Entity +public class Category { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + int id; + + String name; + + @OneToMany(mappedBy = "category") + List products; + + public Category() { + } + + public Category(String name) { + this.name = name; + } + + public Category(int id, String name) { + this.id = id; + this.name = name; + } + + public String getName() { + return name; + } + + public void addProduct(Product product) { + products.add(product); + product.setCategory(this); + } + + public int getId() { + return id; + } + + public List getProducts() { + return this.products; + } + + public void deleteProduct(Product product) { + this.products.remove(product); + } + + public void updateCategoryName(String name) { + this.name = name; + } +} diff --git a/src/main/java/gift/entity/Option.java b/src/main/java/gift/entity/Option.java new file mode 100644 index 000000000..52ee0dd62 --- /dev/null +++ b/src/main/java/gift/entity/Option.java @@ -0,0 +1,65 @@ +package gift.entity; + +import com.fasterxml.jackson.annotation.JsonAutoDetect; +import jakarta.persistence.*; +import jakarta.validation.constraints.Min; +import jakarta.validation.constraints.Pattern; +import jakarta.validation.constraints.Size; + +@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY) +@Entity +public class Option { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + int id; + + @ManyToOne(fetch = FetchType.LAZY) + Product product; + + @Pattern(regexp = "^[a-zA-Z0-9()\\[\\]+\\-&/_]+$", message = "특수기호 안됨") + + String option; + + @Min(0) + int quantity; + + public Product getProduct() { + return product; + } + + public Option(Product product, String option) { + this.product = product; + product.addOptions(this); + this.option = option; + this.quantity = 0; + } + + public int getId() { + return this.id; + } + + public String getOption() { + return this.option; + } + + public int getQuantity() { + return this.quantity; + } + + public Option() { + } + + public void addQuantity(int num) { + this.quantity += num; + } + + public void subQuantity(int num) { + this.quantity -= num; + } + + public Option changeOption(String option) { + this.option = option; + return this; + } +} \ No newline at end of file diff --git a/src/main/java/gift/entity/Product.java b/src/main/java/gift/entity/Product.java new file mode 100644 index 000000000..6c8fd6fda --- /dev/null +++ b/src/main/java/gift/entity/Product.java @@ -0,0 +1,100 @@ +package gift.entity; + +import com.fasterxml.jackson.annotation.JsonAutoDetect; + +import gift.dto.product.ModifyProductDTO; +import jakarta.persistence.*; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.Pattern; +import jakarta.validation.constraints.Size; + +import java.util.ArrayList; +import java.util.List; + +@Entity +@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY) +public class Product { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + int id; + + @NotBlank(message = "이름 공백 안됨") + @Size(max = 15, message = "15글자까지만 가능") + @Pattern(regexp = "^[a-zA-Z0-9()\\[\\]+\\-&/_]+$", message = "특수기호 안됨") + @Column(nullable = false) + String name; + @Column(nullable = false) + int price; + @Column(nullable = false) + String imageUrl; + + @OneToMany(mappedBy = "product") + @Size(max = 100_000_000)//1~1억 + List