Compare commits
3 Commits
a21e43a80e
...
022f81af6a
| Author | SHA1 | Date | |
|---|---|---|---|
| 022f81af6a | |||
| fec98632f0 | |||
| f253354d62 |
@@ -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.
@@ -29,6 +29,13 @@ public class MovieController {
|
||||
@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); }
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,11 @@
|
||||
package ru.ulstu.is.server.entity;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@MappedSuperclass
|
||||
public abstract class BaseEntity {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
public BaseEntity() { }
|
||||
|
||||
@@ -1,11 +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;
|
||||
|
||||
public Director() { }
|
||||
public Director(Long id, String name) { super(id); this.name = 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; }
|
||||
}
|
||||
|
||||
@@ -1,11 +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;
|
||||
|
||||
public Genre() { }
|
||||
public Genre(Long id, String name) { super(id); this.name = 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; }
|
||||
}
|
||||
|
||||
@@ -1,15 +1,30 @@
|
||||
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;
|
||||
private String image;
|
||||
private Genre genre;
|
||||
private Director director;
|
||||
|
||||
public Movie() { }
|
||||
@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) {
|
||||
super(id);
|
||||
setId(id);
|
||||
this.title = title;
|
||||
this.image = image;
|
||||
this.genre = genre;
|
||||
@@ -18,13 +33,10 @@ public class Movie extends BaseEntity {
|
||||
|
||||
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; }
|
||||
}
|
||||
|
||||
@@ -1,16 +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;
|
||||
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; }
|
||||
}
|
||||
|
||||
@@ -1,25 +1,69 @@
|
||||
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;
|
||||
private Boolean isAdmin;
|
||||
|
||||
@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;
|
||||
private String subscriptionUntil;
|
||||
private List<Long> watchlist = new ArrayList<>();
|
||||
|
||||
@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;
|
||||
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; }
|
||||
@@ -43,13 +87,12 @@ public class User extends BaseEntity {
|
||||
public String getAvatar() { return avatar; }
|
||||
public void setAvatar(String avatar) { this.avatar = avatar; }
|
||||
|
||||
public List<Long> getWatchlist() { return watchlist; }
|
||||
public void setWatchlist(List<Long> watchlist) { this.watchlist = watchlist; }
|
||||
|
||||
public Subscription getSubscription() { return subscription; }
|
||||
public void setSubscription(Subscription subscription) { this.subscription = subscription; }
|
||||
|
||||
public String getSubscriptionUntil() { return subscriptionUntil; }
|
||||
public void setSubscriptionUntil(String subscriptionUntil) { this.subscriptionUntil = subscriptionUntil; }
|
||||
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<>(); }
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ 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 {
|
||||
|
||||
@@ -2,77 +2,55 @@ 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;
|
||||
|
||||
public UserMapper(SubscriptionRepository subscriptionRepository) {
|
||||
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();
|
||||
u.setName(rq.getName());
|
||||
u.setLogin(rq.getLogin());
|
||||
u.setPassword(rq.getPassword());
|
||||
u.setIsAdmin(Boolean.TRUE.equals(rq.getIsAdmin()));
|
||||
u.setAge(rq.getAge());
|
||||
u.setGender(rq.getGender());
|
||||
u.setAvatar(rq.getAvatar());
|
||||
u.setSubscriptionUntil(rq.getSubscriptionUntil());
|
||||
u.setWatchlist(rq.getWatchlist() != null ? rq.getWatchlist() : new ArrayList<>());
|
||||
|
||||
SubscriptionRefRq ref = rq.getSubscription();
|
||||
if (ref != null) {
|
||||
Subscription sub = null;
|
||||
if (ref.getId() != null) {
|
||||
try {
|
||||
Long id = Long.parseLong(ref.getId());
|
||||
sub = subscriptionRepository.findById(id).orElse(null);
|
||||
} catch (NumberFormatException ignored) {}
|
||||
}
|
||||
if (sub == null && (ref.getLevel() != null || ref.getPrice() != null)) {
|
||||
sub = new Subscription(null, ref.getLevel(), ref.getPrice());
|
||||
sub = subscriptionRepository.save(sub);
|
||||
}
|
||||
u.setSubscription(sub);
|
||||
}
|
||||
updateEntity(u, rq);
|
||||
return u;
|
||||
}
|
||||
|
||||
public void updateEntity(User u, UserRq rq) {
|
||||
u.setName(rq.getName());
|
||||
u.setLogin(rq.getLogin());
|
||||
u.setPassword(rq.getPassword());
|
||||
u.setIsAdmin(Boolean.TRUE.equals(rq.getIsAdmin()));
|
||||
u.setAge(rq.getAge());
|
||||
u.setGender(rq.getGender());
|
||||
u.setAvatar(rq.getAvatar());
|
||||
u.setSubscriptionUntil(rq.getSubscriptionUntil());
|
||||
u.setWatchlist(rq.getWatchlist() != null ? rq.getWatchlist() : new ArrayList<>());
|
||||
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());
|
||||
|
||||
SubscriptionRefRq ref = rq.getSubscription();
|
||||
if (ref != null) {
|
||||
Subscription sub = null;
|
||||
if (ref.getId() != null) {
|
||||
try {
|
||||
Long id = Long.parseLong(ref.getId());
|
||||
sub = subscriptionRepository.findById(id).orElse(null);
|
||||
} catch (NumberFormatException ignored) {}
|
||||
}
|
||||
if (sub == null && (ref.getLevel() != null || ref.getPrice() != null)) {
|
||||
sub = new Subscription(null, ref.getLevel(), ref.getPrice());
|
||||
sub = subscriptionRepository.save(sub);
|
||||
}
|
||||
u.setSubscription(sub);
|
||||
} else {
|
||||
u.setSubscription(null);
|
||||
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()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,20 +59,62 @@ public class UserMapper {
|
||||
rs.setId(u.getId());
|
||||
rs.setName(u.getName());
|
||||
rs.setLogin(u.getLogin());
|
||||
rs.setIsAdmin(Boolean.TRUE.equals(u.getIsAdmin()));
|
||||
rs.setIsAdmin(u.getIsAdmin());
|
||||
rs.setAge(u.getAge());
|
||||
rs.setGender(u.getGender());
|
||||
rs.setAvatar(u.getAvatar());
|
||||
rs.setSubscriptionUntil(u.getSubscriptionUntil());
|
||||
rs.setWatchlist(u.getWatchlist());
|
||||
|
||||
|
||||
rs.setSubscriptionUntil(formatDate(u.getSubscriptionUntil()));
|
||||
|
||||
if (u.getSubscription() != null) {
|
||||
rs.setSubscription(new SubscriptionRs(
|
||||
u.getSubscription().getId(),
|
||||
u.getSubscription().getLevel(),
|
||||
u.getSubscription().getPrice()
|
||||
));
|
||||
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; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
package ru.ulstu.is.server.repository;
|
||||
|
||||
import ru.ulstu.is.server.entity.BaseEntity;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface CommonRepository<T extends BaseEntity> {
|
||||
List<T> findAll();
|
||||
Optional<T> findById(Long id);
|
||||
T save(T entity);
|
||||
void deleteById(Long id);
|
||||
void delete(T entity);
|
||||
long count();
|
||||
}
|
||||
@@ -1,5 +1,22 @@
|
||||
package ru.ulstu.is.server.repository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public class DirectorRepository extends MapRepository<ru.ulstu.is.server.entity.Director> { }
|
||||
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(); }
|
||||
}
|
||||
|
||||
@@ -1,6 +1,22 @@
|
||||
package ru.ulstu.is.server.repository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import ru.ulstu.is.server.entity.Genre;
|
||||
|
||||
@Repository
|
||||
public class GenreRepository extends MapRepository<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(); }
|
||||
}
|
||||
|
||||
@@ -1,51 +0,0 @@
|
||||
package ru.ulstu.is.server.repository;
|
||||
|
||||
import ru.ulstu.is.server.entity.BaseEntity;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.ConcurrentSkipListMap;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
public abstract class MapRepository<T extends BaseEntity> implements CommonRepository<T> {
|
||||
|
||||
protected final ConcurrentSkipListMap<Long, T> storage = new ConcurrentSkipListMap<>();
|
||||
protected final AtomicLong seq = new AtomicLong(0L);
|
||||
|
||||
@Override
|
||||
public List<T> findAll() {
|
||||
return new ArrayList<>(storage.values());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<T> findById(Long id) {
|
||||
return Optional.ofNullable(storage.get(id));
|
||||
}
|
||||
|
||||
@Override
|
||||
public T save(T entity) {
|
||||
if (entity.getId() == null) {
|
||||
entity.setId(seq.incrementAndGet());
|
||||
}
|
||||
storage.put(entity.getId(), entity);
|
||||
return entity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteById(Long id) {
|
||||
storage.remove(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(T entity) {
|
||||
if (entity != null && entity.getId() != null) {
|
||||
storage.remove(entity.getId());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public long count() {
|
||||
return storage.size();
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,24 @@
|
||||
package ru.ulstu.is.server.repository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public class MovieRepository extends MapRepository<ru.ulstu.is.server.entity.Movie> { }
|
||||
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(); }
|
||||
}
|
||||
|
||||
@@ -1,5 +1,26 @@
|
||||
package ru.ulstu.is.server.repository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public class SubscriptionRepository extends MapRepository<ru.ulstu.is.server.entity.Subscription> { }
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,43 @@
|
||||
package ru.ulstu.is.server.repository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public class UserRepository extends MapRepository<ru.ulstu.is.server.entity.User> { }
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,10 +7,12 @@ 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;
|
||||
@@ -19,10 +21,12 @@ public class DirectorService {
|
||||
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));
|
||||
}
|
||||
|
||||
@@ -7,10 +7,12 @@ 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;
|
||||
@@ -19,10 +21,12 @@ public class GenreService {
|
||||
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));
|
||||
}
|
||||
|
||||
@@ -1,55 +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.MovieRq;
|
||||
import ru.ulstu.is.server.dto.MovieRs;
|
||||
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 repo;
|
||||
private final MovieRepository movies;
|
||||
private final GenreRepository genres;
|
||||
private final DirectorRepository directors;
|
||||
private final MovieMapper mapper;
|
||||
|
||||
public MovieService(MovieRepository repo, MovieMapper mapper) {
|
||||
this.repo = repo; this.mapper = 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 repo.findAll().stream().map(mapper::toRs).toList();
|
||||
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) {
|
||||
return mapper.toRs(getEntity(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 = mapper.toEntity(rq);
|
||||
e = repo.save(e);
|
||||
return mapper.toRs(e);
|
||||
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 existing = getEntity(id);
|
||||
existing.setTitle(rq.getTitle());
|
||||
existing.setImage(rq.getImage());
|
||||
Movie tmp = mapper.toEntity(rq);
|
||||
existing.setGenre(tmp.getGenre());
|
||||
existing.setDirector(tmp.getDirector());
|
||||
existing = repo.save(existing);
|
||||
return mapper.toRs(existing);
|
||||
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) {
|
||||
getEntity(id);
|
||||
repo.deleteById(id);
|
||||
movies.deleteById(id);
|
||||
}
|
||||
|
||||
Movie getEntity(Long id) {
|
||||
return repo.findById(id).orElseThrow(() -> new NotFoundException(Movie.class, String.valueOf(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; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,10 +7,12 @@ 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;
|
||||
@@ -19,10 +21,12 @@ public class SubscriptionService {
|
||||
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));
|
||||
}
|
||||
|
||||
@@ -7,10 +7,12 @@ 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;
|
||||
@@ -19,10 +21,12 @@ public class UserService {
|
||||
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));
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -1,3 +1,8 @@
|
||||
logging.level.ru.ulstu.is.server=DEBUG
|
||||
|
||||
spring.datasource.url=jdbc:h2:mem:testdb
|
||||
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
|
||||
Reference in New Issue
Block a user