Skip to content

Commit

Permalink
Merge pull request #95 from NTOU-Auction/development
Browse files Browse the repository at this point in the history
準備發布1.0.3版本
  • Loading branch information
wei-chun-Nien authored Jan 3, 2024
2 parents fcc1b39 + e0cb93f commit 7a91d81
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,13 @@ List<Product> getFavorite() {
Set<Long> favoriteProductIds = userService.getFavoriteProducts(userService.findByUsername(userIdentity.getUsername()).getId());
List<Product> favoriteProducts = new ArrayList<>();
for (Long favoriteProductId : favoriteProductIds) {
favoriteProducts.add(productService.getID(favoriteProductId));
if(!productService.getID(favoriteProductId).getVisible()){
favoriteProductIds.remove(favoriteProductId);
}else{
favoriteProducts.add(productService.getID(favoriteProductId));
}
}
userService.setFavoriteProducts(userService.findByUsername(userIdentity.getUsername()).getId(),favoriteProductIds);
return favoriteProducts;
}

Expand Down Expand Up @@ -86,32 +91,36 @@ ResponseEntity<Map<String, String>> removeFavorite(@Valid @RequestBody FavoriteR
@PatchMapping("/users")
public ResponseEntity<Map<String, String>> signUp(@Valid @RequestBody SignupRequest request) {
String successMessage = "成功更新";
String usernameDuplicatedMessage = "更新失敗,輸入的帳號已被其他人使用!";
String usernameNotAllowedMessage = "更新失敗,不可更新使用者帳號!";
String emailDuplicatedMessage = "更新失敗,輸入的電子信箱已被其他人使用!";
String emailAndUsernameDuplicatedMessage = "更新失敗,輸入的帳號及電子信箱皆已被其他人使用!";
//String emailAndUsernameDuplicatedMessage = "更新失敗,輸入的帳號及電子信箱皆已被其他人使用!";
String passwordMessage = "至少需要8位密碼,且不超過128位";
Map<String, String> successResponse = Collections.singletonMap("message", successMessage);
Map<String, String> usernameDuplicatedResponse = Collections.singletonMap("message", usernameDuplicatedMessage);
Map<String, String> usernameNotAllowedResponse = Collections.singletonMap("message", usernameNotAllowedMessage);
Map<String, String> emailDuplicatedResponse = Collections.singletonMap("message", emailDuplicatedMessage);
Map<String, String> emailAndUsernameDuplicatedResponse = Collections.singletonMap("message", emailAndUsernameDuplicatedMessage);
//Map<String, String> emailAndUsernameDuplicatedResponse = Collections.singletonMap("message", emailAndUsernameDuplicatedMessage);
Map<String, String> passwordResponse = Collections.singletonMap("message", passwordMessage);
User user = userService.findByUsername(userIdentity.getUsername());
user.setEmail(request.getEmail());
user.setUsername(request.getUsername());
user.setName(request.getName());
//user.setEmail(request.getEmail());
//user.setUsername(request.getUsername());
if(!Objects.equals(request.getUsername(), user.getUsername())){
return ResponseEntity.badRequest().body(usernameNotAllowedResponse);
}
if(request.getPassword().length() < 8 || request.getPassword().length() > 128){
return ResponseEntity.badRequest().body(passwordResponse);
}
if(!userService.isEmailNonExist(request.getEmail()) && !Objects.equals(request.getEmail(), user.getEmail())){
return ResponseEntity.badRequest().body(emailDuplicatedResponse);
}
user.setName(request.getName());
user.setHashedPassword(userService.getPasswordEncoder().encode(request.getPassword()));
if(!userService.isUsernameNonExist(request.getUsername())){
/*if(!userService.isUsernameNonExist(request.getUsername())){
if(!userService.isEmailNonExist(request.getEmail())){
return ResponseEntity.badRequest().body(emailAndUsernameDuplicatedResponse);
}
return ResponseEntity.badRequest().body(usernameDuplicatedResponse);
}
if(!userService.isEmailNonExist(request.getEmail())){
return ResponseEntity.badRequest().body(emailDuplicatedResponse);
}
}*/
user.setEmail(request.getEmail());
userService.update(user);
return ResponseEntity.ok(successResponse);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ public Set<Long> getFavoriteProducts(Long userId) {
}
}

public void setFavoriteProducts(Long userId, Set<Long> newFavoriteProducts) {
if (repository.findById(userId).isPresent()) {
User user = repository.findById(userId).get();
user.setFavoriteProducts(newFavoriteProducts);
repository.save(user);
}
}

public boolean addFavoriteProducts(Long userId, Long productId) {
if (repository.findById(userId).isPresent()) {
User user = repository.findById(userId).get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ ResponseEntity<Map<String, String>> bidProduct(@Valid @RequestBody BidRequest re
Map<String, String> successMessage = Collections.singletonMap("message", "成功出價");
Map<String, String> failMessage = Collections.singletonMap("message", "出價不合理,出價需比當前最高價高" + productService.getID(request.getProductID()).getBidIncrement());
Map<String, String> expired = Collections.singletonMap("message", "競標已結束");
Map<String, String> violate = Collections.singletonMap("message", "不可以出價自己的商品");

if(Objects.equals(userService.findByUsername(userIdentity.getUsername()).getId(), productService.getID(request.getProductID()).getSellerID())){
return ResponseEntity.badRequest().body(violate);
}


LocalDateTime now = LocalDateTime.now();
if (!now.isBefore(productService.getID(request.getProductID()).getFinishTime())) {
Expand All @@ -157,6 +163,12 @@ ResponseEntity<Map<String, String>> buyProduct(@Valid @RequestBody BuyProductReq
Map<String, String> errorMessage = Collections.singletonMap("message", "只能將不二價商品加入購物車");
Map<String, String> productNotExistMessage = Collections.singletonMap("message", "商品不存在或無法購買");

Map<String, String> violate = Collections.singletonMap("message", "不可以買自己的商品");

if(Objects.equals(userService.findByUsername(userIdentity.getUsername()).getId(), productService.getID(request.getProductID()).getSellerID())){
return ResponseEntity.badRequest().body(violate);
}

// 商品是否存在
if (productService.getID(request.getProductID()) == null) {
return ResponseEntity.badRequest().body(productNotExistMessage);
Expand Down

0 comments on commit 7a91d81

Please sign in to comment.