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