Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: small fixes, change English messages to Vietnamese #38

Merged
merged 1 commit into from
May 20, 2024
Merged
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
@@ -1,4 +1,5 @@
package com.github.nhatoriginal.spring.controller;

import com.github.nhatoriginal.spring.constant.Endpoint;
import com.github.nhatoriginal.spring.model.User;
import com.github.nhatoriginal.spring.service.UserService;
Expand All @@ -10,14 +11,15 @@
@RestController
@RequestMapping(Endpoint.User.BASE)
public class UserController {
private final UserService service;
private final UserService service;

public UserController(UserService service) {
this.service = service;
}

public UserController(UserService service) {
this.service = service;
}
@GetMapping(Endpoint.User.GET_ONE)
public User getUserById(@PathVariable String id) {
return this.service.getUserById(id);
}
@GetMapping(Endpoint.User.GET_ONE)
public User getUserById(@PathVariable String id) {
return this.service.getUserById(id);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ public class CartDTOConverter {
public CartItemDTO toCartItemDTO(Cart cart) {
CartItemDTO cartItemDTO = new CartItemDTO();
cartItemDTO.setMenuItemOptionId(cart.getMenuItemOption().getId());
cartItemDTO.setEateryId(cart.getMenuItemOption().getMenuItem().getMenu().getEatery().getId());
cartItemDTO.setEatery(
new CartItemDTO.Eatery(cart.getMenuItemOption().getMenuItem().getMenu().getEatery().getId(),
cart.getMenuItemOption().getMenuItem().getMenu().getEatery().getName()));
cartItemDTO.setImageUrl(cart.getMenuItemOption().getMenuItem().getImageUrl());
cartItemDTO.setName(cart.getMenuItemOption().getMenuItem().getName());
cartItemDTO.setSize(cart.getMenuItemOption().getSize());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,26 @@

import com.github.nhatoriginal.spring.model.Size;

import lombok.Getter;
import lombok.Setter;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Getter
@Setter
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CartItemDTO {
private UUID menuItemOptionId;
private UUID eateryId;
private String imageUrl;
private String name;
private Size size;
private double price;
private int quantity;
private Eatery eatery;

@Data
@AllArgsConstructor
public static class Eatery {
private UUID id;
private String name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ public class MenuItemDetailDto {
public static class Eatery {
private UUID id;
private String name;
private Address address;

@Data
@AllArgsConstructor
public static class Address {
private String province;
private String district;
private String ward;
private String detail;
}
}

@Data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class Eatery {
private String phone;

@Column(name = "is_alive", nullable = false)
private boolean isAlive;
private Boolean isAlive;

@OneToOne
@JoinColumn(name = "owner_id")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
@NoArgsConstructor
@AllArgsConstructor
@Builder

@Table(name = "users")
public class User {
@Id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.UUID;

@Repository
public interface UserRepository extends JpaRepository<User, UUID> {
User findByEmail(String email);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class AddressService {

public List<AddressDto> findByUserId(UUID userId) {
userRepository.findById(userId).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "User not found"));
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Người dùng không tồn tại"));

return addressRepository.findByUserId(userId).stream().map(
address -> new AddressDto(address.getId(), address.getProvince(), address.getDistrict(), address.getWard(),
Expand All @@ -35,7 +35,7 @@ public List<AddressDto> findByUserId(UUID userId) {

public Address save(SaveAddressDto saveAddressDto) {
User user = userRepository.findById(saveAddressDto.getUserId())
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "User not found"));
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Người dùng không tồn tại"));

Address address = new Address();
address.setDistrict(saveAddressDto.getDistrict());
Expand All @@ -52,7 +52,7 @@ public Address save(SaveAddressDto saveAddressDto) {

public void delete(UUID id) {
addressRepository.findById(id)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Address not found"));
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Địa chỉ không tồn tại"));

userRepository.deleteUserAddressesByAddressId(id);
addressRepository.deleteById(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;

@Service
public class AuthService {
Expand All @@ -38,7 +37,7 @@ public AuthService(UserRepository userRepository, PasswordEncoder passwordEncode
public String register(AuthRegisterDto authRegisterDto) {
System.out.println(authRegisterDto);
if (userRepository.findByEmail(authRegisterDto.getEmail()) != null) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Email already exists");
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Email đã tồn tại");
}

User user = User.builder()
Expand All @@ -56,9 +55,10 @@ public String register(AuthRegisterDto authRegisterDto) {

public AuthResponseDto login(AuthLoginDto authLoginDto) {
try {
authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(authLoginDto.getEmail(), authLoginDto.getPassword()));
authenticationManager
.authenticate(new UsernamePasswordAuthenticationToken(authLoginDto.getEmail(), authLoginDto.getPassword()));
} catch (AuthenticationException e) {
throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "Authentication failed");
throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "Xác thực thất bại");
}
var jwtToken = jwtService.generateToken(userService.loadUserByUsername(authLoginDto.getEmail()));
return AuthResponseDto.builder().accessToken(jwtToken).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,17 @@ public List<CartItemDTO> getCartItemList(UUID userId) {

public Cart save(SaveCartItemDto saveCartItemDto) {
User user = userRepository.findById(saveCartItemDto.getUserId())
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "User not found"));
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Người dùng không tồn tại"));

MenuItemOption menuItemOption = menuItemOptionRepository.findById(saveCartItemDto.getMenuItemOptionId())
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Menu item option not found"));
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Tùy chọn món ăn không tồn tại"));

CartIdClassConfig id = new CartIdClassConfig();
id.setUser(user);
id.setMenuItemOption(menuItemOption);
if ((cartRepository.findById(id)).isPresent()) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Tùy chọn món ăn đã tồn tại trong giỏ hàng");
}

Cart cart = new Cart();
cart.setUser(user);
Expand All @@ -60,10 +67,10 @@ public Cart save(SaveCartItemDto saveCartItemDto) {

public void delete(UUID userId, UUID menuItemOptionId) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "User not found"));
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Người dùng không tồn tại"));

MenuItemOption menuItemOption = menuItemOptionRepository.findById(menuItemOptionId)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Menu item option not found"));
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Tùy chọn món ăn không tồn tại"));

CartIdClassConfig id = new CartIdClassConfig();
id.setUser(user);
Expand All @@ -74,17 +81,18 @@ public void delete(UUID userId, UUID menuItemOptionId) {

public void updateQuantity(UUID userId, UUID menuItemOptionId, UpdateCartItemQuantityDto body) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "User not found"));
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Người dùng không tồn tại"));

MenuItemOption menuItemOption = menuItemOptionRepository.findById(menuItemOptionId)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Menu item option not found"));
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Tùy chọn món ăn không tồn tại"));

CartIdClassConfig id = new CartIdClassConfig();
id.setUser(user);
id.setMenuItemOption(menuItemOption);

Cart cart = cartRepository.findById(id)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Cart item not found"));
.orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Tùy chọn món ăn Không tồn tại trong giỏ hàng"));

cart.setQuantity(body.getQuantity());
cartRepository.save(cart);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public List<MenuItemDto> findAll(String name) {

public MenuItemDetailDto findById(UUID id) {
MenuItem menuItem = menuItemRepository.findById(id).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Menu item not found"));
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Món ăn không tồn tại"));

MenuItemDetailDto menuItemDetailDto = new MenuItemDetailDto(
menuItem.getId(),
Expand All @@ -49,7 +49,11 @@ public MenuItemDetailDto findById(UUID id) {
review -> new MenuItemDetailDto.Review(review.getId(), review.getRating(), review.getComment(),
new MenuItemDetailDto.Review.User(review.getUser().getId(), review.getUser().getFullName())))
.toList(),
new MenuItemDetailDto.Eatery(menuItem.getMenu().getId(), menuItem.getMenu().getEatery().getName()));
new MenuItemDetailDto.Eatery(menuItem.getMenu().getId(), menuItem.getMenu().getEatery().getName(),
new MenuItemDetailDto.Eatery.Address(menuItem.getMenu().getEatery().getAddress().getProvince(),
menuItem.getMenu().getEatery().getAddress().getDistrict(),
menuItem.getMenu().getEatery().getAddress().getWard(),
menuItem.getMenu().getEatery().getAddress().getDetail())));

return menuItemDetailDto;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class OrderService {

public List<OrderDto> findByUserId(UUID userId) {
userRepository.findById(userId).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "User not found"));
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Người dùng không tồn tại"));

return orderRepository.findByUserId(userId).stream().map(
order -> new OrderDto(order.getId(), order.getOrderDate(), order.getTotalPrice(), order.getNote(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ public class ReviewService {

public Review save(SaveReviewDto saveReviewDto) {
User user = userRepository.findById(saveReviewDto.getUserId())
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "User not found"));
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Người dùng không tồn tại"));

MenuItem menuItem = menuItemRepository.findById(saveReviewDto.getMenuItemId())
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Menu item not found"));
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Món ăn không tồn tại"));

Review review = new Review();
review.setRating(saveReviewDto.getRating());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@
import com.github.nhatoriginal.spring.model.User;
import com.github.nhatoriginal.spring.repository.UserRepository;
import com.github.nhatoriginal.spring.security.CustomUserDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;

import java.util.UUID;

@Service
public class UserService implements UserDetailsService {

private final UserRepository userRepository;

public UserService(UserRepository userRepository) {
Expand All @@ -26,7 +23,7 @@ public UserService(UserRepository userRepository) {
public UserDetails loadUserByUsername(String email) {
User user = userRepository.findByEmail(email);
if (user == null) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "email does not exists");
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Email không tồn tại");
}
return new CustomUserDetails(user);
}
Expand All @@ -36,7 +33,7 @@ public User findByEmail(String email) {
}

public User getUserById(String id) {
return userRepository.findById(UUID.fromString(id)).orElse(null);
return userRepository.findById(UUID.fromString(id))
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Người dùng không tồn tại"));
}

}
Loading