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

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

Location:
src/main/java/com/wediscussmovies/project/service/impl
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • src/main/java/com/wediscussmovies/project/service/impl/DiscussionServiceImpl.java

    r3ded84d re0ef1b1  
    11package com.wediscussmovies.project.service.impl;
    22
    3 import com.wediscussmovies.project.model.*;
     3import com.wediscussmovies.project.model.exception.DiscussionNotExistException;
     4import com.wediscussmovies.project.model.exception.MovieIdNotFoundException;
     5import com.wediscussmovies.project.model.exception.PersonNotExistException;
     6import com.wediscussmovies.project.model.exception.UserNotExistException;
    47import com.wediscussmovies.project.repository.DiscussionRepository;
     8import com.wediscussmovies.project.model.Discussion;
     9import com.wediscussmovies.project.model.Movie;
     10import com.wediscussmovies.project.model.Person;
     11import com.wediscussmovies.project.model.User;
     12import com.wediscussmovies.project.repository.MovieRepository;
     13import com.wediscussmovies.project.repository.PersonRepository;
     14import com.wediscussmovies.project.repository.UserRepository;
    515import com.wediscussmovies.project.service.DiscussionService;
     16import com.wediscussmovies.project.service.MovieService;
     17import com.wediscussmovies.project.service.PersonService;
     18import com.wediscussmovies.project.service.UserService;
    619import org.springframework.stereotype.Service;
    720
     21import java.sql.Date;
     22import java.time.LocalDate;
    823import java.util.List;
    9 import java.util.Optional;
    1024
    1125@Service
    1226public class DiscussionServiceImpl implements DiscussionService {
     27
    1328    private final DiscussionRepository discussionRepository;
     29    private final UserRepository userRepository;
    1430
    15     public DiscussionServiceImpl(DiscussionRepository discussionRepository) {
     31    private final MovieRepository movieRepository;
     32    private final PersonRepository personRepository;
     33
     34    public DiscussionServiceImpl(DiscussionRepository discussionRepository, UserRepository userRepository,
     35                                 MovieRepository movieRepository, PersonRepository personRepository) {
    1636        this.discussionRepository = discussionRepository;
     37        this.userRepository = userRepository;
     38        this.movieRepository = movieRepository;
     39        this.personRepository = personRepository;
     40    }
     41
     42
     43
     44
     45    @Override
     46    public List<Discussion> listAll() {
     47        return this.discussionRepository.findAll();
    1748    }
    1849
    1950    @Override
    20     public List<Discussion> listAll() {
    21         return discussionRepository.findAll();
    22     }
     51    public void save(Character type,Integer id,String title, String text,User user) {
     52        Date date = Date.valueOf(LocalDate.now());
     53        Discussion discussion;
    2354
    24     @Override
    25     public void save(Discussion discussion) {
     55        if (type.equals('M')) {
     56            discussion = new Discussion('M',text,title,date,user);
     57            this.addMovieForDiscussion(id,discussion);
     58        }
     59        else{
     60            discussion = new Discussion('P',text,title,date,user);
     61            this.addPersonForDiscussion(id,discussion);
     62        }
     63
     64
     65
    2666        discussionRepository.save(discussion);
    2767    }
    2868
    2969    @Override
    30     public Optional<Discussion> findById(Integer id) {
    31         return discussionRepository.findById(id);
     70    public void edit(Integer discussionId, Character type, Integer id, String title, String text) {
     71        Discussion discussion = this.findById(discussionId);
     72
     73        discussion.setText(text);
     74        discussion.setTitle(title);
     75        discussion.setType(type);
     76        discussion.setMovie(null);
     77        discussion.setPerson(null);
     78
     79        if (type.equals('M')) {
     80            this.addMovieForDiscussion(id,discussion);
     81        }
     82        else{
     83          this.addPersonForDiscussion(id,discussion);
     84        }
     85        this.discussionRepository.save(discussion);
     86
     87    }
     88
     89    @Override
     90    public void deleteById(Integer discussionId) {
     91        this.discussionRepository.deleteById(discussionId);
     92    }
     93
     94    @Override
     95    public void likeDiscussion(Integer discussionId, Integer userId) {
     96        //ova fali od dijagram
     97    }
     98
     99    @Override
     100    public void unlikeDiscussion(Integer discussionId, Integer userId) {
     101        // ova fali od dijagram
     102    }
     103
     104    @Override
     105    public List<Discussion> findAllForPersonOrMovie(Integer id,Character type) {
     106        if (type.equals('M'))
     107        {
     108            Movie movie = this.findMovieById(id);
     109            return this.discussionRepository.findAllByMovie(movie);
     110        }
     111        Person person  = this.findPersonById(id);
     112        return this.discussionRepository.findAllByPerson(person);
     113
     114    }
     115
     116    @Override
     117    public Discussion findById(Integer id) {
     118        return discussionRepository.findById(id).orElseThrow(() -> new DiscussionNotExistException(id));
    32119    }
    33120
    34121    @Override
    35122    public List<Discussion> listAllByTitle(String title) {
     123        if(title == null || title.isEmpty())
     124         return this.listAll();
    36125        return discussionRepository.findAllByTitleLike("%"+title+"%");
    37126    }
     127    private void addMovieForDiscussion(Integer id,Discussion discussion){
     128        Movie movie = findMovieById(id);
     129        discussion.setMovie(movie);
     130
     131    }
     132
     133    private void addPersonForDiscussion(Integer id, Discussion discussion){
     134        Person person = findPersonById(id);
     135        discussion.setPerson(person);
     136
     137    }
     138    private Movie findMovieById(Integer id){
     139        return this.movieRepository.findById(id).orElseThrow(() -> new MovieIdNotFoundException(id));
     140    }
     141    private Person findPersonById(Integer id){
     142        return this.personRepository.findById(id).orElseThrow(() -> new PersonNotExistException(id));
     143
     144    }
    38145}
  • src/main/java/com/wediscussmovies/project/service/impl/GenreServiceImpl.java

    r3ded84d re0ef1b1  
    11package com.wediscussmovies.project.service.impl;
    22
    3 import com.wediscussmovies.project.model.*;
     3import com.wediscussmovies.project.model.exception.GenreNotExistException;
     4import com.wediscussmovies.project.querymodels.GenreLikes;
    45import com.wediscussmovies.project.repository.GenreRepository;
     6import com.wediscussmovies.project.model.Genre;
    57import com.wediscussmovies.project.service.GenreService;
    68import org.springframework.stereotype.Service;
    79
    810import java.util.List;
    9 import java.util.Optional;
    1011
    1112@Service
     
    1819
    1920    @Override
    20     public Optional<Genre> findById(Integer id) {
    21         return genreRepository.findById(id);
     21    public Genre findById(Integer id) {
     22        return this.genreRepository.findById(id).orElseThrow(() -> new GenreNotExistException(id));
    2223    }
    2324
    2425    @Override
    2526    public List<Genre> findAllByType(String genre) {
    26         return genreRepository.findAllByGenreType(genre);
     27        return this.genreRepository.findAllByGenreType(genre);
    2728    }
    2829
     
    3031    public Genre save(String genreName) {
    3132       Genre genre = new Genre(genreName);
    32         return genreRepository.save(genre);
     33        return this.genreRepository.save(genre);
     34    }
     35
     36    @Override
     37    public List<GenreLikes> findAllWithLikes() {
     38        return this.genreRepository.findAllWithLikes();
    3339    }
    3440
    3541    @Override
    3642    public List<Genre> findAll() {
    37         return genreRepository.findAll();
     43        return this.genreRepository.findAll();
    3844    }
    3945}
  • src/main/java/com/wediscussmovies/project/service/impl/MovieServiceImpl.java

    r3ded84d re0ef1b1  
    11package com.wediscussmovies.project.service.impl;
    22
     3
     4import com.wediscussmovies.project.ajaxmodels.Grade;
    35import com.wediscussmovies.project.model.*;
    4 import com.wediscussmovies.project.repository.MovieRepository;
     6import com.wediscussmovies.project.model.exception.GenreNotExistException;
     7import com.wediscussmovies.project.model.exception.PersonNotExistException;
     8import com.wediscussmovies.project.model.exception.UserNotExistException;
     9import com.wediscussmovies.project.model.primarykeys.MovieLikesPK;
     10import com.wediscussmovies.project.model.primarykeys.MovieRatesPK;
     11import com.wediscussmovies.project.model.relation.MovieActors;
     12import com.wediscussmovies.project.model.relation.MovieGenres;
     13import com.wediscussmovies.project.model.relation.MovieLikes;
     14import com.wediscussmovies.project.model.relation.MovieRates;
     15import com.wediscussmovies.project.repository.*;
     16import com.wediscussmovies.project.model.exception.MovieIdNotFoundException;
    517import com.wediscussmovies.project.service.MovieService;
    618import org.springframework.stereotype.Service;
    719
     20import javax.swing.text.html.Option;
     21import javax.transaction.Transactional;
     22import java.sql.Date;
    823import java.util.List;
    924import java.util.Optional;
     25import java.util.stream.Collectors;
    1026
    1127@Service
    1228public class MovieServiceImpl implements MovieService {
     29
    1330    private final MovieRepository movieRepository;
    14 
    15     public MovieServiceImpl(MovieRepository movieRepository) {
     31    private final PersonRepository personRepository;
     32    private final GenreRepository genreRepository;
     33    private final MovieActorsRepository movieActorsRepository;
     34    private final MovieGenresRepository movieGenresRepository;
     35    private final UserRepository userRepository;
     36    private final MovieLikesRepository movieLikesRepository;
     37    private final MovieRatesRepository movieRatesRepository;
     38
     39    public MovieServiceImpl(MovieRepository movieRepository, PersonRepository personRepository,
     40                            GenreRepository genreRepository, MovieActorsRepository movieActorsRepository,
     41                            MovieGenresRepository movieGenresRepository,
     42                            UserRepository userRepository, MovieLikesRepository movieLikesRepository,
     43                            MovieRatesRepository movieRatesRepository) {
     44
    1645        this.movieRepository = movieRepository;
     46        this.personRepository = personRepository;
     47        this.genreRepository = genreRepository;
     48        this.movieActorsRepository = movieActorsRepository;
     49        this.movieGenresRepository = movieGenresRepository;
     50        this.userRepository = userRepository;
     51        this.movieLikesRepository = movieLikesRepository;
     52        this.movieRatesRepository = movieRatesRepository;
    1753    }
    1854
    1955    @Override
    2056    public List<Movie> listAll() {
    21         return movieRepository.findAll();
    22     }
    23 
    24     @Override
    25     public Optional<Movie> findById(Integer id) {
    26         return movieRepository.findById(1);
    27     }
    28 
    29     @Override
    30     public Movie save(Movie movie) {
    31         return movieRepository.save(movie);
    32     }
     57        return this.movieRepository.findAll();
     58    }
     59
     60    @Override
     61    public List<Movie> listAllByType(Character type) {
     62        if (type.equals('A'))
     63            return this.listAll();
     64        return this.listAllWithoutDirector();
     65    }
     66
     67    @Override
     68    public List<Movie> listAllWithoutDirector() {
     69        return this.movieRepository.findAllByDirectorIsNull();
     70    }
     71
     72    @Override
     73    public Movie findById(Integer id) {
     74        return this.movieRepository.findById(id).orElseThrow(() -> new MovieIdNotFoundException(id));
     75    }
     76
     77    @Override
     78    @Transactional
     79    public Movie save(String title, String description, String imageUrl,
     80                      Date airingDate, Double rating, Integer directorId,List<Integer> actorIds,List<Integer> genreIds) {
     81
     82
     83        Person director =getDirector(directorId);
     84        Movie movie = new Movie(title,description,imageUrl,airingDate,rating,director);
     85       movie = this.movieRepository.saveAndFlush(movie);
     86       addActorsAndGenresForMovie(movie,actorIds,genreIds);
     87       return movie;
     88    }
     89
     90
     91    @Transactional
     92    @Override
     93    public Movie edit(Integer movieId, String title, String description,
     94                      String imageUrl, Date airingDate, Double rating,
     95                      Integer directorId, List<Integer> actorIds,List<Integer> genreIds) {
     96
     97
     98        Person director =getDirector(directorId);
     99        Movie movie = this.findById(movieId);
     100
     101        movie.setTitle(title);
     102        movie.setDescription(description);
     103        movie.setImageUrl(imageUrl);
     104        movie.setAiringDate(airingDate);
     105        movie.setImdbRating(rating);
     106        movie.setDirector(director);
     107
     108        this.movieActorsRepository.deleteAllByMovie(movie);
     109        this.movieGenresRepository.deleteAllByMovie(movie);
     110
     111        movie = this.movieRepository.save(movie);
     112        this.addActorsAndGenresForMovie(movie,actorIds,genreIds);
     113
     114        return movie;
     115    }
     116
     117    @Override
     118    public List<Person> findAllActorsForMovie(Movie movie) {
     119        return this.movieActorsRepository.findAllByMovie(movie)
     120                .stream()
     121                .map(MovieActors::getPerson)
     122                .collect(Collectors.toList());
     123    }
     124
     125    @Override
     126    public List<Genre> findAllGenresForMovie(Movie movie) {
     127        return this.movieGenresRepository.findAllByMovie(movie)
     128                .stream()
     129                .map(MovieGenres::getGenre)
     130                .collect(Collectors.toList());
     131    }
     132
     133    @Override
     134    public void likeMovie(Integer movieId, Integer userId) {
     135        User user = this.userRepository.findById(userId).orElseThrow(() -> new UserNotExistException(userId.toString()));
     136        Movie movie = this.findById(movieId);
     137
     138        this.movieLikesRepository.save(new MovieLikes(movie, user));
     139    }
     140
     141    @Override
     142    public void unlikeMovie(Integer movieId, Integer userId) {
     143        MovieLikesPK movieLikesPK = new MovieLikesPK(movieId,userId);
     144        this.movieLikesRepository.deleteById(movieLikesPK);
     145    }
     146
     147    @Override
     148    public List<Movie> findLikedMoviesByUser(User user) {
     149        return
     150                this.movieLikesRepository
     151                        .findAllByUser(user)
     152                        .stream()
     153                        .map(MovieLikes::getMovie)
     154                        .collect(Collectors.toList());
     155    }
     156
    33157
    34158    @Override
    35159    public void deleteById(Integer id) {
    36         movieRepository.deleteById(id);
     160        this.movieRepository.deleteById(id);
     161    }
     162
     163    @Override
     164    public void addGradeMovie(Integer movieId, User user, Grade grade) {
     165        MovieRatesPK key =  new MovieRatesPK(movieId, user.getUserId());
     166        Optional<MovieRates> movieRates = this.movieRatesRepository.findById(key);
     167        if (movieRates.isPresent()){
     168           MovieRates rates = movieRates.get();
     169            rates.setReason(grade.getReason());
     170            rates.setStarsRated(grade.getRating());
     171            this.movieRatesRepository.save(rates);
     172        }
     173        else{
     174            Movie movie = this.findById(movieId);
     175            MovieRates rates = new MovieRates(user,movie,grade.getReason(),grade.getRating());
     176            this.movieRatesRepository.save(rates);
     177        }
    37178    }
    38179
    39180    @Override
    40181    public List<Movie> searchByTitle(String title) {
    41         return movieRepository.findAllByTitleLike("%"+title+"%");
     182        return this.movieRepository.findAllByTitleLike("%"+title+"%");
     183    }
     184
     185    @Transactional
     186    void addActorsAndGenresForMovie(Movie movie, List<Integer> actorIds, List<Integer> genreIds)
     187    {
     188
     189        actorIds.stream()
     190                .map(this::findPersonById)
     191                .forEach(actor -> this.movieActorsRepository.save(new MovieActors(movie,actor)));
     192
     193        genreIds.stream()
     194                .map(this::findGenreById)
     195                .forEach(genre -> this.movieGenresRepository.save(new MovieGenres(movie,genre)));
     196
     197    }
     198    private Person getDirector(Integer directorId){
     199
     200        if (directorId != null)
     201            return this.findPersonById(directorId);
     202        return null;
     203
     204    }
     205    private Person findPersonById(Integer id){
     206        return this.personRepository.findById(id).orElseThrow(() -> new PersonNotExistException(id));
     207    }
     208    private Genre findGenreById(Integer id){
     209        return this.genreRepository.findById(id).orElseThrow(() -> new GenreNotExistException(id));
    42210    }
    43211}
  • src/main/java/com/wediscussmovies/project/service/impl/PersonServiceImpl.java

    r3ded84d re0ef1b1  
    11package com.wediscussmovies.project.service.impl;
    22
    3 import com.wediscussmovies.project.model.*;
    4 import com.wediscussmovies.project.model.enumerations.PersonType;
     3import com.wediscussmovies.project.ajaxmodels.Grade;
     4import com.wediscussmovies.project.model.Movie;
     5import com.wediscussmovies.project.model.User;
     6import com.wediscussmovies.project.model.exception.MovieHasAlreadyDirector;
     7import com.wediscussmovies.project.model.exception.MovieIdNotFoundException;
     8import com.wediscussmovies.project.model.exception.PersonNotExistException;
     9import com.wediscussmovies.project.model.primarykeys.PersonRatesPK;
     10import com.wediscussmovies.project.model.relation.MovieActors;
     11import com.wediscussmovies.project.model.relation.MovieRates;
     12import com.wediscussmovies.project.model.relation.PersonRates;
     13import com.wediscussmovies.project.repository.MovieActorsRepository;
     14import com.wediscussmovies.project.repository.MovieRepository;
     15import com.wediscussmovies.project.repository.PersonRatesRepository;
    516import com.wediscussmovies.project.repository.PersonRepository;
     17import com.wediscussmovies.project.model.Person;
    618import com.wediscussmovies.project.service.PersonService;
    719import org.springframework.stereotype.Service;
    820
     21import javax.transaction.Transactional;
     22import java.sql.Date;
    923import java.util.List;
    1024import java.util.Optional;
     25import java.util.stream.Collectors;
    1126
    1227@Service
    1328public class PersonServiceImpl implements PersonService {
     29
    1430    private final PersonRepository personRepository;
     31    private final MovieRepository movieRepository;
     32    private final MovieActorsRepository movieActorsRepository;
     33    private final PersonRatesRepository personRatesRepository;
    1534
    1635
    17     public PersonServiceImpl(PersonRepository personRepository) {
     36    public PersonServiceImpl(PersonRepository personRepository,
     37                             MovieRepository movieRepository, MovieActorsRepository movieActorsRepository,
     38                             PersonRatesRepository personRatesRepository) {
    1839        this.personRepository = personRepository;
     40        this.movieRepository = movieRepository;
     41        this.movieActorsRepository = movieActorsRepository;
     42        this.personRatesRepository = personRatesRepository;
     43    }
     44
     45    @Override
     46    public List<Person> findAll() {
     47        return this.personRepository.findAll();
    1948    }
    2049
    2150    @Override
    2251    public List<Person> findAllDirectors() {
    23         return personRepository.findAllByType('D');
     52        return this.personRepository.findAllByType('D');
    2453    }
    2554
    2655    @Override
    27     public Optional<Person> findActorById(Integer id) {
    28         return personRepository.findPersonByPersonIdAndType(id, 'A');
     56    public Person findById(Integer id) {
     57        return this.personRepository.findById(id).orElseThrow(() -> new PersonNotExistException(id));
     58    }
     59
     60
     61
     62    @Override
     63    public List<Person> findAllActors() {
     64        return this.personRepository.findAllByType('A');
    2965    }
    3066
    3167    @Override
    32     public Optional<Person> findById(Integer person_id) {
    33         return personRepository.findById(person_id);
     68    @Transactional
     69    public Person save(String name, String surname, Character type, Date birthDate, String image_url,
     70                       String description,List<Integer> movieIds) {
     71
     72        Person person = this.personRepository.saveAndFlush(new Person(name,surname,type,birthDate,image_url,description));
     73
     74        if (movieIds!= null) {
     75            if (type.equals('A'))
     76                this.addActorForMovies(person, movieIds);
     77            else
     78                this.addDirectorForMovies(person, movieIds);
     79        }
     80         return person;
     81
    3482    }
    3583
    3684    @Override
    37     public Optional<Person> findDirectorById(Integer id) {
    38         return personRepository.findPersonByPersonIdAndType(id, 'D');
     85    @Transactional
     86    public Person edit(Integer personId, String name, String surname,
     87                       Character type, Date birthDate, String image_url, String description, List<Integer> movieIds) {
     88        Person person = this.findById(personId);
     89
     90        person.setName(name);
     91        person.setSurname(surname);
     92        person.setDateOfBirth(birthDate);
     93        person.setImageUrl(image_url);
     94        person.setType(type);
     95        person.setDescription(description);
     96
     97
     98        person = this.personRepository.saveAndFlush(person);
     99
     100            this.movieActorsRepository.deleteAllByPerson(person);
     101            this.removeMovieDirectorsBeforeEdit(person);
     102        if (movieIds != null) {
     103            if (type.equals('A'))
     104                this.addActorForMovies(person, movieIds);
     105            else
     106                this.addDirectorForMovies(person, movieIds);
     107        }
     108
     109        return person;
     110
    39111    }
    40112
    41113    @Override
    42     public List<Person> findAllActors() {
    43         return personRepository.findAllByType('A');
     114    public List<Person> findPersonsByNameOrSurname(Character type,String searchQuery) {
     115        if (searchQuery != null && searchQuery.isEmpty())
     116            return this.personRepository.findAllByTypeAndNameLikeOrSurnameLike(type,searchQuery,searchQuery);
     117        if (type.equals('D'))
     118            return this.findAllDirectors();
     119        return this.findAllActors();
     120
    44121    }
    45122
    46123    @Override
    47     public boolean save(Person person) {
    48         Person p = personRepository.save(person);
    49         if(p != null)
    50             return true;
    51         return false;
     124    public List<Movie> findAllMoviesByPerson(Person person) {
     125        if (person.getType().equals('A'))
     126            return this.movieActorsRepository
     127                .findAllByPerson(person)
     128                .stream()
     129                .map(MovieActors::getMovie)
     130                .collect(Collectors.toList());
     131        return this.movieRepository.findAllByDirector(person);
    52132    }
    53133
    54134    @Override
    55     public List<Person> findActorsByNameLike(String name) {
    56         return personRepository.findAllByTypeAndNameLike('A', name);
     135    public void deleteById(Integer id) {
     136        this.personRepository.deleteById(id);
    57137    }
    58138
    59139    @Override
    60     public List<Person> findActorsBySurnameLike(String surname) {
    61         return personRepository.findAllByTypeAndSurnameLike('A', surname);
     140    public void addGradePerson(Integer personId, User user, Grade grade) {
     141        PersonRatesPK key =  new PersonRatesPK(personId, user.getUserId());
     142        Optional<PersonRates> personRates = this.personRatesRepository.findById(key);
     143        if (personRates.isPresent()){
     144            PersonRates rates = personRates.get();
     145            rates.setReason(grade.getReason());
     146            rates.setStarsRated(grade.getRating());
     147            this.personRatesRepository.save(rates);
     148        }
     149        else{
     150            Person person = this.findById(personId);
     151            PersonRates rates = new PersonRates(user,person,grade.getReason(),grade.getRating());
     152            this.personRatesRepository.save(rates);
     153        }
     154
    62155    }
    63156
    64     @Override
    65     public List<Person> findDirectorsByNameLike(String name) {
    66         return personRepository.findAllByTypeAndNameLike('D', name);
     157
     158    private void addActorForMovies(Person person, List<Integer> movieIds){
     159
     160        movieIds.stream()
     161                .map(this::findMovieById)
     162                .forEach(m -> this.movieActorsRepository.save(new MovieActors(m,person)));
     163
     164    }
     165    private void addDirectorForMovies(Person person, List<Integer> movieIds){
     166        movieIds.stream()
     167                .map(this::findMovieById)
     168                .forEach(m -> this.addMovieDirector(m,person));
     169    }
     170    private void addMovieDirector(Movie movie,Person person){
     171        Person director = movie.getDirector();
     172        if (director != null)
     173            throw new MovieHasAlreadyDirector(movie.getTitle(),director.getName(),director.getSurname());
     174        movie.setDirector(person);
     175        this.movieRepository.save(movie);
     176    }
     177    private void removeMovieDirectorsBeforeEdit(Person person){
     178        this.movieRepository.findAllByDirector(person)
     179                .forEach(m ->{ m.setDirector(null); this.movieRepository.save(m); });
     180
    67181    }
    68182
    69     @Override
    70     public List<Person> findDirectorsBySurnameLike(String surname) {
    71         return personRepository.findAllByTypeAndSurnameLike('D', surname);
     183    private Movie findMovieById(Integer id){
     184        return this.movieRepository.findById(id).orElseThrow(() -> new MovieIdNotFoundException(id));
    72185    }
     186
    73187}
  • src/main/java/com/wediscussmovies/project/service/impl/ReplyServiceImpl.java

    r3ded84d re0ef1b1  
    11package com.wediscussmovies.project.service.impl;
    22
    3 import com.wediscussmovies.project.model.*;
     3import com.wediscussmovies.project.model.Discussion;
     4import com.wediscussmovies.project.model.exception.DiscussionNotExistException;
     5import com.wediscussmovies.project.model.exception.ReplyNotExistException;
     6import com.wediscussmovies.project.model.primarykeys.ReplyPK;
     7import com.wediscussmovies.project.repository.DiscussionRepository;
    48import com.wediscussmovies.project.repository.ReplyRepository;
     9import com.wediscussmovies.project.model.Reply;
     10import com.wediscussmovies.project.model.User;
     11import com.wediscussmovies.project.repository.UserRepository;
    512import com.wediscussmovies.project.service.ReplyService;
    613import org.springframework.stereotype.Service;
    714
    8 import java.util.Optional;
     15import java.sql.Date;
     16import java.time.LocalDate;
     17import java.util.List;
    918
    1019@Service
    1120public class ReplyServiceImpl implements ReplyService {
     21
    1222    private final ReplyRepository replyRepository;
     23    private final DiscussionRepository discussionRepository;
    1324
    14     public ReplyServiceImpl(ReplyRepository replyRepository) {
     25    public ReplyServiceImpl(ReplyRepository replyRepository,
     26                            DiscussionRepository discussionRepository) {
    1527        this.replyRepository = replyRepository;
     28        this.discussionRepository = discussionRepository;
     29    }
     30
     31
     32
     33    @Override
     34    public void save(Integer discussionId, String text,User user) {
     35
     36        Discussion discussion = this.discussionRepository.findById(discussionId).orElseThrow(() -> new DiscussionNotExistException(discussionId));
     37
     38        Date date = Date.valueOf(LocalDate.now());
     39        this.replyRepository.insertInto(text,date,user.getUserId(),discussionId);
     40
    1641    }
    1742
    1843    @Override
    19     public Optional<Reply> findById(Long id) {
    20         return replyRepository.findById(1);
     44    public void likeReply(Integer replyId, Integer userId) {
     45        //da se implementira, promena vo baza
    2146    }
    2247
    2348    @Override
    24     public void delete(Reply r) {
    25         replyRepository.delete(r);
     49    public void unlikeReply(Integer replyId, Integer userId) {
     50        // da se implementira, promena vo baza
    2651    }
    2752
    2853    @Override
    29     public Reply save(Reply r) {
    30         return replyRepository.save(r);
     54    public List<Reply> findAllByDiscussion(Discussion discussion) {
     55        return this.replyRepository.findAllByDiscussion(discussion);
     56    }
     57
     58
     59    @Override
     60    public Reply edit(Integer replyId,Integer discussionId,String text) {
     61        ReplyPK replyPK = new ReplyPK(discussionId,replyId);
     62        Reply reply = this.replyRepository.findById(replyPK).orElseThrow();
     63        reply.setText(text);
     64        return  this.replyRepository.save(reply);
     65    }
     66
     67    @Override
     68    public void delete(Integer discussionId, Integer replyId) {
     69        this.replyRepository.deleteById(new ReplyPK(discussionId,replyId));
     70    }
     71
     72    @Override
     73    public Reply findById(Integer discussionId, Integer replyId) {
     74        ReplyPK replyPK  = new ReplyPK(discussionId,replyId);
     75        return this.replyRepository.findById(replyPK).orElseThrow(() -> new ReplyNotExistException(replyPK));
     76
    3177    }
    3278}
  • src/main/java/com/wediscussmovies/project/service/impl/UserServiceImpl.java

    r3ded84d re0ef1b1  
    11package com.wediscussmovies.project.service.impl;
    22
    3 import com.wediscussmovies.project.model.*;
    4 import com.wediscussmovies.project.model.exception.PasswordsDontMatchException;
    5 import com.wediscussmovies.project.model.exception.UserWithEmailAlreadyExists;
    6 import com.wediscussmovies.project.model.exception.UserWithUsernameAlreadyExists;
     3import com.wediscussmovies.project.model.exception.InvalidArgumentsException;
     4import com.wediscussmovies.project.model.exception.PasswordsDoNotMatchException;
     5import com.wediscussmovies.project.model.exception.UserNotExistException;
     6import com.wediscussmovies.project.model.User;
     7import com.wediscussmovies.project.model.exception.UsernameAlreadyExistsException;
    78import com.wediscussmovies.project.repository.UserRepository;
    89import com.wediscussmovies.project.service.UserService;
     10import org.springframework.security.core.userdetails.UserDetails;
     11import org.springframework.security.core.userdetails.UsernameNotFoundException;
     12import org.springframework.security.crypto.password.PasswordEncoder;
    913import org.springframework.stereotype.Service;
    10 
    11 import javax.servlet.http.HttpServletRequest;
    12 import java.util.Optional;
    1314
    1415@Service
    1516public class UserServiceImpl implements UserService {
     17
    1618    private final UserRepository userRepository;
     19    private final PasswordEncoder passwordEncoder;
    1720
    18     public UserServiceImpl(UserRepository userRepository) {
     21
     22    public UserServiceImpl(UserRepository userRepository, PasswordEncoder passwordEncoder) {
    1923        this.userRepository = userRepository;
     24        this.passwordEncoder = passwordEncoder;
    2025    }
    2126
    2227    @Override
    23     public Optional<User> login(String email, String password) {
    24         Optional<User> userEmail = userRepository.findByEmailAndPassword(email, password);
    25         Optional<User> userUsername = userRepository.findByUsernameAndPassword(email, password);
    26         if(userEmail.isPresent())
    27             return userEmail;
    28         else if(userUsername.isPresent())
    29             return userUsername;
    30         else
    31             return Optional.empty();
     28    public User findByUsername(String username) {
     29        return this.userRepository.findByUsername(username).orElseThrow(() -> new UserNotExistException(username));
     30    }
     31
     32
     33
     34    @Override
     35    public User register( String email, String username, String password, String confirmPassword
     36                                  , String name, String surname) {
     37
     38        if ( username.isEmpty() || password.isEmpty())
     39            throw new InvalidArgumentsException();
     40        if (!password.equals(confirmPassword))
     41            throw new PasswordsDoNotMatchException();
     42        if(this.userRepository.findByUsername(username).isPresent())
     43            throw new UsernameAlreadyExistsException(username);
     44        User user = new User(email,username,passwordEncoder.encode(password),name,surname);
     45        return userRepository.save(user);
     46
    3247    }
    3348
    3449    @Override
    35     public Optional<User> register(HttpServletRequest request, String email, String password, String confirmPassword,
    36                                    String username, String name, String surname) {
    37         if(email == null || email.isEmpty() || password == null || password.isEmpty() ||
    38                 confirmPassword == null || confirmPassword.isEmpty() || username == null || username.isEmpty() ||
    39                 name == null || name.isEmpty() || surname == null || surname.isEmpty()) {
    40             request.getSession().setAttribute("error", "Not all of the fields had a value in them, check again.");
    41             return Optional.empty();
    42         }
    43         if(userRepository.findByUsername(username).isPresent()){
    44             request.getSession().setAttribute("error", new UserWithUsernameAlreadyExists(username).getMessage());
    45             return Optional.empty();
    46         }
    47         if(userRepository.findByEmail(email).isPresent()){
    48             request.getSession().setAttribute("error", new UserWithEmailAlreadyExists(email).getMessage());
    49             return Optional.empty();
    50         }
    51         if(!password.equals(confirmPassword)){
    52             request.getSession().setAttribute("error", new PasswordsDontMatchException().getMessage());
    53             return Optional.empty();
    54         }
    55         User user = new User(email, username, password, name, surname);
    56         return Optional.of(userRepository.save(user));
     50    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
     51        return this.findByUsername(username);
    5752    }
    5853}
Note: See TracChangeset for help on using the changeset viewer.