1
0

StoreServiceImpl.java 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package com.seecoder.BlueWhale.serviceImpl;
  2. import com.seecoder.BlueWhale.exception.BlueWhaleException;
  3. import com.seecoder.BlueWhale.po.Store;
  4. import com.seecoder.BlueWhale.repository.StoreRepository;
  5. import com.seecoder.BlueWhale.service.StoreService;
  6. import com.seecoder.BlueWhale.vo.StoreVO;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.stereotype.Service;
  9. import java.util.List;
  10. import java.util.stream.Collectors;
  11. @Service
  12. public class StoreServiceImpl implements StoreService {
  13. @Autowired
  14. StoreRepository storeRepository;
  15. @Override
  16. public Boolean create(StoreVO storeVO) {
  17. Store store = storeRepository.findByName(storeVO.getName());
  18. if (store != null) {
  19. throw BlueWhaleException.nameAlreadyExists();
  20. }
  21. Store newStore = storeVO.toPO();
  22. newStore.setRating(0.0);
  23. newStore.setNumber(0);
  24. storeRepository.save(newStore);
  25. return true;
  26. }
  27. @Override
  28. public StoreVO getStore(Integer id) {
  29. Store store = storeRepository.findById(id).orElse(null);
  30. if (store == null) {
  31. throw BlueWhaleException.storeNotExists();
  32. }
  33. return store.toVO();
  34. }
  35. @Override
  36. public List<StoreVO> getAllStores() {
  37. return storeRepository.findAll().stream().map(Store::toVO).collect(Collectors.toList());
  38. }
  39. }