Changeset e0ef1b1 in Git for src/main/java/com/wediscussmovies/project/service/impl/DiscussionServiceImpl.java
- Timestamp:
- 02/04/22 23:22:13 (3 years ago)
- Branches:
- main
- Children:
- 5b447b0
- Parents:
- 3ded84d
- File:
-
- 1 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 }
Note:
See TracChangeset
for help on using the changeset viewer.