[2a5d6a3] | 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}")
|
---|
[7fafead] | 21 | public String getReplyEdit(@PathVariable Long id, Model model){
|
---|
[2a5d6a3] | 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}")
|
---|
[7fafead] | 31 | public String getReplyEdit(@PathVariable Long id, @RequestParam String text){
|
---|
[2a5d6a3] | 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);
|
---|
[7fafead] | 39 | return "redirect:/discussions/"+reply.getDiscussion().getId();
|
---|
[2a5d6a3] | 40 | }
|
---|
| 41 | }
|
---|