1 | package com.wediscussmovies.project.web.controller;
|
---|
2 |
|
---|
3 | import com.wediscussmovies.project.model.Reply;
|
---|
4 | import com.wediscussmovies.project.service.ReplyService;
|
---|
5 | import org.springframework.ui.Model;
|
---|
6 | import org.springframework.web.bind.annotation.GetMapping;
|
---|
7 | import org.springframework.web.bind.annotation.PathVariable;
|
---|
8 | import org.springframework.web.bind.annotation.PostMapping;
|
---|
9 | import org.springframework.web.bind.annotation.RequestParam;
|
---|
10 |
|
---|
11 | import java.util.Optional;
|
---|
12 |
|
---|
13 | public 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 Integer 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 Integer 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().getDiscussion_id();
|
---|
40 | }
|
---|
41 | }
|
---|