source: Git/src/main/java/com/wediscussmovies/project/web/controller/ReplyController.java@ 7fafead

main
Last change on this file since 7fafead was 7fafead, checked in by Test <matonikolov77@…>, 2 years ago

Resolving models

  • Property mode set to 100644
File size: 1.5 KB
Line 
1package com.wediscussmovies.project.web.controller;
2
3import com.wediscussmovies.project.model.Reply;
4import com.wediscussmovies.project.service.ReplyService;
5import org.springframework.ui.Model;
6import org.springframework.web.bind.annotation.GetMapping;
7import org.springframework.web.bind.annotation.PathVariable;
8import org.springframework.web.bind.annotation.PostMapping;
9import org.springframework.web.bind.annotation.RequestParam;
10
11import java.util.Optional;
12
13public class ReplyController {
14 private final ReplyService replyService;
15
16 public ReplyController(ReplyService replyService) {
17 this.replyService = replyService;
18 }
19
20 @GetMapping("/edit/{id}")
21 public String getReplyEdit(@PathVariable Long id, Model model){
22 Optional<Reply> reply = replyService.findById(id);
23 if(reply.isEmpty())
24 return "redirect:/movies";
25 model.addAttribute("reply", reply.get());
26 model.addAttribute("templateContext", "replyEdit");
27 return "template";
28 }
29
30 @PostMapping("/edit/confirm/{id}")
31 public String getReplyEdit(@PathVariable Long id, @RequestParam String text){
32 Optional<Reply> replyOp = replyService.findById(id);
33 if(replyOp.isEmpty())
34 return "redirect:/discussions";
35 Reply reply = replyOp.get();
36 replyService.delete(reply);
37 reply.setText(text);
38 replyService.save(reply);
39 return "redirect:/discussions/"+reply.getDiscussion().getId();
40 }
41}
Note: See TracBrowser for help on using the repository browser.