[2a5d6a3] | 1 | package com.wediscussmovies.project.web.controller;
|
---|
| 2 |
|
---|
[3ded84d] | 3 | import com.wediscussmovies.project.model.*;
|
---|
[2a5d6a3] | 4 | import com.wediscussmovies.project.service.ReplyService;
|
---|
[3ded84d] | 5 | import org.springframework.stereotype.Controller;
|
---|
[2a5d6a3] | 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 |
|
---|
[3ded84d] | 14 | @Controller
|
---|
[2a5d6a3] | 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}")
|
---|
[7fafead] | 23 | public String getReplyEdit(@PathVariable Long id, Model model){
|
---|
[2a5d6a3] | 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}")
|
---|
[7fafead] | 33 | public String getReplyEdit(@PathVariable Long id, @RequestParam String text){
|
---|
[2a5d6a3] | 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);
|
---|
[3ded84d] | 41 | return "redirect:/discussions/"+reply.getDiscussionId();
|
---|
[2a5d6a3] | 42 | }
|
---|
| 43 | }
|
---|