[2a5d6a3] | 1 | package com.wediscussmovies.project.web.controller;
|
---|
| 2 |
|
---|
[e0ef1b1] | 3 | import com.wediscussmovies.project.LoggedUser;
|
---|
| 4 | import com.wediscussmovies.project.model.Reply;
|
---|
| 5 | import com.wediscussmovies.project.model.User;
|
---|
| 6 | import com.wediscussmovies.project.model.primarykeys.ReplyPK;
|
---|
[2a5d6a3] | 7 | import com.wediscussmovies.project.service.ReplyService;
|
---|
[e0ef1b1] | 8 | import org.springframework.security.authentication.AnonymousAuthenticationToken;
|
---|
| 9 | import org.springframework.security.core.Authentication;
|
---|
| 10 | import org.springframework.security.core.context.SecurityContextHolder;
|
---|
| 11 | import org.springframework.security.core.userdetails.UserDetails;
|
---|
[3ded84d] | 12 | import org.springframework.stereotype.Controller;
|
---|
[2a5d6a3] | 13 | import org.springframework.ui.Model;
|
---|
[e0ef1b1] | 14 | import org.springframework.web.bind.annotation.*;
|
---|
[2a5d6a3] | 15 |
|
---|
| 16 |
|
---|
[3ded84d] | 17 | @Controller
|
---|
[e0ef1b1] | 18 | @RequestMapping("/replies")
|
---|
[2a5d6a3] | 19 | public class ReplyController {
|
---|
| 20 | private final ReplyService replyService;
|
---|
| 21 |
|
---|
| 22 | public ReplyController(ReplyService replyService) {
|
---|
| 23 | this.replyService = replyService;
|
---|
| 24 | }
|
---|
| 25 |
|
---|
[e0ef1b1] | 26 | @GetMapping("/add/{discussionId}")
|
---|
| 27 | public String addForm(@PathVariable Integer discussionId, Model model){
|
---|
| 28 | model.addAttribute("discussionId",discussionId);
|
---|
| 29 | model.addAttribute("contentTemplate","repliesAdd");
|
---|
[2a5d6a3] | 30 | return "template";
|
---|
| 31 | }
|
---|
| 32 |
|
---|
[e0ef1b1] | 33 | @GetMapping("/edit/{discussionId}/{replyId}")
|
---|
| 34 | public String editForm(@PathVariable Integer discussionId,@PathVariable Integer replyId, Model model){
|
---|
| 35 |
|
---|
| 36 | try {
|
---|
| 37 | Reply reply = replyService.findById(discussionId,replyId);
|
---|
| 38 | model.addAttribute("reply", reply);
|
---|
| 39 | model.addAttribute("contentTemplate", "repliesAdd");
|
---|
| 40 | return "template";
|
---|
| 41 |
|
---|
| 42 |
|
---|
| 43 | }catch (RuntimeException exc){
|
---|
| 44 | return "redirect:/discussions?error="+exc.getMessage();
|
---|
| 45 |
|
---|
| 46 | }
|
---|
[2a5d6a3] | 47 | }
|
---|
[e0ef1b1] | 48 | @PostMapping("/save")
|
---|
| 49 | public String saveReply(
|
---|
| 50 | @RequestParam Integer discussionId,
|
---|
| 51 | @RequestParam String text){
|
---|
| 52 |
|
---|
| 53 | try {
|
---|
| 54 |
|
---|
| 55 | this.replyService.save(discussionId,text, LoggedUser.getLoggedUser());
|
---|
| 56 | return "redirect:/discussions/"+discussionId;
|
---|
| 57 |
|
---|
| 58 | }
|
---|
| 59 | catch (RuntimeException exc){
|
---|
| 60 | return "redirect:/discussions?error="+exc.getMessage();
|
---|
| 61 |
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | @PostMapping("/save/{replyId}")
|
---|
| 66 | public String editReply(
|
---|
| 67 | @PathVariable Integer replyId,
|
---|
| 68 | @RequestParam Integer discussionId,
|
---|
| 69 | @RequestParam String text){
|
---|
| 70 |
|
---|
| 71 | /*
|
---|
| 72 | 1. Da se realizira so ajax baranje na restController
|
---|
| 73 | */
|
---|
| 74 | try {
|
---|
| 75 | Reply reply = replyService.edit(replyId,discussionId, text);
|
---|
| 76 |
|
---|
| 77 | return "redirect:/discussions/" + reply.getDiscussion().getDiscussionId();
|
---|
| 78 | }
|
---|
| 79 | catch (RuntimeException exc){
|
---|
| 80 | return "redirect:/discussions?error="+exc.getMessage();
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | }
|
---|
| 84 |
|
---|
[2a5d6a3] | 85 | }
|
---|