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.entities.Moderator;
|
---|
6 | import it.finki.charitable.repository.DonationPostRepository;
|
---|
7 | import org.springframework.data.domain.Page;
|
---|
8 | import org.springframework.data.domain.PageRequest;
|
---|
9 | import org.springframework.data.domain.Pageable;
|
---|
10 | import org.springframework.data.domain.Sort;
|
---|
11 | import org.springframework.stereotype.Service;
|
---|
12 |
|
---|
13 | import java.util.List;
|
---|
14 |
|
---|
15 | @Service
|
---|
16 | public class DonationPostService {
|
---|
17 |
|
---|
18 | private final DonationPostRepository donationPostRepository;
|
---|
19 |
|
---|
20 | public DonationPostService(DonationPostRepository donationPostRepository) {
|
---|
21 | this.donationPostRepository = donationPostRepository;
|
---|
22 | }
|
---|
23 |
|
---|
24 | public DonationPost save(DonationPost donationPost) {
|
---|
25 | return donationPostRepository.save(donationPost);
|
---|
26 | }
|
---|
27 |
|
---|
28 | public DonationPost getById(Long id) {
|
---|
29 | if(donationPostRepository.existsById(id)) {
|
---|
30 | return donationPostRepository.getById(id);
|
---|
31 | }
|
---|
32 |
|
---|
33 | return null;
|
---|
34 | }
|
---|
35 |
|
---|
36 | public List<DonationPost> findAll() {
|
---|
37 | return donationPostRepository.findAll();
|
---|
38 | }
|
---|
39 |
|
---|
40 | public List<DonationPost> findAllByUser(AppUser user) {
|
---|
41 | return donationPostRepository.findAllByUser(user);
|
---|
42 | }
|
---|
43 |
|
---|
44 | public List<DonationPost> findAllByApproved(Boolean approved) {
|
---|
45 | return donationPostRepository.findAllByApproved(approved);
|
---|
46 | }
|
---|
47 |
|
---|
48 | public List<DonationPost> findAllByModerator(Moderator moderator) {
|
---|
49 | return donationPostRepository.findAllByModerator(moderator);
|
---|
50 | }
|
---|
51 |
|
---|
52 | public void delete(DonationPost donationPost) {
|
---|
53 | donationPostRepository.delete(donationPost);
|
---|
54 | }
|
---|
55 |
|
---|
56 | public Page<DonationPost> findPaginated(int pageNo, int pageSize, String sort, String order, boolean approved) {
|
---|
57 | Sort s = Sort.by(sort);
|
---|
58 | s = order.equals("asc") ? s.ascending() : s.descending();
|
---|
59 | Pageable pageable = PageRequest.of(pageNo - 1, pageSize, s);
|
---|
60 | return donationPostRepository.findAllByApproved(pageable, approved);
|
---|
61 | }
|
---|
62 | }
|
---|