Ignore:
Timestamp:
09/04/21 12:17:17 (3 years ago)
Author:
KostaFortumanov <kfortumanov@…>
Branches:
master
Children:
0f4f552
Parents:
5306751
Message:

Dodadeno prijavuvanje na objavi

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/main/java/it/finki/charitable/controller/DonationPostController.java

    r5306751 rab49338  
    22
    33import it.finki.charitable.entities.*;
    4 import it.finki.charitable.services.DonationInformationService;
    5 import it.finki.charitable.services.DonationPostService;
    6 import it.finki.charitable.services.FundsCollectedService;
    7 import it.finki.charitable.services.UserService;
     4import it.finki.charitable.services.*;
    85import it.finki.charitable.util.FileUploadUtil;
    9 import org.springframework.data.domain.Page;
    106import org.springframework.format.annotation.DateTimeFormat;
    117import org.springframework.security.core.context.SecurityContextHolder;
     
    1612import org.springframework.web.multipart.MultipartFile;
    1713
    18 import java.io.File;
    1914import java.io.IOException;
    2015import java.time.LocalDate;
     
    3227    private final FundsCollectedService fundsCollectedService;
    3328    private final DonationInformationService donationInformationService;
    34 
    35     public DonationPostController(DonationPostService donationPostService, UserService userService, FundsCollectedService fundsCollectedService, DonationInformationService donationInformationService) {
     29    private final ReportPostService reportPostService;
     30    private final ReasonService reasonService;
     31
     32    public DonationPostController(DonationPostService donationPostService, UserService userService, FundsCollectedService fundsCollectedService, DonationInformationService donationInformationService, ReportPostService reportPostService, ReasonService reasonService) {
    3633        this.donationPostService = donationPostService;
    3734        this.userService = userService;
    3835        this.fundsCollectedService = fundsCollectedService;
    3936        this.donationInformationService = donationInformationService;
     37        this.reportPostService = reportPostService;
     38        this.reasonService = reasonService;
    4039    }
    4140
     
    140139            return "post";
    141140        }
    142         AppUser user = post.getUser();
    143         model.addAttribute("post", post);
    144         model.addAttribute("createdByFirstName", user.getFirstName());
    145         model.addAttribute("createdByLastName", user.getLastName());
    146         double total = post.getFundsCollected().stream().mapToDouble(FundsCollected::getFunds).sum();
    147         model.addAttribute("total", total);
     141
     142        if (post.getApproved() || (post.getUser().getUsername().equals(SecurityContextHolder.getContext().getAuthentication().getName()) && !post.getApproved())) {
     143            AppUser user = post.getUser();
     144            Moderator moderator = post.getModerator();
     145            model.addAttribute("post", post);
     146            model.addAttribute("createdByFirstName", user.getFirstName());
     147            model.addAttribute("createdByLastName", user.getLastName());
     148            if (moderator != null) {
     149                model.addAttribute("moderatorFirstName", moderator.getFirstName());
     150                model.addAttribute("moderatorLastName", moderator.getLastName());
     151            }
     152            double total = post.getFundsCollected().stream().mapToDouble(FundsCollected::getFunds).sum();
     153            model.addAttribute("total", total);
     154        } else {
     155            model.addAttribute("notFound", true);
     156        }
    148157        return "post";
    149158    }
    150 
    151 //    @RequestMapping("/deletePost")
    152 //    public String deletePost(@RequestParam Long postid) {
    153 //        DonationPost post = donationPostService.getById(postid);
    154 //        if (post.getUser().getUsername().equals(SecurityContextHolder.getContext().getAuthentication().getName())) {
    155 //            List<String> fileForDeletion = post.getPhotosForDeletion();
    156 //            for (String f : fileForDeletion) {
    157 //                File file = new File(f);
    158 //                file.delete();
    159 //            }
    160 //            donationPostService.delete(post);
    161 //        }
    162 //
    163 //        return "redirect:/myPosts";
    164 //    }
    165159
    166160    @RequestMapping("/donate")
     
    188182    }
    189183
     184    @RequestMapping("/report")
     185    public String report(@RequestParam Long postid,
     186                         @RequestParam String description) {
     187
     188        DonationPost donationPost = donationPostService.getById(postid);
     189        ReportPost reportPost = reportPostService.findByDonationPost(donationPost);
     190        if(reportPost == null) {
     191            reportPost = new ReportPost();
     192            reportPost.setDonationPost(donationPost);
     193        }
     194
     195        Reason reason = new Reason();
     196        AppUser user = (AppUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
     197        reason.setUser(user);
     198        reason.setDescription(description);
     199        reasonService.save(reason);
     200        reportPost.getReasons().add(reason);
     201        reportPostService.save(reportPost);
     202        return String.format("redirect:/post?postid=%d", postid);
     203    }
     204
    190205    @ModelAttribute("user")
    191206    public AppUser addAttributes() {
Note: See TracChangeset for help on using the changeset viewer.