Index: src/main/java/com/wediscussmovies/project/web/controller/MovieController.java
===================================================================
--- src/main/java/com/wediscussmovies/project/web/controller/MovieController.java	(revision 3ded84dfbf8ff25bf27cc822ac97b84a063fa95a)
+++ src/main/java/com/wediscussmovies/project/web/controller/MovieController.java	(revision e0ef1b13a29101c3f7a56f68da8dcdc8cce98b76)
@@ -1,10 +1,16 @@
 package com.wediscussmovies.project.web.controller;
 
-import com.wediscussmovies.project.exception.MovieIdNotFoundException;
-import com.wediscussmovies.project.model.*;
-
+import com.wediscussmovies.project.model.Movie;
+import com.wediscussmovies.project.model.Person;
+import com.wediscussmovies.project.model.User;
 import com.wediscussmovies.project.service.GenreService;
 import com.wediscussmovies.project.service.MovieService;
 import com.wediscussmovies.project.service.PersonService;
+import lombok.Data;
+import org.springframework.format.annotation.DateTimeFormat;
+import org.springframework.security.authentication.AnonymousAuthenticationToken;
+import org.springframework.security.core.Authentication;
+import org.springframework.security.core.context.SecurityContextHolder;
+import org.springframework.security.core.userdetails.UserDetails;
 import org.springframework.stereotype.Controller;
 import org.springframework.ui.Model;
@@ -12,7 +18,6 @@
 
 import java.sql.Date;
-import java.util.LinkedList;
+import java.time.LocalDate;
 import java.util.List;
-import java.util.Optional;
 
 @Controller
@@ -30,5 +35,6 @@
 
     @GetMapping
