Сделал допку
This commit is contained in:
@@ -14,22 +14,37 @@ import java.util.List;
|
||||
public class MovieController {
|
||||
|
||||
private final MovieService service;
|
||||
public MovieController(MovieService service) { this.service = service; }
|
||||
|
||||
public MovieController(MovieService service) {
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<MovieRs> all() { return service.getAll(); }
|
||||
public List<MovieRs> all() {
|
||||
return service.getAll();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public MovieRs one(@PathVariable Long id) { return service.get(id); }
|
||||
@GetMapping("/page")
|
||||
public List<MovieRs> getPage(
|
||||
@RequestParam(defaultValue = "0") int page,
|
||||
@RequestParam(defaultValue = "10") int size) {
|
||||
return service.getPage(page, size);
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
public MovieRs create(@RequestBody @Valid MovieRq rq) { return service.create(rq); }
|
||||
public MovieRs create(@RequestBody @Valid MovieRq rq) {
|
||||
return service.create(rq);
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public MovieRs update(@PathVariable Long id, @RequestBody @Valid MovieRq rq) { return service.update(id, rq); }
|
||||
public MovieRs update(@PathVariable Long id, @RequestBody @Valid MovieRq rq) {
|
||||
return service.update(id, rq);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
@ResponseStatus(HttpStatus.NO_CONTENT)
|
||||
public void delete(@PathVariable Long id) { service.delete(id); }
|
||||
public void delete(@PathVariable Long id) {
|
||||
service.delete(id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,22 +5,50 @@ import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
public class MovieRq {
|
||||
|
||||
@NotBlank
|
||||
private String title;
|
||||
|
||||
@NotBlank
|
||||
private String image;
|
||||
|
||||
@NotNull @Valid
|
||||
private GenreRefRq genre;
|
||||
@NotNull
|
||||
@Valid
|
||||
private GenreRefRq genre;
|
||||
|
||||
@NotNull @Valid
|
||||
private DirectorRefRq director;
|
||||
@NotNull
|
||||
@Valid
|
||||
private DirectorRefRq director;
|
||||
|
||||
public String getTitle() { return title; }
|
||||
public void setTitle(String title) { this.title = title; }
|
||||
public String getImage() { return image; }
|
||||
public void setImage(String image) { this.image = image; }
|
||||
public GenreRefRq getGenre() { return genre; }
|
||||
public void setGenre(GenreRefRq genre) { this.genre = genre; }
|
||||
public DirectorRefRq getDirector() { return director; }
|
||||
public void setDirector(DirectorRefRq director) { this.director = director; }
|
||||
}
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getImage() {
|
||||
return image;
|
||||
}
|
||||
|
||||
public void setImage(String image) {
|
||||
this.image = image;
|
||||
}
|
||||
|
||||
public GenreRefRq getGenre() {
|
||||
return genre;
|
||||
}
|
||||
|
||||
public void setGenre(GenreRefRq genre) {
|
||||
this.genre = genre;
|
||||
}
|
||||
|
||||
public DirectorRefRq getDirector() {
|
||||
return director;
|
||||
}
|
||||
|
||||
public void setDirector(DirectorRefRq director) {
|
||||
this.director = director;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,22 @@
|
||||
package ru.ulstu.is.server.entity;
|
||||
|
||||
public class Genre extends BaseEntity {
|
||||
|
||||
private String name;
|
||||
|
||||
public Genre() { }
|
||||
public Genre(Long id, String name) { super(id); this.name = name; }
|
||||
public Genre() {
|
||||
}
|
||||
|
||||
public String getName() { return name; }
|
||||
public void setName(String name) { this.name = name; }
|
||||
public Genre(Long id, String name) {
|
||||
super(id);
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,18 @@
|
||||
package ru.ulstu.is.server.repository;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
import ru.ulstu.is.server.entity.Movie;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public class MovieRepository extends MapRepository<ru.ulstu.is.server.entity.Movie> { }
|
||||
public class MovieRepository extends MapRepository<Movie> implements CommonRepository<Movie> {
|
||||
|
||||
public List<Movie> findPage(int page, int size) {
|
||||
int start = Math.max(page * size, 0);
|
||||
return findAll().stream()
|
||||
.skip(start)
|
||||
.limit(size)
|
||||
.toList();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,17 +12,23 @@ import java.util.List;
|
||||
|
||||
@Service
|
||||
public class MovieService {
|
||||
|
||||
private final MovieRepository repo;
|
||||
private final MovieMapper mapper;
|
||||
|
||||
public MovieService(MovieRepository repo, MovieMapper mapper) {
|
||||
this.repo = repo; this.mapper = mapper;
|
||||
this.repo = repo;
|
||||
this.mapper = mapper;
|
||||
}
|
||||
|
||||
public List<MovieRs> getAll() {
|
||||
return repo.findAll().stream().map(mapper::toRs).toList();
|
||||
}
|
||||
|
||||
public List<MovieRs> getPage(int page, int size) {
|
||||
return repo.findPage(page, size).stream().map(mapper::toRs).toList();
|
||||
}
|
||||
|
||||
public MovieRs get(Long id) {
|
||||
return mapper.toRs(getEntity(id));
|
||||
}
|
||||
@@ -45,7 +51,7 @@ public class MovieService {
|
||||
}
|
||||
|
||||
public void delete(Long id) {
|
||||
getEntity(id);
|
||||
getEntity(id);
|
||||
repo.deleteById(id);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user