1 | package finki.it.phoneluxbackend.services;
|
---|
2 |
|
---|
3 | import finki.it.phoneluxbackend.entities.OfferReport;
|
---|
4 | import finki.it.phoneluxbackend.entities.PhoneOffer;
|
---|
5 | import finki.it.phoneluxbackend.entities.User;
|
---|
6 | import finki.it.phoneluxbackend.repositories.OfferReportRepository;
|
---|
7 | import finki.it.phoneluxbackend.repositories.PhoneOfferRepository;
|
---|
8 | import finki.it.phoneluxbackend.repositories.UserRepository;
|
---|
9 | import lombok.AllArgsConstructor;
|
---|
10 | import org.springframework.http.ResponseEntity;
|
---|
11 | import org.springframework.stereotype.Service;
|
---|
12 |
|
---|
13 | import java.time.LocalDateTime;
|
---|
14 | import java.util.List;
|
---|
15 | import java.util.stream.Collectors;
|
---|
16 |
|
---|
17 | @AllArgsConstructor
|
---|
18 | @Service
|
---|
19 | public 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 | }
|
---|