Backend changes.
This commit is contained in:
5
.gitignore
vendored
5
.gitignore
vendored
@@ -88,4 +88,7 @@ nbdist/
|
||||
.nb-gradle/
|
||||
|
||||
### VS Code ###
|
||||
.vscode/
|
||||
.vscode/
|
||||
/frontend/node_modules/
|
||||
/.idea/
|
||||
/.gradle/
|
||||
|
||||
@@ -26,7 +26,7 @@ public class Customer
|
||||
@Column
|
||||
@NotBlank(message = "Password can't be empty")
|
||||
private String password;
|
||||
@OneToMany(fetch = FetchType.EAGER)
|
||||
@OneToMany(fetch = FetchType.EAGER, mappedBy = "customer", cascade = CascadeType.ALL)
|
||||
private List<Movie> movies;
|
||||
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ public class Genre
|
||||
@NotBlank(message = "Genre's can't be null or empty")
|
||||
private String name;
|
||||
|
||||
@OneToMany(fetch = FetchType.EAGER, mappedBy = "genre")
|
||||
@OneToMany(fetch = FetchType.EAGER, mappedBy = "genre", cascade = CascadeType.ALL)
|
||||
private List<Movie> movies;
|
||||
|
||||
public Genre()
|
||||
|
||||
@@ -25,25 +25,43 @@ public class MovieController {
|
||||
.toList();
|
||||
}
|
||||
|
||||
@GetMapping("/{customerId}")
|
||||
public List<MovieDTO> getCustomerMovies(@PathVariable("customerId") Long customerId) {
|
||||
return movieService.findCustomerMovies(customerId).stream()
|
||||
.map(MovieDTO::new)
|
||||
.toList();
|
||||
}
|
||||
|
||||
@GetMapping("/{genre}")
|
||||
public List<MovieDTO> getSpecificMovies(@RequestParam("genre") String genreName) {
|
||||
public List<MovieDTO> getSpecificMovies(@PathVariable("genre") String genreName) {
|
||||
return movieService.findAllSpecificMovies(genreName).stream()
|
||||
.map(MovieDTO::new)
|
||||
.toList();
|
||||
}
|
||||
@PostMapping
|
||||
public MovieDTO createMovie(@RequestParam("title") String title, @RequestParam("length") int length, @RequestParam("score") double score, @RequestParam("genre") Long genreId, @RequestParam("customer") Long customerId) {
|
||||
return new MovieDTO(movieService.addMovie(title,length,score,genreId,customerId));
|
||||
public MovieDTO createMovie(@RequestParam("title") String title, @RequestParam("length") int length, @RequestParam("score") double score, @RequestParam("genre") Long genreId) {
|
||||
return new MovieDTO(movieService.addMovie(title,length,score,genreId));
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public MovieDTO updateMovie(@PathVariable Long id,
|
||||
public MovieDTO updateMovie(@PathVariable("id") Long id,
|
||||
@RequestParam("title") String title,@RequestParam("length") int length,@RequestParam("score") double score ) {
|
||||
return new MovieDTO(movieService.updateMovie(id,title,length,score));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public MovieDTO deleteMovie(@PathVariable("id") Long id)
|
||||
{
|
||||
return new MovieDTO(movieService.deleteMovie(id));
|
||||
}
|
||||
|
||||
@PostMapping("/{customerId}/{id}")
|
||||
public MovieDTO assignMovie(@PathVariable("customerId") Long customerId, @PathVariable("id") Long id)
|
||||
{
|
||||
return new MovieDTO(movieService.assignMovie(customerId,id));
|
||||
}
|
||||
@DeleteMapping("/{customerId}/{id}")
|
||||
public MovieDTO deleteMovie(@PathVariable("customerId") Long customerId,@PathVariable Long id) {
|
||||
return new MovieDTO(movieService.deleteMovie(id,customerId));
|
||||
public MovieDTO deleteMovieCustomer(@PathVariable("customerId") Long customerId,@PathVariable("id") Long id) {
|
||||
return new MovieDTO(movieService.deleteMovieCustomer(id,customerId));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,13 +9,16 @@ import ru.ulstu.is.sbapp.Customer.Repository.CustomerRepository;
|
||||
import ru.ulstu.is.sbapp.Genre.Exception.GenreNotFoundException;
|
||||
import ru.ulstu.is.sbapp.Genre.Model.Genre;
|
||||
import ru.ulstu.is.sbapp.Genre.Repository.GenreRepository;
|
||||
import ru.ulstu.is.sbapp.Movie.Controller.MovieDTO;
|
||||
import ru.ulstu.is.sbapp.Movie.Exception.MovieNotFoundException;
|
||||
import ru.ulstu.is.sbapp.Movie.Model.Movie;
|
||||
import ru.ulstu.is.sbapp.Movie.Repository.MovieRepository;
|
||||
import ru.ulstu.is.sbapp.Utilities.validation.ValidatorUtil;
|
||||
|
||||
import javax.swing.text.html.Option;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
public class MovieService
|
||||
@@ -33,14 +36,14 @@ public class MovieService
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Movie addMovie(String title, int length, double score, Long genre, Long customer)
|
||||
public Movie addMovie(String title, int length, double score, Long genre)
|
||||
{
|
||||
if(!StringUtils.hasText(title) || length == 0 || score == 0 || genre == 0 || customer == 0)
|
||||
if(!StringUtils.hasText(title) || length == 0 || score == 0 || genre == 0)
|
||||
{
|
||||
throw new IllegalArgumentException("Some of the movie's properties are incorrect.");
|
||||
}
|
||||
final Optional<Genre> specGenre = genreRepository.findById(genre);
|
||||
final Optional<Customer> specCustomer = customerRepository.findById(customer);
|
||||
|
||||
|
||||
if(specGenre.isEmpty())
|
||||
{
|
||||
@@ -52,16 +55,29 @@ public class MovieService
|
||||
|
||||
specGenre.get().getMovies().add(movie);
|
||||
|
||||
if(specCustomer.isEmpty())
|
||||
{
|
||||
throw new CustomerNotFoundException(customer);
|
||||
}
|
||||
|
||||
specCustomer.get().getMovies().add(movie);
|
||||
|
||||
return movieRepository.save(movie);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Movie assignMovie(Long customerId, Long movieId)
|
||||
{
|
||||
if(customerId == 0 || movieId == 0)
|
||||
{
|
||||
throw new IllegalArgumentException("Some of the properties are incorrect.");
|
||||
}
|
||||
final Optional<Customer> specCustomer = customerRepository.findById(customerId);
|
||||
final Optional<Movie> specMovie = movieRepository.findById(movieId);
|
||||
|
||||
if(specCustomer.get().getMovies().contains(specMovie.get()))
|
||||
{
|
||||
throw new IllegalArgumentException("You already have this movie");
|
||||
}
|
||||
specCustomer.get().getMovies().add(specMovie.get());
|
||||
|
||||
return specMovie.get();
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Movie findMovie(Long id)
|
||||
{
|
||||
@@ -75,12 +91,19 @@ public class MovieService
|
||||
return movieRepository.findAll();
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Movie> findCustomerMovies(Long customerId){
|
||||
|
||||
final Optional<Customer> customer = customerRepository.findById(customerId);
|
||||
return customer.get().getMovies();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Movie updateMovie(Long id, String title, int length, double score)
|
||||
{
|
||||
if(!StringUtils.hasText(title) || length == 0 || score == 0)
|
||||
if(!StringUtils.hasText(title) && length == 0 && score == 0)
|
||||
{
|
||||
throw new IllegalArgumentException("Some of the movie's properties are incorrect.");
|
||||
throw new IllegalArgumentException("Nothing to change.");
|
||||
}
|
||||
final Optional<Movie> movie = movieRepository.findById(id);
|
||||
if(movie.isEmpty())
|
||||
@@ -91,16 +114,26 @@ public class MovieService
|
||||
Movie specificMovie = movie.get();
|
||||
Genre specificGenre = movie.get().getGenre();
|
||||
|
||||
specificMovie.setLength(length);
|
||||
if(length != 0)
|
||||
{
|
||||
specificMovie.setLength(length);
|
||||
}
|
||||
if(StringUtils.hasText(title))
|
||||
{
|
||||
specificMovie.setTitle(title);
|
||||
}
|
||||
if(score != 0)
|
||||
{
|
||||
specificMovie.setScore(score);
|
||||
}
|
||||
|
||||
specificMovie.setGenre(specificGenre);
|
||||
specificMovie.setTitle(title);
|
||||
specificMovie.setScore(score);
|
||||
|
||||
return movieRepository.save(specificMovie);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Movie deleteMovie(Long id, Long customerId)
|
||||
public Movie deleteMovieCustomer(Long id, Long customerId)
|
||||
{
|
||||
final Optional<Movie> movie = movieRepository.findById(id);
|
||||
if(movie.isEmpty())
|
||||
@@ -124,6 +157,27 @@ public class MovieService
|
||||
return specificMovie;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Movie deleteMovie(Long id)
|
||||
{
|
||||
final Optional<Movie> movie = movieRepository.findById(id);
|
||||
if(movie.isEmpty())
|
||||
{
|
||||
throw new MovieNotFoundException(id);
|
||||
}
|
||||
Movie specificMovie = movie.get();
|
||||
|
||||
specificMovie.getGenre().getMovies().remove(specificMovie);
|
||||
|
||||
final List<Customer> customers = customerRepository.findAll();
|
||||
|
||||
customers.forEach(customer -> {
|
||||
customer.getMovies().remove(specificMovie);
|
||||
});
|
||||
|
||||
return specificMovie;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public List<Movie> findAllSpecificMovies(String genreName)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user