Index: src/main/java/com/wediscussmovies/project/WeDiscussMoviesApplication.java
===================================================================
--- src/main/java/com/wediscussmovies/project/WeDiscussMoviesApplication.java	(revision 3ded84dfbf8ff25bf27cc822ac97b84a063fa95a)
+++ src/main/java/com/wediscussmovies/project/WeDiscussMoviesApplication.java	(revision e0ef1b13a29101c3f7a56f68da8dcdc8cce98b76)
@@ -1,9 +1,10 @@
 package com.wediscussmovies.project;
 
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.boot.CommandLineRunner;
+
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
-import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.context.annotation.Bean;
+import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
+import org.springframework.security.crypto.password.PasswordEncoder;
 
 @SpringBootApplication
@@ -14,4 +15,8 @@
         SpringApplication.run(WeDiscussMoviesApplication.class, args);
     }
+    @Bean
+    PasswordEncoder getPasswordEncoder(){
+        return new BCryptPasswordEncoder(10);
+    }
 
 
Index: src/main/java/com/wediscussmovies/project/exception/MovieIdNotFoundException.java
===================================================================
--- src/main/java/com/wediscussmovies/project/exception/MovieIdNotFoundException.java	(revision 3ded84dfbf8ff25bf27cc822ac97b84a063fa95a)
+++ 	(revision )
@@ -1,7 +1,0 @@
-package com.wediscussmovies.project.exception;
-
-public class MovieIdNotFoundException extends RuntimeException{
-    public MovieIdNotFoundException(Integer id){
-        super("Movie with that id=" + id + " does not exist!");
-    }
-}
Index: src/main/java/com/wediscussmovies/project/repository/DiscussionRepository.java
===================================================================
--- src/main/java/com/wediscussmovies/project/repository/DiscussionRepository.java	(revision 3ded84dfbf8ff25bf27cc822ac97b84a063fa95a)
+++ src/main/java/com/wediscussmovies/project/repository/DiscussionRepository.java	(revision e0ef1b13a29101c3f7a56f68da8dcdc8cce98b76)
@@ -1,12 +1,16 @@
 package com.wediscussmovies.project.repository;
 
+import com.wediscussmovies.project.model.Discussion;
+import com.wediscussmovies.project.model.Movie;
+import com.wediscussmovies.project.model.Person;
 import org.springframework.data.jpa.repository.JpaRepository;
 import org.springframework.stereotype.Repository;
 
 import java.util.List;
-import com.wediscussmovies.project.model.*;
 
 @Repository
 public interface DiscussionRepository extends JpaRepository<Discussion, Integer> {
-    public List<Discussion> findAllByTitleLike(String title);
+     List<Discussion> findAllByTitleLike(String title);
+     List<Discussion> findAllByMovie(Movie movie);
+     List<Discussion> findAllByPerson (Person person);
 }
Index: src/main/java/com/wediscussmovies/project/repository/GenreRepository.java
===================================================================
--- src/main/java/com/wediscussmovies/project/repository/GenreRepository.java	(revision 3ded84dfbf8ff25bf27cc822ac97b84a063fa95a)
+++ src/main/java/com/wediscussmovies/project/repository/GenreRepository.java	(revision e0ef1b13a29101c3f7a56f68da8dcdc8cce98b76)
@@ -1,12 +1,22 @@
 package com.wediscussmovies.project.repository;
 
-import com.wediscussmovies.project.model.*;
+import com.wediscussmovies.project.model.Genre;
+import com.wediscussmovies.project.querymodels.GenreLikes;
 import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.Query;
 import org.springframework.stereotype.Repository;
 
+import javax.transaction.Transactional;
 import java.util.List;
 
 @Repository
 public interface GenreRepository extends JpaRepository<Genre, Integer> {
-    public List<Genre> findAllByGenreType(String genre);
+     List<Genre> findAllByGenreType(String genre);
+
+     @Query(value = "select new com.wediscussmovies.project.querymodels.GenreLikes(g.genreType, count(ug.id.userId)) from Genre g" +
+             " left join UserGenres ug on ug.id.genreId = g.genreId" +
+             " group by g.genreType" +
+             " order by count(ug.id.userId) desc")
+     @Transactional
+     List<GenreLikes> findAllWithLikes();
 }
Index: src/main/java/com/wediscussmovies/project/repository/MovieRepository.java
===================================================================
--- src/main/java/com/wediscussmovies/project/repository/MovieRepository.java	(revision 3ded84dfbf8ff25bf27cc822ac97b84a063fa95a)
+++ src/main/java/com/wediscussmovies/project/repository/MovieRepository.java	(revision e0ef1b13a29101c3f7a56f68da8dcdc8cce98b76)
@@ -1,12 +1,15 @@
 package com.wediscussmovies.project.repository;
 
+import com.wediscussmovies.project.model.Movie;
+import com.wediscussmovies.project.model.Person;
 import org.springframework.data.jpa.repository.JpaRepository;
 import org.springframework.stereotype.Repository;
 
 import java.util.List;
-import com.wediscussmovies.project.model.*;
 
 @Repository
 public interface MovieRepository extends JpaRepository<Movie, Integer> {
-    public List<Movie> findAllByTitleLike(String Title);
+     List<Movie> findAllByTitleLike(String Title);
+     List<Movie> findAllByDirector(Person director);
+     List<Movie> findAllByDirectorIsNull();
 }
Index: src/main/java/com/wediscussmovies/project/repository/PersonRepository.java
===================================================================
--- src/main/java/com/wediscussmovies/project/repository/PersonRepository.java	(revision 3ded84dfbf8ff25bf27cc822ac97b84a063fa95a)
+++ src/main/java/com/wediscussmovies/project/repository/PersonRepository.java	(revision e0ef1b13a29101c3f7a56f68da8dcdc8cce98b76)
@@ -1,17 +1,16 @@
 package com.wediscussmovies.project.repository;
 
-import com.wediscussmovies.project.model.*;
-import com.wediscussmovies.project.model.enumerations.PersonType;
+import com.wediscussmovies.project.model.Person;
 import org.springframework.data.jpa.repository.JpaRepository;
 import org.springframework.stereotype.Repository;
 
 import java.util.List;
-import java.util.Optional;
 
 @Repository
 public interface PersonRepository extends JpaRepository<Person, Integer> {
-    public List<Person> findAllByType(Character type);
-    public List<Person> findAllByTypeAndNameLike(Character type, String name);
-    public List<Person> findAllByTypeAndSurnameLike(Character type, String surname);
-    public Optional<Person> findPersonByPersonIdAndType(Integer id, Character type);
+     List<Person> findAllByType(Character type);
+     List<Person> findAllByTypeAndNameLike(Character type, String name);
+     List<Person> findAllByTypeAndSurnameLike(Character type, String surname);
+    // Optional<Person> findPersonByPersonIdAndType(Integer id, Character type);
+     List<Person> findAllByTypeAndNameLikeOrSurnameLike(Character type,String name, String surname);
 }
Index: src/main/java/com/wediscussmovies/project/repository/ReplyRepository.java
===================================================================
--- src/main/java/com/wediscussmovies/project/repository/ReplyRepository.java	(revision 3ded84dfbf8ff25bf27cc822ac97b84a063fa95a)
+++ src/main/java/com/wediscussmovies/project/repository/ReplyRepository.java	(revision e0ef1b13a29101c3f7a56f68da8dcdc8cce98b76)
@@ -1,10 +1,26 @@
 package com.wediscussmovies.project.repository;
 
+import com.wediscussmovies.project.model.Discussion;
+import com.wediscussmovies.project.model.Reply;
+import com.wediscussmovies.project.model.primarykeys.ReplyPK;
 import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.Modifying;
+import org.springframework.data.jpa.repository.Query;
+import org.springframework.data.repository.query.Param;
 import org.springframework.stereotype.Repository;
-import com.wediscussmovies.project.model.*;
+
+import javax.transaction.Transactional;
+import java.sql.Date;
+import java.util.List;
 
 
 @Repository
