Changeset 2a5d6a3 in Git for src/main/java/com/wediscussmovies/project/web/controller/DiscussionsController.java
- Timestamp:
- 01/16/22 15:29:49 (3 years ago)
- Branches:
- main
- Children:
- 7bc8942
- Parents:
- 7a0bf79
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/com/wediscussmovies/project/web/controller/DiscussionsController.java
r7a0bf79 r2a5d6a3 1 1 package com.wediscussmovies.project.web.controller; 2 2 3 import com.wediscussmovies.project.model.*; 4 import com.wediscussmovies.project.service.DiscussionService; 5 import com.wediscussmovies.project.service.MovieService; 6 import com.wediscussmovies.project.service.PersonService; 7 import com.wediscussmovies.project.service.ReplyService; 3 8 import org.springframework.stereotype.Controller; 9 import org.springframework.ui.Model; 10 import org.springframework.web.bind.annotation.*; 11 12 import javax.servlet.http.HttpServletRequest; 13 import java.sql.Date; 14 import java.time.LocalDate; 15 import java.util.List; 16 import java.util.Optional; 4 17 5 18 @Controller 19 @RequestMapping("/discussions") 6 20 public class DiscussionsController { 21 private final DiscussionService discussionService; 22 private final ReplyService replyService; 23 private final MovieService movieService; 24 private final PersonService personService; 25 public DiscussionsController(DiscussionService discussionService, ReplyService replyService, MovieService movieService, PersonService personService) { 26 this.discussionService = discussionService; 27 this.replyService = replyService; 28 this.movieService = movieService; 29 this.personService = personService; 30 } 31 32 @GetMapping("/") 33 public String getDiscussions(@RequestParam(required = false) String titleSearch, 34 Model model){ 35 List<Discussion> discussions = discussionService.listAll(); 36 if(titleSearch != null && !titleSearch.isEmpty()){ 37 discussions = discussionService.listAllByTitle(titleSearch); 38 } 39 model.addAttribute("discussions", discussions); 40 model.addAttribute("contentTemplate", "discussionsList"); 41 return "template"; 42 } 43 44 @GetMapping("/{id}") 45 public String getDiscussion( 46 @PathVariable Integer id, 47 Model model){ 48 Optional<Discussion> discussion = discussionService.findById(id); 49 if(discussion.isEmpty()) 50 return "redirect:/discussions"; 51 model.addAttribute("discussion", discussion); 52 model.addAttribute("contentTemplate", "discussionsDiscussion"); 53 return "template"; 54 } 55 56 @GetMapping("/{id}/reply") 57 public String getReplyToDiscussion( 58 @PathVariable Integer id, 59 Model model){ 60 Optional<Discussion> discussion = discussionService.findById(id); 61 if(discussion.isEmpty()) 62 return "redirect:/discussions"; 63 model.addAttribute("discussion", discussion); 64 model.addAttribute("contentTemplate", "discussionsReply"); 65 return "template"; 66 } 67 @PostMapping("/{id}/reply/confirm") 68 public String getReplyToDiscussionConfirm( 69 @PathVariable Integer id, 70 @RequestParam String text, 71 HttpServletRequest request){ 72 Optional<Discussion> discussion = discussionService.findById(id); 73 User user = (User) request.getSession().getAttribute("user"); 74 if(user == null){ 75 return "redirect:/login"; 76 } 77 Date date = Date.valueOf(LocalDate.now()); 78 if(discussion.isEmpty()) 79 return "redirect:/discussions"; 80 Reply reply = new Reply(discussion.get(), user, date, text); 81 replyService.save(reply); 82 return "redirect:/discussions/"+id; 83 } 84 85 @GetMapping("/movies/add/{id}") 86 public String getAddDiscussionForMovie(Model model, 87 @PathVariable Integer id, 88 HttpServletRequest request){ 89 model.addAttribute("contentTemplate", "discussionsAdd"); 90 request.setAttribute("movieId", id); 91 return "template"; 92 } 93 94 @PostMapping("/movies/add/confirm") 95 public String getAddDiscussionMovieConfirm(Model model, 96 HttpServletRequest request, 97 @RequestParam String title, 98 @RequestParam String text){ 99 User user = (User) request.getSession().getAttribute("user"); 100 if(user == null){ 101 return "redirect:/login"; 102 } 103 Integer movie_id = (Integer) request.getSession().getAttribute("movieId"); 104 request.getSession().setAttribute("movieId", null); 105 Optional<Movie> movieOp = movieService.findById(movie_id); 106 if(movieOp.isEmpty()) 107 return "redirect:/movies"; 108 Discussion discussion = new Discussion(title, text, user, movieOp.get(), Date.valueOf(LocalDate.now())); 109 discussionService.save(discussion); 110 return "redirect:/discussions"; 111 } 112 113 114 @GetMapping("/persons/add/{id}") 115 public String getAddDiscussionForPerson(Model model, 116 @PathVariable Integer id, 117 HttpServletRequest request){ 118 model.addAttribute("contentTemplate", "discussionsAdd"); 119 request.setAttribute("personId", id); 120 return "template"; 121 } 122 123 @PostMapping("/persons/add/confirm") 124 public String getAddDiscussionForPersonConfirm(Model model, 125 HttpServletRequest request, 126 @RequestParam String title, 127 @RequestParam String text){ 128 User user = (User) request.getSession().getAttribute("user"); 129 if(user == null){ 130 return "redirect:/login"; 131 } 132 Integer person_id = (Integer) request.getSession().getAttribute("personId"); 133 Optional<Person> personOp = personService.findById(person_id); 134 request.getSession().setAttribute("personId", null); 135 if(personOp.isEmpty()) 136 return "redirect:/discussions"; 137 Discussion discussion = new Discussion(title, text, user, personOp.get(), Date.valueOf(LocalDate.now())); 138 discussionService.save(discussion); 139 return "redirect:/discussions"; 140 } 141 142 7 143 }
Note:
See TracChangeset
for help on using the changeset viewer.