Browse Source

feat: 完成了给店铺打分的功能

DingXiaoYu 2 years ago
parent
commit
d6ca65e894

+ 11 - 6
src/main/java/com/seecoder/BlueWhale/controller/StoreController.java

@@ -2,12 +2,7 @@ package com.seecoder.BlueWhale.controller;
 
 import java.util.List;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.PathVariable;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 
 import com.seecoder.BlueWhale.service.StoreService;
 import com.seecoder.BlueWhale.vo.ResultVO;
@@ -34,4 +29,14 @@ public class StoreController {
     public ResultVO<List<StoreVO>> getAllStores(){
         return ResultVO.buildSuccess(storeService.getAllStores());
     }
+
+    @PostMapping("/star")
+    public ResultVO<Boolean> starStores(@RequestParam("storeId")Integer storeId,@RequestParam("rate")Integer rate){
+        return ResultVO.buildSuccess(storeService.starStores(storeId,rate));
+    }
+
+    @GetMapping("/star")
+    public ResultVO<Integer> getStoreStar(@RequestParam("storeId")Integer storeId){
+        return ResultVO.buildSuccess(storeService.getStoreStar(storeId));
+    }
 }

+ 4 - 0
src/main/java/com/seecoder/BlueWhale/exception/BlueWhaleException.java

@@ -37,4 +37,8 @@ public class BlueWhaleException extends RuntimeException{
     public static BlueWhaleException productNotExists(){
         return new BlueWhaleException("商品不存在!");
     }
+
+    public static BlueWhaleException starAlreadyExists(){
+        return new BlueWhaleException("已经给店铺打过分!");
+    }
 }

+ 32 - 0
src/main/java/com/seecoder/BlueWhale/po/StoreStar.java

@@ -0,0 +1,32 @@
+package com.seecoder.BlueWhale.po;
+
+import lombok.Getter;
+import lombok.NoArgsConstructor;
+import lombok.Setter;
+
+import javax.persistence.*;
+
+@Getter
+@Setter
+@NoArgsConstructor
+@Entity
+public class StoreStar {
+
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
+    @Id
+    @Column(name = "id")
+    private Integer id;
+
+    @Basic
+    @Column(name = "user_id")
+    private Integer userId;
+
+    @Basic
+    @Column(name = "store_id")
+    private Integer storeId;
+
+    @Basic
+    @Column(name = "rate")
+    private Integer rate;
+
+}

+ 8 - 0
src/main/java/com/seecoder/BlueWhale/repository/StoreStarRepository.java

@@ -0,0 +1,8 @@
+package com.seecoder.BlueWhale.repository;
+
+import com.seecoder.BlueWhale.po.StoreStar;
+import org.springframework.data.jpa.repository.JpaRepository;
+
+public interface StoreStarRepository extends JpaRepository<StoreStar, Integer> {
+    StoreStar findByUserIdAndStoreId(Integer userId,Integer storeId);
+}

+ 4 - 0
src/main/java/com/seecoder/BlueWhale/service/StoreService.java

@@ -10,4 +10,8 @@ public interface StoreService {
     StoreVO getStore(Integer id);
 
     List<StoreVO> getAllStores();
+
+    Boolean starStores(Integer storeId, Integer rate);
+
+    Integer getStoreStar(Integer storeId);
 }

+ 31 - 1
src/main/java/com/seecoder/BlueWhale/serviceImpl/StoreServiceImpl.java

@@ -3,6 +3,9 @@ package com.seecoder.BlueWhale.serviceImpl;
 import java.util.List;
 import java.util.stream.Collectors;
 
+import com.seecoder.BlueWhale.po.StoreStar;
+import com.seecoder.BlueWhale.repository.StoreStarRepository;
+import com.seecoder.BlueWhale.util.SecurityUtil;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
@@ -18,6 +21,12 @@ public class StoreServiceImpl implements StoreService {
     @Autowired
     StoreRepository storeRepository;
 
+    @Autowired
+    SecurityUtil securityUtil;
+
+    @Autowired
+    StoreStarRepository storeStarRepository;
+
     @Override
     public Boolean create(StoreVO storeVO) {
         Store store = storeRepository.findByName(storeVO.getName());
@@ -44,5 +53,26 @@ public class StoreServiceImpl implements StoreService {
     public List<StoreVO> getAllStores() {
         return storeRepository.findAll().stream().map(Store::toVO).collect(Collectors.toList());
     }
-    
+
+    @Override
+    public Boolean starStores(Integer storeId, Integer rate) {
+        int userId=securityUtil.getCurrentUser().getId();
+        if (storeStarRepository.findByUserIdAndStoreId(userId,storeId)!=null){
+            throw BlueWhaleException.starAlreadyExists();
+        }
+        StoreStar storeStar=new StoreStar();
+        storeStar.setRate(rate);
+        storeStar.setStoreId(storeId);
+        storeStar.setUserId(userId);
+        storeStarRepository.save(storeStar);
+        return true;
+    }
+
+    @Override
+    public Integer getStoreStar(Integer storeId) {
+        int userId=securityUtil.getCurrentUser().getId();
+        StoreStar storeStar=storeStarRepository.findByUserIdAndStoreId(userId,storeId);
+        return storeStar==null?0:storeStar.getRate();
+    }
+
 }