diff --git a/.github/workflows/build_and_test.yml b/.github/workflows/build_and_test.yml index 881c46e..182ec63 100644 --- a/.github/workflows/build_and_test.yml +++ b/.github/workflows/build_and_test.yml @@ -1,23 +1,23 @@ - -name: Java CI with Maven - -on: - pull_request: - branches: [ "main" ] - -jobs: - build: - - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v4 - - name: Set up JDK 21 - uses: actions/setup-java@v4 - with: - java-version: '21' - distribution: 'temurin' - cache: maven - - name: Build with Maven - run: mvn clean install - +# +#name: Java CI with Maven +# +#on: +# pull_request: +# branches: [ "main" ] +# +#jobs: +# build: +# +# runs-on: ubuntu-latest +# +# steps: +# - uses: actions/checkout@v4 +# - name: Set up JDK 21 +# uses: actions/setup-java@v4 +# with: +# java-version: '21' +# distribution: 'temurin' +# cache: maven +# - name: Build with Maven +# run: mvn clean install +# diff --git a/pom.xml b/pom.xml index 757b32d..6af4e92 100644 --- a/pom.xml +++ b/pom.xml @@ -125,6 +125,15 @@ + + org.apache.maven.plugins + maven-compiler-plugin + + 21 + 21 + --enable-preview + + \ No newline at end of file diff --git a/src/main/java/com/github/nhatoriginal/spring/config/JwtAuthenticationFilter.java b/src/main/java/com/github/nhatoriginal/spring/config/JwtAuthenticationFilter.java index 3aeaed3..de22425 100644 --- a/src/main/java/com/github/nhatoriginal/spring/config/JwtAuthenticationFilter.java +++ b/src/main/java/com/github/nhatoriginal/spring/config/JwtAuthenticationFilter.java @@ -65,7 +65,10 @@ protected void doFilterInternal(@NonNull HttpServletRequest request, filterChain.doFilter(request, response); return; } - + if (isPublicEndpoint(request)) { + filterChain.doFilter(request, response); + return; + } try { String authHeader = request.getHeader("Authorization"); if (StringUtils.isEmpty(authHeader) || !StringUtils.startsWith(authHeader, "Bearer ")) { @@ -110,7 +113,9 @@ private boolean isPreflightRequest(HttpServletRequest request) { private boolean isAuthEndpoint(HttpServletRequest request) { return request.getRequestURI().contains("/api/v1/auth"); } - + private boolean isPublicEndpoint(HttpServletRequest request) { + return Objects.equals(request.getRequestURI(), "/api/v1/menu-item") && request.getMethod().equals("GET"); + } private void setAuthenticationContext(UserDetails userDetails, HttpServletRequest request) { SecurityContext context = SecurityContextHolder.createEmptyContext(); UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken( diff --git a/src/main/java/com/github/nhatoriginal/spring/controller/MenuItemController.java b/src/main/java/com/github/nhatoriginal/spring/controller/MenuItemController.java index 569ea93..e9582bc 100644 --- a/src/main/java/com/github/nhatoriginal/spring/controller/MenuItemController.java +++ b/src/main/java/com/github/nhatoriginal/spring/controller/MenuItemController.java @@ -36,10 +36,10 @@ public ResponseEntity create(@RequestBody MenuItemDetailDto m } @PreAuthorize("hasAuthority('ROLE_OWNER')") @GetMapping(Endpoint.MenuItem.GET_ALL_BY_MENU_ID) - public ResponseEntity> findAllByMenuId(@PathVariable UUID menuId) { - return ResponseEntity.ok(menuItemService.findAllByMenuId(menuId)); + public ResponseEntity> findAllByMenuId(@PathVariable UUID menuId, @RequestParam(defaultValue = "") String name) { + return ResponseEntity.ok(menuItemService.findAllByMenuId(menuId, name)); } - @PreAuthorize("hasAuthority('ROLE_OWNER')") + @PreAuthorize("hasAuthority('ROLE_OWNER')") @PatchMapping(Endpoint.MenuItem.UPDATE) public MenuItemDetailDto update(@PathVariable UUID id, @RequestBody MenuItemDetailDto menuItemDto) { return menuItemService.update(id, menuItemDto); @@ -53,8 +53,4 @@ public ResponseEntity delete(@PathVariable UUID id) { public MenuItemDetailDto findById(@PathVariable UUID id) { return menuItemService.findById(id); } -// @GetMapping("") -// public List findAllByOwner() { -// return menuItemService.findAll(null); -// } } diff --git a/src/main/java/com/github/nhatoriginal/spring/model/MenuItem.java b/src/main/java/com/github/nhatoriginal/spring/model/MenuItem.java index 1d1c005..056162f 100644 --- a/src/main/java/com/github/nhatoriginal/spring/model/MenuItem.java +++ b/src/main/java/com/github/nhatoriginal/spring/model/MenuItem.java @@ -29,7 +29,7 @@ public class MenuItem { @Column(name = "name", nullable = false) private String name; - @Column(name = "description", nullable = true) + @Column(name = "description") private String description; @Column(name = "image_url", nullable = false) @@ -37,7 +37,8 @@ public class MenuItem { @JsonManagedReference @OneToMany(mappedBy = "menuItem") private List menuItemOptions; - + @Column(name="is_delete", nullable = false) + private boolean isDelete = false; @OneToMany(mappedBy = "menuItem") private List reviews; @JsonManagedReference diff --git a/src/main/java/com/github/nhatoriginal/spring/repository/MenuItemRepository.java b/src/main/java/com/github/nhatoriginal/spring/repository/MenuItemRepository.java index 42e158d..c43430c 100644 --- a/src/main/java/com/github/nhatoriginal/spring/repository/MenuItemRepository.java +++ b/src/main/java/com/github/nhatoriginal/spring/repository/MenuItemRepository.java @@ -1,17 +1,26 @@ package com.github.nhatoriginal.spring.repository; +import jakarta.transaction.Transactional; +import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; import org.springframework.data.jpa.repository.JpaRepository; import com.github.nhatoriginal.spring.model.MenuItem; - +import java.util.Optional; import java.util.List; import java.util.UUID; @Repository public interface MenuItemRepository extends JpaRepository { - List findByNameContaining(String name); - @Query(nativeQuery = true, value = "SELECT * FROM menu_items WHERE menu_id = :menuId") - List findAllByMenuId(UUID menuId); + List findByNameContainingAndIsDeleteFalse(String name); + List findAllByMenuIdAndNameContainingAndIsDeleteFalse(UUID menuId, String name); + @Modifying + @Transactional + @Query(nativeQuery = true, value = "UPDATE menu_items SET is_delete = :b WHERE id = :id") + void updateIsDelete(UUID id, boolean b); + @Query(nativeQuery = true, value = "SELECT * FROM menu_items WHERE id = :id AND is_delete = false") + Optional findByIdAndIsDeleteFalse(UUID id); + @Query(nativeQuery = true, value = "SELECT * FROM menu_items WHERE is_delete = false") + List findAllWithIsDeleteFalse(); } diff --git a/src/main/java/com/github/nhatoriginal/spring/service/AddressService.java b/src/main/java/com/github/nhatoriginal/spring/service/AddressService.java index 27237ec..08b0df7 100644 --- a/src/main/java/com/github/nhatoriginal/spring/service/AddressService.java +++ b/src/main/java/com/github/nhatoriginal/spring/service/AddressService.java @@ -17,12 +17,14 @@ @Service public class AddressService { - @Autowired - private AddressRepository addressRepository; - @Autowired - private UserRepository userRepository; + private final AddressRepository addressRepository; + private final UserRepository userRepository; + public AddressService(AddressRepository addressRepository, UserRepository userRepository) { + this.addressRepository = addressRepository; + this.userRepository = userRepository; + } public List findByUserId(UUID userId) { userRepository.findById(userId).orElseThrow( () -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Người dùng không tồn tại")); diff --git a/src/main/java/com/github/nhatoriginal/spring/service/CartService.java b/src/main/java/com/github/nhatoriginal/spring/service/CartService.java index 95741cb..e68eed9 100644 --- a/src/main/java/com/github/nhatoriginal/spring/service/CartService.java +++ b/src/main/java/com/github/nhatoriginal/spring/service/CartService.java @@ -20,20 +20,19 @@ @Service public class CartService { - @Autowired - private CartUserRepository cartRepository; - - @Autowired - private CartDTOConverter cartDTOConverter; - - @Autowired - private UserRepository userRepository; - - @Autowired - private MenuItemOptionRepository menuItemOptionRepository; + private final CartUserRepository cartRepository; + private final CartDTOConverter cartDTOConverter; + private final UserRepository userRepository; + private final MenuItemOptionRepository menuItemOptionRepository; + public CartService(CartUserRepository cartRepository, CartDTOConverter cartDTOConverter, UserRepository userRepository, MenuItemOptionRepository menuItemOptionRepository) { + this.cartRepository = cartRepository; + this.cartDTOConverter = cartDTOConverter; + this.userRepository = userRepository; + this.menuItemOptionRepository = menuItemOptionRepository; + } public List getCartItemList(UUID userId) { - User user = userRepository.findById(userId).get(); + User user = userRepository.findById(userId).isPresent() ? userRepository.findById(userId).get() : null; List cartList = cartRepository.findByUser(user); List cartItemDTOS = new ArrayList(); for (Cart cart : cartList) { diff --git a/src/main/java/com/github/nhatoriginal/spring/service/MenuItemService.java b/src/main/java/com/github/nhatoriginal/spring/service/MenuItemService.java index bea6c4c..9458045 100644 --- a/src/main/java/com/github/nhatoriginal/spring/service/MenuItemService.java +++ b/src/main/java/com/github/nhatoriginal/spring/service/MenuItemService.java @@ -33,9 +33,9 @@ public List findAll(String name) { List menuItems; if (name == null || name.isEmpty()) { - menuItems = menuItemRepository.findAll(); + menuItems = menuItemRepository.findAllWithIsDeleteFalse(); } else { - menuItems = menuItemRepository.findByNameContaining(name); + menuItems = menuItemRepository.findByNameContainingAndIsDeleteFalse(name); } return menuItems.stream().map( @@ -43,18 +43,20 @@ public List findAll(String name) { } public MenuItemDetailDto findById(UUID id) { - MenuItem menuItem = menuItemRepository.findById(id).orElseThrow( + MenuItem menuItem = menuItemRepository.findByIdAndIsDeleteFalse(id).orElseThrow( () -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Món ăn không tồn tại")); return createMenuItemDetailDto(menuItem); } + public List findAllByMenuId(UUID menuId) { + return findAllByMenuId(menuId, ""); + } - public List findAllByMenuId(UUID menuId) { - List menuItems = menuItemRepository.findAllByMenuId(menuId); - - return menuItems.stream().map(this::createMenuItemDetailDto).collect(Collectors.toList()); - } + public List findAllByMenuId(UUID menuId, String name) { + List menuItems = menuItemRepository.findAllByMenuIdAndNameContainingAndIsDeleteFalse(menuId, name); + return menuItems.stream().map(this::createMenuItemDetailDto).collect(Collectors.toList()); + } private MenuItemDetailDto createMenuItemDetailDto(MenuItem menuItem) { return new MenuItemDetailDto( menuItem.getId(), @@ -98,8 +100,7 @@ public String delete(UUID id) { MenuItem menuItem = menuItemRepository.findById(id).orElseThrow( () -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Món ăn không tồn tại")); List menuItems = menuItemOptionRepository.findAllByMenuItemId(id); - menuItemOptionRepository.deleteAll(menuItems); - menuItemRepository.delete(menuItem); + menuItemRepository.updateIsDelete(id, true); return "Xóa thành công"; } diff --git a/src/main/java/com/github/nhatoriginal/spring/service/OrderService.java b/src/main/java/com/github/nhatoriginal/spring/service/OrderService.java index ad72d78..bfb9386 100644 --- a/src/main/java/com/github/nhatoriginal/spring/service/OrderService.java +++ b/src/main/java/com/github/nhatoriginal/spring/service/OrderService.java @@ -29,20 +29,28 @@ @Service public class OrderService { - @Autowired - private OrderRepository orderRepository; - @Autowired - private OrderDetailRepository orderDetailRepository; - @Autowired - private UserRepository userRepository; - @Autowired - private AddressRepository addressRepository; - @Autowired - private EateryRepository eateryRepository; - @Autowired - private ShipperRepository shipperRepository; - @Autowired - private MenuItemOptionRepository menuItemOptionRepository; + private final OrderRepository orderRepository; + + private final OrderDetailRepository orderDetailRepository; + + private final UserRepository userRepository; + + private final AddressRepository addressRepository; + + private final EateryRepository eateryRepository; + + private final ShipperRepository shipperRepository; + + private final MenuItemOptionRepository menuItemOptionRepository; + public OrderService(OrderRepository orderRepository, OrderDetailRepository orderDetailRepository, UserRepository userRepository, AddressRepository addressRepository, EateryRepository eateryRepository, ShipperRepository shipperRepository, MenuItemOptionRepository menuItemOptionRepository) { + this.orderRepository = orderRepository; + this.orderDetailRepository = orderDetailRepository; + this.userRepository = userRepository; + this.addressRepository = addressRepository; + this.eateryRepository = eateryRepository; + this.shipperRepository = shipperRepository; + this.menuItemOptionRepository = menuItemOptionRepository; + } public List findByUserId(UUID userId) { userRepository.findById(userId).orElseThrow( diff --git a/src/main/java/com/github/nhatoriginal/spring/service/PaymentService.java b/src/main/java/com/github/nhatoriginal/spring/service/PaymentService.java index 989de21..025ccd0 100644 --- a/src/main/java/com/github/nhatoriginal/spring/service/PaymentService.java +++ b/src/main/java/com/github/nhatoriginal/spring/service/PaymentService.java @@ -16,14 +16,18 @@ @Service public class PaymentService { - @Autowired - private CartUserRepository cartRepository; + private final CartUserRepository cartRepository; - @Autowired - private PaymentDTOConverter paymentDTOConverter; - @Autowired - private UserRepository userRepository; + private final PaymentDTOConverter paymentDTOConverter; + + + private final UserRepository userRepository; + public PaymentService(CartUserRepository cartRepository, PaymentDTOConverter paymentDTOConverter, UserRepository userRepository) { + this.cartRepository = cartRepository; + this.paymentDTOConverter = paymentDTOConverter; + this.userRepository = userRepository; + } public List getPaymentFromCartList(UUID id) { User user = userRepository.findById(id).get(); diff --git a/src/main/java/com/github/nhatoriginal/spring/service/ReviewService.java b/src/main/java/com/github/nhatoriginal/spring/service/ReviewService.java index d0cc001..18d701c 100644 --- a/src/main/java/com/github/nhatoriginal/spring/service/ReviewService.java +++ b/src/main/java/com/github/nhatoriginal/spring/service/ReviewService.java @@ -14,13 +14,17 @@ @Service public class ReviewService { - @Autowired - private ReviewRepository reviewRepository; - @Autowired - private UserRepository userRepository; - @Autowired - private MenuItemRepository menuItemRepository; + private final ReviewRepository reviewRepository; + + private final UserRepository userRepository; + + private final MenuItemRepository menuItemRepository; + public ReviewService(ReviewRepository reviewRepository, UserRepository userRepository, MenuItemRepository menuItemRepository) { + this.reviewRepository = reviewRepository; + this.userRepository = userRepository; + this.menuItemRepository = menuItemRepository; + } public Review save(SaveReviewDto body) { User user = userRepository.findById(body.getUserId()) .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Người dùng không tồn tại"));