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

將product合併到development #64

Merged
merged 13 commits into from
Dec 20, 2023
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
117 changes: 99 additions & 18 deletions src/main/java/ntou/auction/spring/controller/ProductController.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;


@RestController
Expand All @@ -35,25 +36,17 @@ public ProductController(ProductService productService, UserIdentity userIdentit
}


@GetMapping("/product")
@GetMapping("/product/name/{name}")
@ResponseBody
public List<Product>getProductName(@Valid @RequestBody ProductRequestGet request) {

long type =Integer.parseInt(request.getSearchType());

if(type == 1) { //find by name
String pn = request.getProductName();
return productService.findByProductName(pn);
}

else if(type == 2){ //find by classification
String pt = request.getProductType();
return productService.findByProductClassification(pt);
}

return productService.list();
public List<Product>getProductName(@PathVariable String name ) {
return productService.findByProductName(name);
}

@GetMapping("/product/classification/{classification}")
@ResponseBody
public List<Product>getProductClassification(@PathVariable String classification) {
return productService.findByProductClassification(classification);
}

@GetMapping("/products")
@ResponseBody
Expand Down Expand Up @@ -85,6 +78,7 @@ ResponseEntity<Map<String,String>> postProduct(@Valid @RequestBody PostFixedPric
product.setBidIncrement(null);
product.setProductAmount(request.getProductAmount());
product.setIsAuction(false);
product.setVisible(true);
product.setSellerID(userService.findByUsername(userIdentity.getUsername()).getId());
product.setSellerName(userIdentity.getUsername());

Expand Down Expand Up @@ -113,6 +107,7 @@ ResponseEntity<Map<String,String>> postProduct(@Valid @RequestBody PostNonFixedP
product.setBidIncrement(request.getBidIncrement());
product.setProductAmount(1L);
product.setIsAuction(false);
product.setVisible(true);

LocalDateTime now = LocalDateTime.now();

Expand Down Expand Up @@ -153,12 +148,14 @@ ResponseEntity<Map<String,String>> bidProduct(@Valid @RequestBody BidRequest req
return ResponseEntity.ok(successMessage);
}



@PostMapping("/buy")
ResponseEntity<Map<String,String>> buyProduct(@Valid @RequestBody BuyProductRequest request){

Map<String,String> successMessage = Collections.singletonMap("message","成功加入購物車");
Map<String,String> notEnoughMessage = Collections.singletonMap("message","買太多嚕");
Map<String,String> errorMessage = Collections.singletonMap("message","你只能將不二價商品加入購物車");
Map<String,String> notEnoughMessage = Collections.singletonMap("message","商品剩餘數量不足");
Map<String,String> errorMessage = Collections.singletonMap("message","只能將不二價商品加入購物車");
Map<String, String> productNotExistMessage = Collections.singletonMap("message", "商品不存在或無法購買");

// 商品是否存在
Expand Down Expand Up @@ -206,4 +203,88 @@ List<Product> getProductInSellerCenter() {
return productService.findBySellerID(userService.findByUsername(userIdentity.getUsername()).getId());
}

@DeleteMapping("/{ID}")
ResponseEntity<Map<String,String>> deleteProduct(@PathVariable long ID){
Map<String,String> successMessage = Collections.singletonMap("message","成功刪除");
Map<String,String> failMessage = Collections.singletonMap("message","刪錯商品嚕");

Product p = productService.getID(ID);

if(!Objects.equals(userService.findByUsername(userIdentity.getUsername()).getId(), p.getSellerID())){
return ResponseEntity.badRequest().body(failMessage);
}
p.setProductAmount(0L);
p.setVisible(false);
productService.store(p);

return ResponseEntity.ok(successMessage);
}

@PutMapping("/fixedproduct/{ID}")
ResponseEntity<Map<String,String>> putFixedProduct(@PathVariable long ID , @Valid @RequestBody UpdateFixedPriceProductRequest request){

Map<String,String> successMessage = Collections.singletonMap("message","成功更新不二價商品");
Map<String,String> failMessage = Collections.singletonMap("message","更新錯商品嚕");

Product product = productService.getID(ID);
if(!Objects.equals(userService.findByUsername(userIdentity.getUsername()).getId(), product.getSellerID())){
return ResponseEntity.badRequest().body(failMessage);
}
product.setProductName(request.getProductName());
product.setProductDescription(request.getProductDescription());
product.setProductImage(request.getProductImage());
product.setProductType(request.getProductType());
product.setCurrentPrice(request.getCurrentPrice());
product.setProductAmount(request.getProductAmount());

productService.store(product);
return ResponseEntity.ok(successMessage);
}

@PutMapping("/nonfixedproduct/{ID}")
ResponseEntity<Map<String,String>> putNonFixedProduct(@PathVariable long ID , @Valid @RequestBody UpdateNonFixedPriceProductRequest request){

Map<String,String> successMessage = Collections.singletonMap("message","成功更新競標商品");
Map<String,String> failToPostponeAuction = Collections.singletonMap("message","延長競標截止時間失敗,因為有人得標嚕");
Map<String,String> fail = Collections.singletonMap("message","截止時間錯誤");
Map<String,String> failToSetUpsetPrice = Collections.singletonMap("message","底價不得更改,因為有人出價了");
Map<String,String> failToSetBidIncrement = Collections.singletonMap("message","每次增加金額不得更改,因為有人出價了");
Map<String,String> failMessage = Collections.singletonMap("message","更新錯商品嚕阿");
Product product = productService.getID(ID);

if(!Objects.equals(userService.findByUsername(userIdentity.getUsername()).getId(), product.getSellerID())){
return ResponseEntity.badRequest().body(failMessage);
}
product.setProductName(request.getProductName());
product.setProductDescription(request.getProductDescription());
product.setProductImage(request.getProductImage());
product.setProductType(request.getProductType());


Map<Long,Long> productMap= product.getBidInfo();
if(!productMap.isEmpty() && !Objects.equals(request.getUpsetPrice(), product.getUpsetPrice())){ //map不為空,有人出價過了。且更改的底價 != 原本底價
return ResponseEntity.badRequest().body(failToSetUpsetPrice);
}
product.setUpsetPrice(request.getUpsetPrice());

if(!productMap.isEmpty() && !Objects.equals(request.getBidIncrement(), product.getBidIncrement())){ //map不為空,有人出價過了。且被更改每口叫價
return ResponseEntity.badRequest().body(failToSetBidIncrement);
}
product.setBidIncrement(request.getBidIncrement());

LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime dateTime = LocalDateTime.parse(request.getFinishTime(), formatter);
if(!now.isBefore(dateTime)){
return ResponseEntity.badRequest().body(fail);
}
if(product.getIsAuction()) { //代表競標結束且有被加入購物車
return ResponseEntity.badRequest().body(failToPostponeAuction);
}

product.setFinishTime(dateTime);

productService.store(product);
return ResponseEntity.ok(successMessage);
}
}
4 changes: 4 additions & 0 deletions src/main/java/ntou/auction/spring/data/entity/Product.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ntou.auction.spring.data.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
Expand Down Expand Up @@ -44,6 +45,7 @@ public class Product extends AbstractEntity {

//followings are non-isFixedPrice feature

@JsonIgnore
@ElementCollection
@CollectionTable(name = "bidInfo")
private Map<Long,Long> bidInfo;
Expand All @@ -57,6 +59,8 @@ public class Product extends AbstractEntity {

private Boolean isAuction; //競標商品已經被加進購物車?

private Boolean visible;

@NotNull
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
private LocalDateTime updateTime;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package ntou.auction.spring.data.entity;

import jakarta.persistence.Column;
import jakarta.persistence.Lob;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.validator.constraints.Length;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class UpdateFixedPriceProductRequest {

@NotNull
@Length(min = 1, max = 128 , message = "商品名稱至多32個中文字")
private String productName;

@NotNull
@Min (value = 1,message = "價格須為正整數")
private Long currentPrice;


@Length(min = 1, max = 32)
private String productType;

@Length(min = 1, max = 20971520,message = "商品敘述過長")
private String productDescription;

@Lob
@Column(length = 5242880)
@Length(min = 1, max = 5242880 ,message = "圖片檔案過大,請重新上傳")
private String productImage;

@NotNull
@Min (value = 1,message = "商品至少一個")
private Long productAmount;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package ntou.auction.spring.data.entity;

import jakarta.persistence.Column;
import jakarta.persistence.Lob;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.validator.constraints.Length;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class UpdateNonFixedPriceProductRequest {

@NotNull (message="商品名稱不得為空")
@Length(min = 1, max = 128 , message = "商品名稱至多32個中文字")
private String productName;

@NotNull
@Min (value = 1,message = "價格須為正整數")
private Long upsetPrice; //lowest requested price

@NotNull
@Min (value = 1,message = "每口叫價須為正整數")
private Long bidIncrement;

@NotNull (message="商品數量不得為空")
private Long productAmount;

@NotNull
private String finishTime;

@Length(min = 1, max = 32)
private String productType;

@Length(min = 1, max = 20971520,message = "商品敘述過長")
private String productDescription;

@Lob
@Column(length = 5242880)
@Length(min = 1, max = 5242880 ,message = "圖片檔案過大,請重新上傳")
private String productImage;



}
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@ public interface ProductRepository extends JpaRepository<Product, Long>,

List <Product> findAllByIsFixedPriceFalseAndIsAuctionFalse();

List <Product> findAllByVisibleTrue();
Product findById(long id);

@Query("select p from Product p " +
"where p.productName like %?1%") //string-like
"where p.productName like %?1% and p.visible = true") //string-like
List<Product> findAllByFuzzyProductName(@Param("productName") String productName);
// ?1:productName

List<Product> findBySellerID(long ID);
List<Product> findBySellerIDAndVisibleTrue(long ID);

List<Product> findAllByProductType(String productType);
List<Product> findAllByProductTypeAndVisibleTrue(String productType);


}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public void delete(Long id) {
}
*/
public List<Product> list() {
return repository.findAll();
}
return repository.findAllByVisibleTrue();
} //browse homepage

public Product getID(Long id){
return repository.findById(id).orElse(null);
Expand Down Expand Up @@ -80,18 +80,23 @@ public void productAmountIncrease(Long id,Long increment){
product.setProductAmount(productAmount + increment);
this.store(product);
}
public void deleteProduct(Long id){
Product product = this.getID(id);
product.setVisible(false);
this.store(product);
}

public List<Product> findByProductName(String productName) {
return repository.findAllByFuzzyProductName(productName);
}

public List<Product> findByProductClassification(String productType){
return repository.findAllByProductType(productType);
return repository.findAllByProductTypeAndVisibleTrue(productType);
}

public List<Product> findByProductNonFixed(){
return repository.findAllByIsFixedPriceFalseAndIsAuctionFalse();
}

public List<Product> findBySellerID(Long sellerID){return repository.findBySellerID(sellerID);}//賣家中心
public List<Product> findBySellerID(Long sellerID){return repository.findBySellerIDAndVisibleTrue(sellerID);}//賣家中心
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void execute() {

for (Product product : productList) {
System.out.println(product.getId());
if (product.isExpired()) {
if (product.isExpired()) { //競標結束
Map<Long,Long> productMap= product.getBidInfo();


Expand All @@ -36,11 +36,6 @@ public void execute() {
product.setIsAuction(true);
productService.store(product);
}
else {
product.setIsAuction(true);
productService.store(product);
}

}
}
}
Expand Down
Loading