1
0

StoreServiceImpl.java 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 Boolean update(StoreVO storeVO) {
  29. Store store = storeRepository.findById(storeVO.getId()).orElse(null);
  30. if (store == null) {
  31. throw BlueWhaleException.storeNotExists();
  32. }
  33. store.setName(storeVO.getName());
  34. store.setLogoUrl(storeVO.getLogoUrl());
  35. store.setLocation(storeVO.getLocation());
  36. store.setRating(storeVO.getRating());
  37. store.setNumber(storeVO.getNumber());
  38. storeRepository.save(store);
  39. return true;
  40. }
  41. @Override
  42. public StoreVO getStore(Integer id) {
  43. Store store = storeRepository.findById(id).orElse(null);
  44. if (store == null) {
  45. throw BlueWhaleException.storeNotExists();
  46. }
  47. return store.toVO();
  48. }
  49. @Override
  50. public List<StoreVO> getAllStores() {
  51. return storeRepository.findAll().stream().map(Store::toVO).collect(Collectors.toList());
  52. }
  53. }