1 | package com.wediscussmovies.project.web.controller;
|
---|
2 |
|
---|
3 | import com.wediscussmovies.project.model.*;
|
---|
4 | import com.wediscussmovies.project.service.ReplyService;
|
---|
5 | import org.springframework.stereotype.Controller;
|
---|
6 | 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;
|
---|
11 |
|
---|
12 | import java.util.Optional;
|
---|
13 |
|
---|
14 | @Controller
|
---|
15 | public class ReplyController {
|
---|
16 | private final ReplyService replyService;
|
---|
17 |
|
---|
18 | public ReplyController(ReplyService replyService) {
|
---|
19 | this.replyService = replyService;
|
---|
20 | }
|
---|
21 |
|
---|
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");
|
---|
29 | return "template";
|
---|
30 | }
|
---|
31 |
|
---|
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();
|
---|
42 | }
|
---|
43 | }
|
---|