Changeset e0ef1b1 in Git for src/main/java/com/wediscussmovies/project/service/impl
- Timestamp:
- 02/04/22 23:22:13 (3 years ago)
- Branches:
- main
- Children:
- 5b447b0
- Parents:
- 3ded84d
- 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 1 1 package com.wediscussmovies.project.service.impl; 2 2 3 import com.wediscussmovies.project.model.*; 3 import com.wediscussmovies.project.model.exception.DiscussionNotExistException; 4 import com.wediscussmovies.project.model.exception.MovieIdNotFoundException; 5 import com.wediscussmovies.project.model.exception.PersonNotExistException; 6 import com.wediscussmovies.project.model.exception.UserNotExistException; 4 7 import com.wediscussmovies.project.repository.DiscussionRepository; 8 import com.wediscussmovies.project.model.Discussion; 9 import com.wediscussmovies.project.model.Movie; 10 import com.wediscussmovies.project.model.Person; 11 import com.wediscussmovies.project.model.User; 12 import com.wediscussmovies.project.repository.MovieRepository; 13 import com.wediscussmovies.project.repository.PersonRepository; 14 import com.wediscussmovies.project.repository.UserRepository; 5 15 import com.wediscussmovies.project.service.DiscussionService; 16 import com.wediscussmovies.project.service.MovieService; 17 import com.wediscussmovies.project.service.PersonService; 18 import com.wediscussmovies.project.service.UserService; 6 19 import org.springframework.stereotype.Service; 7 20 21 import java.sql.Date; 22 import java.time.LocalDate; 8 23 import java.util.List; 9 import java.util.Optional;10 24 11 25 @Service 12 26 public class DiscussionServiceImpl implements DiscussionService { 27 13 28 private final DiscussionRepository discussionRepository; 29 private final UserRepository userRepository; 14 30 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) { 16 36 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(); 17 48 } 18 49 19 50 @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; 23 54 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 26 66 discussionRepository.save(discussion); 27 67 } 28 68 29 69 @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)); 32 119 } 33 120 34 121 @Override 35 122 public List<Discussion> listAllByTitle(String title) { 123 if(title == null || title.isEmpty()) 124 return this.listAll(); 36 125 return discussionRepository.findAllByTitleLike("%"+title+"%"); 37 126 } 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 } 38 145 } -
src/main/java/com/wediscussmovies/project/service/impl/GenreServiceImpl.java
r3ded84d re0ef1b1 1 1 package com.wediscussmovies.project.service.impl; 2 2 3 import com.wediscussmovies.project.model.*; 3 import com.wediscussmovies.project.model.exception.GenreNotExistException; 4 import com.wediscussmovies.project.querymodels.GenreLikes; 4 5 import com.wediscussmovies.project.repository.GenreRepository; 6 import com.wediscussmovies.project.model.Genre; 5 7 import com.wediscussmovies.project.service.GenreService; 6 8 import org.springframework.stereotype.Service; 7 9 8 10 import java.util.List; 9 import java.util.Optional;10 11 11 12 @Service … … 18 19 19 20 @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)); 22 23 } 23 24 24 25 @Override 25 26 public List<Genre> findAllByType(String genre) { 26 return genreRepository.findAllByGenreType(genre);27 return this.genreRepository.findAllByGenreType(genre); 27 28 } 28 29 … … 30 31 public Genre save(String genreName) { 31 32 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(); 33 39 } 34 40 35 41 @Override 36 42 public List<Genre> findAll() { 37 return genreRepository.findAll();43 return this.genreRepository.findAll(); 38 44 } 39 45 } -
src/main/java/com/wediscussmovies/project/service/impl/MovieServiceImpl.java
r3ded84d re0ef1b1 1 1 package com.wediscussmovies.project.service.impl; 2 2 3 4 import com.wediscussmovies.project.ajaxmodels.Grade; 3 5 import com.wediscussmovies.project.model.*; 4 import com.wediscussmovies.project.repository.MovieRepository; 6 import com.wediscussmovies.project.model.exception.GenreNotExistException; 7 import com.wediscussmovies.project.model.exception.PersonNotExistException; 8 import com.wediscussmovies.project.model.exception.UserNotExistException; 9 import com.wediscussmovies.project.model.primarykeys.MovieLikesPK; 10 import com.wediscussmovies.project.model.primarykeys.MovieRatesPK; 11 import com.wediscussmovies.project.model.relation.MovieActors; 12 import com.wediscussmovies.project.model.relation.MovieGenres; 13 import com.wediscussmovies.project.model.relation.MovieLikes; 14 import com.wediscussmovies.project.model.relation.MovieRates; 15 import com.wediscussmovies.project.repository.*; 16 import com.wediscussmovies.project.model.exception.MovieIdNotFoundException; 5 17 import com.wediscussmovies.project.service.MovieService; 6 18 import org.springframework.stereotype.Service; 7 19 20 import javax.swing.text.html.Option; 21 import javax.transaction.Transactional; 22 import java.sql.Date; 8 23 import java.util.List; 9 24 import java.util.Optional; 25 import java.util.stream.Collectors; 10 26 11 27 @Service 12 28 public class MovieServiceImpl implements MovieService { 29 13 30 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 16 45 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; 17 53 } 18 54 19 55 @Override 20 56 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 33 157 34 158 @Override 35 159 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 } 37 178 } 38 179 39 180 @Override 40 181 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)); 42 210 } 43 211 } -
src/main/java/com/wediscussmovies/project/service/impl/PersonServiceImpl.java
r3ded84d re0ef1b1 1 1 package com.wediscussmovies.project.service.impl; 2 2 3 import com.wediscussmovies.project.model.*; 4 import com.wediscussmovies.project.model.enumerations.PersonType; 3 import com.wediscussmovies.project.ajaxmodels.Grade; 4 import com.wediscussmovies.project.model.Movie; 5 import com.wediscussmovies.project.model.User; 6 import com.wediscussmovies.project.model.exception.MovieHasAlreadyDirector; 7 import com.wediscussmovies.project.model.exception.MovieIdNotFoundException; 8 import com.wediscussmovies.project.model.exception.PersonNotExistException; 9 import com.wediscussmovies.project.model.primarykeys.PersonRatesPK; 10 import com.wediscussmovies.project.model.relation.MovieActors; 11 import com.wediscussmovies.project.model.relation.MovieRates; 12 import com.wediscussmovies.project.model.relation.PersonRates; 13 import com.wediscussmovies.project.repository.MovieActorsRepository; 14 import com.wediscussmovies.project.repository.MovieRepository; 15 import com.wediscussmovies.project.repository.PersonRatesRepository; 5 16 import com.wediscussmovies.project.repository.PersonRepository; 17 import com.wediscussmovies.project.model.Person; 6 18 import com.wediscussmovies.project.service.PersonService; 7 19 import org.springframework.stereotype.Service; 8 20 21 import javax.transaction.Transactional; 22 import java.sql.Date; 9 23 import java.util.List; 10 24 import java.util.Optional; 25 import java.util.stream.Collectors; 11 26 12 27 @Service 13 28 public class PersonServiceImpl implements PersonService { 29 14 30 private final PersonRepository personRepository; 31 private final MovieRepository movieRepository; 32 private final MovieActorsRepository movieActorsRepository; 33 private final PersonRatesRepository personRatesRepository; 15 34 16 35 17 public PersonServiceImpl(PersonRepository personRepository) { 36 public PersonServiceImpl(PersonRepository personRepository, 37 MovieRepository movieRepository, MovieActorsRepository movieActorsRepository, 38 PersonRatesRepository personRatesRepository) { 18 39 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(); 19 48 } 20 49 21 50 @Override 22 51 public List<Person> findAllDirectors() { 23 return personRepository.findAllByType('D');52 return this.personRepository.findAllByType('D'); 24 53 } 25 54 26 55 @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'); 29 65 } 30 66 31 67 @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 34 82 } 35 83 36 84 @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 39 111 } 40 112 41 113 @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 44 121 } 45 122 46 123 @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); 52 132 } 53 133 54 134 @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); 57 137 } 58 138 59 139 @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 62 155 } 63 156 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 67 181 } 68 182 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)); 72 185 } 186 73 187 } -
src/main/java/com/wediscussmovies/project/service/impl/ReplyServiceImpl.java
r3ded84d re0ef1b1 1 1 package com.wediscussmovies.project.service.impl; 2 2 3 import com.wediscussmovies.project.model.*; 3 import com.wediscussmovies.project.model.Discussion; 4 import com.wediscussmovies.project.model.exception.DiscussionNotExistException; 5 import com.wediscussmovies.project.model.exception.ReplyNotExistException; 6 import com.wediscussmovies.project.model.primarykeys.ReplyPK; 7 import com.wediscussmovies.project.repository.DiscussionRepository; 4 8 import com.wediscussmovies.project.repository.ReplyRepository; 9 import com.wediscussmovies.project.model.Reply; 10 import com.wediscussmovies.project.model.User; 11 import com.wediscussmovies.project.repository.UserRepository; 5 12 import com.wediscussmovies.project.service.ReplyService; 6 13 import org.springframework.stereotype.Service; 7 14 8 import java.util.Optional; 15 import java.sql.Date; 16 import java.time.LocalDate; 17 import java.util.List; 9 18 10 19 @Service 11 20 public class ReplyServiceImpl implements ReplyService { 21 12 22 private final ReplyRepository replyRepository; 23 private final DiscussionRepository discussionRepository; 13 24 14 public ReplyServiceImpl(ReplyRepository replyRepository) { 25 public ReplyServiceImpl(ReplyRepository replyRepository, 26 DiscussionRepository discussionRepository) { 15 27 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 16 41 } 17 42 18 43 @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 21 46 } 22 47 23 48 @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 26 51 } 27 52 28 53 @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 31 77 } 32 78 } -
src/main/java/com/wediscussmovies/project/service/impl/UserServiceImpl.java
r3ded84d re0ef1b1 1 1 package com.wediscussmovies.project.service.impl; 2 2 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; 3 import com.wediscussmovies.project.model.exception.InvalidArgumentsException; 4 import com.wediscussmovies.project.model.exception.PasswordsDoNotMatchException; 5 import com.wediscussmovies.project.model.exception.UserNotExistException; 6 import com.wediscussmovies.project.model.User; 7 import com.wediscussmovies.project.model.exception.UsernameAlreadyExistsException; 7 8 import com.wediscussmovies.project.repository.UserRepository; 8 9 import com.wediscussmovies.project.service.UserService; 10 import org.springframework.security.core.userdetails.UserDetails; 11 import org.springframework.security.core.userdetails.UsernameNotFoundException; 12 import org.springframework.security.crypto.password.PasswordEncoder; 9 13 import org.springframework.stereotype.Service; 10 11 import javax.servlet.http.HttpServletRequest;12 import java.util.Optional;13 14 14 15 @Service 15 16 public class UserServiceImpl implements UserService { 17 16 18 private final UserRepository userRepository; 19 private final PasswordEncoder passwordEncoder; 17 20 18 public UserServiceImpl(UserRepository userRepository) { 21 22 public UserServiceImpl(UserRepository userRepository, PasswordEncoder passwordEncoder) { 19 23 this.userRepository = userRepository; 24 this.passwordEncoder = passwordEncoder; 20 25 } 21 26 22 27 @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 32 47 } 33 48 34 49 @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); 57 52 } 58 53 }
Note:
See TracChangeset
for help on using the changeset viewer.