Skip to content

Commit

Permalink
feat: update search query and refactory code
Browse files Browse the repository at this point in the history
  • Loading branch information
loingtan committed May 28, 2024
1 parent dbc9973 commit 7c621c0
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 90 deletions.
46 changes: 23 additions & 23 deletions .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
@@ -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
#
9 changes: 9 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,15 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>21</source>
<target>21</target>
<compilerArgs>--enable-preview</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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 ")) {
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ public ResponseEntity<MenuItemDetailDto> create(@RequestBody MenuItemDetailDto m
}
@PreAuthorize("hasAuthority('ROLE_OWNER')")
@GetMapping(Endpoint.MenuItem.GET_ALL_BY_MENU_ID)
public ResponseEntity<List<MenuItemDetailDto>> findAllByMenuId(@PathVariable UUID menuId) {
return ResponseEntity.ok(menuItemService.findAllByMenuId(menuId));
public ResponseEntity<List<MenuItemDetailDto>> 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);
Expand All @@ -53,8 +53,4 @@ public ResponseEntity<String> delete(@PathVariable UUID id) {
public MenuItemDetailDto findById(@PathVariable UUID id) {
return menuItemService.findById(id);
}
// @GetMapping("")
// public List<MenuItemDto> findAllByOwner() {
// return menuItemService.findAll(null);
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@ 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)
private String imageUrl;
@JsonManagedReference
@OneToMany(mappedBy = "menuItem")
private List<MenuItemOption> menuItemOptions;

@Column(name="is_delete", nullable = false)
private boolean isDelete = false;
@OneToMany(mappedBy = "menuItem")
private List<Review> reviews;
@JsonManagedReference
Expand Down
Original file line number Diff line number Diff line change
@@ -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<MenuItem, UUID> {
List<MenuItem> findByNameContaining(String name);

@Query(nativeQuery = true, value = "SELECT * FROM menu_items WHERE menu_id = :menuId")
List<MenuItem> findAllByMenuId(UUID menuId);
List<MenuItem> findByNameContainingAndIsDeleteFalse(String name);
List<MenuItem> 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<MenuItem> findByIdAndIsDeleteFalse(UUID id);
@Query(nativeQuery = true, value = "SELECT * FROM menu_items WHERE is_delete = false")
List<MenuItem> findAllWithIsDeleteFalse();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<AddressDto> findByUserId(UUID userId) {
userRepository.findById(userId).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Người dùng không tồn tại"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<CartItemDTO> getCartItemList(UUID userId) {
User user = userRepository.findById(userId).get();
User user = userRepository.findById(userId).isPresent() ? userRepository.findById(userId).get() : null;
List<Cart> cartList = cartRepository.findByUser(user);
List<CartItemDTO> cartItemDTOS = new ArrayList<CartItemDTO>();
for (Cart cart : cartList) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,30 @@ public List<MenuItemDto> findAll(String name) {
List<MenuItem> 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(
menuItem -> new MenuItemDto(menuItem.getId(), menuItem.getName(), menuItem.getImageUrl())).toList();
}

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<MenuItemDetailDto> findAllByMenuId(UUID menuId) {
return findAllByMenuId(menuId, "");
}

public List<MenuItemDetailDto> findAllByMenuId(UUID menuId) {
List<MenuItem> menuItems = menuItemRepository.findAllByMenuId(menuId);

return menuItems.stream().map(this::createMenuItemDetailDto).collect(Collectors.toList());
}
public List<MenuItemDetailDto> findAllByMenuId(UUID menuId, String name) {
List<MenuItem> menuItems = menuItemRepository.findAllByMenuIdAndNameContainingAndIsDeleteFalse(menuId, name);

return menuItems.stream().map(this::createMenuItemDetailDto).collect(Collectors.toList());
}
private MenuItemDetailDto createMenuItemDetailDto(MenuItem menuItem) {
return new MenuItemDetailDto(
menuItem.getId(),
Expand Down Expand Up @@ -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<MenuItemOption> menuItems = menuItemOptionRepository.findAllByMenuItemId(id);
menuItemOptionRepository.deleteAll(menuItems);
menuItemRepository.delete(menuItem);
menuItemRepository.updateIsDelete(id, true);
return "Xóa thành công";

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<OrderDto> findByUserId(UUID userId) {
userRepository.findById(userId).orElseThrow(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<PaymentDTO> getPaymentFromCartList(UUID id) {
User user = userRepository.findById(id).get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down

0 comments on commit 7c621c0

Please sign in to comment.