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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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}
Note: See TracChangeset for help on using the changeset viewer.