Gudge-ZL пре 2 година
родитељ
комит
d3e85ffa2b

+ 37 - 0
src/main/java/com/seecoder/BlueWhale/Controller/StoreController.java

@@ -0,0 +1,37 @@
+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 com.seecoder.BlueWhale.Service.StoreService;
+import com.seecoder.BlueWhale.VO.ResultVO;
+import com.seecoder.BlueWhale.VO.StoreVO;
+
+@RestController
+@RequestMapping("/api/store")
+public class StoreController {
+    
+    @Autowired
+    StoreService storeService;
+
+    @PostMapping("/create")
+    public ResultVO<Boolean> create(@RequestBody StoreVO storeVO){
+        return ResultVO.buildSuccess(storeService.create(storeVO));
+    }
+
+    @GetMapping("{id}")
+    public ResultVO<StoreVO> getStore(@PathVariable Integer id){
+        return ResultVO.buildSuccess(storeService.getStore(id));
+    }
+
+    @GetMapping("all")
+    public ResultVO<List<StoreVO>> getAllStores(){
+        return ResultVO.buildSuccess(storeService.getAllStores());
+    }
+}

+ 8 - 0
src/main/java/com/seecoder/BlueWhale/Exception/BlueWhaleException.java

@@ -25,4 +25,12 @@ public class BlueWhaleException extends RuntimeException{
     public static BlueWhaleException fileUploadFail(){
         return new BlueWhaleException("文件上传失败!");
     }
+
+    public static BlueWhaleException nameAlreadyExists(){
+        return new BlueWhaleException("名称已经存在!");
+    }
+
+    public static BlueWhaleException storeNotExists(){
+        return new BlueWhaleException("店铺不存在!");
+    }
 }

+ 12 - 0
src/main/java/com/seecoder/BlueWhale/Mapper/StoreMapper.java

@@ -0,0 +1,12 @@
+package com.seecoder.BlueWhale.Mapper;
+
+import java.util.Optional;
+
+import org.springframework.data.jpa.repository.JpaRepository;
+
+import com.seecoder.BlueWhale.PO.Store;
+
+public interface StoreMapper extends JpaRepository<Store, Integer> {
+    Store findByName(String name);
+    Optional<Store> findById(Integer id);
+}

+ 52 - 0
src/main/java/com/seecoder/BlueWhale/PO/Store.java

@@ -0,0 +1,52 @@
+package com.seecoder.BlueWhale.PO;
+
+import javax.persistence.*;
+
+import com.seecoder.BlueWhale.VO.StoreVO;
+
+import lombok.Getter;
+import lombok.NoArgsConstructor;
+import lombok.Setter;
+
+@Getter
+@Setter
+@NoArgsConstructor
+@Entity
+public class Store {
+    
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
+    @Id
+    @Column(name = "id")
+    private Integer id;
+
+    @Basic
+    @Column(name = "name")
+    private String name;
+
+    @Basic
+    @Column(name = "logo_url")
+    private String logoUrl;
+
+    @Basic
+    @Column(name = "rating")
+    private Double rating;
+
+    @Basic
+    @Column(name = "number")
+    private Integer number;
+
+    @Basic
+    @Column(name = "location")
+    private String location;
+
+    public StoreVO toVO(){
+        StoreVO storeVO=new StoreVO();
+        storeVO.setId(this.id);
+        storeVO.setLocation(this.location);
+        storeVO.setLogoUrl(this.logoUrl);
+        storeVO.setName(this.name);
+        storeVO.setNumber(this.number);
+        storeVO.setRating(this.rating);
+        return storeVO;
+    }
+}

+ 13 - 0
src/main/java/com/seecoder/BlueWhale/Service/StoreService.java

@@ -0,0 +1,13 @@
+package com.seecoder.BlueWhale.Service;
+
+import java.util.List;
+
+import com.seecoder.BlueWhale.VO.StoreVO;
+
+public interface StoreService {
+    Boolean create(StoreVO storeVO);
+
+    StoreVO getStore(Integer id);
+
+    List<StoreVO> getAllStores();
+}

+ 45 - 0
src/main/java/com/seecoder/BlueWhale/ServiceImpl/StoreServiceImpl.java

@@ -0,0 +1,45 @@
+package com.seecoder.BlueWhale.ServiceImpl;
+
+import java.util.List;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import com.seecoder.BlueWhale.Exception.BlueWhaleException;
+import com.seecoder.BlueWhale.Mapper.StoreMapper;
+import com.seecoder.BlueWhale.PO.Store;
+import com.seecoder.BlueWhale.Service.StoreService;
+import com.seecoder.BlueWhale.VO.StoreVO;
+
+@Service
+public class StoreServiceImpl implements StoreService {
+
+    @Autowired
+    StoreMapper storeMapper;
+
+    @Override
+    public Boolean create(StoreVO storeVO) {
+        Store store = storeMapper.findByName(storeVO.getName());
+        if (store != null) {
+            throw BlueWhaleException.nameAlreadyExists();
+        }
+        Store newStore = storeVO.toPO();
+        storeMapper.save(newStore);
+        return true;
+    }
+
+    @Override
+    public StoreVO getStore(Integer id) {
+        Store store = storeMapper.findById(id).orElse(null);
+        if (store == null) {
+            throw BlueWhaleException.storeNotExists();
+        }
+        return store.toVO();
+    }
+
+    @Override
+    public List<StoreVO> getAllStores() {
+        return storeMapper.findAll().stream().map(Store::toVO).toList();
+    }
+    
+}

+ 35 - 0
src/main/java/com/seecoder/BlueWhale/VO/StoreVO.java

@@ -0,0 +1,35 @@
+package com.seecoder.BlueWhale.VO;
+
+import com.seecoder.BlueWhale.PO.Store;
+
+import lombok.Getter;
+import lombok.NoArgsConstructor;
+import lombok.Setter;
+
+@Getter
+@Setter
+@NoArgsConstructor
+public class StoreVO {
+    private Integer id;
+    
+    private String name;
+
+    private String logoUrl;
+
+    private Double rating;
+
+    private Integer number;
+
+    private String location;
+
+    public Store toPO(){
+        Store store=new Store();
+        store.setId(this.id);
+        store.setLocation(this.location);
+        store.setLogoUrl(this.logoUrl);
+        store.setName(this.name);
+        store.setNumber(this.number);
+        store.setRating(this.rating);
+        return store;
+    }
+}