Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import shop.mtcoding.metamall.core.filter.JwtVerifyFilter;
import shop.mtcoding.metamall.core.filter.SellerFilter;


@Configuration
Expand All @@ -13,7 +14,19 @@ public FilterRegistrationBean<?> jwtVerifyFilterAdd() {
FilterRegistrationBean<JwtVerifyFilter> registration = new FilterRegistrationBean<>();
registration.setFilter(new JwtVerifyFilter());
registration.addUrlPatterns("/user/*");
registration.addUrlPatterns("/seller/*");
registration.setOrder(1);

return registration;
}

@Bean
public FilterRegistrationBean<?> sellerFilterAdd() {
FilterRegistrationBean<SellerFilter> registration = new FilterRegistrationBean<>();
registration.setFilter(new SellerFilter());
registration.addUrlPatterns("/seller/*");
registration.setOrder(1);

return registration;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package shop.mtcoding.metamall.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import shop.mtcoding.metamall.core.exception.Exception400;
import shop.mtcoding.metamall.dto.user.UserRequest;
import shop.mtcoding.metamall.model.product.Product;
import shop.mtcoding.metamall.model.product.ProductRepository;

import java.util.List;
import java.util.Optional;

@RequiredArgsConstructor
@RestController
public class ProductController {

private final ProductRepository productRepository;

@PostMapping("/seller/product/enroll")
public Product enroll(@RequestBody UserRequest.ProductDto productDto) {
Optional<Product> productOP = productRepository.findByName();
if (productOP.isPresent()) {
throw new Exception400("이미 등록된 상품입니다.");
}

Product product = Product.builder().name(productDto.getName())
.price(productDto.getPrice())
.qty(productDto.getQty())
.createdAt(productDto.getCreatedAt())
.build();

return product;
}

@GetMapping("/product")
public List<Product> allProduct() {
List<Product> products = productRepository.findAll();
return products;
}

@GetMapping("/product/{id}")
public Product product(@PathVariable Long id) {
Optional<Product> findProduct = productRepository.findById(id);
if (!findProduct.isPresent()) {
throw new Exception400("잘못된 접근입니다.");
}
return findProduct.get();
}

@PutMapping("/seller/product/{id}")
public Product updateProduct(@PathVariable Long id, @RequestBody UserRequest.ProductDto productDto) {
Optional<Product> findProduct = productRepository.findById(id);
if (!findProduct.isPresent()) {
throw new Exception400("잘못된 접근입니다.");
}
Product product = findProduct.get();
product.setName(productDto.getName());
product.setUpdatedAt(productDto.getUpdatedAt());
product.setQty(productDto.getQty());
product.setPrice(productDto.getPrice());

return product;
}

@DeleteMapping("/seller/product/{id}")
public void deleteProduct(@PathVariable Long id) {
Optional<Product> product = productRepository.findById(id);
if (!product.isPresent()) {
throw new Exception400("잘못된 접근입니다.");
}
productRepository.deleteById(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import shop.mtcoding.metamall.dto.user.UserResponse;
import shop.mtcoding.metamall.model.log.login.LoginLog;
import shop.mtcoding.metamall.model.log.login.LoginLogRepository;
import shop.mtcoding.metamall.model.product.Product;
import shop.mtcoding.metamall.model.product.ProductRepository;
import shop.mtcoding.metamall.model.user.User;
import shop.mtcoding.metamall.model.user.UserRepository;

Expand All @@ -25,6 +27,7 @@ public class UserController {

private final UserRepository userRepository;
private final LoginLogRepository loginLogRepository;
private final ProductRepository productRepository;
private final HttpSession session;

@PostMapping("/login")
Expand Down Expand Up @@ -60,4 +63,26 @@ public ResponseEntity<?> login(@RequestBody UserRequest.LoginDto loginDto, HttpS
throw new Exception400("유저네임 혹은 아이디가 잘못되었습니다");
}
}

@PostMapping("/join")
public ResponseEntity<?> join(@RequestBody UserRequest.JoinDto joinDto, HttpServletRequest request) {
Optional<User> userOP = userRepository.findByUsername(joinDto.getUsername());

if (userOP.isPresent()) {
throw new Exception400("이미 가입된 회원의 아이디입니다.");
}

User joinedUser = User.builder().username(joinDto.getUsername())
.password(joinDto.getPassword())
.email(joinDto.getEmail())
.role(joinDto.getRole())
.createdAt(joinDto.getCreatedAt())
.build();

ResponseDto<?> responseDto = new ResponseDto<>().data(joinedUser);
return ResponseEntity.ok().body(responseDto);
}



}
44 changes: 44 additions & 0 deletions src/main/java/shop/mtcoding/metamall/core/filter/SellerFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package shop.mtcoding.metamall.core.filter;


import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.HttpStatus;
import shop.mtcoding.metamall.core.exception.Exception400;
import shop.mtcoding.metamall.core.jwt.JwtProvider;
import shop.mtcoding.metamall.core.session.LoginUser;
import shop.mtcoding.metamall.dto.ResponseDto;
import shop.mtcoding.metamall.model.user.User;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class SellerFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("디버그 : SellerFilter 동작함");
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse resp = (HttpServletResponse) response;
User user = (User) req.getSession().getAttribute("loginUser");
if (!user.getRole().equals("SELLER")) {
error(resp, new Exception400("등록된 SELLER 가 아닙니다."));
return;
}
chain.doFilter(req,resp);
}

private void error(HttpServletResponse resp, Exception e) throws IOException {
resp.setStatus(403);
resp.setContentType("application/json; charset=utf-8");
ResponseDto<?> responseDto = new ResponseDto<>().fail(HttpStatus.UNAUTHORIZED, "권한이 없습니다", e.getMessage());
ObjectMapper om = new ObjectMapper();
String responseBody = om.writeValueAsString(responseDto);
resp.getWriter().println(responseBody);
}

}
22 changes: 22 additions & 0 deletions src/main/java/shop/mtcoding/metamall/dto/user/UserRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,32 @@
import lombok.Getter;
import lombok.Setter;

import java.time.LocalDateTime;

public class UserRequest {
@Getter @Setter
public static class LoginDto {
private String username;
private String password;
}

@Getter
@Setter
public static class JoinDto {
private String username;
private String password;
private String email;
private String role; // USER(고객), SELLER(판매자), ADMIN(관리자)
private LocalDateTime createdAt;
private LocalDateTime updatedAt;
}

@Getter @Setter
public static class ProductDto {
private String name;
private Integer price;
private Integer qty;
private LocalDateTime createdAt;
private LocalDateTime updatedAt;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ public class OrderProduct { // 주문 상품
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
private Product product;
private Integer count; // 상품 주문 개수
private Integer orderPrice; // 상품 주문 금액
private LocalDateTime createdAt;
private LocalDateTime updatedAt;

@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
private OrderSheet orderSheet;

@PrePersist
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class OrderSheet { // 주문서
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
private User user; // 주문자
@OneToMany(mappedBy = "orderSheet")
private List<OrderProduct> orderProductList = new ArrayList<>(); // 총 주문 상품 리스트
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@

import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface ProductRepository extends JpaRepository<Product, Long> {

Optional<Product> findByName();
}
5 changes: 5 additions & 0 deletions src/main/java/shop/mtcoding/metamall/model/user/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import shop.mtcoding.metamall.model.ordersheet.OrderSheet;

import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@NoArgsConstructor
@Setter // DTO 만들면 삭제해야됨
Expand All @@ -23,6 +26,8 @@ public class User {
private String role; // USER(고객), SELLER(판매자), ADMIN(관리자)
private LocalDateTime createdAt;
private LocalDateTime updatedAt;
@OneToMany(mappedBy = "user")
private List<OrderSheet> orderSheets = new ArrayList<>();

@PrePersist
protected void onCreate() {
Expand Down