[3fc9e50] | 1 | package it.finki.charitable.services;
|
---|
| 2 |
|
---|
| 3 | import it.finki.charitable.entities.AppUser;
|
---|
| 4 | import it.finki.charitable.entities.DonationPost;
|
---|
[ab49338] | 5 | import it.finki.charitable.entities.Moderator;
|
---|
[3fc9e50] | 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 |
|
---|
[5306751] | 44 | public List<DonationPost> findAllByApproved(Boolean approved) {
|
---|
| 45 | return donationPostRepository.findAllByApproved(approved);
|
---|
| 46 | }
|
---|
| 47 |
|
---|
[ee0e297] | 48 | public Page<DonationPost> findAllByModerator(int pageNo, int pageSize, String sort, String order, Moderator moderator) {
|
---|
| 49 | Sort s = Sort.by(sort);
|
---|
| 50 | s = order.equals("asc") ? s.ascending() : s.descending();
|
---|
| 51 | Pageable pageable = PageRequest.of(pageNo - 1, pageSize, s);
|
---|
| 52 | return donationPostRepository.findAllByModerator(pageable, moderator);
|
---|
[ab49338] | 53 | }
|
---|
| 54 |
|
---|
[3fc9e50] | 55 | public void delete(DonationPost donationPost) {
|
---|
| 56 | donationPostRepository.delete(donationPost);
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[0c37625] | 59 | public Page<DonationPost> findPaginated(int pageNo, int pageSize, String sort, String order, boolean approved) {
|
---|
[276a8b6] | 60 | Sort s = Sort.by(sort);
|
---|
| 61 | s = order.equals("asc") ? s.ascending() : s.descending();
|
---|
| 62 | Pageable pageable = PageRequest.of(pageNo - 1, pageSize, s);
|
---|
[0c37625] | 63 | return donationPostRepository.findAllByApproved(pageable, approved);
|
---|
[3fc9e50] | 64 | }
|
---|
| 65 | }
|
---|