Compare commits
4 Commits
1_lab_back
...
022f81af6a
| Author | SHA1 | Date | |
|---|---|---|---|
| 022f81af6a | |||
| fec98632f0 | |||
| f253354d62 | |||
| a21e43a80e |
@@ -25,6 +25,8 @@ dependencies {
|
||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||
implementation "org.springdoc:springdoc-openapi-starter-webmvc-ui:${springdocVersion}"
|
||||
implementation 'org.springframework.boot:spring-boot-starter-validation'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
||||
runtimeOnly 'com.h2database:h2'
|
||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||
}
|
||||
|
||||
|
||||
BIN
backend/data/appdb.mv.db
Normal file
BIN
backend/data/appdb.mv.db
Normal file
Binary file not shown.
@@ -0,0 +1,33 @@
|
||||
package ru.ulstu.is.server.api;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import ru.ulstu.is.server.dto.DirectorRq;
|
||||
import ru.ulstu.is.server.dto.DirectorRs;
|
||||
import ru.ulstu.is.server.service.DirectorService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/1.0/directors")
|
||||
public class DirectorController {
|
||||
private final DirectorService service;
|
||||
public DirectorController(DirectorService service) { this.service = service; }
|
||||
|
||||
@GetMapping public List<DirectorRs> all() { return service.getAll(); }
|
||||
@GetMapping("/{id}") public DirectorRs one(@PathVariable Long id) { return service.get(id); }
|
||||
|
||||
@PostMapping
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
public DirectorRs create(@RequestBody @Valid DirectorRq rq) { return service.create(rq); }
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public DirectorRs update(@PathVariable Long id, @RequestBody @Valid DirectorRq rq) {
|
||||
return service.update(id, rq);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
@ResponseStatus(HttpStatus.NO_CONTENT)
|
||||
public void delete(@PathVariable Long id) { service.delete(id); }
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
package ru.ulstu.is.server.api;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import ru.ulstu.is.server.dto.DirectorDto;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/1.0/directors")
|
||||
public class DirectorsController {
|
||||
|
||||
private final Map<String, DirectorDto> storage = new ConcurrentHashMap<>();
|
||||
|
||||
public DirectorsController() {
|
||||
DirectorDto d1 = new DirectorDto(); d1.setId("1"); d1.setName("Кристофер Нолан"); storage.put(d1.getId(), d1);
|
||||
DirectorDto d2 = new DirectorDto(); d2.setId("2"); d2.setName("Лана Вачовски"); storage.put(d2.getId(), d2);
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<DirectorDto> getAll() { return new ArrayList<>(storage.values()); }
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public ResponseEntity<DirectorDto> getById(@PathVariable String id) {
|
||||
DirectorDto dto = storage.get(id);
|
||||
return dto != null ? ResponseEntity.ok(dto) : ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public ResponseEntity<DirectorDto> create(@RequestBody DirectorDto dto) {
|
||||
if (dto.getId() == null || dto.getId().isBlank()) dto.setId(UUID.randomUUID().toString());
|
||||
if (storage.containsKey(dto.getId())) return ResponseEntity.status(HttpStatus.CONFLICT).build();
|
||||
storage.put(dto.getId(), dto);
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(dto);
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public ResponseEntity<DirectorDto> update(@PathVariable String id, @RequestBody DirectorDto dto) {
|
||||
if (!storage.containsKey(id)) return ResponseEntity.notFound().build();
|
||||
dto.setId(id);
|
||||
storage.put(id, dto);
|
||||
return ResponseEntity.ok(dto);
|
||||
}
|
||||
|
||||
@PatchMapping("/{id}")
|
||||
public ResponseEntity<DirectorDto> patch(@PathVariable String id, @RequestBody Map<String, Object> patch) {
|
||||
DirectorDto existing = storage.get(id);
|
||||
if (existing == null) return ResponseEntity.notFound().build();
|
||||
if (patch.containsKey("name")) existing.setName((String) patch.get("name"));
|
||||
storage.put(id, existing);
|
||||
return ResponseEntity.ok(existing);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public ResponseEntity<Void> delete(@PathVariable String id) {
|
||||
return storage.remove(id) != null ? ResponseEntity.noContent().build() : ResponseEntity.notFound().build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package ru.ulstu.is.server.api;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import ru.ulstu.is.server.dto.GenreRq;
|
||||
import ru.ulstu.is.server.dto.GenreRs;
|
||||
import ru.ulstu.is.server.service.GenreService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/1.0/genres")
|
||||
public class GenreController {
|
||||
private final GenreService service;
|
||||
public GenreController(GenreService service) { this.service = service; }
|
||||
|
||||
@GetMapping public List<GenreRs> all() { return service.getAll(); }
|
||||
@GetMapping("/{id}") public GenreRs one(@PathVariable Long id) { return service.get(id); }
|
||||
|
||||
@PostMapping
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
public GenreRs create(@RequestBody @Valid GenreRq rq) { return service.create(rq); }
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public GenreRs update(@PathVariable Long id, @RequestBody @Valid GenreRq rq) {
|
||||
return service.update(id, rq);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
@ResponseStatus(HttpStatus.NO_CONTENT)
|
||||
public void delete(@PathVariable Long id) { service.delete(id); }
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
package ru.ulstu.is.server.api;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import ru.ulstu.is.server.dto.GenreDto;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/1.0/genres")
|
||||
public class GenresController {
|
||||
|
||||
private final Map<String, GenreDto> storage = new ConcurrentHashMap<>();
|
||||
|
||||
public GenresController() {
|
||||
GenreDto g1 = new GenreDto(); g1.setId("1"); g1.setName("Фантастика"); storage.put(g1.getId(), g1);
|
||||
GenreDto g2 = new GenreDto(); g2.setId("2"); g2.setName("Драма"); storage.put(g2.getId(), g2);
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<GenreDto> getAll() { return new ArrayList<>(storage.values()); }
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public ResponseEntity<GenreDto> getById(@PathVariable String id) {
|
||||
GenreDto dto = storage.get(id);
|
||||
return dto != null ? ResponseEntity.ok(dto) : ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public ResponseEntity<GenreDto> create(@RequestBody GenreDto dto) {
|
||||
if (dto.getId() == null || dto.getId().isBlank()) dto.setId(UUID.randomUUID().toString());
|
||||
if (storage.containsKey(dto.getId())) return ResponseEntity.status(HttpStatus.CONFLICT).build();
|
||||
storage.put(dto.getId(), dto);
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(dto);
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public ResponseEntity<GenreDto> update(@PathVariable String id, @RequestBody GenreDto dto) {
|
||||
if (!storage.containsKey(id)) return ResponseEntity.notFound().build();
|
||||
dto.setId(id);
|
||||
storage.put(id, dto);
|
||||
return ResponseEntity.ok(dto);
|
||||
}
|
||||
|
||||
@PatchMapping("/{id}")
|
||||
public ResponseEntity<GenreDto> patch(@PathVariable String id, @RequestBody Map<String, Object> patch) {
|
||||
GenreDto existing = storage.get(id);
|
||||
if (existing == null) return ResponseEntity.notFound().build();
|
||||
if (patch.containsKey("name")) existing.setName((String) patch.get("name"));
|
||||
storage.put(id, existing);
|
||||
return ResponseEntity.ok(existing);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public ResponseEntity<Void> delete(@PathVariable String id) {
|
||||
return storage.remove(id) != null ? ResponseEntity.noContent().build() : ResponseEntity.notFound().build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package ru.ulstu.is.server.api;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import ru.ulstu.is.server.dto.MovieRq;
|
||||
import ru.ulstu.is.server.dto.MovieRs;
|
||||
import ru.ulstu.is.server.service.MovieService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/1.0/movies")
|
||||
public class MovieController {
|
||||
|
||||
private final MovieService service;
|
||||
public MovieController(MovieService service) { this.service = service; }
|
||||
|
||||
@GetMapping
|
||||
public List<MovieRs> all() { return service.getAll(); }
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public MovieRs one(@PathVariable Long id) { return service.get(id); }
|
||||
|
||||
@PostMapping
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
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); }
|
||||
|
||||
@GetMapping("/page")
|
||||
public List<MovieRs> getPage(
|
||||
@RequestParam(defaultValue = "0") int page,
|
||||
@RequestParam(defaultValue = "10") int size) {
|
||||
return service.getPage(page, size);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
@ResponseStatus(HttpStatus.NO_CONTENT)
|
||||
public void delete(@PathVariable Long id) { service.delete(id); }
|
||||
}
|
||||
@@ -1,86 +0,0 @@
|
||||
package ru.ulstu.is.server.api;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import ru.ulstu.is.server.dto.DirectorDto;
|
||||
import ru.ulstu.is.server.dto.GenreDto;
|
||||
import ru.ulstu.is.server.dto.MovieDto;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/1.0/movies")
|
||||
public class MoviesController {
|
||||
|
||||
private final Map<String, MovieDto> storage = new ConcurrentHashMap<>();
|
||||
|
||||
public MoviesController() {
|
||||
GenreDto g1 = new GenreDto(); g1.setId("1"); g1.setName("Фантастика");
|
||||
GenreDto g2 = new GenreDto(); g2.setId("2"); g2.setName("Драма");
|
||||
DirectorDto d1 = new DirectorDto(); d1.setId("1"); d1.setName("Кристофер Нолан");
|
||||
DirectorDto d2 = new DirectorDto(); d2.setId("2"); d2.setName("Лана Вачовски");
|
||||
MovieDto m1 = new MovieDto();
|
||||
m1.setId("1"); m1.setTitle("Начало"); m1.setGenre(g1); m1.setDirector(d1);
|
||||
storage.put(m1.getId(), m1);
|
||||
|
||||
MovieDto m2 = new MovieDto();
|
||||
m2.setId("2"); m2.setTitle("Интерстеллар"); m2.setGenre(g2); m2.setDirector(d1);
|
||||
storage.put(m2.getId(), m2);
|
||||
|
||||
MovieDto m3 = new MovieDto();
|
||||
m3.setId("3"); m3.setTitle("Матрица"); m3.setGenre(g1); m3.setDirector(d2);
|
||||
storage.put(m3.getId(), m3);
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<MovieDto> getAll() {
|
||||
return new ArrayList<>(storage.values());
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public ResponseEntity<MovieDto> getById(@PathVariable String id) {
|
||||
MovieDto dto = storage.get(id);
|
||||
return dto != null ? ResponseEntity.ok(dto) : ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public ResponseEntity<MovieDto> create(@RequestBody MovieDto dto) {
|
||||
if (dto.getId() == null || dto.getId().isBlank()) {
|
||||
dto.setId(UUID.randomUUID().toString());
|
||||
}
|
||||
if (storage.containsKey(dto.getId())) {
|
||||
return ResponseEntity.status(HttpStatus.CONFLICT).build();
|
||||
}
|
||||
storage.put(dto.getId(), dto);
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(dto);
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public ResponseEntity<MovieDto> update(@PathVariable String id, @RequestBody MovieDto dto) {
|
||||
if (!storage.containsKey(id)) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
dto.setId(id);
|
||||
storage.put(id, dto);
|
||||
return ResponseEntity.ok(dto);
|
||||
}
|
||||
|
||||
@PatchMapping("/{id}")
|
||||
public ResponseEntity<MovieDto> patch(@PathVariable String id, @RequestBody Map<String, Object> patch) {
|
||||
MovieDto existing = storage.get(id);
|
||||
if (existing == null) return ResponseEntity.notFound().build();
|
||||
|
||||
if (patch.containsKey("title")) existing.setTitle((String) patch.get("title"));
|
||||
if (patch.containsKey("image")) existing.setImage((String) patch.get("image"));
|
||||
|
||||
storage.put(id, existing);
|
||||
return ResponseEntity.ok(existing);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public ResponseEntity<Void> delete(@PathVariable String id) {
|
||||
return storage.remove(id) != null ? ResponseEntity.noContent().build() : ResponseEntity.notFound().build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package ru.ulstu.is.server.api;
|
||||
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import ru.ulstu.is.server.repository.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/1.0/reports")
|
||||
@Tag(name = "Reports", description = "Отчёты и агрегированные выборки")
|
||||
public class ReportController {
|
||||
|
||||
private final GenreRepository genreRepo;
|
||||
private final DirectorRepository directorRepo;
|
||||
private final UserRepository userRepo;
|
||||
private final SubscriptionRepository subscriptionRepo;
|
||||
private final MovieRepository movieRepo;
|
||||
|
||||
public ReportController(GenreRepository genreRepo,
|
||||
DirectorRepository directorRepo,
|
||||
UserRepository userRepo,
|
||||
SubscriptionRepository subscriptionRepo,
|
||||
MovieRepository movieRepo) {
|
||||
this.genreRepo = genreRepo;
|
||||
this.directorRepo = directorRepo;
|
||||
this.userRepo = userRepo;
|
||||
this.subscriptionRepo = subscriptionRepo;
|
||||
this.movieRepo = movieRepo;
|
||||
}
|
||||
|
||||
@GetMapping("/genres/stats")
|
||||
@Operation(summary = "Количество фильмов по жанрам")
|
||||
public List<GenreRepository.GenreStats> genreStats() {
|
||||
return genreRepo.getGenreStats();
|
||||
}
|
||||
|
||||
@GetMapping("/directors/top")
|
||||
@Operation(summary = "Топ режиссёров по числу фильмов")
|
||||
public List<MovieRepository.TopDirectors> topDirectors(
|
||||
@RequestParam(defaultValue = "10") int limit) {
|
||||
return movieRepo.findTopDirectors()
|
||||
.stream().limit(Math.max(limit, 1)).toList();
|
||||
}
|
||||
|
||||
@GetMapping("/subscriptions/usage")
|
||||
@Operation(summary = "Использование подписок: тариф, цена, число пользователей")
|
||||
public List<SubscriptionRepository.SubscriptionUsage> subscriptionUsage() {
|
||||
return subscriptionRepo.usageStats();
|
||||
}
|
||||
|
||||
@GetMapping("/users/by-subscription")
|
||||
@Operation(summary = "Число пользователей по тарифам")
|
||||
public List<UserRepository.UsersBySubscription> usersBySubscription() {
|
||||
return userRepo.countUsersBySubscription();
|
||||
}
|
||||
|
||||
@GetMapping("/users/avg-age-by-subscription")
|
||||
@Operation(summary = "Средний возраст пользователей по тарифам")
|
||||
public List<UserRepository.AvgAgeBySubscription> avgAgeBySubscription() {
|
||||
return userRepo.avgAgeBySubscription();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package ru.ulstu.is.server.api;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import ru.ulstu.is.server.dto.SubscriptionRq;
|
||||
import ru.ulstu.is.server.dto.SubscriptionRs;
|
||||
import ru.ulstu.is.server.service.SubscriptionService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/1.0/subscriptions")
|
||||
public class SubscriptionController {
|
||||
private final SubscriptionService service;
|
||||
public SubscriptionController(SubscriptionService service) { this.service = service; }
|
||||
|
||||
@GetMapping public List<SubscriptionRs> all() { return service.getAll(); }
|
||||
@GetMapping("/{id}") public SubscriptionRs one(@PathVariable Long id) { return service.get(id); }
|
||||
|
||||
@PostMapping
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
public SubscriptionRs create(@RequestBody @Valid SubscriptionRq rq) { return service.create(rq); }
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public SubscriptionRs update(@PathVariable Long id, @RequestBody @Valid SubscriptionRq rq) {
|
||||
return service.update(id, rq);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
@ResponseStatus(HttpStatus.NO_CONTENT)
|
||||
public void delete(@PathVariable Long id) { service.delete(id); }
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
package ru.ulstu.is.server.api;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import ru.ulstu.is.server.dto.SubscriptionDto;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/1.0/subscriptions")
|
||||
public class SubscriptionsController {
|
||||
|
||||
private final Map<String, SubscriptionDto> storage = new ConcurrentHashMap<>();
|
||||
|
||||
public SubscriptionsController() {
|
||||
SubscriptionDto s1 = new SubscriptionDto(); s1.setId("1"); s1.setLevel("Базовая"); s1.setPrice(299); storage.put(s1.getId(), s1);
|
||||
SubscriptionDto s2 = new SubscriptionDto(); s2.setId("2"); s2.setLevel("Премиум"); s2.setPrice(699); storage.put(s2.getId(), s2);
|
||||
SubscriptionDto s3 = new SubscriptionDto(); s3.setId("3"); s3.setLevel("Годовая"); s3.setPrice(2500); storage.put(s3.getId(), s3);
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<SubscriptionDto> getAll() { return new ArrayList<>(storage.values()); }
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public ResponseEntity<SubscriptionDto> getById(@PathVariable String id) {
|
||||
SubscriptionDto dto = storage.get(id);
|
||||
return dto != null ? ResponseEntity.ok(dto) : ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public ResponseEntity<SubscriptionDto> create(@RequestBody SubscriptionDto dto) {
|
||||
if (dto.getId() == null || dto.getId().isBlank()) dto.setId(UUID.randomUUID().toString());
|
||||
if (storage.containsKey(dto.getId())) return ResponseEntity.status(HttpStatus.CONFLICT).build();
|
||||
storage.put(dto.getId(), dto);
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(dto);
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public ResponseEntity<SubscriptionDto> update(@PathVariable String id, @RequestBody SubscriptionDto dto) {
|
||||
if (!storage.containsKey(id)) return ResponseEntity.notFound().build();
|
||||
dto.setId(id);
|
||||
storage.put(id, dto);
|
||||
return ResponseEntity.ok(dto);
|
||||
}
|
||||
|
||||
@PatchMapping("/{id}")
|
||||
public ResponseEntity<SubscriptionDto> patch(@PathVariable String id, @RequestBody Map<String, Object> patch) {
|
||||
SubscriptionDto existing = storage.get(id);
|
||||
if (existing == null) return ResponseEntity.notFound().build();
|
||||
if (patch.containsKey("level")) existing.setLevel((String) patch.get("level"));
|
||||
if (patch.containsKey("price")) existing.setPrice((Integer) patch.get("price"));
|
||||
storage.put(id, existing);
|
||||
return ResponseEntity.ok(existing);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public ResponseEntity<Void> delete(@PathVariable String id) {
|
||||
return storage.remove(id) != null ? ResponseEntity.noContent().build() : ResponseEntity.notFound().build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package ru.ulstu.is.server.api;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import ru.ulstu.is.server.dto.UserRq;
|
||||
import ru.ulstu.is.server.dto.UserRs;
|
||||
import ru.ulstu.is.server.service.UserService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/1.0/users")
|
||||
public class UserController {
|
||||
private final UserService service;
|
||||
public UserController(UserService service) { this.service = service; }
|
||||
|
||||
@GetMapping public List<UserRs> all() { return service.getAll(); }
|
||||
@GetMapping("/{id}") public UserRs one(@PathVariable Long id) { return service.get(id); }
|
||||
|
||||
@PostMapping
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
public UserRs create(@RequestBody @Valid UserRq rq) { return service.create(rq); }
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public UserRs update(@PathVariable Long id, @RequestBody @Valid UserRq rq) {
|
||||
return service.update(id, rq);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
@ResponseStatus(HttpStatus.NO_CONTENT)
|
||||
public void delete(@PathVariable Long id) { service.delete(id); }
|
||||
}
|
||||
@@ -1,87 +0,0 @@
|
||||
package ru.ulstu.is.server.api;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import ru.ulstu.is.server.dto.UserDto;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/1.0/users")
|
||||
public class UsersController {
|
||||
|
||||
private final Map<String, UserDto> storage = new ConcurrentHashMap<>();
|
||||
|
||||
public UsersController() {
|
||||
UserDto u1 = new UserDto();
|
||||
u1.setId("1"); u1.setName("Иван");
|
||||
u1.setLogin("ivan@example.com"); u1.setPassword("1234");
|
||||
u1.setIsAdmin(false); u1.setAge(25); u1.setGender("мужской");
|
||||
u1.setWatchlist(new ArrayList<>(List.of("2","5","8")));
|
||||
u1.setSubscriptionId("1"); u1.setSubscriptionUntil("05.06.2025");
|
||||
storage.put(u1.getId(), u1);
|
||||
|
||||
UserDto admin = new UserDto();
|
||||
admin.setId("admin"); admin.setName("Админ");
|
||||
admin.setLogin("admin@example.com"); admin.setPassword("admin");
|
||||
admin.setIsAdmin(true); admin.setWatchlist(new ArrayList<>());
|
||||
storage.put(admin.getId(), admin);
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<UserDto> getAll() { return new ArrayList<>(storage.values()); }
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public ResponseEntity<UserDto> getById(@PathVariable String id) {
|
||||
UserDto dto = storage.get(id);
|
||||
return dto != null ? ResponseEntity.ok(dto) : ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public ResponseEntity<UserDto> create(@RequestBody UserDto dto) {
|
||||
if (dto.getId() == null || dto.getId().isBlank()) dto.setId(UUID.randomUUID().toString());
|
||||
if (storage.containsKey(dto.getId())) return ResponseEntity.status(HttpStatus.CONFLICT).build();
|
||||
if (dto.getWatchlist() == null) dto.setWatchlist(new ArrayList<>());
|
||||
storage.put(dto.getId(), dto);
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(dto);
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public ResponseEntity<UserDto> update(@PathVariable String id, @RequestBody UserDto dto) {
|
||||
if (!storage.containsKey(id)) return ResponseEntity.notFound().build();
|
||||
dto.setId(id);
|
||||
storage.put(id, dto);
|
||||
return ResponseEntity.ok(dto);
|
||||
}
|
||||
|
||||
@PatchMapping("/{id}")
|
||||
public ResponseEntity<UserDto> patch(@PathVariable String id, @RequestBody Map<String, Object> patch) {
|
||||
UserDto existing = storage.get(id);
|
||||
if (existing == null) return ResponseEntity.notFound().build();
|
||||
|
||||
if (patch.containsKey("name")) existing.setName((String) patch.get("name"));
|
||||
if (patch.containsKey("login")) existing.setLogin((String) patch.get("login"));
|
||||
if (patch.containsKey("password")) existing.setPassword((String) patch.get("password"));
|
||||
if (patch.containsKey("isAdmin")) existing.setIsAdmin((Boolean) patch.get("isAdmin"));
|
||||
if (patch.containsKey("age")) existing.setAge((Integer) patch.get("age"));
|
||||
if (patch.containsKey("gender")) existing.setGender((String) patch.get("gender"));
|
||||
if (patch.containsKey("avatar")) existing.setAvatar((String) patch.get("avatar"));
|
||||
if (patch.containsKey("subscriptionId")) existing.setSubscriptionId((String) patch.get("subscriptionId"));
|
||||
if (patch.containsKey("subscriptionUntil")) existing.setSubscriptionUntil((String) patch.get("subscriptionUntil"));
|
||||
if (patch.containsKey("watchlist")) {
|
||||
@SuppressWarnings("unchecked")
|
||||
List<String> wl = (List<String>) patch.get("watchlist");
|
||||
existing.setWatchlist(wl);
|
||||
}
|
||||
|
||||
storage.put(id, existing);
|
||||
return ResponseEntity.ok(existing);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public ResponseEntity<Void> delete(@PathVariable String id) {
|
||||
return storage.remove(id) != null ? ResponseEntity.noContent().build() : ResponseEntity.notFound().build();
|
||||
}
|
||||
}
|
||||
@@ -2,14 +2,11 @@ package ru.ulstu.is.server.dto;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
|
||||
public class GenreDto {
|
||||
@NotBlank
|
||||
public class DirectorRefRq {
|
||||
private String id;
|
||||
@NotBlank
|
||||
private String name;
|
||||
|
||||
public String getId() { return id; }
|
||||
public void setId(String id) { this.id = id; }
|
||||
public String getName() { return name; }
|
||||
public void setName(String name) { this.name = name; }
|
||||
}
|
||||
}
|
||||
10
backend/src/main/java/ru/ulstu/is/server/dto/DirectorRq.java
Normal file
10
backend/src/main/java/ru/ulstu/is/server/dto/DirectorRq.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package ru.ulstu.is.server.dto;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
|
||||
public class DirectorRq {
|
||||
@NotBlank
|
||||
private String name;
|
||||
public String getName() { return name; }
|
||||
public void setName(String name) { this.name = name; }
|
||||
}
|
||||
14
backend/src/main/java/ru/ulstu/is/server/dto/DirectorRs.java
Normal file
14
backend/src/main/java/ru/ulstu/is/server/dto/DirectorRs.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package ru.ulstu.is.server.dto;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
|
||||
public class DirectorRs {
|
||||
private Long id;
|
||||
private String name;
|
||||
public DirectorRs() { }
|
||||
public DirectorRs(Long id, String name) { this.id = id; this.name = name; }
|
||||
public Long getId() { return id; }
|
||||
public void setId(Long id) { this.id = id; }
|
||||
public String getName() { return name; }
|
||||
public void setName(String name) { this.name = name; }
|
||||
}
|
||||
@@ -2,14 +2,11 @@ package ru.ulstu.is.server.dto;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
|
||||
public class DirectorDto {
|
||||
@NotBlank
|
||||
private String id;
|
||||
@NotBlank
|
||||
private String name;
|
||||
|
||||
public class GenreRefRq {
|
||||
private String id;
|
||||
private String name;
|
||||
public String getId() { return id; }
|
||||
public void setId(String id) { this.id = id; }
|
||||
public String getName() { return name; }
|
||||
public void setName(String name) { this.name = name; }
|
||||
}
|
||||
}
|
||||
10
backend/src/main/java/ru/ulstu/is/server/dto/GenreRq.java
Normal file
10
backend/src/main/java/ru/ulstu/is/server/dto/GenreRq.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package ru.ulstu.is.server.dto;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
|
||||
public class GenreRq {
|
||||
@NotBlank
|
||||
private String name;
|
||||
public String getName() { return name; }
|
||||
public void setName(String name) { this.name = name; }
|
||||
}
|
||||
14
backend/src/main/java/ru/ulstu/is/server/dto/GenreRs.java
Normal file
14
backend/src/main/java/ru/ulstu/is/server/dto/GenreRs.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package ru.ulstu.is.server.dto;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
|
||||
public class GenreRs {
|
||||
private Long id;
|
||||
private String name;
|
||||
public GenreRs() { }
|
||||
public GenreRs(Long id, String name) { this.id = id; this.name = name; }
|
||||
public Long getId() { return id; }
|
||||
public void setId(Long id) { this.id = id; }
|
||||
public String getName() { return name; }
|
||||
public void setName(String name) { this.name = name; }
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
package ru.ulstu.is.server.dto;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
|
||||
public class MovieDto {
|
||||
@NotBlank
|
||||
private String id;
|
||||
@NotBlank
|
||||
private String title;
|
||||
@NotBlank
|
||||
private GenreDto genre;
|
||||
@NotBlank
|
||||
private DirectorDto director;
|
||||
private String image;
|
||||
|
||||
public String getId() { return id; }
|
||||
public void setId(String id) { this.id = id; }
|
||||
public String getTitle() { return title; }
|
||||
public void setTitle(String title) { this.title = title; }
|
||||
public GenreDto getGenre() { return genre; }
|
||||
public void setGenre(GenreDto genre) { this.genre = genre; }
|
||||
public DirectorDto getDirector() { return director; }
|
||||
public void setDirector(DirectorDto director) { this.director = director; }
|
||||
public String getImage() { return image; }
|
||||
public void setImage(String image) { this.image = image; }
|
||||
}
|
||||
26
backend/src/main/java/ru/ulstu/is/server/dto/MovieRq.java
Normal file
26
backend/src/main/java/ru/ulstu/is/server/dto/MovieRq.java
Normal file
@@ -0,0 +1,26 @@
|
||||
package ru.ulstu.is.server.dto;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
public class MovieRq {
|
||||
@NotBlank
|
||||
private String title;
|
||||
private String image;
|
||||
|
||||
@NotNull @Valid
|
||||
private GenreRefRq genre;
|
||||
|
||||
@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; }
|
||||
}
|
||||
25
backend/src/main/java/ru/ulstu/is/server/dto/MovieRs.java
Normal file
25
backend/src/main/java/ru/ulstu/is/server/dto/MovieRs.java
Normal file
@@ -0,0 +1,25 @@
|
||||
package ru.ulstu.is.server.dto;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
public class MovieRs {
|
||||
private Long id;
|
||||
private String title;
|
||||
private String image;
|
||||
|
||||
private GenreRs genre;
|
||||
private DirectorRs director;
|
||||
|
||||
public Long getId() { return id; }
|
||||
public void setId(Long id) { this.id = id; }
|
||||
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 GenreRs getGenre() { return genre; }
|
||||
public void setGenre(GenreRs genre) { this.genre = genre; }
|
||||
public DirectorRs getDirector() { return director; }
|
||||
public void setDirector(DirectorRs director) { this.director = director; }
|
||||
}
|
||||
@@ -1,20 +1,15 @@
|
||||
package ru.ulstu.is.server.dto;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
public class SubscriptionDto {
|
||||
@NotBlank
|
||||
public class SubscriptionRefRq {
|
||||
private String id;
|
||||
@NotBlank
|
||||
private String level;
|
||||
@NotNull
|
||||
private Integer price;
|
||||
|
||||
public String getId() { return id; }
|
||||
public void setId(String id) { this.id = id; }
|
||||
public String getLevel() { return level; }
|
||||
public void setLevel(String level) { this.level = level; }
|
||||
public Integer getPrice() { return price; }
|
||||
public void setPrice(Integer price) { this.price = price; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package ru.ulstu.is.server.dto;
|
||||
|
||||
import jakarta.validation.constraints.Min;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
public class SubscriptionRq {
|
||||
@NotBlank
|
||||
private String level;
|
||||
@NotNull @Min(0)
|
||||
private Integer price;
|
||||
public String getLevel() { return level; }
|
||||
public void setLevel(String level) { this.level = level; }
|
||||
public Integer getPrice() { return price; }
|
||||
public void setPrice(Integer price) { this.price = price; }
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package ru.ulstu.is.server.dto;
|
||||
|
||||
import jakarta.validation.constraints.Min;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
public class SubscriptionRs {
|
||||
private Long id;
|
||||
private String level;
|
||||
private Integer price;
|
||||
public SubscriptionRs() { }
|
||||
public SubscriptionRs(Long id, String level, Integer price) {
|
||||
this.id = id; this.level = level; this.price = price;
|
||||
}
|
||||
public Long getId() { return id; }
|
||||
public void setId(Long id) { this.id = id; }
|
||||
public String getLevel() { return level; }
|
||||
public void setLevel(String level) { this.level = level; }
|
||||
public Integer getPrice() { return price; }
|
||||
public void setPrice(Integer price) { this.price = price; }
|
||||
}
|
||||
@@ -1,27 +1,26 @@
|
||||
package ru.ulstu.is.server.dto;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
import jakarta.validation.constraints.Email;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class UserDto {
|
||||
@NotBlank
|
||||
private String id;
|
||||
private String name;
|
||||
@NotBlank
|
||||
private String login;
|
||||
@NotBlank
|
||||
private String password;
|
||||
public class UserRq {
|
||||
@NotBlank private String name;
|
||||
@NotBlank @Email private String login;
|
||||
@NotBlank private String password;
|
||||
|
||||
private Boolean isAdmin;
|
||||
private List<String> watchlist;
|
||||
private Boolean isAdmin = false;
|
||||
private Integer age;
|
||||
private String gender;
|
||||
private String avatar;
|
||||
private String subscriptionId;
|
||||
private String subscriptionUntil;
|
||||
|
||||
public String getId() { return id; }
|
||||
public void setId(String id) { this.id = id; }
|
||||
private String subscriptionUntil;
|
||||
@Valid private SubscriptionRefRq subscription;
|
||||
|
||||
private List<Long> watchlist = new ArrayList<>();
|
||||
|
||||
public String getName() { return name; }
|
||||
public void setName(String name) { this.name = name; }
|
||||
public String getLogin() { return login; }
|
||||
@@ -30,16 +29,16 @@ public class UserDto {
|
||||
public void setPassword(String password) { this.password = password; }
|
||||
public Boolean getIsAdmin() { return isAdmin; }
|
||||
public void setIsAdmin(Boolean isAdmin) { this.isAdmin = isAdmin; }
|
||||
public List<String> getWatchlist() { return watchlist; }
|
||||
public void setWatchlist(List<String> watchlist) { this.watchlist = watchlist; }
|
||||
public Integer getAge() { return age; }
|
||||
public void setAge(Integer age) { this.age = age; }
|
||||
public String getGender() { return gender; }
|
||||
public void setGender(String gender) { this.gender = gender; }
|
||||
public String getAvatar() { return avatar; }
|
||||
public void setAvatar(String avatar) { this.avatar = avatar; }
|
||||
public String getSubscriptionId() { return subscriptionId; }
|
||||
public void setSubscriptionId(String subscriptionId) { this.subscriptionId = subscriptionId; }
|
||||
public String getSubscriptionUntil() { return subscriptionUntil; }
|
||||
public void setSubscriptionUntil(String subscriptionUntil) { this.subscriptionUntil = subscriptionUntil; }
|
||||
}
|
||||
public SubscriptionRefRq getSubscription() { return subscription; }
|
||||
public void setSubscription(SubscriptionRefRq subscription) { this.subscription = subscription; }
|
||||
public List<Long> getWatchlist() { return watchlist; }
|
||||
public void setWatchlist(List<Long> watchlist) { this.watchlist = watchlist; }
|
||||
}
|
||||
43
backend/src/main/java/ru/ulstu/is/server/dto/UserRs.java
Normal file
43
backend/src/main/java/ru/ulstu/is/server/dto/UserRs.java
Normal file
@@ -0,0 +1,43 @@
|
||||
package ru.ulstu.is.server.dto;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
import jakarta.validation.constraints.Email;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
public class UserRs {
|
||||
private Long id;
|
||||
private String name;
|
||||
private String login;
|
||||
private Boolean isAdmin;
|
||||
private Integer age;
|
||||
private String gender;
|
||||
private String avatar;
|
||||
private String subscriptionUntil;
|
||||
|
||||
private SubscriptionRs subscription;
|
||||
private List<Long> watchlist;
|
||||
|
||||
public Long getId() { return id; }
|
||||
public void setId(Long id) { this.id = id; }
|
||||
public String getName() { return name; }
|
||||
public void setName(String name) { this.name = name; }
|
||||
public String getLogin() { return login; }
|
||||
public void setLogin(String login) { this.login = login; }
|
||||
public Boolean getIsAdmin() { return isAdmin; }
|
||||
public void setIsAdmin(Boolean isAdmin) { this.isAdmin = isAdmin; }
|
||||
public Integer getAge() { return age; }
|
||||
public void setAge(Integer age) { this.age = age; }
|
||||
public String getGender() { return gender; }
|
||||
public void setGender(String gender) { this.gender = gender; }
|
||||
public String getAvatar() { return avatar; }
|
||||
public void setAvatar(String avatar) { this.avatar = avatar; }
|
||||
public String getSubscriptionUntil() { return subscriptionUntil; }
|
||||
public void setSubscriptionUntil(String subscriptionUntil) { this.subscriptionUntil = subscriptionUntil; }
|
||||
public SubscriptionRs getSubscription() { return subscription; }
|
||||
public void setSubscription(SubscriptionRs subscription) { this.subscription = subscription; }
|
||||
public List<Long> getWatchlist() { return watchlist; }
|
||||
public void setWatchlist(List<Long> watchlist) { this.watchlist = watchlist; }
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package ru.ulstu.is.server.entity;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@MappedSuperclass
|
||||
public abstract class BaseEntity {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
public BaseEntity() { }
|
||||
|
||||
public BaseEntity(Long id) { this.id = id; }
|
||||
|
||||
public Long getId() { return id; }
|
||||
|
||||
public void setId(Long id) { this.id = id; }
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package ru.ulstu.is.server.entity;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
@Table(name = "directors")
|
||||
public class Director extends BaseEntity {
|
||||
@Column(nullable = false, unique = true)
|
||||
private String name;
|
||||
|
||||
@OneToMany(mappedBy = "director", cascade = CascadeType.ALL, orphanRemoval = false)
|
||||
private List<Movie> movies = new ArrayList<>();
|
||||
|
||||
public Director() {}
|
||||
public Director(Long id, String name) { setId(id); this.name = name; }
|
||||
|
||||
public void addMovie(Movie m) {
|
||||
if (m == null) return;
|
||||
movies.add(m);
|
||||
m.setDirector(this);
|
||||
}
|
||||
public void removeMovie(Movie m) {
|
||||
if (m == null) return;
|
||||
movies.remove(m);
|
||||
if (m.getDirector() == this) m.setDirector(null);
|
||||
}
|
||||
|
||||
public String getName() { return name; }
|
||||
public void setName(String name) { this.name = name; }
|
||||
public List<Movie> getMovies() { return movies; }
|
||||
}
|
||||
33
backend/src/main/java/ru/ulstu/is/server/entity/Genre.java
Normal file
33
backend/src/main/java/ru/ulstu/is/server/entity/Genre.java
Normal file
@@ -0,0 +1,33 @@
|
||||
package ru.ulstu.is.server.entity;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
@Table(name = "genres")
|
||||
public class Genre extends BaseEntity {
|
||||
@Column(nullable = false, unique = true)
|
||||
private String name;
|
||||
|
||||
@OneToMany(mappedBy = "genre", cascade = CascadeType.ALL, orphanRemoval = false)
|
||||
private List<Movie> movies = new ArrayList<>();
|
||||
|
||||
public Genre() {}
|
||||
public Genre(Long id, String name) { setId(id); this.name = name; }
|
||||
|
||||
public void addMovie(Movie m) {
|
||||
if (m == null) return;
|
||||
movies.add(m);
|
||||
m.setGenre(this);
|
||||
}
|
||||
public void removeMovie(Movie m) {
|
||||
if (m == null) return;
|
||||
movies.remove(m);
|
||||
if (m.getGenre() == this) m.setGenre(null);
|
||||
}
|
||||
|
||||
public String getName() { return name; }
|
||||
public void setName(String name) { this.name = name; }
|
||||
public List<Movie> getMovies() { return movies; }
|
||||
}
|
||||
42
backend/src/main/java/ru/ulstu/is/server/entity/Movie.java
Normal file
42
backend/src/main/java/ru/ulstu/is/server/entity/Movie.java
Normal file
@@ -0,0 +1,42 @@
|
||||
package ru.ulstu.is.server.entity;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import ru.ulstu.is.server.entity.Genre;
|
||||
import ru.ulstu.is.server.entity.Director;
|
||||
|
||||
@Entity
|
||||
@Table(name = "movies")
|
||||
public class Movie extends BaseEntity {
|
||||
@Column(nullable = false)
|
||||
private String title;
|
||||
|
||||
@Lob
|
||||
@Column(columnDefinition = "CLOB")
|
||||
private String image;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "genre_id", nullable = false)
|
||||
private Genre genre;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "director_id", nullable = false)
|
||||
private Director director;
|
||||
|
||||
public Movie() {}
|
||||
public Movie(Long id, String title, String image, Genre genre, Director director) {
|
||||
setId(id);
|
||||
this.title = title;
|
||||
this.image = image;
|
||||
this.genre = genre;
|
||||
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 Genre getGenre() { return genre; }
|
||||
public void setGenre(Genre genre) { this.genre = genre; }
|
||||
public Director getDirector() { return director; }
|
||||
public void setDirector(Director director) { this.director = director; }
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package ru.ulstu.is.server.entity;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
@Table(name = "subscriptions")
|
||||
public class Subscription extends BaseEntity {
|
||||
|
||||
@Column(nullable = false, unique = true)
|
||||
private String level;
|
||||
|
||||
@Column(nullable = false)
|
||||
private Integer price;
|
||||
|
||||
@OneToMany(mappedBy = "subscription", orphanRemoval = false)
|
||||
private List<User> users = new ArrayList<>();
|
||||
|
||||
public Subscription() { }
|
||||
|
||||
public Subscription(Long id, String level, Integer price) {
|
||||
super(id);
|
||||
this.level = level;
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public void addUser(User u) {
|
||||
if (u == null) return;
|
||||
users.add(u);
|
||||
u.setSubscription(this);
|
||||
}
|
||||
|
||||
public void removeUser(User u) {
|
||||
if (u == null) return;
|
||||
users.remove(u);
|
||||
if (u.getSubscription() == this) {
|
||||
u.setSubscription(null);
|
||||
}
|
||||
}
|
||||
|
||||
public String getLevel() { return level; }
|
||||
public void setLevel(String level) { this.level = level; }
|
||||
public Integer getPrice() { return price; }
|
||||
public void setPrice(Integer price) { this.price = price; }
|
||||
public List<User> getUsers() { return users; }
|
||||
}
|
||||
98
backend/src/main/java/ru/ulstu/is/server/entity/User.java
Normal file
98
backend/src/main/java/ru/ulstu/is/server/entity/User.java
Normal file
@@ -0,0 +1,98 @@
|
||||
package ru.ulstu.is.server.entity;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import java.time.LocalDate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
@Table(name = "users")
|
||||
public class User extends BaseEntity {
|
||||
|
||||
@Column(nullable = false)
|
||||
private String name;
|
||||
|
||||
@Column(nullable = false, unique = true)
|
||||
private String login;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String password;
|
||||
|
||||
@Column(nullable = false)
|
||||
private Boolean isAdmin = false;
|
||||
|
||||
@Column
|
||||
private Integer age;
|
||||
|
||||
@Column
|
||||
private String gender;
|
||||
|
||||
@Lob
|
||||
@Column(columnDefinition = "CLOB")
|
||||
private String avatar;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "subscription_id")
|
||||
private Subscription subscription;
|
||||
|
||||
@Column(name = "subscription_until")
|
||||
private LocalDate subscriptionUntil;
|
||||
|
||||
@ManyToMany
|
||||
@JoinTable(
|
||||
name = "user_watchlist",
|
||||
joinColumns = @JoinColumn(name = "user_id"),
|
||||
inverseJoinColumns = @JoinColumn(name = "movie_id")
|
||||
)
|
||||
private List<Movie> watchlist = new ArrayList<>();
|
||||
|
||||
public User() { }
|
||||
|
||||
public User(Long id, String name, String login, String password, Boolean isAdmin) {
|
||||
super(id);
|
||||
this.name = name;
|
||||
this.login = login;
|
||||
this.password = password;
|
||||
this.isAdmin = isAdmin;
|
||||
}
|
||||
|
||||
public void addToWatchlist(Movie m) {
|
||||
if (m == null) return;
|
||||
if (!watchlist.contains(m)) watchlist.add(m);
|
||||
}
|
||||
|
||||
public void removeFromWatchlist(Movie m) {
|
||||
if (m == null) return;
|
||||
watchlist.remove(m);
|
||||
}
|
||||
|
||||
public String getName() { return name; }
|
||||
public void setName(String name) { this.name = name; }
|
||||
|
||||
public String getLogin() { return login; }
|
||||
public void setLogin(String login) { this.login = login; }
|
||||
|
||||
public String getPassword() { return password; }
|
||||
public void setPassword(String password) { this.password = password; }
|
||||
|
||||
public Boolean getIsAdmin() { return isAdmin; }
|
||||
public void setIsAdmin(Boolean isAdmin) { this.isAdmin = isAdmin; }
|
||||
|
||||
public Integer getAge() { return age; }
|
||||
public void setAge(Integer age) { this.age = age; }
|
||||
|
||||
public String getGender() { return gender; }
|
||||
public void setGender(String gender) { this.gender = gender; }
|
||||
|
||||
public String getAvatar() { return avatar; }
|
||||
public void setAvatar(String avatar) { this.avatar = avatar; }
|
||||
|
||||
public Subscription getSubscription() { return subscription; }
|
||||
public void setSubscription(Subscription subscription) { this.subscription = subscription; }
|
||||
|
||||
public LocalDate getSubscriptionUntil() { return subscriptionUntil; }
|
||||
public void setSubscriptionUntil(LocalDate subscriptionUntil) { this.subscriptionUntil = subscriptionUntil; }
|
||||
|
||||
public List<Movie> getWatchlist() { return watchlist; }
|
||||
public void setWatchlist(List<Movie> watchlist) { this.watchlist = watchlist != null ? watchlist : new ArrayList<>(); }
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package ru.ulstu.is.server.mapper;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.ulstu.is.server.dto.DirectorRq;
|
||||
import ru.ulstu.is.server.dto.DirectorRs;
|
||||
import ru.ulstu.is.server.entity.Director;
|
||||
import ru.ulstu.is.server.repository.SubscriptionRepository;
|
||||
|
||||
@Component
|
||||
public class DirectorMapper {
|
||||
public Director toEntity(DirectorRq rq) {
|
||||
Director d = new Director();
|
||||
d.setName(rq.getName());
|
||||
return d;
|
||||
}
|
||||
public void updateEntity(Director d, DirectorRq rq) {
|
||||
d.setName(rq.getName());
|
||||
}
|
||||
public DirectorRs toRs(Director d) {
|
||||
return new DirectorRs(d.getId(), d.getName());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package ru.ulstu.is.server.mapper;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.ulstu.is.server.dto.GenreRq;
|
||||
import ru.ulstu.is.server.dto.GenreRs;
|
||||
import ru.ulstu.is.server.entity.Genre;
|
||||
|
||||
@Component
|
||||
public class GenreMapper {
|
||||
public Genre toEntity(GenreRq rq) {
|
||||
Genre g = new Genre();
|
||||
g.setName(rq.getName());
|
||||
return g;
|
||||
}
|
||||
public void updateEntity(Genre g, GenreRq rq) {
|
||||
g.setName(rq.getName());
|
||||
}
|
||||
public GenreRs toRs(Genre g) {
|
||||
return new GenreRs(g.getId(), g.getName());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package ru.ulstu.is.server.mapper;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.ulstu.is.server.dto.*;
|
||||
import ru.ulstu.is.server.entity.*;
|
||||
import ru.ulstu.is.server.repository.DirectorRepository;
|
||||
import ru.ulstu.is.server.repository.GenreRepository;
|
||||
|
||||
@Component
|
||||
public class MovieMapper {
|
||||
private final GenreRepository genreRepo;
|
||||
private final DirectorRepository directorRepo;
|
||||
|
||||
public MovieMapper(GenreRepository genreRepo, DirectorRepository directorRepo) {
|
||||
this.genreRepo = genreRepo;
|
||||
this.directorRepo = directorRepo;
|
||||
}
|
||||
|
||||
public Movie toEntity(MovieRq rq) {
|
||||
Movie e = new Movie();
|
||||
e.setTitle(rq.getTitle());
|
||||
e.setImage(rq.getImage());
|
||||
|
||||
GenreRefRq gRef = rq.getGenre();
|
||||
e.setGenre(resolveGenre(gRef));
|
||||
|
||||
DirectorRefRq dRef = rq.getDirector();
|
||||
e.setDirector(resolveDirector(dRef));
|
||||
|
||||
return e;
|
||||
}
|
||||
|
||||
public MovieRs toRs(Movie e) {
|
||||
MovieRs rs = new MovieRs();
|
||||
rs.setId(e.getId());
|
||||
rs.setTitle(e.getTitle());
|
||||
rs.setImage(e.getImage());
|
||||
|
||||
Genre g = e.getGenre();
|
||||
if (g != null) rs.setGenre(new GenreRs(g.getId(), g.getName()));
|
||||
|
||||
Director d = e.getDirector();
|
||||
if (d != null) rs.setDirector(new DirectorRs(d.getId(), d.getName()));
|
||||
|
||||
return rs;
|
||||
}
|
||||
|
||||
private Genre resolveGenre(GenreRefRq ref) {
|
||||
if (ref == null) return null;
|
||||
Long id = parseId(ref.getId());
|
||||
if (id != null) return genreRepo.findById(id).orElse(null);
|
||||
if (ref.getName() != null) return new Genre(null, ref.getName());
|
||||
return null;
|
||||
}
|
||||
|
||||
private Director resolveDirector(DirectorRefRq ref) {
|
||||
if (ref == null) return null;
|
||||
Long id = parseId(ref.getId());
|
||||
if (id != null) return directorRepo.findById(id).orElse(null);
|
||||
if (ref.getName() != null) return new Director(null, ref.getName());
|
||||
return null;
|
||||
}
|
||||
|
||||
private Long parseId(String s) {
|
||||
try { return s == null ? null : Long.parseLong(s); }
|
||||
catch (NumberFormatException e) { return null; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package ru.ulstu.is.server.mapper;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.ulstu.is.server.dto.SubscriptionRq;
|
||||
import ru.ulstu.is.server.dto.SubscriptionRs;
|
||||
import ru.ulstu.is.server.entity.Subscription;
|
||||
|
||||
@Component
|
||||
public class SubscriptionMapper {
|
||||
public Subscription toEntity(SubscriptionRq rq) {
|
||||
Subscription s = new Subscription();
|
||||
s.setLevel(rq.getLevel());
|
||||
s.setPrice(rq.getPrice());
|
||||
return s;
|
||||
}
|
||||
public void updateEntity(Subscription s, SubscriptionRq rq) {
|
||||
s.setLevel(rq.getLevel());
|
||||
s.setPrice(rq.getPrice());
|
||||
}
|
||||
public SubscriptionRs toRs(Subscription s) {
|
||||
return new SubscriptionRs(s.getId(), s.getLevel(), s.getPrice());
|
||||
}
|
||||
}
|
||||
120
backend/src/main/java/ru/ulstu/is/server/mapper/UserMapper.java
Normal file
120
backend/src/main/java/ru/ulstu/is/server/mapper/UserMapper.java
Normal file
@@ -0,0 +1,120 @@
|
||||
package ru.ulstu.is.server.mapper;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.ulstu.is.server.dto.*;
|
||||
import ru.ulstu.is.server.entity.Movie;
|
||||
import ru.ulstu.is.server.entity.Subscription;
|
||||
import ru.ulstu.is.server.entity.User;
|
||||
import ru.ulstu.is.server.repository.MovieRepository;
|
||||
import ru.ulstu.is.server.repository.SubscriptionRepository;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
public class UserMapper {
|
||||
private final SubscriptionRepository subscriptionRepository;
|
||||
private final MovieRepository movieRepository;
|
||||
|
||||
private static final DateTimeFormatter DF = DateTimeFormatter.ISO_DATE;
|
||||
|
||||
public UserMapper(SubscriptionRepository subscriptionRepository,
|
||||
MovieRepository movieRepository) {
|
||||
this.subscriptionRepository = subscriptionRepository;
|
||||
this.movieRepository = movieRepository;
|
||||
}
|
||||
|
||||
public User toEntity(UserRq rq) {
|
||||
User u = new User();
|
||||
updateEntity(u, rq);
|
||||
return u;
|
||||
}
|
||||
|
||||
public void updateEntity(User u, UserRq rq) {
|
||||
if (rq.getName() != null) u.setName(rq.getName());
|
||||
if (rq.getLogin() != null) u.setLogin(rq.getLogin());
|
||||
if (rq.getPassword() != null) u.setPassword(rq.getPassword());
|
||||
if (rq.getIsAdmin() != null) u.setIsAdmin(rq.getIsAdmin());
|
||||
if (rq.getAge() != null) u.setAge(rq.getAge());
|
||||
if (rq.getGender() != null) u.setGender(rq.getGender());
|
||||
if (rq.getAvatar() != null) u.setAvatar(rq.getAvatar());
|
||||
|
||||
if (rq.getSubscriptionUntil() != null) {
|
||||
u.setSubscriptionUntil(parseDate(rq.getSubscriptionUntil()));
|
||||
}
|
||||
|
||||
if (rq.getSubscription() != null) {
|
||||
u.setSubscription(resolveSubscription(rq.getSubscription()));
|
||||
}
|
||||
|
||||
if (rq.getWatchlist() != null) {
|
||||
u.setWatchlist(resolveMovies(rq.getWatchlist()));
|
||||
}
|
||||
}
|
||||
|
||||
public UserRs toRs(User u) {
|
||||
UserRs rs = new UserRs();
|
||||
rs.setId(u.getId());
|
||||
rs.setName(u.getName());
|
||||
rs.setLogin(u.getLogin());
|
||||
rs.setIsAdmin(u.getIsAdmin());
|
||||
rs.setAge(u.getAge());
|
||||
rs.setGender(u.getGender());
|
||||
rs.setAvatar(u.getAvatar());
|
||||
|
||||
|
||||
rs.setSubscriptionUntil(formatDate(u.getSubscriptionUntil()));
|
||||
|
||||
if (u.getSubscription() != null) {
|
||||
Subscription s = u.getSubscription();
|
||||
SubscriptionRs srs = new SubscriptionRs();
|
||||
srs.setId(s.getId());
|
||||
srs.setLevel(s.getLevel());
|
||||
srs.setPrice(s.getPrice());
|
||||
rs.setSubscription(srs);
|
||||
}
|
||||
if (u.getWatchlist() != null) {
|
||||
rs.setWatchlist(u.getWatchlist().stream().map(Movie::getId).toList());
|
||||
} else {
|
||||
rs.setWatchlist(new ArrayList<>());
|
||||
}
|
||||
return rs;
|
||||
}
|
||||
|
||||
|
||||
private LocalDate parseDate(String s) {
|
||||
try { return s == null ? null : LocalDate.parse(s, DF); }
|
||||
catch (Exception ex) { return null; }
|
||||
}
|
||||
private String formatDate(LocalDate d) { return d == null ? null : DF.format(d); }
|
||||
|
||||
private Subscription resolveSubscription(SubscriptionRefRq ref) {
|
||||
if (ref == null) return null;
|
||||
|
||||
Long id = tryParseLong(ref.getId());
|
||||
if (id != null) {
|
||||
return subscriptionRepository.findById(id).orElse(null);
|
||||
}
|
||||
if (ref.getLevel() != null) {
|
||||
return subscriptionRepository.findByLevel(ref.getLevel())
|
||||
.orElseGet(() -> subscriptionRepository.save(new Subscription(null, ref.getLevel(), ref.getPrice())));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private List<Movie> resolveMovies(List<Long> ids) {
|
||||
List<Movie> list = new ArrayList<>();
|
||||
if (ids == null) return list;
|
||||
for (Long id : ids) {
|
||||
movieRepository.findById(id).ifPresent(list::add);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
private Long tryParseLong(String s) {
|
||||
try { return s == null ? null : Long.parseLong(s); }
|
||||
catch (NumberFormatException e) { return null; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package ru.ulstu.is.server.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import ru.ulstu.is.server.entity.Director;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface DirectorRepository extends JpaRepository<Director, Long> {
|
||||
Optional<Director> findByName(String name);
|
||||
|
||||
@Query("""
|
||||
select d.name as director, count(m.id) as moviesCount
|
||||
from Director d left join d.movies m
|
||||
group by d.name
|
||||
order by moviesCount desc
|
||||
""")
|
||||
List<DirectorStats> getDirectorStats();
|
||||
|
||||
interface DirectorStats { String getDirector(); long getMoviesCount(); }
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package ru.ulstu.is.server.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import ru.ulstu.is.server.entity.Genre;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface GenreRepository extends JpaRepository<Genre, Long> {
|
||||
Optional<Genre> findByName(String name);
|
||||
|
||||
@Query("""
|
||||
select g.name as genre, count(m.id) as moviesCount
|
||||
from Genre g left join g.movies m
|
||||
group by g.name
|
||||
order by moviesCount desc
|
||||
""")
|
||||
List<GenreStats> getGenreStats();
|
||||
|
||||
interface GenreStats { String getGenre(); long getMoviesCount(); }
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package ru.ulstu.is.server.repository;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import ru.ulstu.is.server.entity.Movie;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface MovieRepository extends JpaRepository<Movie, Long> {
|
||||
Page<Movie> findAll(Pageable pageable);
|
||||
|
||||
List<Movie> findByGenre_Name(String name);
|
||||
List<Movie> findByDirector_Name(String name);
|
||||
|
||||
@Query("""
|
||||
select m.director.name as director, count(m.id) as moviesCount
|
||||
from Movie m group by m.director.name order by moviesCount desc
|
||||
""")
|
||||
List<TopDirectors> findTopDirectors();
|
||||
|
||||
interface TopDirectors { String getDirector(); long getMoviesCount(); }
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package ru.ulstu.is.server.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import ru.ulstu.is.server.entity.Subscription;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface SubscriptionRepository extends JpaRepository<Subscription, Long> {
|
||||
Optional<Subscription> findByLevel(String level);
|
||||
|
||||
@Query("""
|
||||
select s.level as level, s.price as price, count(u.id) as usersCount
|
||||
from Subscription s left join s.users u
|
||||
group by s.level, s.price
|
||||
order by usersCount desc
|
||||
""")
|
||||
List<SubscriptionUsage> usageStats();
|
||||
|
||||
interface SubscriptionUsage {
|
||||
String getLevel();
|
||||
Integer getPrice();
|
||||
long getUsersCount();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package ru.ulstu.is.server.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import ru.ulstu.is.server.entity.User;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface UserRepository extends JpaRepository<User, Long> {
|
||||
|
||||
Optional<User> findByLogin(String login);
|
||||
List<User> findByIsAdmin(boolean isAdmin);
|
||||
List<User> findBySubscription_Level(String level);
|
||||
List<User> findByAgeGreaterThanEqual(Integer age);
|
||||
|
||||
@Query("""
|
||||
select s.level as level, count(u.id) as usersCount
|
||||
from User u join u.subscription s
|
||||
group by s.level
|
||||
order by usersCount desc
|
||||
""")
|
||||
List<UsersBySubscription> countUsersBySubscription();
|
||||
|
||||
interface UsersBySubscription {
|
||||
String getLevel();
|
||||
long getUsersCount();
|
||||
}
|
||||
|
||||
@Query("""
|
||||
select s.level as level, avg(u.age) as avgAge
|
||||
from User u join u.subscription s
|
||||
where u.age is not null
|
||||
group by s.level
|
||||
order by avgAge desc
|
||||
""")
|
||||
List<AvgAgeBySubscription> avgAgeBySubscription();
|
||||
|
||||
interface AvgAgeBySubscription {
|
||||
String getLevel();
|
||||
Double getAvgAge();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package ru.ulstu.is.server.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.ulstu.is.server.api.NotFoundException;
|
||||
import ru.ulstu.is.server.dto.DirectorRq;
|
||||
import ru.ulstu.is.server.dto.DirectorRs;
|
||||
import ru.ulstu.is.server.entity.Director;
|
||||
import ru.ulstu.is.server.mapper.DirectorMapper;
|
||||
import ru.ulstu.is.server.repository.DirectorRepository;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@Transactional
|
||||
public class DirectorService {
|
||||
private final DirectorRepository repo;
|
||||
private final DirectorMapper mapper;
|
||||
|
||||
public DirectorService(DirectorRepository repo, DirectorMapper mapper) {
|
||||
this.repo = repo; this.mapper = mapper;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<DirectorRs> getAll() {
|
||||
return repo.findAll().stream().map(mapper::toRs).toList();
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public DirectorRs get(Long id) {
|
||||
return mapper.toRs(getEntity(id));
|
||||
}
|
||||
|
||||
public DirectorRs create(DirectorRq rq) {
|
||||
Director d = mapper.toEntity(rq);
|
||||
d = repo.save(d);
|
||||
return mapper.toRs(d);
|
||||
}
|
||||
|
||||
public DirectorRs update(Long id, DirectorRq rq) {
|
||||
Director d = getEntity(id);
|
||||
mapper.updateEntity(d, rq);
|
||||
d = repo.save(d);
|
||||
return mapper.toRs(d);
|
||||
}
|
||||
|
||||
public void delete(Long id) {
|
||||
getEntity(id);
|
||||
repo.deleteById(id);
|
||||
}
|
||||
|
||||
Director getEntity(Long id) {
|
||||
return repo.findById(id).orElseThrow(() -> new NotFoundException(Director.class, String.valueOf(id)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package ru.ulstu.is.server.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.ulstu.is.server.api.NotFoundException;
|
||||
import ru.ulstu.is.server.dto.GenreRq;
|
||||
import ru.ulstu.is.server.dto.GenreRs;
|
||||
import ru.ulstu.is.server.entity.Genre;
|
||||
import ru.ulstu.is.server.mapper.GenreMapper;
|
||||
import ru.ulstu.is.server.repository.GenreRepository;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@Transactional
|
||||
public class GenreService {
|
||||
private final GenreRepository repo;
|
||||
private final GenreMapper mapper;
|
||||
|
||||
public GenreService(GenreRepository repo, GenreMapper mapper) {
|
||||
this.repo = repo; this.mapper = mapper;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<GenreRs> getAll() {
|
||||
return repo.findAll().stream().map(mapper::toRs).toList();
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public GenreRs get(Long id) {
|
||||
return mapper.toRs(getEntity(id));
|
||||
}
|
||||
|
||||
public GenreRs create(GenreRq rq) {
|
||||
Genre g = mapper.toEntity(rq);
|
||||
g = repo.save(g);
|
||||
return mapper.toRs(g);
|
||||
}
|
||||
|
||||
public GenreRs update(Long id, GenreRq rq) {
|
||||
Genre g = getEntity(id);
|
||||
mapper.updateEntity(g, rq);
|
||||
g = repo.save(g);
|
||||
return mapper.toRs(g);
|
||||
}
|
||||
|
||||
public void delete(Long id) {
|
||||
getEntity(id);
|
||||
repo.deleteById(id);
|
||||
}
|
||||
|
||||
Genre getEntity(Long id) {
|
||||
return repo.findById(id).orElseThrow(() -> new NotFoundException(Genre.class, String.valueOf(id)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
package ru.ulstu.is.server.service;
|
||||
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.ulstu.is.server.api.NotFoundException;
|
||||
import ru.ulstu.is.server.dto.*;
|
||||
import ru.ulstu.is.server.entity.Movie;
|
||||
import ru.ulstu.is.server.entity.Genre;
|
||||
import ru.ulstu.is.server.entity.Director;
|
||||
import ru.ulstu.is.server.mapper.MovieMapper;
|
||||
import ru.ulstu.is.server.repository.MovieRepository;
|
||||
import ru.ulstu.is.server.repository.GenreRepository;
|
||||
import ru.ulstu.is.server.repository.DirectorRepository;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@Transactional
|
||||
public class MovieService {
|
||||
private final MovieRepository movies;
|
||||
private final GenreRepository genres;
|
||||
private final DirectorRepository directors;
|
||||
private final MovieMapper mapper;
|
||||
|
||||
public MovieService(MovieRepository movies, GenreRepository genres,
|
||||
DirectorRepository directors, MovieMapper mapper) {
|
||||
this.movies = movies;
|
||||
this.genres = genres;
|
||||
this.directors = directors;
|
||||
this.mapper = mapper;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<MovieRs> getAll() {
|
||||
return movies.findAll().stream().map(mapper::toRs).toList();
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<MovieRs> getPage(int page, int size) {
|
||||
return movies.findAll(PageRequest.of(page, size))
|
||||
.map(mapper::toRs)
|
||||
.toList();
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public MovieRs get(Long id) {
|
||||
Movie m = movies.findById(id).orElseThrow(() -> new NotFoundException(ru.ulstu.is.server.entity.Movie.class, "movie " + id + " not found"));
|
||||
return mapper.toRs(m);
|
||||
}
|
||||
|
||||
public MovieRs create(MovieRq rq) {
|
||||
Movie e = new Movie();
|
||||
e.setTitle(rq.getTitle());
|
||||
e.setImage(rq.getImage());
|
||||
|
||||
Genre g = resolveGenre(rq.getGenre());
|
||||
Director d = resolveDirector(rq.getDirector());
|
||||
e.setGenre(g);
|
||||
e.setDirector(d);
|
||||
|
||||
Movie saved = movies.save(e);
|
||||
return mapper.toRs(saved);
|
||||
}
|
||||
|
||||
|
||||
public MovieRs update(Long id, MovieRq rq) {
|
||||
Movie e = movies.findById(id).orElseThrow(() -> new NotFoundException(ru.ulstu.is.server.entity.Movie.class, "movie " + id + " not found"));
|
||||
e.setTitle(rq.getTitle());
|
||||
e.setImage(rq.getImage());
|
||||
|
||||
if (rq.getGenre() != null) e.setGenre(resolveGenre(rq.getGenre()));
|
||||
if (rq.getDirector() != null) e.setDirector(resolveDirector(rq.getDirector()));
|
||||
|
||||
return mapper.toRs(movies.save(e));
|
||||
}
|
||||
|
||||
public void delete(Long id) {
|
||||
movies.deleteById(id);
|
||||
}
|
||||
|
||||
private Genre resolveGenre(GenreRefRq ref) {
|
||||
if (ref == null) return null;
|
||||
if (ref.getId() != null) {
|
||||
Long id = tryParse(ref.getId());
|
||||
if (id != null) return genres.findById(id).orElseThrow();
|
||||
}
|
||||
if (ref.getName() != null) return genres.findByName(ref.getName()).orElseGet(() ->
|
||||
genres.save(new Genre(null, ref.getName())));
|
||||
return null;
|
||||
}
|
||||
|
||||
private Director resolveDirector(DirectorRefRq ref) {
|
||||
if (ref == null) return null;
|
||||
if (ref.getId() != null) {
|
||||
Long id = tryParse(ref.getId());
|
||||
if (id != null) return directors.findById(id).orElseThrow();
|
||||
}
|
||||
if (ref.getName() != null) return directors.findByName(ref.getName()).orElseGet(() ->
|
||||
directors.save(new Director(null, ref.getName())));
|
||||
return null;
|
||||
}
|
||||
|
||||
private Long tryParse(String s) {
|
||||
try { return s == null ? null : Long.parseLong(s); } catch (NumberFormatException e) { return null; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package ru.ulstu.is.server.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.ulstu.is.server.api.NotFoundException;
|
||||
import ru.ulstu.is.server.dto.SubscriptionRq;
|
||||
import ru.ulstu.is.server.dto.SubscriptionRs;
|
||||
import ru.ulstu.is.server.entity.Subscription;
|
||||
import ru.ulstu.is.server.mapper.SubscriptionMapper;
|
||||
import ru.ulstu.is.server.repository.SubscriptionRepository;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@Transactional
|
||||
public class SubscriptionService {
|
||||
private final SubscriptionRepository repo;
|
||||
private final SubscriptionMapper mapper;
|
||||
|
||||
public SubscriptionService(SubscriptionRepository repo, SubscriptionMapper mapper) {
|
||||
this.repo = repo; this.mapper = mapper;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<SubscriptionRs> getAll() {
|
||||
return repo.findAll().stream().map(mapper::toRs).toList();
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public SubscriptionRs get(Long id) {
|
||||
return mapper.toRs(getEntity(id));
|
||||
}
|
||||
|
||||
public SubscriptionRs create(SubscriptionRq rq) {
|
||||
Subscription s = mapper.toEntity(rq);
|
||||
s = repo.save(s);
|
||||
return mapper.toRs(s);
|
||||
}
|
||||
|
||||
public SubscriptionRs update(Long id, SubscriptionRq rq) {
|
||||
Subscription s = getEntity(id);
|
||||
mapper.updateEntity(s, rq);
|
||||
s = repo.save(s);
|
||||
return mapper.toRs(s);
|
||||
}
|
||||
|
||||
public void delete(Long id) {
|
||||
getEntity(id);
|
||||
repo.deleteById(id);
|
||||
}
|
||||
|
||||
Subscription getEntity(Long id) {
|
||||
return repo.findById(id).orElseThrow(() -> new NotFoundException(Subscription.class, String.valueOf(id)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package ru.ulstu.is.server.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.ulstu.is.server.api.NotFoundException;
|
||||
import ru.ulstu.is.server.dto.UserRq;
|
||||
import ru.ulstu.is.server.dto.UserRs;
|
||||
import ru.ulstu.is.server.entity.User;
|
||||
import ru.ulstu.is.server.mapper.UserMapper;
|
||||
import ru.ulstu.is.server.repository.UserRepository;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@Transactional
|
||||
public class UserService {
|
||||
private final UserRepository repo;
|
||||
private final UserMapper mapper;
|
||||
|
||||
public UserService(UserRepository repo, UserMapper mapper) {
|
||||
this.repo = repo; this.mapper = mapper;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<UserRs> getAll() {
|
||||
return repo.findAll().stream().map(mapper::toRs).toList();
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public UserRs get(Long id) {
|
||||
return mapper.toRs(getEntity(id));
|
||||
}
|
||||
|
||||
public UserRs create(UserRq rq) {
|
||||
User u = mapper.toEntity(rq);
|
||||
u = repo.save(u);
|
||||
return mapper.toRs(u);
|
||||
}
|
||||
|
||||
public UserRs update(Long id, UserRq rq) {
|
||||
User u = getEntity(id);
|
||||
mapper.updateEntity(u, rq);
|
||||
u = repo.save(u);
|
||||
return mapper.toRs(u);
|
||||
}
|
||||
|
||||
public void delete(Long id) {
|
||||
getEntity(id);
|
||||
repo.deleteById(id);
|
||||
}
|
||||
|
||||
User getEntity(Long id) {
|
||||
return repo.findById(id).orElseThrow(() -> new NotFoundException(User.class, String.valueOf(id)));
|
||||
}
|
||||
}
|
||||
@@ -2,5 +2,17 @@ spring.main.banner-mode=off
|
||||
spring.application.name=server
|
||||
server.port=8080
|
||||
|
||||
spring.datasource.url=jdbc:h2:file:./data/appdb
|
||||
spring.datasource.driverClassName=org.h2.Driver
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=
|
||||
spring.jpa.hibernate.ddl-auto=update
|
||||
spring.jpa.show-sql=true
|
||||
spring.jpa.properties.hibernate.format_sql=true
|
||||
|
||||
|
||||
spring.h2.console.enabled=true
|
||||
spring.h2.console.path=/h2
|
||||
|
||||
# logging
|
||||
logging.level.ru.ulstu.is.server=DEBUG
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
package ru.ulstu.is.server;
|
||||
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import ru.ulstu.is.server.api.NotFoundException;
|
||||
import ru.ulstu.is.server.dto.DirectorRq;
|
||||
import ru.ulstu.is.server.dto.DirectorRs;
|
||||
import ru.ulstu.is.server.service.DirectorService;
|
||||
|
||||
@SpringBootTest
|
||||
@TestMethodOrder(OrderAnnotation.class)
|
||||
class DirectorServiceTests {
|
||||
|
||||
@Autowired
|
||||
private DirectorService service;
|
||||
|
||||
@Test
|
||||
void getNotFound() {
|
||||
Assertions.assertThrows(NotFoundException.class, () -> service.get(0L));
|
||||
}
|
||||
|
||||
@Test @Order(1)
|
||||
void createTest() {
|
||||
service.create(new DirectorRq() {{ setName("Кристофер Нолан"); }});
|
||||
service.create(new DirectorRq() {{ setName("Лана Вачовски"); }});
|
||||
DirectorRs last = service.create(new DirectorRq() {{ setName("Дени Вильнёв"); }});
|
||||
|
||||
Assertions.assertEquals(3, service.getAll().size());
|
||||
Assertions.assertEquals("Дени Вильнёв", service.get(last.getId()).getName());
|
||||
}
|
||||
|
||||
@Test @Order(2)
|
||||
void updateTest() {
|
||||
DirectorRs upd = service.update(3L, new DirectorRq() {{ setName("Дени Вильнёв (FR)"); }});
|
||||
Assertions.assertEquals("Дени Вильнёв (FR)", upd.getName());
|
||||
}
|
||||
|
||||
@Test @Order(3)
|
||||
void deleteTest() {
|
||||
service.delete(3L);
|
||||
Assertions.assertEquals(2, service.getAll().size());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package ru.ulstu.is.server;
|
||||
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import ru.ulstu.is.server.api.NotFoundException;
|
||||
import ru.ulstu.is.server.dto.GenreRq;
|
||||
import ru.ulstu.is.server.dto.GenreRs;
|
||||
import ru.ulstu.is.server.service.GenreService;
|
||||
|
||||
@SpringBootTest
|
||||
@TestMethodOrder(OrderAnnotation.class)
|
||||
class GenreServiceTests {
|
||||
|
||||
@Autowired
|
||||
private GenreService service;
|
||||
|
||||
@Test
|
||||
void getNotFound() {
|
||||
Assertions.assertThrows(NotFoundException.class, () -> service.get(0L));
|
||||
}
|
||||
|
||||
@Test @Order(1)
|
||||
void createTest() {
|
||||
service.create(new GenreRq() {{ setName("Фантастика"); }});
|
||||
service.create(new GenreRq() {{ setName("Драма"); }});
|
||||
GenreRs last = service.create(new GenreRq() {{ setName("Боевик"); }});
|
||||
|
||||
Assertions.assertEquals(3, service.getAll().size());
|
||||
GenreRs cmp = service.get(last.getId());
|
||||
Assertions.assertEquals(last.getId(), cmp.getId());
|
||||
Assertions.assertEquals(last.getName(), cmp.getName());
|
||||
}
|
||||
|
||||
@Test @Order(2)
|
||||
void updateTest() {
|
||||
GenreRs before = service.get(3L);
|
||||
GenreRs after = service.update(3L, new GenreRq() {{ setName("Триллер"); }});
|
||||
Assertions.assertEquals(3, service.getAll().size());
|
||||
Assertions.assertEquals("Триллер", after.getName());
|
||||
Assertions.assertNotEquals(before.getName(), after.getName());
|
||||
}
|
||||
|
||||
@Test @Order(3)
|
||||
void deleteTest() {
|
||||
service.delete(3L);
|
||||
Assertions.assertEquals(2, service.getAll().size());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package ru.ulstu.is.server;
|
||||
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import ru.ulstu.is.server.api.NotFoundException;
|
||||
import ru.ulstu.is.server.dto.*;
|
||||
import ru.ulstu.is.server.service.*;
|
||||
|
||||
@SpringBootTest
|
||||
@TestMethodOrder(OrderAnnotation.class)
|
||||
class MovieServiceTests {
|
||||
|
||||
@Autowired private GenreService genreService;
|
||||
@Autowired private DirectorService directorService;
|
||||
@Autowired private MovieService movieService;
|
||||
|
||||
@Test
|
||||
void getNotFound() {
|
||||
Assertions.assertThrows(NotFoundException.class, () -> movieService.get(0L));
|
||||
}
|
||||
|
||||
@Test @Order(1)
|
||||
void createTest() {
|
||||
// подготовим справочники
|
||||
GenreRs g = genreService.create(new GenreRq() {{ setName("Фантастика"); }});
|
||||
DirectorRs d = directorService.create(new DirectorRq() {{ setName("Кристофер Нолан"); }});
|
||||
|
||||
MovieRq rq1 = new MovieRq();
|
||||
rq1.setTitle("Начало");
|
||||
rq1.setGenre(new GenreRefRq() {{ setId(String.valueOf(g.getId())); }});
|
||||
rq1.setDirector(new DirectorRefRq() {{ setId(String.valueOf(d.getId())); }});
|
||||
movieService.create(rq1);
|
||||
|
||||
MovieRq rq2 = new MovieRq();
|
||||
rq2.setTitle("Интерстеллар");
|
||||
rq2.setGenre(new GenreRefRq() {{ setId(String.valueOf(g.getId())); }});
|
||||
rq2.setDirector(new DirectorRefRq() {{ setId(String.valueOf(d.getId())); }});
|
||||
MovieRs last = movieService.create(rq2);
|
||||
|
||||
Assertions.assertEquals(2, movieService.getAll().size());
|
||||
Assertions.assertEquals("Интерстеллар", movieService.get(last.getId()).getTitle());
|
||||
Assertions.assertEquals("Фантастика", movieService.get(last.getId()).getGenre().getName());
|
||||
}
|
||||
|
||||
@Test @Order(2)
|
||||
void updateTest() {
|
||||
MovieRq upd = new MovieRq();
|
||||
upd.setTitle("Интерстеллар (2014)");
|
||||
upd.setGenre(new GenreRefRq() {{ setName("Научная фантастика"); }});
|
||||
upd.setDirector(new DirectorRefRq() {{ setName("К. Нолан"); }});
|
||||
MovieRs newEntity = movieService.update(2L, upd);
|
||||
|
||||
Assertions.assertEquals("Интерстеллар (2014)", newEntity.getTitle());
|
||||
Assertions.assertEquals("Научная фантастика", newEntity.getGenre().getName());
|
||||
Assertions.assertEquals("К. Нолан", newEntity.getDirector().getName());
|
||||
}
|
||||
|
||||
@Test @Order(3)
|
||||
void deleteTest() {
|
||||
movieService.delete(2L);
|
||||
Assertions.assertEquals(1, movieService.getAll().size());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package ru.ulstu.is.server;
|
||||
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import ru.ulstu.is.server.api.NotFoundException;
|
||||
import ru.ulstu.is.server.dto.SubscriptionRq;
|
||||
import ru.ulstu.is.server.dto.SubscriptionRs;
|
||||
import ru.ulstu.is.server.service.SubscriptionService;
|
||||
|
||||
@SpringBootTest
|
||||
@TestMethodOrder(OrderAnnotation.class)
|
||||
class SubscriptionServiceTests {
|
||||
|
||||
@Autowired
|
||||
private SubscriptionService service;
|
||||
|
||||
@Test
|
||||
void getNotFound() {
|
||||
Assertions.assertThrows(NotFoundException.class, () -> service.get(0L));
|
||||
}
|
||||
|
||||
@Test @Order(1)
|
||||
void createTest() {
|
||||
service.create(new SubscriptionRq() {{ setLevel("Базовая"); setPrice(299); }});
|
||||
service.create(new SubscriptionRq() {{ setLevel("Стандарт"); setPrice(599); }});
|
||||
SubscriptionRs last = service.create(new SubscriptionRq() {{ setLevel("Премиум"); setPrice(999); }});
|
||||
|
||||
Assertions.assertEquals(3, service.getAll().size());
|
||||
Assertions.assertEquals(999, service.get(last.getId()).getPrice());
|
||||
}
|
||||
|
||||
@Test @Order(2)
|
||||
void updateTest() {
|
||||
SubscriptionRs upd = service.update(3L, new SubscriptionRq() {{ setLevel("Премиум+"); setPrice(1299); }});
|
||||
Assertions.assertEquals("Премиум+", upd.getLevel());
|
||||
Assertions.assertEquals(1299, upd.getPrice());
|
||||
}
|
||||
|
||||
@Test @Order(3)
|
||||
void deleteTest() {
|
||||
service.delete(3L);
|
||||
Assertions.assertEquals(2, service.getAll().size());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package ru.ulstu.is.server;
|
||||
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import ru.ulstu.is.server.api.NotFoundException;
|
||||
import ru.ulstu.is.server.dto.*;
|
||||
import ru.ulstu.is.server.entity.Subscription;
|
||||
import ru.ulstu.is.server.repository.SubscriptionRepository;
|
||||
import ru.ulstu.is.server.service.UserService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@SpringBootTest
|
||||
@TestMethodOrder(OrderAnnotation.class)
|
||||
class UserServiceTests {
|
||||
|
||||
@Autowired private UserService userService;
|
||||
@Autowired private SubscriptionRepository subscriptionRepository;
|
||||
|
||||
@Test
|
||||
void getNotFound() {
|
||||
Assertions.assertThrows(NotFoundException.class, () -> userService.get(0L));
|
||||
}
|
||||
|
||||
@Test @Order(1)
|
||||
void createTest() {
|
||||
// подготовим подписку
|
||||
Subscription sub = subscriptionRepository.save(new Subscription(null, "Базовая", 299));
|
||||
|
||||
UserRq rq = new UserRq();
|
||||
rq.setName("Иван");
|
||||
rq.setLogin("ivan@example.com");
|
||||
rq.setPassword("1234");
|
||||
var ref = new SubscriptionRefRq(); ref.setId(String.valueOf(sub.getId()));
|
||||
rq.setSubscription(ref);
|
||||
rq.setWatchlist(List.of());
|
||||
|
||||
UserRs created = userService.create(rq);
|
||||
Assertions.assertNotNull(created.getId());
|
||||
Assertions.assertEquals("Иван", created.getName());
|
||||
Assertions.assertEquals("Базовая", created.getSubscription().getLevel());
|
||||
}
|
||||
|
||||
@Test @Order(2)
|
||||
void updateTest() {
|
||||
UserRq upd = new UserRq();
|
||||
upd.setName("Иван Петров");
|
||||
upd.setLogin("ivan@example.com");
|
||||
upd.setPassword("1234");
|
||||
upd.setSubscriptionUntil("2026-01-01");
|
||||
var newRef = new SubscriptionRefRq(); newRef.setLevel("Премиум"); newRef.setPrice(999);
|
||||
upd.setSubscription(newRef);
|
||||
|
||||
UserRs after = userService.update(1L, upd);
|
||||
Assertions.assertEquals("Иван Петров", after.getName());
|
||||
Assertions.assertEquals("Премиум", after.getSubscription().getLevel());
|
||||
Assertions.assertEquals(999, after.getSubscription().getPrice());
|
||||
Assertions.assertEquals("2026-01-01", after.getSubscriptionUntil());
|
||||
}
|
||||
|
||||
@Test @Order(3)
|
||||
void deleteTest() {
|
||||
userService.delete(1L);
|
||||
Assertions.assertTrue(userService.getAll().isEmpty());
|
||||
}
|
||||
}
|
||||
8
backend/src/test/resources/application.properties
Normal file
8
backend/src/test/resources/application.properties
Normal file
@@ -0,0 +1,8 @@
|
||||
logging.level.ru.ulstu.is.server=DEBUG
|
||||
|
||||
spring.datasource.url=jdbc:h2:mem:testdb
|
||||
spring.datasource.driverClassName=org.h2.Driver
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=
|
||||
spring.jpa.hibernate.ddl-auto=create-drop
|
||||
spring.jpa.show-sql=false
|
||||
@@ -1,43 +1,31 @@
|
||||
import { Link } from "react-router-dom";
|
||||
import useGenres from "../hooks/useGenres"
|
||||
import useDirectors from "../hooks/useDirectors";
|
||||
|
||||
export default function MovieCard({ movie, onDelete }) {
|
||||
const { genres } = useGenres();
|
||||
const { directors } = useDirectors();
|
||||
|
||||
const genre = genres.find(g => g.id === movie.genreId);
|
||||
const director = directors.find(d => d.id === movie.directorId);
|
||||
const genreName = movie.genre?.name ?? "—";
|
||||
const directorName = movie.director?.name ?? "—";
|
||||
const img = movie.image || "https://via.placeholder.com/300x450?text=No+Image";
|
||||
|
||||
return (
|
||||
<div className="card h-100 shadow-sm">
|
||||
<img
|
||||
src={movie.image || 'https://via.placeholder.com/300x450?text=No+Image'}
|
||||
className="card-img-top"
|
||||
alt={movie.title}
|
||||
/>
|
||||
|
||||
<img src={img} className="card-img-top" alt={movie.title} />
|
||||
<div className="card-body d-flex flex-column">
|
||||
<p className="card-text mb-1">{movie.title}</p>
|
||||
|
||||
<p className="card-text mb-1">
|
||||
<i className="bi bi-film me-1" />
|
||||
Жанр: {genre?.name ?? "—"}
|
||||
Жанр: {genreName}
|
||||
</p>
|
||||
|
||||
<p className="card-text mb-4">
|
||||
<i className="bi bi-person-video2 me-1" />
|
||||
Режиссёр: {director?.name ?? '—'}
|
||||
Режиссёр: {directorName}
|
||||
</p>
|
||||
|
||||
<div className="mt-auto d-grid gap-2">
|
||||
<Link className="btn btn-sm btn-primary" to={`/movie/${movie.id}`}>
|
||||
Подробнее
|
||||
</Link>
|
||||
<button
|
||||
className="btn btn-sm btn-danger"
|
||||
onClick={() => onDelete(movie.id)}
|
||||
>
|
||||
<button className="btn btn-sm btn-danger" onClick={() => onDelete(movie.id)}>
|
||||
Удалить
|
||||
</button>
|
||||
</div>
|
||||
|
||||
@@ -1,46 +1,72 @@
|
||||
import { useState } from "react";
|
||||
import { useEffect, useState } from "react";
|
||||
import useGenres from "../hooks/useGenres";
|
||||
import useDirectors from "../hooks/useDirectors";
|
||||
import ImageCropper from "./ImageCropper";
|
||||
|
||||
export default function MovieForm({ initial, onSubmit }) {
|
||||
const { genres } = useGenres();
|
||||
const {directors} = useDirectors();
|
||||
const { directors } = useDirectors();
|
||||
|
||||
const [form,setForm] = useState(
|
||||
initial ?? { title:"", genreId:"", directorId:"", image:"" }
|
||||
const [form, setForm] = useState(
|
||||
initial ?? { title: "", genreId: "", directorId: "", image: "" }
|
||||
);
|
||||
|
||||
const handle = e => setForm({ ...form, [e.target.name]: e.target.value });
|
||||
useEffect(() => {
|
||||
if (initial) {
|
||||
setForm({
|
||||
title: initial.title ?? "",
|
||||
image: initial.image ?? "",
|
||||
genreId: initial.genreId != null ? String(initial.genreId) : "",
|
||||
directorId: initial.directorId != null ? String(initial.directorId) : ""
|
||||
});
|
||||
}
|
||||
}, [initial]);
|
||||
|
||||
const submit = e => { e.preventDefault(); onSubmit(form); };
|
||||
const handle = (e) => setForm({ ...form, [e.target.name]: e.target.value });
|
||||
|
||||
const submit = (e) => { e.preventDefault(); onSubmit(form); };
|
||||
|
||||
return (
|
||||
<form onSubmit={submit} className="bg-dark p-4 rounded">
|
||||
<div className="mb-3">
|
||||
<label className="form-label">Название фильма</label>
|
||||
<input name="title" className="form-control"
|
||||
value={form.title} onChange={handle} required />
|
||||
<input
|
||||
name="title"
|
||||
className="form-control"
|
||||
value={form.title}
|
||||
onChange={handle}
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="mb-3">
|
||||
<label className="form-label">Жанр</label>
|
||||
<select name="genreId" className="form-select"
|
||||
value={form.genreId} onChange={handle} required>
|
||||
<select
|
||||
name="genreId"
|
||||
className="form-select"
|
||||
value={form.genreId}
|
||||
onChange={handle}
|
||||
required
|
||||
>
|
||||
<option value="">— выберите жанр —</option>
|
||||
{genres.map(g=>(
|
||||
<option key={g.id} value={g.id}>{g.name}</option>
|
||||
{genres.map(g => (
|
||||
<option key={g.id} value={String(g.id)}>{g.name}</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div className="mb-3">
|
||||
<label className="form-label">Режиссёр</label>
|
||||
<select name="directorId" className="form-select"
|
||||
value={form.directorId} onChange={handle} required>
|
||||
<select
|
||||
name="directorId"
|
||||
className="form-select"
|
||||
value={form.directorId}
|
||||
onChange={handle}
|
||||
required
|
||||
>
|
||||
<option value="">— выберите режиссёра —</option>
|
||||
{directors.map(d=>(
|
||||
<option key={d.id} value={d.id}>{d.name}</option>
|
||||
{directors.map(d => (
|
||||
<option key={d.id} value={String(d.id)}>{d.name}</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
@@ -49,14 +75,15 @@ export default function MovieForm({ initial, onSubmit }) {
|
||||
<label className="form-label">Постер</label>
|
||||
<ImageCropper
|
||||
value={form.image}
|
||||
onChange={img => setForm(f=>({ ...f, image: img }))}
|
||||
onChange={img => setForm(f => ({ ...f, image: img }))}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<button className="btn btn-primary w-100">
|
||||
{initial ? <><i className="bi bi-save"/> Сохранить</>
|
||||
: <><i className="bi bi-plus-circle"/> Добавить</>}
|
||||
{initial
|
||||
? <><i className="bi bi-save" /> Сохранить</>
|
||||
: <><i className="bi bi-plus-circle" /> Добавить</>}
|
||||
</button>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,30 +8,46 @@ const api = axios.create({ baseURL: "http://localhost:8080/api/1.0" });
|
||||
export default function MovieEditor({ mode }) {
|
||||
const { id } = useParams();
|
||||
const nav = useNavigate();
|
||||
const [initial,setInitial] = useState(null);
|
||||
const [initial, setInitial] = useState(null);
|
||||
|
||||
useEffect(()=>{
|
||||
useEffect(() => {
|
||||
if (mode === "edit") {
|
||||
api.get(`/movies/${id}`).then(r=>setInitial(r.data))
|
||||
.catch(()=> alert("Фильм не найден"));
|
||||
api.get(`/movies/${id}`)
|
||||
.then(r => {
|
||||
const m = r.data;
|
||||
setInitial({
|
||||
title: m.title || "",
|
||||
image: m.image || "",
|
||||
genreId: m.genre?.id != null ? String(m.genre.id) : "",
|
||||
directorId: m.director?.id != null ? String(m.director.id) : ""
|
||||
});
|
||||
})
|
||||
.catch(() => alert("Фильм не найден"));
|
||||
}
|
||||
},[mode,id]);
|
||||
}, [mode, id]);
|
||||
|
||||
const handleSubmit = async data => {
|
||||
const handleSubmit = async (data) => {
|
||||
if (!data.image) { alert("Сначала обрежьте изображение!"); return; }
|
||||
|
||||
if (mode === "edit") await api.put(`/movies/${id}`, data);
|
||||
else await api.post("/movies", data);
|
||||
const payload = {
|
||||
title: data.title,
|
||||
image: data.image ?? null,
|
||||
genre: data.genreId ? { id: String(data.genreId) } : null,
|
||||
director: data.directorId ? { id: String(data.directorId) } : null,
|
||||
};
|
||||
|
||||
nav("/");
|
||||
if (mode === "edit") await api.put(`/movies/${id}`, payload);
|
||||
else await api.post("/movies", payload);
|
||||
|
||||
nav("/");
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="container py-4">
|
||||
<h2>{mode==="edit" ? "Редактировать фильм" : "Добавить фильм"}</h2>
|
||||
<h2>{mode === "edit" ? "Редактировать фильм" : "Добавить фильм"}</h2>
|
||||
{(mode === "edit" && !initial)
|
||||
? <p>Загрузка…</p>
|
||||
: <MovieForm initial={initial} onSubmit={handleSubmit} />}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user