Changeset e0ef1b1 in Git for src/main/java/com/wediscussmovies/project/web/controller/ReplyController.java
- Timestamp:
- 02/04/22 23:22:13 (3 years ago)
- Branches:
- main
- Children:
- 5b447b0
- Parents:
- 3ded84d
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/com/wediscussmovies/project/web/controller/ReplyController.java
r3ded84d re0ef1b1 1 1 package com.wediscussmovies.project.web.controller; 2 2 3 import com.wediscussmovies.project.model.*; 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; 4 7 import com.wediscussmovies.project.service.ReplyService; 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; 5 12 import org.springframework.stereotype.Controller; 6 13 import org.springframework.ui.Model; 7 import org.springframework.web.bind.annotation.GetMapping; 8 import org.springframework.web.bind.annotation.PathVariable; 9 import org.springframework.web.bind.annotation.PostMapping; 10 import org.springframework.web.bind.annotation.RequestParam; 14 import org.springframework.web.bind.annotation.*; 11 15 12 import java.util.Optional;13 16 14 17 @Controller 18 @RequestMapping("/replies") 15 19 public class ReplyController { 16 20 private final ReplyService replyService; … … 20 24 } 21 25 22 @GetMapping("/edit/{id}") 23 public String getReplyEdit(@PathVariable Long id, Model model){ 24 Optional<Reply> reply = replyService.findById(id); 25 if(reply.isEmpty()) 26 return "redirect:/movies"; 27 model.addAttribute("reply", reply.get()); 28 model.addAttribute("templateContext", "replyEdit"); 26 @GetMapping("/add/{discussionId}") 27 public String addForm(@PathVariable Integer discussionId, Model model){ 28 model.addAttribute("discussionId",discussionId); 29 model.addAttribute("contentTemplate","repliesAdd"); 29 30 return "template"; 30 31 } 31 32 32 @PostMapping("/edit/confirm/{id}") 33 public String getReplyEdit(@PathVariable Long id, @RequestParam String text){ 34 Optional<Reply> replyOp = replyService.findById(id); 35 if(replyOp.isEmpty()) 36 return "redirect:/discussions"; 37 Reply reply = replyOp.get(); 38 replyService.delete(reply); 39 reply.setText(text); 40 replyService.save(reply); 41 return "redirect:/discussions/"+reply.getDiscussionId(); 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 } 42 47 } 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 43 85 }
Note:
See TracChangeset
for help on using the changeset viewer.