Ignore:
Timestamp:
02/04/22 23:22:13 (3 years ago)
Author:
Test <matonikolov77@…>
Branches:
main
Children:
5b447b0
Parents:
3ded84d
Message:

Added CRUD for movies,persons,discussion,replies,genres
Added ajaxcalls

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/main/java/com/wediscussmovies/project/web/controller/MovieController.java

    r3ded84d re0ef1b1  
    11package com.wediscussmovies.project.web.controller;
    22
    3 import com.wediscussmovies.project.exception.MovieIdNotFoundException;
    4 import com.wediscussmovies.project.model.*;
    5 
     3import com.wediscussmovies.project.model.Movie;
     4import com.wediscussmovies.project.model.Person;
     5import com.wediscussmovies.project.model.User;
    66import com.wediscussmovies.project.service.GenreService;
    77import com.wediscussmovies.project.service.MovieService;
    88import com.wediscussmovies.project.service.PersonService;
     9import lombok.Data;
     10import org.springframework.format.annotation.DateTimeFormat;
     11import org.springframework.security.authentication.AnonymousAuthenticationToken;
     12import org.springframework.security.core.Authentication;
     13import org.springframework.security.core.context.SecurityContextHolder;
     14import org.springframework.security.core.userdetails.UserDetails;
    915import org.springframework.stereotype.Controller;
    1016import org.springframework.ui.Model;
     
    1218
    1319import java.sql.Date;
    14 import java.util.LinkedList;
     20import java.time.LocalDate;
    1521import java.util.List;
    16 import java.util.Optional;
    1722
    1823@Controller
     
    3035
    3136    @GetMapping
    32     public String getMovies(@RequestParam(required = false) String titleQuery, Model model){
     37    public String getMovies(@RequestParam(required = false) String titleQuery, Model model,
     38                            @RequestParam(required = false) String error){
    3339        List<Movie> movies;
    3440        if(titleQuery == null ) {
     
    3945        }
    4046
     47        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
     48
     49        if (!(auth instanceof AnonymousAuthenticationToken)){
     50            UserDetails userDetails = (UserDetails) auth.getPrincipal();
     51            User user = (User) userDetails;
     52          model.addAttribute("likedMovies",this.movieService.findLikedMoviesByUser(user));
     53          model.addAttribute("user",user);
     54        }
    4155
    4256        model.addAttribute("movies", movies);
    4357        model.addAttribute("contentTemplate", "moviesList");
    44         return "list";
     58        if (error != null && !error.equals(" "))
     59            model.addAttribute("error",error);
     60        return "template";
    4561    }
    4662
     
    4864    @GetMapping("/add")
    4965    public String addMovie(Model model){
    50         model.addAttribute("directors", personService.findAllDirectors());
    51         model.addAttribute("actors", personService.findAllActors());
    52         model.addAttribute("genres", genreService.findAll());
     66
     67        addModelProperties(model);
    5368        model.addAttribute("contentTemplate", "moviesAdd");
    5469        return "template";
     
    5671
    5772
    58     @PostMapping("/{id}/delete")
    59     public String addMovie(@PathVariable Integer id){
    60         Optional<Movie> movie = movieService.findById(id);
    61         if(movie.isPresent()){
    62             movieService.deleteById(movie.get().getMovieId());
    63         }
    64         return "redirect:/movies";
     73    @GetMapping("/{id}/delete")
     74    public String deleteMovie(@PathVariable Integer id){
     75
     76       try {
     77           this.movieService.deleteById(id);
     78           return "redirect:/movies";
     79
     80       }
     81       catch (RuntimeException exc){
     82           return "redirect:/movies?error="+exc.getMessage();
     83       }
    6584    }
    6685
    67     @PostMapping("/add/confirm")
    68     public String addMoviePost(@RequestParam String title,
     86    @PostMapping("/save")
     87    public String saveMovie(
     88
     89
     90
     91            @RequestParam String title,
    6992                               @RequestParam String description,
    70                                @RequestParam String image_url,
    71                                @RequestParam Date airing_date,
    72                                @RequestParam Double rating,
    73                                @RequestParam Integer director_id,
     93                               @RequestParam String imageUrl,
     94                               @RequestParam(required = false) Double rating,
     95                               @RequestParam(required = false) Integer directorId,
     96                               @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate airingDate,
    7497                               @RequestParam List<Integer> actors,
    75                                @RequestParam List<Integer> genres,
    76                                Model model){
    77         if(title == null || title.isEmpty() ||
    78         description == null || description.isEmpty() ||
    79         image_url == null || image_url.isEmpty() ||
    80         airing_date == null ||
    81         rating == null ||
    82         director_id == null ||
    83         actors == null || actors.size() == 0 ||
    84         genres == null || genres.size() == 0)
    85         {
    86             model.addAttribute("error", "Not enough attributes, make sure all values are inputted, all of them are required");
    87             model.addAttribute("hasError", true);
    88             return "redirect:/add";
     98                               @RequestParam List<Integer> genres
     99                            ){
     100
     101        try {
     102            Movie movie = this.movieService.save(title,description,imageUrl,Date.valueOf(airingDate),rating,directorId,actors,genres);
     103
     104            return "redirect:/movies";
    89105        }
    90         List<Person> actorsList = new LinkedList<>();
    91         for(Integer id: actors){
    92             Optional<Person> person = personService.findActorById(id);
    93             if(person.isEmpty()){
    94                 model.addAttribute("error", "The actor with ID {" + id + "} was not found.");
    95                 model.addAttribute("hasError", true);
    96                 return "redirect:/add";
    97             }
    98             actorsList.add(person.get());
     106        catch (RuntimeException exc){
     107            return "redirect:/movies?error="+exc.getMessage();
    99108        }
     109    }
     110    @PostMapping("/save/{movieId}")
     111    public String updateMovie(
    100112
    101         List<Genre> genreList = new LinkedList<>();
    102         for(Integer id: genres){
    103             Optional<Genre> genre = genreService.findById(id);
    104             if(genre.isEmpty()){
    105                 model.addAttribute("error", "The genre with ID {" + id + "} was not found.");
    106                 model.addAttribute("hasError", true);
    107                 return "redirect:/add";
    108             }
    109             genreList.add(genre.get());
     113
     114            @PathVariable Integer movieId,
     115            @RequestParam String title,
     116            @RequestParam String description,
     117            @RequestParam String imageUrl,
     118            @RequestParam(required = false) Double rating,
     119            @RequestParam(required = false) Integer directorId,
     120            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate airingDate,
     121            @RequestParam List<Integer> actors,
     122            @RequestParam List<Integer> genres
     123    ){
     124
     125        try {
     126            Movie movie = this.movieService.edit(movieId,title,description,imageUrl,Date.valueOf(airingDate),rating,directorId,actors,genres);
     127
     128            return "redirect:/movies";
    110129        }
    111 
    112         Optional<Person> directorOp = personService.findDirectorById(director_id);
    113         if(directorOp.isEmpty()){
    114             model.addAttribute("error", "The director with ID {" + director_id + "} was not found.");
    115             model.addAttribute("hasError", true);
    116             return "redirect:/add";
     130        catch (RuntimeException exc){
     131            return "redirect:/movies?error="+exc.getMessage();
    117132        }
    118 
    119         Person director = directorOp.get();
    120 
    121         Movie movie = new Movie(title, description, image_url, airing_date,
    122                     rating, director.getPersonId());
    123 
    124         movieService.save(movie);
    125 
    126         return "redirect:/movies";
    127133    }
    128134
    129     @PostMapping("/edit/confirm")
    130     public String editMoviePost(
    131                                 @RequestParam Integer movie_id,
    132                                 @RequestParam String title,
    133                                @RequestParam String description,
    134                                @RequestParam String image_url,
    135                                @RequestParam Date airing_date,
    136                                @RequestParam Double rating,
    137                                @RequestParam Integer director_id,
    138                                @RequestParam List<Integer> actors,
    139                                @RequestParam List<Integer> genres,
    140                                Model model){
    141         if(
    142                 movie_id == null ||
    143                 title == null || title.isEmpty() ||
    144                 description == null || description.isEmpty() ||
    145                 image_url == null || image_url.isEmpty() ||
    146                 airing_date == null ||
    147                 rating == null ||
    148                 director_id == null ||
    149                 actors == null || actors.size() == 0 ||
    150                 genres == null || genres.size() == 0)
    151         {
    152             model.addAttribute("error", "Not enough attributes, make sure all values are inputted, all of them are required");
    153             model.addAttribute("hasError", true);
    154             return "redirect:/edit";
    155         }
    156135
    157         Optional<Movie> movieOptional = movieService.findById(movie_id);
    158         if(movieOptional.isEmpty()){
    159             model.addAttribute("error", "The movie with ID {" + movie_id + "} was not found.");
    160             model.addAttribute("hasError", true);
    161             return "redirect:/edit";
    162         }
    163         Movie movie = movieOptional.get();
    164 
    165         List<Person> actorsList = new LinkedList<>();
    166         for(Integer id: actors){
    167             Optional<Person> person = personService.findActorById(id);
    168             if(person.isEmpty()){
    169                 model.addAttribute("error", "The actor with ID {" + id + "} was not found.");
    170                 model.addAttribute("hasError", true);
    171                 return "redirect:/edit";
    172             }
    173             actorsList.add(person.get());
    174         }
    175 
    176         List<Genre> genreList = new LinkedList<>();
    177         for(Integer id: genres){
    178             Optional<Genre> genre = genreService.findById(id);
    179             if(genre.isEmpty()){
    180                 model.addAttribute("error", "The genre with ID {" + id + "} was not found.");
    181                 model.addAttribute("hasError", true);
    182                 return "redirect:/edit";
    183             }
    184             genreList.add(genre.get());
    185         }
    186 
    187         Optional<Person> directorOp = personService.findDirectorById(director_id);
    188         if(directorOp.isEmpty()){
    189             model.addAttribute("error", "The director with ID {" + director_id + "} was not found.");
    190             model.addAttribute("hasError", true);
    191             return "redirect:/edit";
    192         }
    193 
    194         Person director = directorOp.get();
    195 
    196         movieService.deleteById(movie_id);
    197 
    198 //      //  movie.setActors(actorsList);
    199 //        movie.setDirector(director);
    200 //        movie.setGenres(genreList);
    201 //        movie.setTitle(title);
    202 //        movie.setDescription(description);
    203 //        movie.setAringDate(airing_date);
    204 //        movie.setImageUrl(image_url);
    205 //        movie.setImbdRating(rating);
    206 
    207         movieService.save(movie);
    208 
    209         return "redirect:/movies";
    210     }
    211136
    212137    @GetMapping("/{id}/edit")
    213138    public String editMovie(@PathVariable Integer id, Model model){
    214         Movie movie = movieService.findById(id).orElseThrow(() -> new MovieIdNotFoundException(id));
    215         model.addAttribute("directors", personService.findAllDirectors());
     139        try {
     140            Movie movie = movieService.findById(id);
     141            addModelProperties(model);
     142            model.addAttribute("movie", movie);
     143            model.addAttribute("director",movie.getDirector());
     144            model.addAttribute("movieActors",movieService.findAllActorsForMovie(movie));
     145            model.addAttribute("movieGenres",movieService.findAllGenresForMovie(movie));
     146            model.addAttribute("contentTemplate", "moviesAdd");
     147            return "template";
     148
     149        }
     150        catch (RuntimeException exc){
     151            return "redirect:/movies?error="+exc.getMessage();
     152        }
     153
     154    }
     155    private void addModelProperties(Model model){
     156
     157        model.addAttribute("directors",  personService.findAllDirectors());
    216158        model.addAttribute("actors", personService.findAllActors());
    217159        model.addAttribute("genres", genreService.findAll());
    218         model.addAttribute("movie", movie);
    219         model.addAttribute("contentTemplate", "moviesEdit");
    220         return "template";
    221160    }
    222161}
Note: See TracChangeset for help on using the changeset viewer.