@RestController
@RequestMapping("/api/movies")
public class MovieController {
@Autowired
private MovieService movieService;
@GetMapping
public R list(@RequestParam(required = false) String title,
@RequestParam(required = false) String genre,
@RequestParam(required = false) Integer year,
@RequestParam(defaultValue = "1") Integer page,
@RequestParam(defaultValue = "10") Integer size) {
Page<Movie> pageParam = new Page<>(page, size);
LambdaQueryWrapper<Movie> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.like(StringUtils.isNotBlank(title), Movie::getTitle, title)
.eq(StringUtils.isNotBlank(genre), Movie::getGenre, genre)
.eq(year != null, Movie::getReleaseYear, year)
.orderByDesc(Movie::getReleaseDate);
Page<Movie> result = movieService.page(pageParam, queryWrapper);
return R.ok().data("items", result.getRecords()).data("total", result.getTotal());
}
@PostMapping
public R save(@RequestBody Movie movie) {
movieService.save(movie);
return R.ok();
}
@PutMapping("/{id}")
public R update(@PathVariable String id, @RequestBody Movie movie) {
movie.setId(id);
movieService.updateById(movie);
return R.ok();
}
@DeleteMapping("/{id}")
public R remove(@PathVariable String id) {
movieService.removeById(id);
return R.ok();
}
@GetMapping("/{id}")
public R getById(@PathVariable String id) {
Movie movie = movieService.getById(id);
return R.ok().data("item", movie);
}
@GetMapping("/statistics")
public R getStatistics() {
Map<String, Object> statistics = movieService.getMovieStatistics();
return R.ok().data(statistics);
}
}
@RestController
@RequestMapping("/api/visualization")
public class DataVisualizationController {
@Autowired
private MovieService movieService;
@Autowired
private ReviewService reviewService;
@GetMapping("/word-cloud")
public R getWordCloudData() {
List<Map<String, Object>> wordCloudData = movieService.getWordCloudData();
return R.ok().data("wordCloudData", wordCloudData);
}
@GetMapping("/rating-distribution")
public R getRatingDistribution() {
LambdaQueryWrapper<Review> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.groupBy(Review::getRating)
.select(Review::getRating, Review::getRating.count().as("count"));
List<Map<String, Object>> distribution = reviewService.listMaps(queryWrapper);
return R.ok().data("ratingDistribution", distribution);
}
@GetMapping("/rating-by-year")
public R getRatingByYear(@RequestParam(required = false) Integer startYear,
@RequestParam(required = false) Integer endYear) {
LambdaQueryWrapper<Movie> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.ge(startYear != null, Movie::getReleaseYear, startYear)
.le(endYear != null, Movie::getReleaseYear, endYear)
.groupBy(Movie::getReleaseYear)
.select(Movie::getReleaseYear, Movie::getRating.avg().as("avgRating"));
List<Map<String, Object>> ratingByYear = movieService.listMaps(queryWrapper);
return R.ok().data("ratingByYear", ratingByYear);
}
@GetMapping("/genre-distribution")
public R getGenreDistribution() {
LambdaQueryWrapper<Movie> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.groupBy(Movie::getGenre)
.select(Movie::getGenre, Movie::getGenre.count().as("count"));
List<Map<String, Object>> genreDistribution = movieService.listMaps(queryWrapper);
return R.ok().data("genreDistribution", genreDistribution);
}
@GetMapping("/top-rated-movies")
public R getTopRatedMovies(@RequestParam(defaultValue = "10") Integer limit) {
LambdaQueryWrapper<Movie> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.orderByDesc(Movie::getRating)
.last("LIMIT " + limit);
List<Movie> topRatedMovies = movieService.list(queryWrapper);
return R.ok().data("topRatedMovies", topRatedMovies);
}
@GetMapping("/yearly-movie-count")
public R getYearlyMovieCount(@RequestParam(required = false) Integer startYear,
@RequestParam(required = false) Integer endYear) {
LambdaQueryWrapper<Movie> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.ge(startYear != null, Movie::getReleaseYear, startYear)
.le(endYear != null, Movie::getReleaseYear, endYear)
.groupBy(Movie::getReleaseYear)
.select(Movie::getReleaseYear, Movie::getReleaseYear.count().as("count"))
.orderByAsc(Movie::getReleaseYear);
List<Map<String, Object>> yearlyCount = movieService.listMaps(queryWrapper);
return R.ok().data("yearlyMovieCount", yearlyCount);
}
}