1 | package it.finki.charitable.services;
|
---|
2 |
|
---|
3 | import it.finki.charitable.entities.AppUser;
|
---|
4 | import it.finki.charitable.entities.DonationPost;
|
---|
5 | import it.finki.charitable.repository.DonationPostRepository;
|
---|
6 | import org.springframework.data.domain.Page;
|
---|
7 | import org.springframework.data.domain.PageRequest;
|
---|
8 | import org.springframework.data.domain.Pageable;
|
---|
9 | import org.springframework.data.domain.Sort;
|
---|
10 | import org.springframework.stereotype.Service;
|
---|
11 |
|
---|
12 | import java.util.List;
|
---|
13 |
|
---|
14 | @Service
|
---|
15 | public class DonationPostService {
|
---|
16 |
|
---|
17 | private final DonationPostRepository donationPostRepository;
|
---|
18 |
|
---|
19 | public DonationPostService(DonationPostRepository donationPostRepository) {
|
---|
20 | this.donationPostRepository = donationPostRepository;
|
---|
21 | }
|
---|
22 |
|
---|
23 | public DonationPost save(DonationPost donationPost) {
|
---|
24 | return donationPostRepository.save(donationPost);
|
---|
25 | }
|
---|
26 |
|
---|
27 | public DonationPost getById(Long id) {
|
---|
28 | if(donationPostRepository.existsById(id)) {
|
---|
29 | return donationPostRepository.getById(id);
|
---|
30 | }
|
---|
31 |
|
---|
32 | return null;
|
---|
33 | }
|
---|
34 |
|
---|
35 | public List<DonationPost> findAll() {
|
---|
36 | return donationPostRepository.findAll();
|
---|
37 | }
|
---|
38 |
|
---|
39 | public List<DonationPost> findAllByUser(AppUser user) {
|
---|
40 | return donationPostRepository.findAllByUser(user);
|
---|
41 | }
|
---|
42 |
|
---|
43 | public void delete(DonationPost donationPost) {
|
---|
44 | donationPostRepository.delete(donationPost);
|
---|
45 | }
|
---|
46 |
|
---|
47 | public Page<DonationPost> findPaginated(int pageNo, int pageSize, String sort) {
|
---|
48 | Pageable pageable = PageRequest.of(pageNo - 1, pageSize, Sort.by(sort));
|
---|
49 | return donationPostRepository.findAll(pageable);
|
---|
50 | }
|
---|
51 | }
|
---|