Changeset 2a5d6a3 in Git for src/main


Ignore:
Timestamp:
01/16/22 15:29:49 (3 years ago)
Author:
Petar Partaloski <ppartaloski@…>
Branches:
main
Children:
7bc8942
Parents:
7a0bf79
Message:

Controller, Repository and Service layer improvements, Entity updating

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  
    99@Data
    1010@Entity
     11@Table (name="discussions")
    1112public class Discussion {
    1213    @Id
     
    3536    private Date date;
    3637
     38    @Column(name = "type", nullable = false)
     39    private DiscussionType type;
    3740    @OneToMany
    3841    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
    3965}
    4066
  • src/main/java/com/wediscussmovies/project/model/Genre.java

    r7a0bf79 r2a5d6a3  
    33import lombok.Data;
    44
    5 import javax.persistence.Column;
    6 import javax.persistence.Entity;
    7 import javax.persistence.GeneratedValue;
    8 import javax.persistence.Id;
     5import javax.persistence.*;
    96
    107@Data
    11 @Entity(name="genres")
     8@Entity
     9@Table(name="genres")
    1210public class Genre {
    1311    @Id
  • src/main/java/com/wediscussmovies/project/model/Movie.java

    r7a0bf79 r2a5d6a3  
    88import java.util.List;
    99@Data
    10 @Entity(name="movies")
     10@Entity
     11@Table (name="movies")
    1112public class Movie {
    1213    @Id
     
    6162    public static Comparator<Movie> comparatorTitle = Comparator.comparing(Movie::getTitle);
    6263
     64    public Movie() {
     65    }
     66
    6367    public Movie(String title, String description, String image_url, Date airing_date, float imdb_rating, Person director, List<Person> actors, List<Genre> genres) {
    6468        Title = title;
  • src/main/java/com/wediscussmovies/project/model/Person.java

    r7a0bf79 r2a5d6a3  
    55import javax.persistence.*;
    66import java.sql.Date;
     7import java.util.Comparator;
    78import java.util.List;
    89
    910@Data
    10 @Entity(name="persons")
     11@Entity
     12@Table(name="persons")
    1113public class Person {
    1214    @Id
     
    3436    @ManyToMany
    3537    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);
    3652}
    3753
  • src/main/java/com/wediscussmovies/project/model/Reply.java

    r7a0bf79 r2a5d6a3  
    55import javax.persistence.*;
    66import java.sql.Date;
     7import java.util.Optional;
    78
    89@Data
    9 @Entity(name="replies")
     10@Entity
     11@Table(name="replies")
    1012public class Reply {
    1113    @Id
     
    2729    @Column(name= "text", length = 1000, nullable = false)
    2830    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    }
    2938}
    3039
  • src/main/java/com/wediscussmovies/project/model/User.java

    r7a0bf79 r2a5d6a3  
    77
    88@Data
    9 @Entity(name="users")
     9@Entity
     10@Table(name="users")
    1011public class User {
    1112    @Id
     
    3637    private List<Genre> likes_genres;
    3738
     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    }
    3849}
    3950
  • src/main/java/com/wediscussmovies/project/repository/DiscussionRepository.java

    r7a0bf79 r2a5d6a3  
    55import org.springframework.stereotype.Repository;
    66
     7import java.util.List;
     8
    79@Repository
    810public interface DiscussionRepository extends JpaRepository<Discussion, Integer> {
     11    public List<Discussion> findAllByTitleLike(String title);
    912}
  • src/main/java/com/wediscussmovies/project/repository/PersonRepository.java

    r7a0bf79 r2a5d6a3  
    1313public interface PersonRepository extends JpaRepository<Person, Integer> {
    1414    public List<Person> findAllByPersonType(PersonType type);
     15    public List<Person> findAllByPersonTypeAndNameLike(PersonType type, String name);
     16    public List<Person> findAllByPersonTypeAndSurnameLike(PersonType type, String surname);
    1517    public Optional<Person> findPersonByPerson_idAndPersonType(Integer id, PersonType type);
    1618}
  • src/main/java/com/wediscussmovies/project/repository/UserRepository.java

    r7a0bf79 r2a5d6a3  
    55import org.springframework.stereotype.Repository;
    66
     7import java.util.EnumMap;
     8import java.util.Optional;
     9
    710@Repository
    811public 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);
    916}
  • src/main/java/com/wediscussmovies/project/service/DiscussionService.java

    r7a0bf79 r2a5d6a3  
    11package com.wediscussmovies.project.service;
    22
     3import com.wediscussmovies.project.model.Discussion;
     4
     5import java.util.List;
     6import java.util.Optional;
     7
    38public interface DiscussionService {
     9    List<Discussion> listAll();
     10    List<Discussion> listAllByTitle(String title);
     11    Optional<Discussion> findById(Integer id);
     12    void save(Discussion discussion);
    413}
  • src/main/java/com/wediscussmovies/project/service/PersonService.java

    r7a0bf79 r2a5d6a3  
    99    public List<Person> findAllDirectors();
    1010    public List<Person> findAllActors();
     11    Optional<Person> findById(Integer person_id);
    1112    Optional<Person> findActorById(Integer id);
    1213    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
    1320}
  • src/main/java/com/wediscussmovies/project/service/ReplyService.java

    r7a0bf79 r2a5d6a3  
    11package com.wediscussmovies.project.service;
    22
     3import com.wediscussmovies.project.model.Reply;
     4
     5import java.util.Optional;
     6
    37public interface ReplyService {
     8    public Reply save(Reply r);
     9    public void delete(Reply r);
     10    Optional<Reply> findById(Integer id);
    411}
  • src/main/java/com/wediscussmovies/project/service/UserService.java

    r7a0bf79 r2a5d6a3  
    11package com.wediscussmovies.project.service;
    22
     3import com.wediscussmovies.project.model.User;
     4
     5import javax.servlet.http.HttpServletRequest;
     6import java.util.Optional;
     7
    38public 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);
    411}
  • src/main/java/com/wediscussmovies/project/service/impl/PersonServiceImpl.java

    r7a0bf79 r2a5d6a3  
    2828    }
    2929
     30    @Override
     31    public Optional<Person> findById(Integer person_id) {
     32        return personRepository.findById(person_id);
     33    }
    3034
    3135    @Override
     
    3842        return personRepository.findAllByPersonType(PersonType.A);
    3943    }
     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    }
    4072}
  • src/main/java/com/wediscussmovies/project/web/controller/DiscussionsController.java

    r7a0bf79 r2a5d6a3  
    11package com.wediscussmovies.project.web.controller;
    22
     3import com.wediscussmovies.project.model.*;
     4import com.wediscussmovies.project.service.DiscussionService;
     5import com.wediscussmovies.project.service.MovieService;
     6import com.wediscussmovies.project.service.PersonService;
     7import com.wediscussmovies.project.service.ReplyService;
    38import org.springframework.stereotype.Controller;
     9import org.springframework.ui.Model;
     10import org.springframework.web.bind.annotation.*;
     11
     12import javax.servlet.http.HttpServletRequest;
     13import java.sql.Date;
     14import java.time.LocalDate;
     15import java.util.List;
     16import java.util.Optional;
    417
    518@Controller
     19@RequestMapping("/discussions")
    620public 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
    7143}
  • src/main/java/com/wediscussmovies/project/web/controller/UserController.java

    r7a0bf79 r2a5d6a3  
    11package com.wediscussmovies.project.web.controller;
    22
     3import com.wediscussmovies.project.model.User;
     4import com.wediscussmovies.project.model.exception.InvalidUserCredentialsException;
     5import com.wediscussmovies.project.service.UserService;
    36import org.springframework.stereotype.Controller;
     7import org.springframework.ui.Model;
     8import org.springframework.web.bind.annotation.GetMapping;
     9import org.springframework.web.bind.annotation.PostMapping;
     10import org.springframework.web.bind.annotation.RequestMapping;
     11import org.springframework.web.bind.annotation.RequestParam;
     12
     13import javax.servlet.http.HttpServletRequest;
     14import java.util.Optional;
    415
    516@Controller
     17@RequestMapping("/")
    618public 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    }
    776}
Note: See TracChangeset for help on using the changeset viewer.