source: src/main/java/it/finki/charitable/services/DonationPostService.java@ ee0e297

Last change on this file since ee0e297 was ee0e297, checked in by KostaFortumanov <kfortumanov@…>, 3 years ago

bug fix

  • Property mode set to 100644
File size: 2.2 KB
Line 
1package it.finki.charitable.services;
2
3import it.finki.charitable.entities.AppUser;
4import it.finki.charitable.entities.DonationPost;
5import it.finki.charitable.entities.Moderator;
6import it.finki.charitable.repository.DonationPostRepository;
7import org.springframework.data.domain.Page;
8import org.springframework.data.domain.PageRequest;
9import org.springframework.data.domain.Pageable;
10import org.springframework.data.domain.Sort;
11import org.springframework.stereotype.Service;
12
13import java.util.List;
14
15@Service
16public 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 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);
53 }
54
55 public void delete(DonationPost donationPost) {
56 donationPostRepository.delete(donationPost);
57 }
58
59 public Page<DonationPost> findPaginated(int pageNo, int pageSize, String sort, String order, boolean approved) {
60 Sort s = Sort.by(sort);
61 s = order.equals("asc") ? s.ascending() : s.descending();
62 Pageable pageable = PageRequest.of(pageNo - 1, pageSize, s);
63 return donationPostRepository.findAllByApproved(pageable, approved);
64 }
65}
Note: See TracBrowser for help on using the repository browser.