Changeset 2a5d6a3 in Git
- Timestamp:
- 01/16/22 15:29:49 (3 years ago)
- Branches:
- main
- Children:
- 7bc8942
- Parents:
- 7a0bf79
- Location:
- src/main/java/com/wediscussmovies/project
- Files:
-
- 11 added
- 16 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/com/wediscussmovies/project/model/Discussion.java
r7a0bf79 r2a5d6a3 9 9 @Data 10 10 @Entity 11 @Table (name="discussions") 11 12 public class Discussion { 12 13 @Id … … 35 36 private Date date; 36 37 38 @Column(name = "type", nullable = false) 39 private DiscussionType type; 37 40 @OneToMany 38 41 private List<Reply> replies; 42 43 public Discussion() { 44 } 45 46 public Discussion(String title, String text, User user, Movie movie, Date valueOf) { 47 this.title = title; 48 this.text = text; 49 this.user = user; 50 this.date = valueOf; 51 this.movie = movie; 52 this.type = DiscussionType.M; 53 } 54 55 public Discussion(String title, String text, User user, Person person, Date valueOf) { 56 this.title = title; 57 this.text = text; 58 this.user = user; 59 this.date = valueOf; 60 this.person = person; 61 this.type = DiscussionType.P; 62 } 63 64 39 65 } 40 66 -
src/main/java/com/wediscussmovies/project/model/Genre.java
r7a0bf79 r2a5d6a3 3 3 import lombok.Data; 4 4 5 import javax.persistence.Column; 6 import javax.persistence.Entity; 7 import javax.persistence.GeneratedValue; 8 import javax.persistence.Id; 5 import javax.persistence.*; 9 6 10 7 @Data 11 @Entity(name="genres") 8 @Entity 9 @Table(name="genres") 12 10 public class Genre { 13 11 @Id -
src/main/java/com/wediscussmovies/project/model/Movie.java
r7a0bf79 r2a5d6a3 8 8 import java.util.List; 9 9 @Data 10 @Entity(name="movies") 10 @Entity 11 @Table (name="movies") 11 12 public class Movie { 12 13 @Id … … 61 62 public static Comparator<Movie> comparatorTitle = Comparator.comparing(Movie::getTitle); 62 63 64 public Movie() { 65 } 66 63 67 public Movie(String title, String description, String image_url, Date airing_date, float imdb_rating, Person director, List<Person> actors, List<Genre> genres) { 64 68 Title = title; -
src/main/java/com/wediscussmovies/project/model/Person.java
r7a0bf79 r2a5d6a3 5 5 import javax.persistence.*; 6 6 import java.sql.Date; 7 import java.util.Comparator; 7 8 import java.util.List; 8 9 9 10 @Data 10 @Entity(name="persons") 11 @Entity 12 @Table(name="persons") 11 13 public class Person { 12 14 @Id … … 34 36 @ManyToMany 35 37 private List<Movie> acts_in; 38 39 public Person(String name, String surname, PersonType personType, Date date_of_birth, String image_url, String description) { 40 this.name = name; 41 this.surname = surname; 42 this.personType = personType; 43 this.date_of_birth = date_of_birth; 44 this.image_url = image_url; 45 this.description = description; 46 } 47 48 public Person() { 49 } 50 51 public static Comparator<Person> personComparatorByNameSurname = Comparator.comparing(Person::getName).thenComparing(Person::getSurname); 36 52 } 37 53 -
src/main/java/com/wediscussmovies/project/model/Reply.java
r7a0bf79 r2a5d6a3 5 5 import javax.persistence.*; 6 6 import java.sql.Date; 7 import java.util.Optional; 7 8 8 9 @Data 9 @Entity(name="replies") 10 @Entity 11 @Table(name="replies") 10 12 public class Reply { 11 13 @Id … … 27 29 @Column(name= "text", length = 1000, nullable = false) 28 30 private String text; 31 32 public Reply(Discussion discussion, User user, Date date, String text) { 33 this.discussion = discussion; 34 this.user = user; 35 this.date = date; 36 this.text = text; 37 } 29 38 } 30 39 -
src/main/java/com/wediscussmovies/project/model/User.java
r7a0bf79 r2a5d6a3 7 7 8 8 @Data 9 @Entity(name="users") 9 @Entity 10 @Table(name="users") 10 11 public class User { 11 12 @Id … … 36 37 private List<Genre> likes_genres; 37 38 39 public User(String username, String name, String surname, String email, String password) { 40 this.username = username; 41 this.name = name; 42 this.surname = surname; 43 this.email = email; 44 this.password = password; 45 } 46 47 public User() { 48 } 38 49 } 39 50 -
src/main/java/com/wediscussmovies/project/repository/DiscussionRepository.java
r7a0bf79 r2a5d6a3 5 5 import org.springframework.stereotype.Repository; 6 6 7 import java.util.List; 8 7 9 @Repository 8 10 public interface DiscussionRepository extends JpaRepository<Discussion, Integer> { 11 public List<Discussion> findAllByTitleLike(String title); 9 12 } -
src/main/java/com/wediscussmovies/project/repository/PersonRepository.java
r7a0bf79 r2a5d6a3 13 13 public interface PersonRepository extends JpaRepository<Person, Integer> { 14 14 public List<Person> findAllByPersonType(PersonType type); 15 public List<Person> findAllByPersonTypeAndNameLike(PersonType type, String name); 16 public List<Person> findAllByPersonTypeAndSurnameLike(PersonType type, String surname); 15 17 public Optional<Person> findPersonByPerson_idAndPersonType(Integer id, PersonType type); 16 18 } -
src/main/java/com/wediscussmovies/project/repository/UserRepository.java
r7a0bf79 r2a5d6a3 5 5 import org.springframework.stereotype.Repository; 6 6 7 import java.util.EnumMap; 8 import java.util.Optional; 9 7 10 @Repository 8 11 public interface UserRepository extends JpaRepository<User, Integer> { 12 public Optional<User> findByEmailAndPassword(String email, String password); 13 public Optional<User> findByUsernameAndPassword(String email, String password); 14 public Optional<User> findByUsername(String username); 15 public Optional<User> findByEmail(String email); 9 16 } -
src/main/java/com/wediscussmovies/project/service/DiscussionService.java
r7a0bf79 r2a5d6a3 1 1 package com.wediscussmovies.project.service; 2 2 3 import com.wediscussmovies.project.model.Discussion; 4 5 import java.util.List; 6 import java.util.Optional; 7 3 8 public interface DiscussionService { 9 List<Discussion> listAll(); 10 List<Discussion> listAllByTitle(String title); 11 Optional<Discussion> findById(Integer id); 12 void save(Discussion discussion); 4 13 } -
src/main/java/com/wediscussmovies/project/service/PersonService.java
r7a0bf79 r2a5d6a3 9 9 public List<Person> findAllDirectors(); 10 10 public List<Person> findAllActors(); 11 Optional<Person> findById(Integer person_id); 11 12 Optional<Person> findActorById(Integer id); 12 13 Optional<Person> findDirectorById(Integer id); 14 boolean save(Person person); 15 List<Person> findActorsByNameLike(String name); 16 List<Person> findActorsBySurnameLike(String surname); 17 List<Person> findDirectorsByNameLike(String name); 18 List<Person> findDirectorsBySurnameLike(String surname); 19 13 20 } -
src/main/java/com/wediscussmovies/project/service/ReplyService.java
r7a0bf79 r2a5d6a3 1 1 package com.wediscussmovies.project.service; 2 2 3 import com.wediscussmovies.project.model.Reply; 4 5 import java.util.Optional; 6 3 7 public interface ReplyService { 8 public Reply save(Reply r); 9 public void delete(Reply r); 10 Optional<Reply> findById(Integer id); 4 11 } -
src/main/java/com/wediscussmovies/project/service/UserService.java
r7a0bf79 r2a5d6a3 1 1 package com.wediscussmovies.project.service; 2 2 3 import com.wediscussmovies.project.model.User; 4 5 import javax.servlet.http.HttpServletRequest; 6 import java.util.Optional; 7 3 8 public interface UserService { 9 public Optional<User> login(String email, String password); 10 public Optional<User> register(HttpServletRequest request, String email, String password, String confirmPassword, String username, String name, String surname); 4 11 } -
src/main/java/com/wediscussmovies/project/service/impl/PersonServiceImpl.java
r7a0bf79 r2a5d6a3 28 28 } 29 29 30 @Override 31 public Optional<Person> findById(Integer person_id) { 32 return personRepository.findById(person_id); 33 } 30 34 31 35 @Override … … 38 42 return personRepository.findAllByPersonType(PersonType.A); 39 43 } 44 45 @Override 46 public boolean save(Person person) { 47 Person p = personRepository.save(person); 48 if(p != null) 49 return true; 50 return false; 51 } 52 53 @Override 54 public List<Person> findActorsByNameLike(String name) { 55 return personRepository.findAllByPersonTypeAndNameLike(PersonType.A, name); 56 } 57 58 @Override 59 public List<Person> findActorsBySurnameLike(String surname) { 60 return personRepository.findAllByPersonTypeAndSurnameLike(PersonType.A, surname); 61 } 62 63 @Override 64 public List<Person> findDirectorsByNameLike(String name) { 65 return personRepository.findAllByPersonTypeAndNameLike(PersonType.D, name); 66 } 67 68 @Override 69 public List<Person> findDirectorsBySurnameLike(String surname) { 70 return personRepository.findAllByPersonTypeAndSurnameLike(PersonType.D, surname); 71 } 40 72 } -
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 } -
src/main/java/com/wediscussmovies/project/web/controller/UserController.java
r7a0bf79 r2a5d6a3 1 1 package com.wediscussmovies.project.web.controller; 2 2 3 import com.wediscussmovies.project.model.User; 4 import com.wediscussmovies.project.model.exception.InvalidUserCredentialsException; 5 import com.wediscussmovies.project.service.UserService; 3 6 import org.springframework.stereotype.Controller; 7 import org.springframework.ui.Model; 8 import org.springframework.web.bind.annotation.GetMapping; 9 import org.springframework.web.bind.annotation.PostMapping; 10 import org.springframework.web.bind.annotation.RequestMapping; 11 import org.springframework.web.bind.annotation.RequestParam; 12 13 import javax.servlet.http.HttpServletRequest; 14 import java.util.Optional; 4 15 5 16 @Controller 17 @RequestMapping("/") 6 18 public class UserController { 19 private final UserService userService; 20 21 public UserController(UserService userService) { 22 this.userService = userService; 23 } 24 25 @GetMapping("/login") 26 public String getLoginPage(){ 27 return "login"; 28 } 29 30 @PostMapping("/login/confirm") 31 public String confirmLogin(HttpServletRequest request, Model model){ 32 Optional<User> user; 33 try{ 34 user = this.userService.login(request.getParameter("username"), request.getParameter("password")); 35 request.getSession().setAttribute("user", user); 36 request.getSession().setAttribute("loggedIn",true); 37 return "redirect:/movies"; 38 } 39 catch (InvalidUserCredentialsException ex){ 40 model.addAttribute("hasError", true); 41 model.addAttribute("error", ex.getMessage()); 42 return "login"; 43 } 44 } 45 46 @GetMapping("/register") 47 public String getRegisterPage(){ 48 return "register"; 49 } 50 51 @PostMapping("/login/confirm") 52 public String confirmRegister(HttpServletRequest request, 53 @RequestParam String username, 54 @RequestParam String email, 55 @RequestParam String password, 56 @RequestParam String confirmPassword, 57 @RequestParam String name, 58 @RequestParam String surname){ 59 Optional<User> user; 60 user = this.userService.register(request, email, password, confirmPassword, username, name, surname); 61 if(user.isEmpty()){ 62 request.setAttribute("hasError", "true"); 63 }else{ 64 request.setAttribute("hasError", "false"); 65 } 66 request.getSession().setAttribute("user", user.get()); 67 request.getSession().setAttribute("loggedIn",true); 68 return "redirect:/movies"; 69 } 70 71 @GetMapping 72 public String logout(HttpServletRequest request){ 73 request.getSession().invalidate(); 74 return "redirect:/login"; 75 } 7 76 }
Note:
See TracChangeset
for help on using the changeset viewer.