source: phonelux-backend/src/main/java/finki/it/phoneluxbackend/services/OfferReportService.java

Last change on this file was 47f4eaf, checked in by Marko <Marko@…>, 2 years ago

Final features implemented

  • Property mode set to 100644
File size: 2.7 KB
Line 
1package finki.it.phoneluxbackend.services;
2
3import finki.it.phoneluxbackend.entities.OfferReport;
4import finki.it.phoneluxbackend.entities.PhoneOffer;
5import finki.it.phoneluxbackend.entities.User;
6import finki.it.phoneluxbackend.repositories.OfferReportRepository;
7import finki.it.phoneluxbackend.repositories.PhoneOfferRepository;
8import finki.it.phoneluxbackend.repositories.UserRepository;
9import lombok.AllArgsConstructor;
10import org.springframework.http.ResponseEntity;
11import org.springframework.stereotype.Service;
12
13import java.time.LocalDateTime;
14import java.util.List;
15import java.util.stream.Collectors;
16
17@AllArgsConstructor
18@Service
19public class OfferReportService {
20 private final OfferReportRepository offerReportRepository;
21 private final PhoneOfferRepository phoneOfferRepository;
22 private final UserRepository userRepository;
23
24 public ResponseEntity<Object> reportOffer(Long offerId, Long userId) {
25 boolean exists = phoneOfferRepository.findById(offerId).isPresent();
26
27 if(!exists){
28 return ResponseEntity.badRequest().body("Понудата не постои");
29 }
30
31 exists = userRepository.findById(userId).isPresent();
32
33 if(!exists){
34 return ResponseEntity.badRequest().body("Корисникот не постои");
35 }
36
37 User user = userRepository.findById(userId).get();
38 PhoneOffer offer = phoneOfferRepository.findById(offerId).get();
39
40 exists = offerReportRepository.findOfferReportByPhoneOffer(offer).isPresent();
41 OfferReport offerReport;
42 if(exists)
43 {
44 offerReport = offerReportRepository.findOfferReportByPhoneOffer(offer).get();
45 offerReport.setTimes_reported(offerReport.getTimes_reported()+1);
46 offerReport.setReportedAt(LocalDateTime.now());
47 offerReport.setReportedBy(user.getEmail());
48 }
49 else{
50 offerReport = new OfferReport(offer, 1, LocalDateTime.now(), user.getEmail());
51 }
52
53 offerReportRepository.save(offerReport);
54
55 return ResponseEntity.ok().build();
56 }
57
58 public List<OfferReport> getAllOfferReports() {
59 return offerReportRepository.findAll();
60 }
61
62 public ResponseEntity<Object> removeOfferReport(Long offerReportId) {
63 boolean exists = offerReportRepository.findById(offerReportId).isPresent();
64
65 if(!exists){
66 return ResponseEntity.badRequest().body("Пријавата не постои");
67 }
68
69 offerReportRepository.deleteById(offerReportId);
70
71 return ResponseEntity.ok().build();
72 }
73
74 public ResponseEntity<Object> removeAllOfferReports() {
75 offerReportRepository.deleteAll();
76 return ResponseEntity.ok().build();
77 }
78}
Note: See TracBrowser for help on using the repository browser.