-public interface ReplyRepository extends JpaRepository<Reply, Integer> {
+public interface ReplyRepository extends JpaRepository<Reply, ReplyPK> {
+    List<Reply> findAllByDiscussion(Discussion discussion);
+
+    @Modifying
+    @Query(value = "insert into project.replies (text,date,user_id,discussion_id) values(:text,:date,:user_id,:discussion_id)",nativeQuery = true)
+    @Transactional
+    void insertInto(@Param("text") String text, @Param("date")Date date,
+                    @Param("user_id")Integer userId,@Param("discussion_id")Integer discussionId);
 }
Index: src/main/java/com/wediscussmovies/project/repository/UserRepository.java
===================================================================
--- src/main/java/com/wediscussmovies/project/repository/UserRepository.java	(revision 3ded84dfbf8ff25bf27cc822ac97b84a063fa95a)
+++ src/main/java/com/wediscussmovies/project/repository/UserRepository.java	(revision e0ef1b13a29101c3f7a56f68da8dcdc8cce98b76)
@@ -1,5 +1,5 @@
 package com.wediscussmovies.project.repository;
 
-import com.wediscussmovies.project.model.*;
+import com.wediscussmovies.project.model.User;
 import org.springframework.data.jpa.repository.JpaRepository;
 import org.springframework.stereotype.Repository;
@@ -9,7 +9,4 @@
 @Repository
 public interface UserRepository extends JpaRepository<User, Integer> {
-    public Optional<User> findByEmailAndPassword(String email, String password);
-    public Optional<User> findByUsernameAndPassword(String email, String password);
-    public Optional<User> findByUsername(String username);
-    public Optional<User> findByEmail(String email);
+     Optional<User> findByUsername(String username);
 }
Index: src/main/java/com/wediscussmovies/project/service/DiscussionService.java
===================================================================
--- src/main/java/com/wediscussmovies/project/service/DiscussionService.java	(revision 3ded84dfbf8ff25bf27cc822ac97b84a063fa95a)
+++ src/main/java/com/wediscussmovies/project/service/DiscussionService.java	(revision e0ef1b13a29101c3f7a56f68da8dcdc8cce98b76)
@@ -1,13 +1,18 @@
 package com.wediscussmovies.project.service;
 
-import com.wediscussmovies.project.model.*;
+import com.wediscussmovies.project.model.Discussion;
+import com.wediscussmovies.project.model.User;
 
 import java.util.List;
-import java.util.Optional;
 
 public interface DiscussionService {
     List<Discussion> listAll();
     List<Discussion> listAllByTitle(String title);
-    Optional<Discussion> findById(Integer id);
-    void save(Discussion discussion);
+    Discussion findById(Integer id);
+    void save(Character type,Integer id, String title, String text, User user);
+    void edit(Integer discussionId,Character type,Integer id, String title, String text);
+    void deleteById(Integer discussionId);
+    void likeDiscussion(Integer discussionId,Integer userId);
+    void unlikeDiscussion(Integer discussionId,Integer userId);
+    List<Discussion> findAllForPersonOrMovie(Integer id,Character type);
 }
Index: src/main/java/com/wediscussmovies/project/service/GenreService.java
===================================================================
--- src/main/java/com/wediscussmovies/project/service/GenreService.java	(revision 3ded84dfbf8ff25bf27cc822ac97b84a063fa95a)
+++ src/main/java/com/wediscussmovies/project/service/GenreService.java	(revision e0ef1b13a29101c3f7a56f68da8dcdc8cce98b76)
@@ -1,15 +1,17 @@
 package com.wediscussmovies.project.service;
 
-import com.wediscussmovies.project.model.*;
+import com.wediscussmovies.project.model.Genre;
+import com.wediscussmovies.project.querymodels.GenreLikes;
 import org.springframework.stereotype.Service;
 
 import java.util.List;
-import java.util.Optional;
 
 @Service
 public interface GenreService {
-    public List<Genre> findAll();
-    public Optional<Genre> findById(Integer id);
-    public List<Genre> findAllByType(String genre);
-    public Genre save(String genreName);
+    List<Genre> findAll();
+    Genre findById(Integer id);
+    List<Genre> findAllByType(String genre);
+    Genre save(String genreName);
+    List<GenreLikes> findAllWithLikes();
+
 }
Index: src/main/java/com/wediscussmovies/project/service/MovieService.java
===================================================================
--- src/main/java/com/wediscussmovies/project/service/MovieService.java	(revision 3ded84dfbf8ff25bf27cc822ac97b84a063fa95a)
+++ src/main/java/com/wediscussmovies/project/service/MovieService.java	(revision e0ef1b13a29101c3f7a56f68da8dcdc8cce98b76)
@@ -1,14 +1,35 @@
 package com.wediscussmovies.project.service;
 
-import com.wediscussmovies.project.model.*;
+import com.wediscussmovies.project.ajaxmodels.Grade;
+import com.wediscussmovies.project.model.Genre;
+import com.wediscussmovies.project.model.Movie;
+import com.wediscussmovies.project.model.Person;
+import com.wediscussmovies.project.model.User;
 
+import java.sql.Date;
 import java.util.List;
-import java.util.Optional;
 
 public interface MovieService {
-    public List<Movie> listAll();
-    public List<Movie> searchByTitle(String title);
-    public Optional<Movie> findById(Integer id);
-    public Movie save(Movie movie);
-    public void deleteById(Integer id);
+     List<Movie> listAll();
+     List<Movie> listAllByType(Character type);
+     List<Movie> listAllWithoutDirector();
+     List<Movie> searchByTitle(String title);
+     Movie findById(Integer id);
+     Movie save(String title, String description, String imageUrl, Date airingDate,Double  rating,
+                Integer directorId,List<Integer> actorIds,List<Integer> genreIds);
+
+     Movie edit(Integer movieId,String title, String description, String imageUrl, Date airingDate,Double  rating,
+                Integer directorId,List<Integer> actorIds, List<Integer> genreIds);
+
+     List<Person> findAllActorsForMovie(Movie movie);
+     List<Genre> findAllGenresForMovie(Movie movie);
+
+     void likeMovie(Integer movieId, Integer userId);
+     void unlikeMovie(Integer movieId,Integer userId);
+
+     List<Movie> findLikedMoviesByUser(User user);
+
+     void deleteById(Integer id);
+
+     void addGradeMovie(Integer movieId, User user, Grade grade);
 }
Index: src/main/java/com/wediscussmovies/project/service/PersonService.java
===================================================================
--- src/main/java/com/wediscussmovies/project/service/PersonService.java	(revision 3ded84dfbf8ff25bf27cc822ac97b84a063fa95a)
+++ src/main/java/com/wediscussmovies/project/service/PersonService.java	(revision e0ef1b13a29101c3f7a56f68da8dcdc8cce98b76)
@@ -1,20 +1,30 @@
 package com.wediscussmovies.project.service;
 
-import com.wediscussmovies.project.model.*;
+import com.wediscussmovies.project.ajaxmodels.Grade;
+import com.wediscussmovies.project.model.Movie;
+import com.wediscussmovies.project.model.Person;
+import com.wediscussmovies.project.model.User;
 
+import java.sql.Date;
 import java.util.List;
-import java.util.Optional;
 
 public interface PersonService {
+    List<Person> findAll();
      List<Person> findAllDirectors();
      List<Person> findAllActors();
-    Optional<Person> findById(Integer person_id);
-    Optional<Person> findActorById(Integer id);
-    Optional<Person> findDirectorById(Integer id);
-    boolean save(Person person);
-    List<Person> findActorsByNameLike(String name);
-    List<Person> findActorsBySurnameLike(String surname);
-    List<Person> findDirectorsByNameLike(String name);
-    List<Person> findDirectorsBySurnameLike(String surname);
+    Person findById(Integer person_id);
+
+    Person  save(String name, String surname, Character type,
+                 Date birthDate, String image_url, String description, List<Integer> movieIds);
+
+    Person edit(Integer personId, String name, String surname, Character type,
+                Date birthDate, String image_url, String description, List<Integer> movieIds);
+
+    List<Person> findPersonsByNameOrSurname(Character type,String searchQuery);
+
+    List<Movie> findAllMoviesByPerson(Person person);
+
+    void deleteById(Integer id);
+    void addGradePerson(Integer personId, User user, Grade grade);
 
 }
Index: src/main/java/com/wediscussmovies/project/service/ReplyService.java
===================================================================
--- src/main/java/com/wediscussmovies/project/service/ReplyService.java	(revision 3ded84dfbf8ff25bf27cc822ac97b84a063fa95a)
+++ src/main/java/com/wediscussmovies/project/service/ReplyService.java	(revision e0ef1b13a29101c3f7a56f68da8dcdc8cce98b76)
@@ -1,12 +1,21 @@
 package com.wediscussmovies.project.service;
 
+import com.wediscussmovies.project.model.Discussion;
 import com.wediscussmovies.project.model.Reply;
+import com.wediscussmovies.project.model.User;
+import com.wediscussmovies.project.model.primarykeys.ReplyPK;
 
-import java.util.Optional;
+import java.util.List;
 
 public interface ReplyService {
-    public Reply save(Reply r);
-    public void delete(Reply r);
-    Optional<Reply> findById(Long id);
+    Reply edit(Integer replyId, Integer discussionId, String text);
+    void delete(Integer discussionId,Integer replyId);
+    Reply findById(Integer discussionId, Integer replyId);
+    void save(Integer discussionId, String text, User user);
+    void likeReply(Integer replyId,Integer userId);
+    void unlikeReply(Integer replyId,Integer userId);
+    List<Reply> findAllByDiscussion(Discussion discussion);
+
+
 
 }
Index: src/main/java/com/wediscussmovies/project/service/UserService.java
===================================================================
--- src/main/java/com/wediscussmovies/project/service/UserService.java	(revision 3ded84dfbf8ff25bf27cc822ac97b84a063fa95a)
+++ src/main/java/com/wediscussmovies/project/service/UserService.java	(revision e0ef1b13a29101c3f7a56f68da8dcdc8cce98b76)
@@ -1,11 +1,12 @@
 package com.wediscussmovies.project.service;
 
-import com.wediscussmovies.project.model.*;
+import com.wediscussmovies.project.model.User;
+import org.springframework.security.core.userdetails.UserDetailsService;
 
 import javax.servlet.http.HttpServletRequest;
 import java.util.Optional;
 
-public interface UserService {
-    public Optional<User> login(String email, String password);
-    public Optional<User> register(HttpServletRequest request, String email, String password, String confirmPassword, String username, String name, String surname);
+public interface UserService extends UserDetailsService {
+     User findByUsername(String username);
+     User register(String email, String username, String password, String confirmPassword, String name, String surname);
 }
Index: src/main/java/com/wediscussmovies/project/service/impl/DiscussionServiceImpl.java
===================================================================
--- src/main/java/com/wediscussmovies/project/service/impl/DiscussionServiceImpl.java	(revision 3ded84dfbf8ff25bf27cc822ac97b84a063fa95a)
+++ src/main/java/com/wediscussmovies/project/service/impl/DiscussionServiceImpl.java	(revision e0ef1b13a29101c3f7a56f68da8dcdc8cce98b76)
@@ -1,38 +1,145 @@
 package com.wediscussmovies.project.service.impl;
 
-import com.wediscussmovies.project.model.*;
+import com.wediscussmovies.project.model.exception.DiscussionNotExistException;
+import com.wediscussmovies.project.model.exception.MovieIdNotFoundException;
+import com.wediscussmovies.project.model.exception.PersonNotExistException;
+import com.wediscussmovies.project.model.exception.UserNotExistException;
 import com.wediscussmovies.project.repository.DiscussionRepository;
+import com.wediscussmovies.project.model.Discussion;
+import com.wediscussmovies.project.model.Movie;
+import com.wediscussmovies.project.model.Person;
+import com.wediscussmovies.project.model.User;
+import com.wediscussmovies.project.repository.MovieRepository;
+import com.wediscussmovies.project.repository.PersonRepository;
+import com.wediscussmovies.project.repository.UserRepository;
 import com.wediscussmovies.project.service.DiscussionService;
+import com.wediscussmovies.project.service.MovieService;
+import com.wediscussmovies.project.service.PersonService;
+import com.wediscussmovies.project.service.UserService;
 import org.springframework.stereotype.Service;
 
+import java.sql.Date;
+import java.time.LocalDate;
 import java.util.List;
-import java.util.Optional;
 
 @Service
 public class DiscussionServiceImpl implements DiscussionService {
+
     private final DiscussionRepository discussionRepository;
+    private final UserRepository userRepository;
 
-    public DiscussionServiceImpl(DiscussionRepository discussionRepository) {
+    private final MovieRepository movieRepository;
+    private final PersonRepository personRepository;
+
+    public DiscussionServiceImpl(DiscussionRepository discussionRepository, UserRepository userRepository,
+                                 MovieRepository movieRepository, PersonRepository personRepository) {
         this.discussionRepository = discussionRepository;
+        this.userRepository = userRepository;
+        this.movieRepository = movieRepository;
+        this.personRepository = personRepository;
+    }
+
+
+
+
+    @Override
+    public List<Discussion> listAll() {
+        return this.discussionRepository.findAll();
     }
 
     @Override
-    public List<Discussion> listAll() {
-        return discussionRepository.findAll();
-    }
+    public void save(Character type,Integer id,String title, String text,User user) {
+        Date date = Date.valueOf(LocalDate.now());
+        Discussion discussion;
 
-    @Override
-    public void save(Discussion discussion) {
+        if (type.equals('M')) {
+            discussion = new Discussion('M',text,title,date,user);
+            this.addMovieForDiscussion(id,discussion);
+        }
+        else{
+            discussion = new Discussion('P',text,title,date,user);
+            this.addPersonForDiscussion(id,discussion);
+        }
+
+
+
         discussionRepository.save(discussion);
     }
 
     @Override
-    public Optional<Discussion> findById(Integer id) {
-        return discussionRepository.findById(id);
+    public void edit(Integer discussionId, Character type, Integer id, String title, String text) {
+        Discussion discussion = this.findById(discussionId);
+
+        discussion.setText(text);
+        discussion.setTitle(title);
+        discussion.setType(type);
+        discussion.setMovie(null);
+        discussion.setPerson(null);
+
+        if (type.equals('M')) {
+            this.addMovieForDiscussion(id,discussion);
+        }
+        else{
+          this.addPersonForDiscussion(id,discussion);
+        }
+        this.discussionRepository.save(discussion);
+
+    }
+
+    @Override
+    public void deleteById(Integer discussionId) {
+        this.discussionRepository.deleteById(discussionId);
+    }
+
+    @Override
+    public void likeDiscussion(Integer discussionId, Integer userId) {
+        //ova fali od dijagram
+    }
+
+    @Override
+    public void unlikeDiscussion(Integer discussionId, Integer userId) {
+        // ova fali od dijagram
+    }
+
+    @Override
+    public List<Discussion> findAllForPersonOrMovie(Integer id,Character type) {
+        if (type.equals('M'))
+        {
+            Movie movie = this.findMovieById(id);
+            return this.discussionRepository.findAllByMovie(movie);
+        }
+        Person person  = this.findPersonById(id);
+        return this.discussionRepository.findAllByPerson(person);
+
+    }
+
+    @Override
+    public Discussion findById(Integer id) {
+        return discussionRepository.findById(id).orElseThrow(() -> new DiscussionNotExistException(id));
     }
 
     @Override
     public List<Discussion> listAllByTitle(String title) {
+        if(title == null || title.isEmpty())
+         return this.listAll();
         return discussionRepository.findAllByTitleLike("%"+title+"%");
     }
+    private void addMovieForDiscussion(Integer id,Discussion discussion){
+        Movie movie = findMovieById(id);
+        discussion.setMovie(movie);
+
+    }
+
+    private void addPersonForDiscussion(Integer id, Discussion discussion){
+        Person person = findPersonById(id);
+        discussion.setPerson(person);
+
+    }
+    private Movie findMovieById(Integer id){
+        return this.movieRepository.findById(id).orElseThrow(() -> new MovieIdNotFoundException(id));
+    }
+    private Person findPersonById(Integer id){
+        return this.personRepository.findById(id).orElseThrow(() -> new PersonNotExistException(id));
+
+    }
 }
Index: src/main/java/com/wediscussmovies/project/service/impl/GenreServiceImpl.java
===================================================================
--- src/main/java/com/wediscussmovies/project/service/impl/GenreServiceImpl.java	(revision 3ded84dfbf8ff25bf27cc822ac97b84a063fa95a)
+++ src/main/java/com/wediscussmovies/project/service/impl/GenreServiceImpl.java	(revision e0ef1b13a29101c3f7a56f68da8dcdc8cce98b76)
@@ -1,11 +1,12 @@
 package com.wediscussmovies.project.service.impl;
 
-import com.wediscussmovies.project.model.*;
+import com.wediscussmovies.project.model.exception.GenreNotExistException;
+import com.wediscussmovies.project.querymodels.GenreLikes;
 import com.wediscussmovies.project.repository.GenreRepository;
+import com.wediscussmovies.project.model.Genre;
 import com.wediscussmovies.project.service.GenreService;
 import org.springframework.stereotype.Service;
 
 import java.util.List;
-import java.util.Optional;
 
 @Service
@@ -18,11 +19,11 @@
 
     @Override
-    public Optional<Genre> findById(Integer id) {
-        return genreRepository.findById(id);
+    public Genre findById(Integer id) {
+        return this.genreRepository.findById(id).orElseThrow(() -> new GenreNotExistException(id));
     }
 
     @Override
     public List<Genre> findAllByType(String genre) {
-        return genreRepository.findAllByGenreType(genre);
+        return this.genreRepository.findAllByGenreType(genre);
     }
 
@@ -30,10 +31,15 @@
     public Genre save(String genreName) {
        Genre genre = new Genre(genreName);
-        return genreRepository.save(genre);
+        return this.genreRepository.save(genre);
+    }
+
+    @Override
+    public List<GenreLikes> findAllWithLikes() {
+        return this.genreRepository.findAllWithLikes();
     }
 
     @Override
     public List<Genre> findAll() {
-        return genreRepository.findAll();
+        return this.genreRepository.findAll();
     }
 }
Index: src/main/java/com/wediscussmovies/project/service/impl/MovieServiceImpl.java
===================================================================
--- src/main/java/com/wediscussmovies/project/service/impl/MovieServiceImpl.java	(revision 3ded84dfbf8ff25bf27cc822ac97b84a063fa95a)
+++ src/main/java/com/wediscussmovies/project/service/impl/MovieServiceImpl.java	(revision e0ef1b13a29101c3f7a56f68da8dcdc8cce98b76)
@@ -1,43 +1,211 @@
 package com.wediscussmovies.project.service.impl;
 
+
+import com.wediscussmovies.project.ajaxmodels.Grade;
 import com.wediscussmovies.project.model.*;
-import com.wediscussmovies.project.repository.MovieRepository;
+import com.wediscussmovies.project.model.exception.GenreNotExistException;
+import com.wediscussmovies.project.model.exception.PersonNotExistException;
+import com.wediscussmovies.project.model.exception.UserNotExistException;
+import com.wediscussmovies.project.model.primarykeys.MovieLikesPK;
+import com.wediscussmovies.project.model.primarykeys.MovieRatesPK;
+import com.wediscussmovies.project.model.relation.MovieActors;
+import com.wediscussmovies.project.model.relation.MovieGenres;
+import com.wediscussmovies.project.model.relation.MovieLikes;
+import com.wediscussmovies.project.model.relation.MovieRates;
+import com.wediscussmovies.project.repository.*;
+import com.wediscussmovies.project.model.exception.MovieIdNotFoundException;
 import com.wediscussmovies.project.service.MovieService;
 import org.springframework.stereotype.Service;
 
+import javax.swing.text.html.Option;
+import javax.transaction.Transactional;
+import java.sql.Date;
 import java.util.List;
 import java.util.Optional;
+import java.util.stream.Collectors;
 
 @Service
 public class MovieServiceImpl implements MovieService {
+
     private final MovieRepository movieRepository;
-
-    public MovieServiceImpl(MovieRepository movieRepository) {
+    private final PersonRepository personRepository;
+    private final GenreRepository genreRepository;
+    private final MovieActorsRepository movieActorsRepository;
+    private final MovieGenresRepository movieGenresRepository;
+    private final UserRepository userRepository;
+    private final MovieLikesRepository movieLikesRepository;
+    private final MovieRatesRepository movieRatesRepository;
+
+    public MovieServiceImpl(MovieRepository movieRepository, PersonRepository personRepository,
+                            GenreRepository genreRepository, MovieActorsRepository movieActorsRepository,
+                            MovieGenresRepository movieGenresRepository,
+                            UserRepository userRepository, MovieLikesRepository movieLikesRepository,
+                            MovieRatesRepository movieRatesRepository) {
+
         this.movieRepository = movieRepository;
+        this.personRepository = personRepository;
+        this.genreRepository = genreRepository;
+        this.movieActorsRepository = movieActorsRepository;
+        this.movieGenresRepository = movieGenresRepository;
+        this.userRepository = userRepository;
+        this.movieLikesRepository = movieLikesRepository;
+        this.movieRatesRepository = movieRatesRepository;
     }
 
     @Override
     public List<Movie> listAll() {
-        return movieRepository.findAll();
-    }
-
-    @Override
-    public Optional<Movie> findById(Integer id) {
-        return movieRepository.findById(1);
-    }
-
-    @Override
-    public Movie save(Movie movie) {
-        return movieRepository.save(movie);
-    }
+        return this.movieRepository.findAll();
+    }
+
+    @Override
+    public List<Movie> listAllByType(Character type) {
+        if (type.equals('A'))
+            return this.listAll();
+        return this.listAllWithoutDirector();
+    }
+
+    @Override
+    public List<Movie> listAllWithoutDirector() {
+        return this.movieRepository.findAllByDirectorIsNull();
+    }
+
+    @Override
+    public Movie findById(Integer id) {
+        return this.movieRepository.findById(id).orElseThrow(() -> new MovieIdNotFoundException(id));
+    }
+
+    @Override
+    @Transactional
+    public Movie save(String title, String description, String imageUrl,
+                      Date airingDate, Double rating, Integer directorId,List<Integer> actorIds,List<Integer> genreIds) {
+
+
+        Person director =getDirector(directorId);
+        Movie movie = new Movie(title,description,imageUrl,airingDate,rating,director);
+       movie = this.movieRepository.saveAndFlush(movie);
+       addActorsAndGenresForMovie(movie,actorIds,genreIds);
+       return movie;
+    }
+
+
+    @Transactional
+    @Override
+    public Movie edit(Integer movieId, String title, String description,
+                      String imageUrl, Date airingDate, Double rating,
+                      Integer directorId, List<Integer> actorIds,List<Integer> genreIds) {
+
+
+        Person director =getDirector(directorId);
+        Movie movie = this.findById(movieId);
+
+        movie.setTitle(title);
+        movie.setDescription(description);
+        movie.setImageUrl(imageUrl);
+        movie.setAiringDate(airingDate);
+        movie.setImdbRating(rating);
+        movie.setDirector(director);
+
+        this.movieActorsRepository.deleteAllByMovie(movie);
+        this.movieGenresRepository.deleteAllByMovie(movie);
+
+        movie = this.movieRepository.save(movie);
+        this.addActorsAndGenresForMovie(movie,actorIds,genreIds);
+
+        return movie;
+    }
+
+    @Override
+    public List<Person> findAllActorsForMovie(Movie movie) {
+        return this.movieActorsRepository.findAllByMovie(movie)
+                .stream()
+                .map(MovieActors::getPerson)
+                .collect(Collectors.toList());
+    }
+
+    @Override
+    public List<Genre> findAllGenresForMovie(Movie movie) {
+        return this.movieGenresRepository.findAllByMovie(movie)
+                .stream()
+                .map(MovieGenres::getGenre)
+                .collect(Collectors.toList());
+    }
+
+    @Override
+    public void likeMovie(Integer movieId, Integer userId) {
+        User user = this.userRepository.findById(userId).orElseThrow(() -> new UserNotExistException(userId.toString()));
+        Movie movie = this.findById(movieId);
+
+        this.movieLikesRepository.save(new MovieLikes(movie, user));
+    }
+
+    @Override
+    public void unlikeMovie(Integer movieId, Integer userId) {
+        MovieLikesPK movieLikesPK = new MovieLikesPK(movieId,userId);
+        this.movieLikesRepository.deleteById(movieLikesPK);
+    }
+
+    @Override
+    public List<Movie> findLikedMoviesByUser(User user) {
+        return
+                this.movieLikesRepository
+                        .findAllByUser(user)
+                        .stream()
+                        .map(MovieLikes::getMovie)
+                        .collect(Collectors.toList());
+    }
+
 
     @Override
     public void deleteById(Integer id) {
-        movieRepository.deleteById(id);
+        this.movieRepository.deleteById(id);
+    }
+
+    @Override
+    public void addGradeMovie(Integer movieId, User user, Grade grade) {
+        MovieRatesPK key =  new MovieRatesPK(movieId, user.getUserId());
+        Optional<MovieRates> movieRates = this.movieRatesRepository.findById(key);
+        if (movieRates.isPresent()){
+           MovieRates rates = movieRates.get();
+            rates.setReason(grade.getReason());
+            rates.setStarsRated(grade.getRating());
+            this.movieRatesRepository.save(rates);
+        }
+        else{
+            Movie movie = this.findById(movieId);
+            MovieRates rates = new MovieRates(user,movie,grade.getReason(),grade.getRating());
+            this.movieRatesRepository.save(rates);
+        }
     }
 
     @Override
     public List<Movie> searchByTitle(String title) {
-        return movieRepository.findAllByTitleLike("%"+title+"%");
+        return this.movieRepository.findAllByTitleLike("%"+title+"%");
+    }
+
+    @Transactional
+    void addActorsAndGenresForMovie(Movie movie, List<Integer> actorIds, List<Integer> genreIds)
+    {
+
+        actorIds.stream()
+                .map(this::findPersonById)
+                .forEach(actor -> this.movieActorsRepository.save(new MovieActors(movie,actor)));
+
+        genreIds.stream()
+                .map(this::findGenreById)
+                .forEach(genre -> this.movieGenresRepository.save(new MovieGenres(movie,genre)));
+
+    }
+    private Person getDirector(Integer directorId){
+
+        if (directorId != null)
+            return this.findPersonById(directorId);
+        return null;
+
+    }
+    private Person findPersonById(Integer id){
+        return this.personRepository.findById(id).orElseThrow(() -> new PersonNotExistException(id));
+    }
+    private Genre findGenreById(Integer id){
+        return this.genreRepository.findById(id).orElseThrow(() -> new GenreNotExistException(id));
     }
 }
Index: src/main/java/com/wediscussmovies/project/service/impl/PersonServiceImpl.java
===================================================================
--- src/main/java/com/wediscussmovies/project/service/impl/PersonServiceImpl.java	(revision 3ded84dfbf8ff25bf27cc822ac97b84a063fa95a)
+++ src/main/java/com/wediscussmovies/project/service/impl/PersonServiceImpl.java	(revision e0ef1b13a29101c3f7a56f68da8dcdc8cce98b76)
@@ -1,73 +1,187 @@
 package com.wediscussmovies.project.service.impl;
 
-import com.wediscussmovies.project.model.*;
-import com.wediscussmovies.project.model.enumerations.PersonType;
+import com.wediscussmovies.project.ajaxmodels.Grade;
+import com.wediscussmovies.project.model.Movie;
+import com.wediscussmovies.project.model.User;
+import com.wediscussmovies.project.model.exception.MovieHasAlreadyDirector;
+import com.wediscussmovies.project.model.exception.MovieIdNotFoundException;
+import com.wediscussmovies.project.model.exception.PersonNotExistException;
+import com.wediscussmovies.project.model.primarykeys.PersonRatesPK;
+import com.wediscussmovies.project.model.relation.MovieActors;
+import com.wediscussmovies.project.model.relation.MovieRates;
+import com.wediscussmovies.project.model.relation.PersonRates;
+import com.wediscussmovies.project.repository.MovieActorsRepository;
+import com.wediscussmovies.project.repository.MovieRepository;
+import com.wediscussmovies.project.repository.PersonRatesRepository;
 import com.wediscussmovies.project.repository.PersonRepository;
+import com.wediscussmovies.project.model.Person;
 import com.wediscussmovies.project.service.PersonService;
 import org.springframework.stereotype.Service;
 
+import javax.transaction.Transactional;
+import java.sql.Date;
 import java.util.List;
 import java.util.Optional;
+import java.util.stream.Collectors;
 
 @Service
 public class PersonServiceImpl implements PersonService {
+
     private final PersonRepository personRepository;
+    private final MovieRepository movieRepository;
+    private final MovieActorsRepository movieActorsRepository;
+    private final PersonRatesRepository personRatesRepository;
 
 
-    public PersonServiceImpl(PersonRepository personRepository) {
+    public PersonServiceImpl(PersonRepository personRepository,
+                             MovieRepository movieRepository, MovieActorsRepository movieActorsRepository,
+                             PersonRatesRepository personRatesRepository) {
         this.personRepository = personRepository;
+        this.movieRepository = movieRepository;
+        this.movieActorsRepository = movieActorsRepository;
+        this.personRatesRepository = personRatesRepository;
+    }
+
+    @Override
+    public List<Person> findAll() {
+        return this.personRepository.findAll();
     }
 
     @Override
     public List<Person> findAllDirectors() {
-        return personRepository.findAllByType('D');
+        return this.personRepository.findAllByType('D');
     }
 
     @Override
-    public Optional<Person> findActorById(Integer id) {
-        return personRepository.findPersonByPersonIdAndType(id, 'A');
+    public Person findById(Integer id) {
+        return this.personRepository.findById(id).orElseThrow(() -> new PersonNotExistException(id));
+    }
+
+
+
+    @Override
+    public List<Person> findAllActors() {
+        return this.personRepository.findAllByType('A');
     }
 
     @Override
-    public Optional<Person> findById(Integer person_id) {
-        return personRepository.findById(person_id);
+    @Transactional
+    public Person save(String name, String surname, Character type, Date birthDate, String image_url,
+                       String description,List<Integer> movieIds) {
+
+        Person person = this.personRepository.saveAndFlush(new Person(name,surname,type,birthDate,image_url,description));
+
+        if (movieIds!= null) {
+            if (type.equals('A'))
+                this.addActorForMovies(person, movieIds);
+            else
+                this.addDirectorForMovies(person, movieIds);
+        }
+         return person;
+
     }
 
     @Override
-    public Optional<Person> findDirectorById(Integer id) {
-        return personRepository.findPersonByPersonIdAndType(id, 'D');
+    @Transactional
+    public Person edit(Integer personId, String name, String surname,
+                       Character type, Date birthDate, String image_url, String description, List<Integer> movieIds) {
+        Person person = this.findById(personId);
+
+        person.setName(name);
+        person.setSurname(surname);
+        person.setDateOfBirth(birthDate);
+        person.setImageUrl(image_url);
+        person.setType(type);
+        person.setDescription(description);
+
+
+        person = this.personRepository.saveAndFlush(person);
+
+            this.movieActorsRepository.deleteAllByPerson(person);
+            this.removeMovieDirectorsBeforeEdit(person);
+        if (movieIds != null) {
+            if (type.equals('A'))
+                this.addActorForMovies(person, movieIds);
+            else
+                this.addDirectorForMovies(person, movieIds);
+        }
+
+        return person;
+
     }
 
     @Override
-    public List<Person> findAllActors() {
-        return personRepository.findAllByType('A');
+    public List<Person> findPersonsByNameOrSurname(Character type,String searchQuery) {
+        if (searchQuery != null && searchQuery.isEmpty())
+            return this.personRepository.findAllByTypeAndNameLikeOrSurnameLike(type,searchQuery,searchQuery);
+        if (type.equals('D'))
+            return this.findAllDirectors();
+        return this.findAllActors();
+
     }
 
     @Override
-    public boolean save(Person person) {
-        Person p = personRepository.save(person);
-        if(p != null)
-            return true;
-        return false;
+    public List<Movie> findAllMoviesByPerson(Person person) {
+        if (person.getType().equals('A'))
+            return this.movieActorsRepository
+                .findAllByPerson(person)
+                .stream()
+                .map(MovieActors::getMovie)
+                .collect(Collectors.toList());
+        return this.movieRepository.findAllByDirector(person);
     }
 
     @Override
-    public List<Person> findActorsByNameLike(String name) {
-        return personRepository.findAllByTypeAndNameLike('A', name);
+    public void deleteById(Integer id) {
+        this.personRepository.deleteById(id);
     }
 
     @Override
-    public List<Person> findActorsBySurnameLike(String surname) {
-        return personRepository.findAllByTypeAndSurnameLike('A', surname);
+    public void addGradePerson(Integer personId, User user, Grade grade) {
+        PersonRatesPK key =  new PersonRatesPK(personId, user.getUserId());
+        Optional<PersonRates> personRates = this.personRatesRepository.findById(key);
+        if (personRates.isPresent()){
+            PersonRates rates = personRates.get();
+            rates.setReason(grade.getReason());
+            rates.setStarsRated(grade.getRating());
+            this.personRatesRepository.save(rates);
+        }
+        else{
+            Person person = this.findById(personId);
+            PersonRates rates = new PersonRates(user,person,grade.getReason(),grade.getRating());
+            this.personRatesRepository.save(rates);
+        }
+
     }
 
-    @Override
-    public List<Person> findDirectorsByNameLike(String name) {
-        return personRepository.findAllByTypeAndNameLike('D', name);
+
+    private void addActorForMovies(Person person, List<Integer> movieIds){
+
+        movieIds.stream()
+                .map(this::findMovieById)
+                .forEach(m -> this.movieActorsRepository.save(new MovieActors(m,person)));
+
+    }
+    private void addDirectorForMovies(Person person, List<Integer> movieIds){
+        movieIds.stream()
+                .map(this::findMovieById)
+                .forEach(m -> this.addMovieDirector(m,person));
+    }
+    private void addMovieDirector(Movie movie,Person person){
+        Person director = movie.getDirector();
+        if (director != null)
+            throw new MovieHasAlreadyDirector(movie.getTitle(),director.getName(),director.getSurname());
+        movie.setDirector(person);
+        this.movieRepository.save(movie);
+    }
+    private void removeMovieDirectorsBeforeEdit(Person person){
+        this.movieRepository.findAllByDirector(person)
+                .forEach(m ->{ m.setDirector(null); this.movieRepository.save(m); });
+
     }
 
-    @Override
-    public List<Person> findDirectorsBySurnameLike(String surname) {
-        return personRepository.findAllByTypeAndSurnameLike('D', surname);
+    private Movie findMovieById(Integer id){
+        return this.movieRepository.findById(id).orElseThrow(() -> new MovieIdNotFoundException(id));
     }
+
 }
Index: src/main/java/com/wediscussmovies/project/service/impl/ReplyServiceImpl.java
===================================================================
--- src/main/java/com/wediscussmovies/project/service/impl/ReplyServiceImpl.java	(revision 3ded84dfbf8ff25bf27cc822ac97b84a063fa95a)
+++ src/main/java/com/wediscussmovies/project/service/impl/ReplyServiceImpl.java	(revision e0ef1b13a29101c3f7a56f68da8dcdc8cce98b76)
@@ -1,32 +1,78 @@
 package com.wediscussmovies.project.service.impl;
 
-import com.wediscussmovies.project.model.*;
+import com.wediscussmovies.project.model.Discussion;
+import com.wediscussmovies.project.model.exception.DiscussionNotExistException;
+import com.wediscussmovies.project.model.exception.ReplyNotExistException;
+import com.wediscussmovies.project.model.primarykeys.ReplyPK;
+import com.wediscussmovies.project.repository.DiscussionRepository;
 import com.wediscussmovies.project.repository.ReplyRepository;
+import com.wediscussmovies.project.model.Reply;
+import com.wediscussmovies.project.model.User;
+import com.wediscussmovies.project.repository.UserRepository;
 import com.wediscussmovies.project.service.ReplyService;
 import org.springframework.stereotype.Service;
 
-import java.util.Optional;
+import java.sql.Date;
+import java.time.LocalDate;
+import java.util.List;
 
 @Service
 public class ReplyServiceImpl implements ReplyService {
+
     private final ReplyRepository replyRepository;
+    private final DiscussionRepository discussionRepository;
 
-    public ReplyServiceImpl(ReplyRepository replyRepository) {
+    public ReplyServiceImpl(ReplyRepository replyRepository,
+                            DiscussionRepository discussionRepository) {
         this.replyRepository = replyRepository;
+        this.discussionRepository = discussionRepository;
+    }
+
+
+
+    @Override
+    public void save(Integer discussionId, String text,User user) {
+
+        Discussion discussion = this.discussionRepository.findById(discussionId).orElseThrow(() -> new DiscussionNotExistException(discussionId));
+
+        Date date = Date.valueOf(LocalDate.now());
+        this.replyRepository.insertInto(text,date,user.getUserId(),discussionId);
+
     }
 
     @Override
-    public Optional<Reply> findById(Long id) {
-        return replyRepository.findById(1);
+    public void likeReply(Integer replyId, Integer userId) {
+        //da se implementira, promena vo baza
     }
 
     @Override
-    public void delete(Reply r) {
-        replyRepository.delete(r);
+    public void unlikeReply(Integer replyId, Integer userId) {
+        // da se implementira, promena vo baza
     }
 
     @Override
-    public Reply save(Reply r) {
-        return replyRepository.save(r);
+    public List<Reply> findAllByDiscussion(Discussion discussion) {
+        return this.replyRepository.findAllByDiscussion(discussion);
+    }
+
+
+    @Override
+    public Reply edit(Integer replyId,Integer discussionId,String text) {
+        ReplyPK replyPK = new ReplyPK(discussionId,replyId);
+        Reply reply = this.replyRepository.findById(replyPK).orElseThrow();
+        reply.setText(text);
+        return  this.replyRepository.save(reply);
+    }
+
+    @Override
+    public void delete(Integer discussionId, Integer replyId) {
+        this.replyRepository.deleteById(new ReplyPK(discussionId,replyId));
+    }
+
+    @Override
+    public Reply findById(Integer discussionId, Integer replyId) {
+        ReplyPK replyPK  = new ReplyPK(discussionId,replyId);
+        return this.replyRepository.findById(replyPK).orElseThrow(() -> new ReplyNotExistException(replyPK));
+
     }
 }
Index: src/main/java/com/wediscussmovies/project/service/impl/UserServiceImpl.java
===================================================================
--- src/main/java/com/wediscussmovies/project/service/impl/UserServiceImpl.java	(revision 3ded84dfbf8ff25bf27cc822ac97b84a063fa95a)
+++ src/main/java/com/wediscussmovies/project/service/impl/UserServiceImpl.java	(revision e0ef1b13a29101c3f7a56f68da8dcdc8cce98b76)
@@ -1,58 +1,53 @@
 package com.wediscussmovies.project.service.impl;
 
-import com.wediscussmovies.project.model.*;
-import com.wediscussmovies.project.model.exception.PasswordsDontMatchException;
-import com.wediscussmovies.project.model.exception.UserWithEmailAlreadyExists;
-import com.wediscussmovies.project.model.exception.UserWithUsernameAlreadyExists;
+import com.wediscussmovies.project.model.exception.InvalidArgumentsException;
+import com.wediscussmovies.project.model.exception.PasswordsDoNotMatchException;
+import com.wediscussmovies.project.model.exception.UserNotExistException;
+import com.wediscussmovies.project.model.User;
+import com.wediscussmovies.project.model.exception.UsernameAlreadyExistsException;
 import com.wediscussmovies.project.repository.UserRepository;
 import com.wediscussmovies.project.service.UserService;
+import org.springframework.security.core.userdetails.UserDetails;
+import org.springframework.security.core.userdetails.UsernameNotFoundException;
+import org.springframework.security.crypto.password.PasswordEncoder;
 import org.springframework.stereotype.Service;
-
-import javax.servlet.http.HttpServletRequest;
-import java.util.Optional;
 
 @Service
 public class UserServiceImpl implements UserService {
+
     private final UserRepository userRepository;
+    private final PasswordEncoder passwordEncoder;
 
-    public UserServiceImpl(UserRepository userRepository) {
+
+    public UserServiceImpl(UserRepository userRepository, PasswordEncoder passwordEncoder) {
         this.userRepository = userRepository;
+        this.passwordEncoder = passwordEncoder;
     }
 
     @Override
-    public Optional<User> login(String email, String password) {
-        Optional<User> userEmail = userRepository.findByEmailAndPassword(email, password);
-        Optional<User> userUsername = userRepository.findByUsernameAndPassword(email, password);
-        if(userEmail.isPresent())
-            return userEmail;
-        else if(userUsername.isPresent())
-            return userUsername;
-        else
-            return Optional.empty();
+    public User findByUsername(String username) {
+        return this.userRepository.findByUsername(username).orElseThrow(() -> new UserNotExistException(username));
+    }
+
+
+
+    @Override
+    public User register( String email, String username, String password, String confirmPassword
+                                  , String name, String surname) {
+
+        if ( username.isEmpty() || password.isEmpty())
+            throw new InvalidArgumentsException();
+        if (!password.equals(confirmPassword))
+            throw new PasswordsDoNotMatchException();
+        if(this.userRepository.findByUsername(username).isPresent())
+            throw new UsernameAlreadyExistsException(username);
+        User user = new User(email,username,passwordEncoder.encode(password),name,surname);
+        return userRepository.save(user);
+
     }
 
     @Override
-    public Optional<User> register(HttpServletRequest request, String email, String password, String confirmPassword,
-                                   String username, String name, String surname) {
-        if(email == null || email.isEmpty() || password == null || password.isEmpty() ||
-                confirmPassword == null || confirmPassword.isEmpty() || username == null || username.isEmpty() ||
-                name == null || name.isEmpty() || surname == null || surname.isEmpty()) {
-            request.getSession().setAttribute("error", "Not all of the fields had a value in them, check again.");
-            return Optional.empty();
-        }
-        if(userRepository.findByUsername(username).isPresent()){
-            request.getSession().setAttribute("error", new UserWithUsernameAlreadyExists(username).getMessage());
-            return Optional.empty();
-        }
-        if(userRepository.findByEmail(email).isPresent()){
-            request.getSession().setAttribute("error", new UserWithEmailAlreadyExists(email).getMessage());
-            return Optional.empty();
-        }
-        if(!password.equals(confirmPassword)){
-            request.getSession().setAttribute("error", new PasswordsDontMatchException().getMessage());
-            return Optional.empty();
-        }
-        User user = new User(email, username, password, name, surname);
-        return Optional.of(userRepository.save(user));
+    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
+        return this.findByUsername(username);
     }
 }
Index: src/main/java/com/wediscussmovies/project/web/controller/DiscussionController.java
===================================================================
--- src/main/java/com/wediscussmovies/project/web/controller/DiscussionController.java	(revision e0ef1b13a29101c3f7a56f68da8dcdc8cce98b76)
+++ src/main/java/com/wediscussmovies/project/web/controller/DiscussionController.java	(revision e0ef1b13a29101c3f7a56f68da8dcdc8cce98b76)
@@ -0,0 +1,175 @@
+package com.wediscussmovies.project.web.controller;
+
+import com.wediscussmovies.project.LoggedUser;
+import com.wediscussmovies.project.model.Discussion;
+import com.wediscussmovies.project.model.Reply;
+import com.wediscussmovies.project.model.User;
+import com.wediscussmovies.project.service.DiscussionService;
+import com.wediscussmovies.project.service.MovieService;
+import com.wediscussmovies.project.service.PersonService;
+import com.wediscussmovies.project.service.ReplyService;
+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;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.List;
+
+@Controller
+@RequestMapping("/discussions")
+public class DiscussionController {
+
+    private final DiscussionService discussionService;
+    private final ReplyService replyService;
+    private final MovieService movieService;
+    private final PersonService personService;
+
+    public DiscussionController(DiscussionService discussionService, ReplyService replyService, MovieService movieService, PersonService personService) {
+        this.discussionService = discussionService;
+        this.replyService = replyService;
+        this.movieService = movieService;
+        this.personService = personService;
+    }
+
+    @GetMapping()
+    public String getDiscussions(@RequestParam(required = false) String titleSearch,
+                                    Model model){
+        /* 1.Validacija i  na frontend so, da ne se isprakja baranjeto, ako titleSearch e null ili empty
+         da ne se preoptovaruva serverot so baranja za titleSearch koga e null!
+
+         */
+        List<Discussion> discussions = discussionService.listAllByTitle(titleSearch);
+        model.addAttribute("discussions", discussions);
+        model.addAttribute("contentTemplate", "discussionsList");
+        model.addAttribute("user",LoggedUser.getLoggedUser());
+        return "template";
+    }
+
+    @GetMapping("/{id}")
+    public String getDiscussion(
+            @PathVariable Integer id,
+            Model model){
+        /*
+            2. Moze da se vrati indikator deka takva diskusija ne postoi! - postaveno
+         */
+        try {
+            Discussion disc = discussionService.findById(id);
+            model.addAttribute("disc", disc);
+            model.addAttribute("contentTemplate", "discussion");
+            model.addAttribute("user",LoggedUser.getLoggedUser());
+            model.addAttribute("replies",this.replyService.findAllByDiscussion(disc));
+
+            return "template";
+        }
+        catch (RuntimeException exception){
+            return "redirect:/discussions?error="+exception.getMessage();
+
+        }
+    }
+    @GetMapping("/all/{id}")
+    public String getDiscussionForId(@PathVariable Integer id,@RequestParam Character type, Model model){
+
+        model.addAttribute("discussions", this.discussionService.findAllForPersonOrMovie(id,type));
+        model.addAttribute("contentTemplate", "discussionForType");
+        return "template";
+
+    }
+
+    @GetMapping("/{id}/replies")
+    public String getRepliesToDiscussion(
+            @PathVariable Integer id,
+            Model model){
+
+        try {
+            Discussion discussion = discussionService.findById(id);
+            model.addAttribute("discussion", discussion);
+            model.addAttribute("contentTemplate", "discussionReply");
+            return "template";
+        }
+        catch (RuntimeException exception){
+            return "redirect:/discussions?error="+exception.getMessage();
+
+        }
+
+    }
+
+
+    @GetMapping("/add/{id}")
+    public String getFormForEdit(Model model,
+                                           @PathVariable Integer id)
+    {
+        Discussion discussion = this.discussionService.findById(id);
+        setModelPropertiesForForm(model);
+        model.addAttribute("discussion", discussion);
+
+        return "template";
+    }
+    @GetMapping("/add")
+    public String getFormForAdd(Model model)
+    {
+        setModelPropertiesForForm(model);
+
+        return "template";
+    }
+
+
+    @PostMapping("/save")
+    public String saveDiscussion(
+           @RequestParam Integer idDiscussed,
+            @RequestParam String title,
+            @RequestParam String text,
+            @RequestParam Character type    ){
+
+
+
+        try{
+
+            discussionService.save(type,idDiscussed, title, text, LoggedUser.getLoggedUser());
+            return "redirect:/discussions";
+
+        }
+
+        catch (RuntimeException exc){
+            return "redirect:/discussions?error="+exc.getMessage();
+
+        }
+    }
+
+    @PostMapping("/save/{discussionId}/")
+    public String editDiscussion(
+                                               @PathVariable Integer discussionId,
+                                               @RequestParam String title,
+                                               @RequestParam String text,
+                                               @RequestParam Character type,
+                                               @RequestParam Integer idDiscussed
+                                               ){
+
+
+
+
+        try{
+
+
+            discussionService.edit(discussionId,type,idDiscussed, title, text);
+            return "redirect:/discussions";
+
+        }
+
+        catch (RuntimeException exc){
+            return "redirect:/discussions?error="+exc.getMessage();
+
+        }
+    }
+
+    private void setModelPropertiesForForm(Model model){
+        model.addAttribute("movies",this.movieService.listAll());
+        model.addAttribute("persons",this.personService.findAll());
+        model.addAttribute("contentTemplate", "discussionsAdd");
+
+    }
+
+
+}
Index: src/main/java/com/wediscussmovies/project/web/controller/DiscussionsController.java
===================================================================
--- src/main/java/com/wediscussmovies/project/web/controller/DiscussionsController.java	(revision 3ded84dfbf8ff25bf27cc822ac97b84a063fa95a)
+++ 	(revision )
@@ -1,145 +1,0 @@
-package com.wediscussmovies.project.web.controller;
-
-import com.wediscussmovies.project.model.*;
-
-import com.wediscussmovies.project.service.DiscussionService;
-import com.wediscussmovies.project.service.MovieService;
-import com.wediscussmovies.project.service.PersonService;
-import com.wediscussmovies.project.service.ReplyService;
-import org.springframework.stereotype.Controller;
-import org.springframework.ui.Model;
-import org.springframework.web.bind.annotation.*;
-
-import javax.servlet.http.HttpServletRequest;
-import java.sql.Date;
-import java.time.LocalDate;
-import java.util.List;
-import java.util.Optional;
-
-@Controller
-@RequestMapping("/discussions")
-public class DiscussionsController {
-    private final DiscussionService discussionService;
-    private final ReplyService replyService;
-    private final MovieService movieService;
-    private final PersonService personService;
-    public DiscussionsController(DiscussionService discussionService, ReplyService replyService, MovieService movieService, PersonService personService) {
-        this.discussionService = discussionService;
-        this.replyService = replyService;
-        this.movieService = movieService;
-        this.personService = personService;
-    }
-
-    @GetMapping("/")
-    public String getDiscussions(@RequestParam(required = false) String titleSearch,
-                                    Model model){
-        List<Discussion> discussions = discussionService.listAll();
-        if(titleSearch != null && !titleSearch.isEmpty()){
-            discussions = discussionService.listAllByTitle(titleSearch);
-        }
-        model.addAttribute("discussions", discussions);
-        model.addAttribute("contentTemplate", "discussionsList");
-        return "template";
-    }
-
-    @GetMapping("/{id}")
-    public String getDiscussion(
-            @PathVariable Integer id,
-            Model model){
-        Optional<Discussion> discussion = discussionService.findById(id);
-        if(discussion.isEmpty())
-            return "redirect:/discussions";
-        model.addAttribute("discussion", discussion);
-        model.addAttribute("contentTemplate", "discussionsDiscussion");
-        return "template";
-    }
-
-    @GetMapping("/{id}/reply")
-    public String getReplyToDiscussion(
-            @PathVariable Integer id,
-            Model model){
-        Optional<Discussion> discussion = discussionService.findById(id);
-        if(discussion.isEmpty())
-            return "redirect:/discussions";
-        model.addAttribute("discussion", discussion);
-        model.addAttribute("contentTemplate", "discussionsReply");
-        return "template";
-    }
-    @PostMapping("/{id}/reply/confirm")
-    public String getReplyToDiscussionConfirm(
-            @PathVariable Integer id,
-            @RequestParam String text,
-            HttpServletRequest request){
-        Optional<Discussion> discussion = discussionService.findById(id);
-        User user = (User) request.getSession().getAttribute("user");
-        if(user == null){
-            return "redirect:/login";
-        }
-        Date date = Date.valueOf(LocalDate.now());
-        if(discussion.isEmpty())
-            return "redirect:/discussions";
-        //Reply reply = new Reply(discussion.get(), user, date, text);
-       // replyService.save(reply);
-        return "redirect:/discussions/"+id;
-    }
-
-    @GetMapping("/movies/add/{id}")
-    public String getAddDiscussionForMovie(Model model,
-                                           @PathVariable Integer id,
-                                           HttpServletRequest request){
-        model.addAttribute("contentTemplate", "discussionsAdd");
-        request.setAttribute("movieId", id);
-        return "template";
-    }
-
-    @PostMapping("/movies/add/confirm")
-    public String getAddDiscussionMovieConfirm(Model model,
-                                               HttpServletRequest request,
-                                               @RequestParam String title,
-                                               @RequestParam String text){
-        User user = (User) request.getSession().getAttribute("user");
-        if(user == null){
-            return "redirect:/login";
-        }
-        Integer movie_id = (Integer) request.getSession().getAttribute("movieId");
-        request.getSession().setAttribute("movieId", null);
-        Optional<Movie> movieOp = movieService.findById(movie_id);
-        if(movieOp.isEmpty())
-            return "redirect:/movies";
-      //  Discussion discussion = new Discussion('M',text, title, Date.valueOf(LocalDate.now()),user,movieOp.get(),null);
-       // discussionService.save(discussion);
-        return "redirect:/discussions";
-    }
-
-
-    @GetMapping("/persons/add/{id}")
-    public String getAddDiscussionForPerson(Model model,
-                                           @PathVariable Integer id,
-                                           HttpServletRequest request){
-        model.addAttribute("contentTemplate", "discussionsAdd");
-        request.setAttribute("personId", id);
-        return "template";
-    }
-
-    @PostMapping("/persons/add/confirm")
-    public String getAddDiscussionForPersonConfirm(Model model,
-                                               HttpServletRequest request,
-                                               @RequestParam String title,
-                                               @RequestParam String text){
-        User user = (User) request.getSession().getAttribute("user");
-        if(user == null){
-            return "redirect:/login";
-        }
-        Integer person_id = (Integer) request.getSession().getAttribute("personId");
-        Optional<Person> personOp = personService.findById(person_id);
-        request.getSession().setAttribute("personId", null);
-        if(personOp.isEmpty())
-            return "redirect:/discussions";
-
-      //  Discussion discussion = new Discussion('P',title, text,Date.valueOf(LocalDate.now()),user,null,personOp.get());
-      //  discussionService.save(discussion);
-        return "redirect:/discussions";
-    }
-
-
-}
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";
     }
 }
Index: src/main/java/com/wediscussmovies/project/web/controller/PersonController.java
===================================================================
--- src/main/java/com/wediscussmovies/project/web/controller/PersonController.java	(revision e0ef1b13a29101c3f7a56f68da8dcdc8cce98b76)
+++ src/main/java/com/wediscussmovies/project/web/controller/PersonController.java	(revision e0ef1b13a29101c3f7a56f68da8dcdc8cce98b76)
@@ -0,0 +1,128 @@
+package com.wediscussmovies.project.web.controller;
+
+import com.wediscussmovies.project.model.Person;
+import com.wediscussmovies.project.service.MovieService;
+import com.wediscussmovies.project.service.PersonService;
+import org.springframework.stereotype.Controller;
+import org.springframework.ui.Model;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+
+import java.sql.Date;
+import java.util.List;
+
+@Controller
+public class PersonController {
+    private final PersonService personService;
+    private final MovieService movieService;
+
+    public PersonController(PersonService personService, MovieService movieService) {
+        this.personService = personService;
+        this.movieService = movieService;
+    }
+
+    /*
+    1. Prebaruvanjeto po ime i prezime posoodvetno e da bide na frontend
+        Da ne se preoptovaruva server i klient da ceka koga moze vo browser vekje dobieni da se filtrirat
+     */
+    @GetMapping("/actors")
+    public String getActors(Model model, @RequestParam(required = false) String nameAndSurname){
+        List<Person> persons = personService.findPersonsByNameOrSurname('A',nameAndSurname);
+        model.addAttribute("persons", persons);
+        model.addAttribute("contentTemplate", "personsList");
+
+        return "template";
+    }
+    /*
+      2. Prebaruvanjeto po ime i prezime posoodvetno e da bide na frontend
+          Da ne se preoptovaruva server i klient da ceka koga moze vo browser vekje dobieni da se filtrirat
+       */
+    @GetMapping("/directors")
+    public String getDirectors(Model model, @RequestParam(required = false) String nameAndSurname){
+        List<Person> persons = personService.findPersonsByNameOrSurname('D',nameAndSurname);
+        model.addAttribute("persons", persons);
+        model.addAttribute("contentTemplate", "personsList");
+        return "template";
+    }
+
+    @GetMapping("/persons/add")
+    public String addingFormForPerson( Model model){
+
+        addModelPropertiesForForm(model);
+        return "template";
+    }
+    @GetMapping("persons/edit/{personId}")
+    public String editPerson(@PathVariable Integer personId, Model model){
+
+        try {
+            Person person = this.personService.findById(personId);
+            addModelPropertiesForForm(model);
+            model.addAttribute("person",person);
+            model.addAttribute("movieActors",this.personService.findAllMoviesByPerson(person));
+            return "template";
+        }
+        catch (RuntimeException exc){
+            return "redirect:/actors?erorr="+exc.getMessage();
+        }
+    }
+
+    @PostMapping("/persons/save")
+    public String savePerson(
+            @RequestParam String name,
+            @RequestParam String surname,
+            @RequestParam Character type,
+            @RequestParam Date birthDate,
+            @RequestParam String imageUrl,
+            @RequestParam String description,
+            @RequestParam(required = false) List<Integer> movieIds){
+
+        String returnedUrl = getReturnedUrl(type);
+
+        try {
+
+            Person person = this.personService.save(name, surname, type, birthDate, imageUrl, description, movieIds);
+           return returnedUrl;
+        }
+        catch (RuntimeException exc){
+            return returnedUrl + "?error" + exc.getMessage();
+        }
+
+    }
+    @PostMapping("/persons/save/{personId}")
+    public String editPerson(
+            @PathVariable Integer personId,
+            @RequestParam String name,
+            @RequestParam String surname,
+            @RequestParam Character type,
+            @RequestParam Date birthDate,
+            @RequestParam String imageUrl,
+            @RequestParam String description,
+            @RequestParam(required = false) List<Integer> movieIds){
+
+        String returnedUrl = getReturnedUrl(type);
+        try {
+
+            Person person = this.personService.edit(personId,name, surname, type, birthDate, imageUrl, description, movieIds);
+            return returnedUrl;
+        }
+        catch (RuntimeException exc){
+            return returnedUrl + "?error" + exc.getMessage();
+        }
+
+    }
+    private String getReturnedUrl(Character type){
+        if (type.equals('A'))
+           return   "redirect:/actors";
+        return "redirect:/directors";
+    }
+    private void addModelPropertiesForForm(Model model){
+        model.addAttribute("contentTemplate", "personsAdd");
+        model.addAttribute("moviesActors",this.movieService.listAllByType('A'));
+        model.addAttribute("moviesDirectors",this.movieService.listAllByType('D'));
+
+    }
+
+
+}
Index: src/main/java/com/wediscussmovies/project/web/controller/PersonsController.java
===================================================================
--- src/main/java/com/wediscussmovies/project/web/controller/PersonsController.java	(revision 3ded84dfbf8ff25bf27cc822ac97b84a063fa95a)
+++ 	(revision )
@@ -1,118 +1,0 @@
-package com.wediscussmovies.project.web.controller;
-
-import com.wediscussmovies.project.model.*;
-import com.wediscussmovies.project.model.enumerations.PersonType;
-import com.wediscussmovies.project.service.PersonService;
-import org.springframework.stereotype.Controller;
-import org.springframework.ui.Model;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestParam;
-
-import java.sql.Date;
-import java.util.List;
-
-@Controller
-public class PersonsController {
-    private final PersonService personSerivce;
-
-    public PersonsController(PersonService personSerivce) {
-        this.personSerivce = personSerivce;
-    }
-
-    @GetMapping("/actors")
-    public String getActorsPage(Model model, @RequestParam(required = false) String nameAndSurname){
-        List<Person> actors = personSerivce.findAllActors();
-        if(nameAndSurname != null && !nameAndSurname.isEmpty()){
-            String [] parts = nameAndSurname.split(" ");
-            String name = parts[0];
-            String surname = "";
-            if(parts.length > 1)
-                surname = parts[1];
-            List<Person> actorsByName = personSerivce.findActorsByNameLike("%"+name+"%");
-            if(!surname.isEmpty()){
-                List<Person> actorsBySurname = personSerivce.findActorsBySurnameLike("%"+surname+"%");
-                for(Person p: actorsBySurname){
-                    boolean add = true;
-                    for(Person a: actorsByName){
-                        if(a.getPersonId() == p.getPersonId()){
-                            add=false;
-                            break;
-                        }
-                    }
-                    if(!add)
-                        continue;
-                    actorsByName.add(p);
-                }
-            }
-            actors = actorsByName;
-        }
-       // actors.sort(Person.personComparatorByNameSurname);
-        model.addAttribute("actors", actors);
-        model.addAttribute("contentTemplate", "actorsList");
-        return "template";
-    }
-
-    @GetMapping("/directors")
-    public String getDirectorsPage(Model model, @RequestParam(required = false) String nameAndSurname){
-        List<Person> directors = personSerivce.findAllDirectors();
-        if(nameAndSurname != null && !nameAndSurname.isEmpty()){
-            String [] parts = nameAndSurname.split(" ");
-            String name = parts[0];
-            String surname = "";
-            if(parts.length > 1)
-                surname = parts[1];
-            List<Person> directorsByName = personSerivce.findDirectorsByNameLike("%"+name+"%");
-            if(!surname.isEmpty()){
-                List<Person> directorsBySurname = personSerivce.findDirectorsBySurnameLike("%"+surname+"%");
-                for(Person p: directorsBySurname){
-                    boolean add = true;
-                    for(Person a: directorsByName){
-                        if(a.getPersonId() == p.getPersonId()){
-                            add=false;
-                            break;
-                        }
-                    }
-                    if(!add)
-                        continue;
-                    directorsByName.add(p);
-                }
-            }
-            directors = directorsByName;
-        }
-     //   directors.sort(Person.personComparatorByNameSurname);
-        model.addAttribute("directors", directors);
-        model.addAttribute("contentTemplate", "directorsList");
-        return "template";
-    }
-
-    @GetMapping("/persons/add")
-    public String getPersonsAddPage(Model model){
-        PersonType [] types = PersonType.values();
-        model.addAttribute("types", types);
-        model.addAttribute("contentTemplate", "personsAdd");
-        return "template";
-    }
-
-    @PostMapping("/persons/add/confirm")
-    public String confirmPersonsAdd(
-            @RequestParam String name,
-            @RequestParam String surname,
-            @RequestParam Character type,
-            @RequestParam Date date_of_birth,
-            @RequestParam String image_url,
-            @RequestParam String description){
-        Person person = new Person(name, surname, type, date_of_birth, image_url, description);
-        if(personSerivce.save(person)){
-            String toWhere = "actors";
-            //if(type == PersonType.D)
-                toWhere = "directors";
-            return "redirect:/"+toWhere;
-        }
-        else{
-            return "redirect:/persons/add";
-        }
-    }
-
-}
Index: src/main/java/com/wediscussmovies/project/web/controller/ReplyController.java
===================================================================
--- src/main/java/com/wediscussmovies/project/web/controller/ReplyController.java	(revision 3ded84dfbf8ff25bf27cc822ac97b84a063fa95a)
+++ src/main/java/com/wediscussmovies/project/web/controller/ReplyController.java	(revision e0ef1b13a29101c3f7a56f68da8dcdc8cce98b76)
@@ -1,16 +1,20 @@
 package com.wediscussmovies.project.web.controller;
 
-import com.wediscussmovies.project.model.*;
+import com.wediscussmovies.project.LoggedUser;
+import com.wediscussmovies.project.model.Reply;
+import com.wediscussmovies.project.model.User;
+import com.wediscussmovies.project.model.primarykeys.ReplyPK;
 import com.wediscussmovies.project.service.ReplyService;
+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;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.PathVariable;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.*;
 
-import java.util.Optional;
 
 @Controller
+@RequestMapping("/replies")
 public class ReplyController {
     private final ReplyService replyService;
@@ -20,24 +24,62 @@
     }
 
-    @GetMapping("/edit/{id}")
-    public String getReplyEdit(@PathVariable Long id, Model model){
-        Optional<Reply> reply = replyService.findById(id);
-        if(reply.isEmpty())
-            return "redirect:/movies";
-        model.addAttribute("reply", reply.get());
-        model.addAttribute("templateContext", "replyEdit");
+    @GetMapping("/add/{discussionId}")
+    public String addForm(@PathVariable Integer discussionId, Model model){
+        model.addAttribute("discussionId",discussionId);
+        model.addAttribute("contentTemplate","repliesAdd");
         return "template";
     }
 
-    @PostMapping("/edit/confirm/{id}")
-    public String getReplyEdit(@PathVariable Long id, @RequestParam String text){
-        Optional<Reply> replyOp = replyService.findById(id);
-        if(replyOp.isEmpty())
-            return "redirect:/discussions";
-        Reply reply = replyOp.get();
-        replyService.delete(reply);
-        reply.setText(text);
-        replyService.save(reply);
-        return "redirect:/discussions/"+reply.getDiscussionId();
+    @GetMapping("/edit/{discussionId}/{replyId}")
+    public String editForm(@PathVariable Integer discussionId,@PathVariable Integer replyId, Model model){
+
+        try {
+            Reply reply = replyService.findById(discussionId,replyId);
+            model.addAttribute("reply", reply);
+            model.addAttribute("contentTemplate", "repliesAdd");
+            return "template";
+
+
+        }catch (RuntimeException exc){
+            return "redirect:/discussions?error="+exc.getMessage();
+
+        }
     }
+    @PostMapping("/save")
+    public String saveReply(
+            @RequestParam Integer discussionId,
+            @RequestParam String text){
+
+        try {
+
+            this.replyService.save(discussionId,text, LoggedUser.getLoggedUser());
+            return "redirect:/discussions/"+discussionId;
+
+        }
+        catch (RuntimeException exc){
+            return "redirect:/discussions?error="+exc.getMessage();
+
+        }
+    }
+
+    @PostMapping("/save/{replyId}")
+    public String editReply(
+                            @PathVariable Integer replyId,
+                            @RequestParam Integer discussionId,
+                            @RequestParam String text){
+
+        /*
+            1. Da se realizira so ajax baranje na restController
+         */
+        try {
+            Reply reply = replyService.edit(replyId,discussionId, text);
+
+            return "redirect:/discussions/" + reply.getDiscussion().getDiscussionId();
+        }
+        catch (RuntimeException exc){
+            return "redirect:/discussions?error="+exc.getMessage();
+        }
+
+    }
+
 }
Index: src/main/java/com/wediscussmovies/project/web/controller/UsersController.java
===================================================================
--- src/main/java/com/wediscussmovies/project/web/controller/UsersController.java	(revision 3ded84dfbf8ff25bf27cc822ac97b84a063fa95a)
+++ src/main/java/com/wediscussmovies/project/web/controller/UsersController.java	(revision e0ef1b13a29101c3f7a56f68da8dcdc8cce98b76)
@@ -1,98 +1,68 @@
-//package com.wediscussmovies.project.web.controller;
-//
-//import com.wediscussmovies.project.model.PasswordEncoder;
-//import com.wediscussmovies.project.model.*;
-//import com.wediscussmovies.project.model.exception.InvalidUserCredentialsException;
-//import com.wediscussmovies.project.service.UserService;
-//import org.springframework.stereotype.Controller;
-//import org.springframework.ui.Model;
-//import org.springframework.web.bind.annotation.GetMapping;
-//import org.springframework.web.bind.annotation.PostMapping;
-//import org.springframework.web.bind.annotation.RequestParam;
-//
-//import javax.servlet.http.HttpServletRequest;
-//import java.security.NoSuchAlgorithmException;
-//import java.util.Optional;
-//
-//@Controller
-//public class UsersController{
-//    private final UserService userService;
-//
-//    public UsersController(UserService userService) {
-//        this.userService = userService;
-//    }
-//
-//    @GetMapping("/login")
-//    public String getLoginPage(Model model){
-//        return "login";
-//    }
-//
-//    @PostMapping("/login/confirm")
-//    public String confirmLogin(HttpServletRequest request, Model model,
-//                               @RequestParam String username,
-//                               @RequestParam String password){
-//        Optional<User> user;
-//        try{
-//            password = PasswordEncoder.getEncodedPasswordString(password);
-//        }
-//        catch (NoSuchAlgorithmException ex){
-//            model.addAttribute("hasError", true);
-//            model.addAttribute("error", ex.getMessage());
-//            return "login";
-//        }
-//
-//        try{
-//            user = this.userService.login(username, password);
-//            request.getSession().setAttribute("user", user);
-//            request.getSession().setAttribute("loggedIn",true);
-//            return "redirect:/movies";
-//        }
-//        catch (InvalidUserCredentialsException ex){
-//            model.addAttribute("hasError", true);
-//            model.addAttribute("error", ex.getMessage());
-//            return "login";
-//        }
-//    }
-//
-//    @GetMapping("/register")
-//    public String getRegisterPage(){
-//        return "register";
-//    }
-//
-//    @PostMapping("/login/confirm")
-//    public String confirmRegister(HttpServletRequest request,
-//                                  @RequestParam String username,
-//                                  @RequestParam String email,
-//                                  @RequestParam String password,
-//                                  @RequestParam String confirmPassword,
-//                                  @RequestParam String name,
-//                                  @RequestParam String surname){
-//        Optional<User> user;
-//
-//        try{
-//            password = PasswordEncoder.getEncodedPasswordString(password);
-//            confirmPassword = PasswordEncoder.getEncodedPasswordString(confirmPassword);
-//        }
-//        catch (NoSuchAlgorithmException ex){
-//            request.getSession().setAttribute("error", "Contact the administrators!");
-//            request.getSession().setAttribute("hasError", "true");
-//            return "redirect:/movies";
-//        }
-//
-//        user = this.userService.register(request, email, password, confirmPassword, username, name, surname);
-//        if(user.isEmpty()){
-//            request.setAttribute("hasError", "true");
-//        }else{
-//            request.getSession().setAttribute("hasError", "false");
-//            request.getSession().setAttribute("user", user.get());
-//            request.getSession().setAttribute("loggedIn",true);
-//        }
-//        return "redirect:/movies";
-//    }
-//
-//    @GetMapping
-//    public String logout(HttpServletRequest request){
-//        request.getSession().invalidate();
-//        return "redirect:/login";
-//    }
-//}
+package com.wediscussmovies.project.web.controller;
+
+
+import com.wediscussmovies.project.LoggedUser;
+import com.wediscussmovies.project.model.exception.InvalidArgumentsException;
+import com.wediscussmovies.project.model.exception.PasswordsDoNotMatchException;
+import com.wediscussmovies.project.service.MovieService;
+import com.wediscussmovies.project.service.UserService;
+import org.springframework.stereotype.Controller;
+import org.springframework.ui.Model;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+
+@Controller
+@RequestMapping
+public class UsersController{
+    private  final UserService userService;
+    private final MovieService movieService;
+
+    public UsersController(UserService userService, MovieService movieService) {
+        this.userService = userService;
+        this.movieService = movieService;
+    }
+    @PostMapping("/register")
+    public String register(@RequestParam String username,
+                           @RequestParam String password,
+                           @RequestParam String repeatedPassword,
+                           @RequestParam String email,
+                           @RequestParam String name,
+                           @RequestParam String surname) {
+        try{
+            this.userService.register(email,username,password,repeatedPassword,name,surname);
+            return "redirect:/login";
+        } catch (InvalidArgumentsException | PasswordsDoNotMatchException exception) {
+            return "redirect:/register?error=" + exception.getMessage();
+        }
+    }
+    @GetMapping("/register")
+    public String getRegisterPage(@RequestParam(required = false) String error, Model model) {
+        addModelError(model,error);
+        model.addAttribute("contentTemplate","register");
+        return "template";
+    }
+    @GetMapping("/login")
+    public String getLoginPage(@RequestParam(required = false) String error,Model model){
+        addModelError(model,error);
+        model.addAttribute("contentTemplate","login");
+        return "template";
+    }
+
+    private void addModelError(Model model,String error){
+        if(error != null && !error.isEmpty()) {
+            model.addAttribute("hasError", true);
+            model.addAttribute("error", error);
+        }
+    }
+    @GetMapping("/favoriteList")
+    public String getFavoriteList(Model model){
+        model.addAttribute("movies",this.movieService.findLikedMoviesByUser(LoggedUser.getLoggedUser()));
+        model.addAttribute("contentTemplate","favoriteList");
+        return "template";
+
+    }
+
+
+}