-    public String getMovies(@RequestParam(required = false) String titleQuery, Model model){
+    public String getMovies(@RequestParam(required = false) String titleQuery, Model model,
+                            @RequestParam(required = false) String error){
         List<Movie> movies;
         if(titleQuery == null ) {
@@ -39,8 +45,18 @@
         }
 
+        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
+
+        if (!(auth instanceof AnonymousAuthenticationToken)){
+            UserDetails userDetails = (UserDetails) auth.getPrincipal();
+            User user = (User) userDetails;
+          model.addAttribute("likedMovies",this.movieService.findLikedMoviesByUser(user));
+          model.addAttribute("user",user);
+        }
 
         model.addAttribute("movies", movies);
         model.addAttribute("contentTemplate", "moviesList");
-        return "list";
+        if (error != null && !error.equals(" "))
+            model.addAttribute("error",error);
+        return "template";
     }
 
@@ -48,7 +64,6 @@
     @GetMapping("/add")
     public String addMovie(Model model){
-        model.addAttribute("directors", personService.findAllDirectors());
-        model.addAttribute("actors", personService.findAllActors());
-        model.addAttribute("genres", genreService.findAll());
+
+        addModelProperties(model);
         model.addAttribute("contentTemplate", "moviesAdd");
         return "template";
@@ -56,167 +71,91 @@
 
 
-    @PostMapping("/{id}/delete")
-    public String addMovie(@PathVariable Integer id){
-        Optional<Movie> movie = movieService.findById(id);
-        if(movie.isPresent()){
-            movieService.deleteById(movie.get().getMovieId());
-        }
-        return "redirect:/movies";
+    @GetMapping("/{id}/delete")
+    public String deleteMovie(@PathVariable Integer id){
+
+       try {
+           this.movieService.deleteById(id);
+           return "redirect:/movies";
+
+       }
+       catch (RuntimeException exc){
+           return "redirect:/movies?error="+exc.getMessage();
+       }
     }
 
-    @PostMapping("/add/confirm")
-    public String addMoviePost(@RequestParam String title,
+    @PostMapping("/save")
+    public String saveMovie(
+
+
+
+            @RequestParam String title,
                                @RequestParam String description,
-                               @RequestParam String image_url,
-                               @RequestParam Date airing_date,
-                               @RequestParam Double rating,
-                               @RequestParam Integer director_id,
+                               @RequestParam String imageUrl,
+                               @RequestParam(required = false) Double rating,
+                               @RequestParam(required = false) Integer directorId,
+                               @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate airingDate,
                                @RequestParam List<Integer> actors,
-                               @RequestParam List<Integer> genres,
-                               Model model){
-        if(title == null || title.isEmpty() ||
-        description == null || description.isEmpty() ||
-        image_url == null || image_url.isEmpty() ||
-        airing_date == null ||
-        rating == null ||
-        director_id == null ||
-        actors == null || actors.size() == 0 ||
-        genres == null || genres.size() == 0)
-        {
-            model.addAttribute("error", "Not enough attributes, make sure all values are inputted, all of them are required");
-            model.addAttribute("hasError", true);
-            return "redirect:/add";
+                               @RequestParam List<Integer> genres
+                            ){
+
+        try {
+            Movie movie = this.movieService.save(title,description,imageUrl,Date.valueOf(airingDate),rating,directorId,actors,genres);
+
+            return "redirect:/movies";
         }
-        List<Person> actorsList = new LinkedList<>();
-        for(Integer id: actors){
-            Optional<Person> person = personService.findActorById(id);
-            if(person.isEmpty()){
-                model.addAttribute("error", "The actor with ID {" + id + "} was not found.");
-                model.addAttribute("hasError", true);
-                return "redirect:/add";
-            }
-            actorsList.add(person.get());
+        catch (RuntimeException exc){
+            return "redirect:/movies?error="+exc.getMessage();
         }
+    }
+    @PostMapping("/save/{movieId}")
+    public String updateMovie(
 
-        List<Genre> genreList = new LinkedList<>();
-        for(Integer id: genres){
-            Optional<Genre> genre = genreService.findById(id);
-            if(genre.isEmpty()){
-                model.addAttribute("error", "The genre with ID {" + id + "} was not found.");
-                model.addAttribute("hasError", true);
-                return "redirect:/add";
-            }
-            genreList.add(genre.get());
+
+            @PathVariable Integer movieId,
+            @RequestParam String title,
+            @RequestParam String description,
+            @RequestParam String imageUrl,
+            @RequestParam(required = false) Double rating,
+            @RequestParam(required = false) Integer directorId,
+            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate airingDate,
+            @RequestParam List<Integer> actors,
+            @RequestParam List<Integer> genres
+    ){
+
+        try {
+            Movie movie = this.movieService.edit(movieId,title,description,imageUrl,Date.valueOf(airingDate),rating,directorId,actors,genres);
+
+            return "redirect:/movies";
         }
-
-        Optional<Person> directorOp = personService.findDirectorById(director_id);
-        if(directorOp.isEmpty()){
-            model.addAttribute("error", "The director with ID {" + director_id + "} was not found.");
-            model.addAttribute("hasError", true);
-            return "redirect:/add";
+        catch (RuntimeException exc){
+            return "redirect:/movies?error="+exc.getMessage();
         }
-
-        Person director = directorOp.get();
-
-        Movie movie = new Movie(title, description, image_url, airing_date,
-                    rating, director.getPersonId());
-
-        movieService.save(movie);
-
-        return "redirect:/movies";
     }
 
-    @PostMapping("/edit/confirm")
-    public String editMoviePost(
-                                @RequestParam Integer movie_id,
-                                @RequestParam String title,
-                               @RequestParam String description,
-                               @RequestParam String image_url,
-                               @RequestParam Date airing_date,
-                               @RequestParam Double rating,
-                               @RequestParam Integer director_id,
-                               @RequestParam List<Integer> actors,
-                               @RequestParam List<Integer> genres,
-                               Model model){
-        if(
-                movie_id == null ||
-                title == null || title.isEmpty() ||
-                description == null || description.isEmpty() ||
-                image_url == null || image_url.isEmpty() ||
-                airing_date == null ||
-                rating == null ||
-                director_id == null ||
-                actors == null || actors.size() == 0 ||
-                genres == null || genres.size() == 0)
-        {
-            model.addAttribute("error", "Not enough attributes, make sure all values are inputted, all of them are required");
-            model.addAttribute("hasError", true);
-            return "redirect:/edit";
-        }
 
-        Optional<Movie> movieOptional = movieService.findById(movie_id);
-        if(movieOptional.isEmpty()){
-            model.addAttribute("error", "The movie with ID {" + movie_id + "} was not found.");
-            model.addAttribute("hasError", true);
-            return "redirect:/edit";
-        }
-        Movie movie = movieOptional.get();
-
-        List<Person> actorsList = new LinkedList<>();
-        for(Integer id: actors){
-            Optional<Person> person = personService.findActorById(id);
-            if(person.isEmpty()){
-                model.addAttribute("error", "The actor with ID {" + id + "} was not found.");
-                model.addAttribute("hasError", true);
-                return "redirect:/edit";
-            }
-            actorsList.add(person.get());
-        }
-
-        List<Genre> genreList = new LinkedList<>();
-        for(Integer id: genres){
-            Optional<Genre> genre = genreService.findById(id);
-            if(genre.isEmpty()){
-                model.addAttribute("error", "The genre with ID {" + id + "} was not found.");
-                model.addAttribute("hasError", true);
-                return "redirect:/edit";
-            }
-            genreList.add(genre.get());
-        }
-
-        Optional<Person> directorOp = personService.findDirectorById(director_id);
-        if(directorOp.isEmpty()){
-            model.addAttribute("error", "The director with ID {" + director_id + "} was not found.");
-            model.addAttribute("hasError", true);
-            return "redirect:/edit";
-        }
-
-        Person director = directorOp.get();
-
-        movieService.deleteById(movie_id);
-
-//      //  movie.setActors(actorsList);
-//        movie.setDirector(director);
-//        movie.setGenres(genreList);
-//        movie.setTitle(title);
-//        movie.setDescription(description);
-//        movie.setAringDate(airing_date);
-//        movie.setImageUrl(image_url);
-//        movie.setImbdRating(rating);
-
-        movieService.save(movie);
-
-        return "redirect:/movies";
-    }
 
     @GetMapping("/{id}/edit")
     public String editMovie(@PathVariable Integer id, Model model){
-        Movie movie = movieService.findById(id).orElseThrow(() -> new MovieIdNotFoundException(id));
-        model.addAttribute("directors", personService.findAllDirectors());
+        try {
+            Movie movie = movieService.findById(id);
+            addModelProperties(model);
+            model.addAttribute("movie", movie);
+            model.addAttribute("director",movie.getDirector());
+            model.addAttribute("movieActors",movieService.findAllActorsForMovie(movie));
+            model.addAttribute("movieGenres",movieService.findAllGenresForMovie(movie));
+            model.addAttribute("contentTemplate", "moviesAdd");
+            return "template";
+
+        }
+        catch (RuntimeException exc){
+            return "redirect:/movies?error="+exc.getMessage();
+        }
+
+    }
+    private void addModelProperties(Model model){
+
+        model.addAttribute("directors",  personService.findAllDirectors());
         model.addAttribute("actors", personService.findAllActors());
         model.addAttribute("genres", genreService.findAll());
-        model.addAttribute("movie", movie);
-        model.addAttribute("contentTemplate", "moviesEdit");
-        return "template";
     }
 }
