Changeset 7a0bf79 in Git for src/main/java


Ignore:
Timestamp:
01/16/22 03:40:31 (3 years ago)
Author:
Petar Partaloski <ppartaloski@…>
Branches:
main
Children:
2a5d6a3
Parents:
839f96a
Message:

Early implementations, MovieController CRUD implementation included

Location:
src/main/java/com/wediscussmovies/project
Files:
4 added
13 edited

Legend:

Unmodified
Added
Removed
  • src/main/java/com/wediscussmovies/project/model/Discussion.java

    r839f96a r7a0bf79  
    11package com.wediscussmovies.project.model;
     2
     3import lombok.Data;
    24
    35import javax.persistence.*;
     
    57import java.util.List;
    68
     9@Data
    710@Entity
    811public class Discussion {
  • src/main/java/com/wediscussmovies/project/model/Genre.java

    r839f96a r7a0bf79  
    11package com.wediscussmovies.project.model;
     2
     3import lombok.Data;
    24
    35import javax.persistence.Column;
     
    68import javax.persistence.Id;
    79
     10@Data
    811@Entity(name="genres")
    912public class Genre {
     
    1518    @Column(name="genre_type", length = 100, nullable = false, unique = true)
    1619    private String genre_type;
     20
     21    public Genre(String genreName) {
     22        this.genre_type = genreName;
     23    }
     24
     25    public Genre() {
     26    }
    1727}
    1828
  • src/main/java/com/wediscussmovies/project/model/Movie.java

    r839f96a r7a0bf79  
    11package com.wediscussmovies.project.model;
     2
     3import lombok.Data;
    24
    35import javax.persistence.*;
    46import java.sql.Date;
     7import java.util.Comparator;
    58import java.util.List;
    6 
     9@Data
    710@Entity(name="movies")
    811public class Movie {
     
    3639    @ManyToMany(mappedBy = "movie_genres")
    3740    private List<Genre> genres;
     41
     42    public boolean isFromGenre(Genre genre){
     43        for(Genre g: genres){
     44            if(g.getGenre_id() == genre.getGenre_id())
     45                return true;
     46        }
     47        return false;
     48    }
     49    public boolean hasActor(Person p){
     50        for(Person person: actors){
     51            if(person.getPerson_id() == p.getPerson_id())
     52                return true;
     53        }
     54        return false;
     55    }
     56
     57    public boolean isDirectedBy(Person p){
     58        return director.getPerson_id() == p.getPerson_id();
     59    }
     60
     61    public static Comparator<Movie> comparatorTitle = Comparator.comparing(Movie::getTitle);
     62
     63    public Movie(String title, String description, String image_url, Date airing_date, float imdb_rating, Person director, List<Person> actors, List<Genre> genres) {
     64        Title = title;
     65        this.description = description;
     66        this.image_url = image_url;
     67        this.airing_date = airing_date;
     68        this.imdb_rating = imdb_rating;
     69        this.director = director;
     70        this.actors = actors;
     71        this.genres = genres;
     72    }
    3873}
    3974
  • src/main/java/com/wediscussmovies/project/model/Person.java

    r839f96a r7a0bf79  
    11package com.wediscussmovies.project.model;
     2
     3import lombok.Data;
    24
    35import javax.persistence.*;
     
    57import java.util.List;
    68
     9@Data
    710@Entity(name="persons")
    811public class Person {
  • src/main/java/com/wediscussmovies/project/model/Reply.java

    r839f96a r7a0bf79  
    11package com.wediscussmovies.project.model;
     2
     3import lombok.Data;
    24
    35import javax.persistence.*;
    46import java.sql.Date;
    57
     8@Data
    69@Entity(name="replies")
    710public class Reply {
  • src/main/java/com/wediscussmovies/project/model/User.java

    r839f96a r7a0bf79  
    11package com.wediscussmovies.project.model;
     2
     3import lombok.Data;
    24
    35import javax.persistence.*;
    46import java.util.List;
    57
     8@Data
    69@Entity(name="users")
    710public class User {
  • src/main/java/com/wediscussmovies/project/repository/GenreRepository.java

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

    r839f96a r7a0bf79  
    22
    33import com.wediscussmovies.project.model.Person;
     4import com.wediscussmovies.project.model.PersonType;
    45import org.springframework.data.jpa.repository.JpaRepository;
     6import org.springframework.data.jpa.repository.Query;
    57import org.springframework.stereotype.Repository;
     8
     9import java.util.List;
     10import java.util.Optional;
    611
    712@Repository
    813public interface PersonRepository extends JpaRepository<Person, Integer> {
     14    public List<Person> findAllByPersonType(PersonType type);
     15    public Optional<Person> findPersonByPerson_idAndPersonType(Integer id, PersonType type);
    916}
  • src/main/java/com/wediscussmovies/project/service/GenreService.java

    r839f96a r7a0bf79  
    11package com.wediscussmovies.project.service;
    22
     3import com.wediscussmovies.project.model.Genre;
     4
     5import java.util.List;
     6import java.util.Optional;
     7
    38public interface GenreService {
     9    public List<Genre> findAll();
     10    public Optional<Genre> findById(Integer id);
     11    public List<Genre> findAllByType(String genre);
     12    public Genre save(String genreName);
    413}
  • src/main/java/com/wediscussmovies/project/service/MovieService.java

    r839f96a r7a0bf79  
    44
    55import java.util.List;
     6import java.util.Optional;
    67
    78public interface MovieService {
    89    public List<Movie> listAll();
    910    public List<Movie> searchByTitle(String title);
     11    public Optional<Movie> findById(Integer id);
     12    public Movie save(Movie movie);
     13    public void deleteById(int id);
    1014}
  • src/main/java/com/wediscussmovies/project/service/PersonService.java

    r839f96a r7a0bf79  
    11package com.wediscussmovies.project.service;
    22
     3import com.wediscussmovies.project.model.Person;
     4
     5import java.util.List;
     6import java.util.Optional;
     7
    38public interface PersonService {
     9    public List<Person> findAllDirectors();
     10    public List<Person> findAllActors();
     11    Optional<Person> findActorById(Integer id);
     12    Optional<Person> findDirectorById(Integer id);
    413}
  • src/main/java/com/wediscussmovies/project/service/impl/MovieServiceImpl.java

    r839f96a r7a0bf79  
    77
    88import java.util.List;
     9import java.util.Optional;
    910
    1011@Service
     
    2223
    2324    @Override
     25    public Optional<Movie> findById(Integer id) {
     26        return movieRepository.findById(id);
     27    }
     28
     29    @Override
     30    public Movie save(Movie movie) {
     31        return movieRepository.save(movie);
     32    }
     33
     34    @Override
     35    public void deleteById(int id) {
     36        movieRepository.deleteById(id);
     37    }
     38
     39    @Override
    2440    public List<Movie> searchByTitle(String title) {
    2541        return movieRepository.findAllByTitleLike("%"+title+"%");
    2642    }
    2743}
     44
     45
  • src/main/java/com/wediscussmovies/project/web/controller/MovieController.java

    r839f96a r7a0bf79  
    11package com.wediscussmovies.project.web.controller;
    22
     3import com.wediscussmovies.project.model.Genre;
    34import com.wediscussmovies.project.model.Movie;
     5import com.wediscussmovies.project.model.Person;
     6import com.wediscussmovies.project.model.exception.MovieIdNotFoundException;
     7import com.wediscussmovies.project.service.GenreService;
    48import com.wediscussmovies.project.service.MovieService;
     9import com.wediscussmovies.project.service.PersonService;
    510import org.springframework.stereotype.Controller;
    611import org.springframework.ui.Model;
    7 import org.springframework.web.bind.annotation.GetMapping;
    8 import org.springframework.web.bind.annotation.RequestMapping;
    9 import org.springframework.web.bind.annotation.RequestParam;
    10 
     12import org.springframework.web.bind.annotation.*;
     13
     14import java.sql.Date;
     15import java.util.LinkedList;
    1116import java.util.List;
     17import java.util.Optional;
    1218
    1319@Controller
     
    1521public class MovieController {
    1622    private final MovieService movieService;
    17 
    18     public MovieController(MovieService movieService) {
     23    private final GenreService genreService;
     24    private final PersonService personService;
     25
     26    public MovieController(MovieService movieService, GenreService genreService, PersonService personService) {
    1927        this.movieService = movieService;
     28        this.genreService = genreService;
     29        this.personService = personService;
    2030    }
    2131
     
    2939            movies = movieService.searchByTitle(titleQuery);
    3040        }
     41
     42        movies.sort(Movie.comparatorTitle);
     43
    3144        model.addAttribute("movies", movies);
    3245        model.addAttribute("contentTemplate", "moviesList");
     
    3447    }
    3548
     49
     50    @GetMapping("/add")
     51    public String addMovie(Model model){
     52        model.addAttribute("directors", personService.findAllDirectors());
     53        model.addAttribute("actors", personService.findAllActors());
     54        model.addAttribute("genres", genreService.findAll());
     55        model.addAttribute("contentTemplate", "moviesAdd");
     56        return "template";
     57    }
     58
     59
     60    @PostMapping("/{id}/delete")
     61    public String addMovie(@PathVariable int id){
     62        Optional<Movie> movie = movieService.findById(id);
     63        if(movie.isPresent()){
     64            movieService.deleteById(movie.get().getMovie_id());
     65        }
     66        return "redirect:/movies";
     67    }
     68
     69    @PostMapping("/add/confirm")
     70    public String addMoviePost(@RequestParam String title,
     71                               @RequestParam String description,
     72                               @RequestParam String image_url,
     73                               @RequestParam Date airing_date,
     74                               @RequestParam Float rating,
     75                               @RequestParam Integer director_id,
     76                               @RequestParam List<Integer> actors,
     77                               @RequestParam List<Integer> genres,
     78                               Model model){
     79        if(title == null || title.isEmpty() ||
     80        description == null || description.isEmpty() ||
     81        image_url == null || image_url.isEmpty() ||
     82        airing_date == null ||
     83        rating == null ||
     84        director_id == null ||
     85        actors == null || actors.size() == 0 ||
     86        genres == null || genres.size() == 0)
     87        {
     88            model.addAttribute("error", "Not enough attributes, make sure all values are inputted, all of them are required");
     89            model.addAttribute("hasError", true);
     90            return "redirect:/add";
     91        }
     92        List<Person> actorsList = new LinkedList<>();
     93        for(Integer id: actors){
     94            Optional<Person> person = personService.findActorById(id);
     95            if(person.isEmpty()){
     96                model.addAttribute("error", "The actor with ID {" + id + "} was not found.");
     97                model.addAttribute("hasError", true);
     98                return "redirect:/add";
     99            }
     100            actorsList.add(person.get());
     101        }
     102
     103        List<Genre> genreList = new LinkedList<>();
     104        for(Integer id: genres){
     105            Optional<Genre> genre = genreService.findById(id);
     106            if(genre.isEmpty()){
     107                model.addAttribute("error", "The genre with ID {" + id + "} was not found.");
     108                model.addAttribute("hasError", true);
     109                return "redirect:/add";
     110            }
     111            genreList.add(genre.get());
     112        }
     113
     114        Optional<Person> directorOp = personService.findDirectorById(director_id);
     115        if(directorOp.isEmpty()){
     116            model.addAttribute("error", "The director with ID {" + director_id + "} was not found.");
     117            model.addAttribute("hasError", true);
     118            return "redirect:/add";
     119        }
     120
     121        Person director = directorOp.get();
     122
     123        Movie movie = new Movie(title, description, image_url, airing_date,
     124                    rating, director, actorsList, genreList);
     125
     126        movieService.save(movie);
     127
     128        return "redirect:/movies";
     129    }
     130
     131    @PostMapping("/edit/confirm")
     132    public String editMoviePost(
     133                                @RequestParam Integer movie_id,
     134                                @RequestParam String title,
     135                               @RequestParam String description,
     136                               @RequestParam String image_url,
     137                               @RequestParam Date airing_date,
     138                               @RequestParam Float rating,
     139                               @RequestParam Integer director_id,
     140                               @RequestParam List<Integer> actors,
     141                               @RequestParam List<Integer> genres,
     142                               Model model){
     143        if(
     144                movie_id == null ||
     145                title == null || title.isEmpty() ||
     146                description == null || description.isEmpty() ||
     147                image_url == null || image_url.isEmpty() ||
     148                airing_date == null ||
     149                rating == null ||
     150                director_id == null ||
     151                actors == null || actors.size() == 0 ||
     152                genres == null || genres.size() == 0)
     153        {
     154            model.addAttribute("error", "Not enough attributes, make sure all values are inputted, all of them are required");
     155            model.addAttribute("hasError", true);
     156            return "redirect:/edit";
     157        }
     158
     159        Optional<Movie> movieOptional = movieService.findById(movie_id);
     160        if(movieOptional.isEmpty()){
     161            model.addAttribute("error", "The movie with ID {" + movie_id + "} was not found.");
     162            model.addAttribute("hasError", true);
     163            return "redirect:/edit";
     164        }
     165        Movie movie = movieOptional.get();
     166
     167        List<Person> actorsList = new LinkedList<>();
     168        for(Integer id: actors){
     169            Optional<Person> person = personService.findActorById(id);
     170            if(person.isEmpty()){
     171                model.addAttribute("error", "The actor with ID {" + id + "} was not found.");
     172                model.addAttribute("hasError", true);
     173                return "redirect:/edit";
     174            }
     175            actorsList.add(person.get());
     176        }
     177
     178        List<Genre> genreList = new LinkedList<>();
     179        for(Integer id: genres){
     180            Optional<Genre> genre = genreService.findById(id);
     181            if(genre.isEmpty()){
     182                model.addAttribute("error", "The genre with ID {" + id + "} was not found.");
     183                model.addAttribute("hasError", true);
     184                return "redirect:/edit";
     185            }
     186            genreList.add(genre.get());
     187        }
     188
     189        Optional<Person> directorOp = personService.findDirectorById(director_id);
     190        if(directorOp.isEmpty()){
     191            model.addAttribute("error", "The director with ID {" + director_id + "} was not found.");
     192            model.addAttribute("hasError", true);
     193            return "redirect:/edit";
     194        }
     195
     196        Person director = directorOp.get();
     197
     198        movieService.deleteById(movie_id);
     199
     200        movie.setActors(actorsList);
     201        movie.setDirector(director);
     202        movie.setGenres(genreList);
     203        movie.setTitle(title);
     204        movie.setDescription(description);
     205        movie.setAiring_date(airing_date);
     206        movie.setImage_url(image_url);
     207        movie.setImdb_rating(rating);
     208
     209        movieService.save(movie);
     210
     211        return "redirect:/movies";
     212    }
     213
     214    @GetMapping("/{id}/edit")
     215    public String editMovie(@PathVariable Integer id, Model model){
     216        Movie movie = movieService.findById(id).orElseThrow(() -> new MovieIdNotFoundException(id));
     217        model.addAttribute("directors", personService.findAllDirectors());
     218        model.addAttribute("actors", personService.findAllActors());
     219        model.addAttribute("genres", genreService.findAll());
     220        model.addAttribute("movie", movie);
     221        model.addAttribute("contentTemplate", "moviesEdit");
     222        return "template";
     223    }
    36224}
Note: See TracChangeset for help on using the changeset viewer.