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);
     }
 }
