source: src/main/java/finki/it/terapijamkbackend/spring/services/CouponsService.java

Last change on this file was 43c9090, checked in by macagaso <gasoskamarija@…>, 5 weeks ago

Updated version

  • Property mode set to 100644
File size: 1.4 KB
Line 
1package finki.it.terapijamkbackend.spring.services;
2
3import finki.it.terapijamkbackend.spring.entities.Coupon;
4import finki.it.terapijamkbackend.spring.repositories.CouponRepository;
5import jakarta.persistence.EntityNotFoundException;
6import org.springframework.beans.factory.annotation.Autowired;
7import org.springframework.stereotype.Service;
8
9import java.util.List;
10import java.util.Optional;
11import java.util.stream.Collectors;
12
13@Service
14public class CouponsService {
15 @Autowired
16 private CouponRepository couponRepository;
17
18 public Coupon saveCoupon(Coupon newsItem) {
19 return couponRepository.save(newsItem);
20 }
21
22 public List<Coupon> getAllCoupons(){
23 return couponRepository.findAll();
24 }
25 public void deleteById(String id) {
26 Long temp=Long.parseLong(id);
27 if (couponRepository.existsById(temp)) {
28 couponRepository.deleteById(temp);
29 } else {
30 throw new EntityNotFoundException("Entity with id " + id + " not found");
31 }
32 }
33 public Optional<Coupon> findByIdentifier(String identifier) {
34 return couponRepository.findByTitle(identifier);
35 }
36 public List<String> getCouponNames() {
37 return couponRepository.findAll().stream()
38 .map(Coupon::getCode)
39 .collect(Collectors.toList());
40 }
41
42 public void save(Coupon coupon) {
43 couponRepository.save(coupon);
44 }
45}
Note: See TracBrowser for help on using the repository browser.