ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 60 TIL
    내일배움캠프 2023. 1. 19. 20:59

     

    프로잭트 기능구현을 완료하고 오늘 각자 추가 기능 및 최적화 선택하여 진행했다

    혼자 point 추가 기능을 구현했는데 전 프로잭트에서  CRUD 열라해서 그런지

    막힘 없이 진행했는데 UserId 를 PK 로 사용해서 진행하다가 만들다 보니

    굳이 이걸 Id 로 해야되나 의문이 생겨 다 완성한 코드를 다시 갈아 엎었다 ..

     

    PointController

     

    @RestController
    @RequiredArgsConstructor
    @RequestMapping( "/point")
    public class PointController {
        private final PointServiceImpl pointService;
    
        @GetMapping("/inquir")
        public ResponseEntity<PointResponse> getPoint(@AuthenticationPrincipal UserDetailsImpl userDetails) {
            String username = userDetails.getUsername();
            PointResponse response = pointService.getPoint(username);
            return ResponseEntity.ok().body(response);
        }
    
        @PostMapping("/buygoods/{productId}")
        public ResponseEntity<PointResponse> BuyGoods(@PathVariable Long productId, @AuthenticationPrincipal UserDetailsImpl userDetails) {
            String username = userDetails.getUsername();
            PointResponse pointBuyGoods = pointService.pointBuyGoods(username, productId);
            return ResponseEntity.ok().body(pointBuyGoods);
        }
    
        @PostMapping("/admin")
        public ResponseEntity<AdminGivePresentResponse> adminGivePresent(@RequestBody AdminGivePresentRequest request, @AuthenticationPrincipal UserDetailsImpl userDetails) {
            User user = userDetails.getUser();
            AdminGivePresentResponse adminGivePresentResponse = pointService.adminGivePresent(request, user);
            return ResponseEntity.ok().body(adminGivePresentResponse);
        }
    }

     

     

    PointServcie

     

     

    @Service
    @RequiredArgsConstructor
    public class PointServiceImpl implements PointService {
        private final PointRepository pointRepository;
        private final ProductRepository productRepository;
        private final UserRepository userRepository;
        private final SellerRegistRepository sellerRegistRepository;
    
        @Override
        public PointResponse getPoint(String username) {
            Point pointinfo = pointRepository.findByUsername(username);
            int point = pointinfo.getPoint();
    
            if (point <= 0) {
                throw new IllegalArgumentException("포인트가 없습니다");
            }
    
            return new PointResponse(pointinfo);
        }
    
        @Override
        public PointResponse pointBuyGoods(String username, Long produckId) {
            Point pointinfo = pointRepository.findByUsername(username);
            int point = pointinfo.getPoint();
    
            Product product = productRepository.findById(produckId)
                    .orElseThrow(() -> new IllegalArgumentException("상품이 존재하지 않습니다"));
            int productPrice = product.getPrice();
            Long sellerId = product.getUserId();
    
            if (productPrice > point) { throw new IllegalArgumentException("포인트가 부족하여 구매할 수 없습니다"); }
    
            point = point - productPrice;
            Point update = new Point(pointinfo.getUsername(), point, null);
            pointRepository.save(update);
    
            SellerRegist sellerinfo = sellerRegistRepository.findById(sellerId).orElseThrow(() -> new IllegalArgumentException("판매자를 찾을 수 없습니다"));
            String sellerName = sellerinfo.getUsername();
    
            Point pointSellerinfo = pointRepository.findByUsername(sellerName);
            int sellerPoint = pointSellerinfo.getPoint() + productPrice;
            Point seller = new Point(sellerName, sellerPoint, null);
            pointRepository.save(seller);
    
    //        productRepository.delete(product);
    
            return new PointResponse(update);
        }
    
        @Override
        public AdminGivePresentResponse adminGivePresent(AdminGivePresentRequest request, User admin) {
            if (!admin.getRole().equals(UserRoleEnum.ADMIN)) { throw new IllegalArgumentException("권한이 없습니다"); }
    
            User userinfo = userRepository.findByUsername(request.getUsername()).orElseThrow(() -> new IllegalArgumentException("존재하지 않는 회원"));
            Point pointinfo = pointRepository.findByUsername(userinfo.getUsername());
            int point = pointinfo.getPoint() + request.getGivePresent();
            Point update = new Point(userinfo.getUsername(), point, admin.getUsername());
            pointRepository.save(update);
            return new AdminGivePresentResponse(update) ;
        }
    }

    '내일배움캠프' 카테고리의 다른 글

    65 TIL  (1) 2023.01.30
    61 TIL  (0) 2023.01.20
    59 TIL  (0) 2023.01.18
    58 TIL  (0) 2023.01.17
    57 TIL  (0) 2023.01.16
Designed by Tistory.