1 | package finki.it.terapijamkbackend.spring.services;
|
---|
2 |
|
---|
3 | import finki.it.terapijamkbackend.spring.entities.Coupon;
|
---|
4 | import finki.it.terapijamkbackend.spring.repositories.CouponRepository;
|
---|
5 | import jakarta.persistence.EntityNotFoundException;
|
---|
6 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
7 | import org.springframework.stereotype.Service;
|
---|
8 |
|
---|
9 | import java.util.List;
|
---|
10 | import java.util.Optional;
|
---|
11 | import java.util.stream.Collectors;
|
---|
12 |
|
---|
13 | @Service
|
---|
14 | public 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 | }
|
---|