feat: 初始化音乐项目后台管理系统- 新增后台管理相关页面和功能组件
- 实现管理员评论管理、数据统计等功能 - 添加后台布局和路由管理 -配置数据库和Redis连接 - 实现文件上传和访问路径配置 - 添加Sa-Token安全配置
This commit is contained in:
@@ -0,0 +1,153 @@
|
||||
package com.test.musichouduan.controller;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaCheckRole;
|
||||
import com.test.musichouduan.dto.BannerDTO;
|
||||
import com.test.musichouduan.dto.PageResult;
|
||||
import com.test.musichouduan.dto.Result;
|
||||
import com.test.musichouduan.service.BannerService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 轮播图控制器
|
||||
*/
|
||||
@Tag(name = "轮播图接口", description = "轮播图的增删改查操作")
|
||||
@RestController
|
||||
@RequestMapping("/api/banner")
|
||||
public class BannerController {
|
||||
|
||||
@Autowired
|
||||
private BannerService bannerService;
|
||||
|
||||
/**
|
||||
* 根据ID获取轮播图
|
||||
*
|
||||
* @param id 轮播图ID
|
||||
* @return 响应结果
|
||||
*/
|
||||
@Operation(summary = "根据ID获取轮播图", description = "根据轮播图ID获取轮播图详情")
|
||||
@GetMapping("/{id}")
|
||||
public Result<BannerDTO> getBannerById(@Parameter(description = "轮播图ID") @PathVariable Long id) {
|
||||
BannerDTO bannerDTO = bannerService.getBannerById(id);
|
||||
return Result.success(bannerDTO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取轮播图列表
|
||||
*
|
||||
* @return 响应结果
|
||||
*/
|
||||
@Operation(summary = "获取轮播图列表", description = "获取所有启用状态的轮播图列表")
|
||||
@GetMapping("/list")
|
||||
public Result<List<BannerDTO>> getBannerList() {
|
||||
List<BannerDTO> bannerList = bannerService.getBannerList();
|
||||
return Result.success(bannerList);
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页获取轮播图列表
|
||||
*
|
||||
* @param page 页码
|
||||
* @param size 每页大小
|
||||
* @return 响应结果
|
||||
*/
|
||||
@Operation(summary = "分页获取轮播图列表", description = "分页获取轮播图列表,需要管理员权限")
|
||||
@SaCheckRole("admin")
|
||||
@GetMapping("/page")
|
||||
public Result<PageResult<BannerDTO>> getBannerPage(
|
||||
@Parameter(description = "页码", example = "1") @RequestParam(defaultValue = "1") Integer page,
|
||||
@Parameter(description = "每页大小", example = "10") @RequestParam(defaultValue = "10") Integer size) {
|
||||
PageResult<BannerDTO> pageResult = bannerService.getBannerPage(page, size);
|
||||
return Result.success(pageResult);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加轮播图
|
||||
*
|
||||
* @param bannerDTO 轮播图DTO
|
||||
* @param image 图片文件
|
||||
* @return 响应结果
|
||||
*/
|
||||
@Operation(summary = "添加轮播图", description = "添加轮播图,需要管理员权限")
|
||||
@SaCheckRole("admin")
|
||||
@PostMapping("/add")
|
||||
public Result<BannerDTO> addBanner(
|
||||
@Parameter(description = "轮播图信息") @RequestPart("banner") BannerDTO bannerDTO,
|
||||
@Parameter(description = "轮播图图片文件") @RequestPart("image") MultipartFile image) {
|
||||
BannerDTO result = bannerService.addBanner(bannerDTO, image);
|
||||
return Result.success("添加成功", result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新轮播图
|
||||
*
|
||||
* @param id 轮播图ID
|
||||
* @param bannerDTO 轮播图DTO
|
||||
* @param image 图片文件
|
||||
* @return 响应结果
|
||||
*/
|
||||
@Operation(summary = "更新轮播图", description = "更新轮播图信息,需要管理员权限")
|
||||
@SaCheckRole("admin")
|
||||
@PutMapping("/{id}")
|
||||
public Result<BannerDTO> updateBanner(
|
||||
@Parameter(description = "轮播图ID") @PathVariable Long id,
|
||||
@Parameter(description = "轮播图信息") @RequestPart("banner") BannerDTO bannerDTO,
|
||||
@Parameter(description = "轮播图图片文件,可选") @RequestPart(value = "image", required = false) MultipartFile image) {
|
||||
BannerDTO result = bannerService.updateBanner(id, bannerDTO, image);
|
||||
return Result.success("更新成功", result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除轮播图
|
||||
*
|
||||
* @param id 轮播图ID
|
||||
* @return 响应结果
|
||||
*/
|
||||
@Operation(summary = "删除轮播图", description = "删除轮播图,需要管理员权限")
|
||||
@SaCheckRole("admin")
|
||||
@DeleteMapping("/{id}")
|
||||
public Result<Boolean> deleteBanner(@Parameter(description = "轮播图ID") @PathVariable Long id) {
|
||||
boolean result = bannerService.deleteBanner(id);
|
||||
return Result.success("删除成功", result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新轮播图状态
|
||||
*
|
||||
* @param id 轮播图ID
|
||||
* @param status 状态
|
||||
* @return 响应结果
|
||||
*/
|
||||
@Operation(summary = "更新轮播图状态", description = "更新轮播图状态,0-禁用,1-启用,需要管理员权限")
|
||||
@SaCheckRole("admin")
|
||||
@PutMapping("/{id}/status")
|
||||
public Result<Boolean> updateBannerStatus(
|
||||
@Parameter(description = "轮播图ID") @PathVariable Long id,
|
||||
@Parameter(description = "状态,0-禁用,1-启用", example = "1") @RequestParam Integer status) {
|
||||
boolean result = bannerService.updateBannerStatus(id, status);
|
||||
return Result.success("更新成功", result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新轮播图排序
|
||||
*
|
||||
* @param id 轮播图ID
|
||||
* @param orderNum 排序号
|
||||
* @return 响应结果
|
||||
*/
|
||||
@Operation(summary = "更新轮播图排序", description = "更新轮播图排序号,数字越小越靠前,需要管理员权限")
|
||||
@SaCheckRole("admin")
|
||||
@PutMapping("/{id}/order")
|
||||
public Result<Boolean> updateBannerOrder(
|
||||
@Parameter(description = "轮播图ID") @PathVariable Long id,
|
||||
@Parameter(description = "排序号,数字越小越靠前", example = "1") @RequestParam Integer orderNum) {
|
||||
boolean result = bannerService.updateBannerOrder(id, orderNum);
|
||||
return Result.success("更新成功", result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.test.musichouduan.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 基本测试控制器
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/basic-test")
|
||||
public class BasicTestController {
|
||||
|
||||
/**
|
||||
* 基本测试接口
|
||||
*/
|
||||
@GetMapping
|
||||
public Map<String, Object> test() {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("code", 200);
|
||||
result.put("message", "基本测试成功");
|
||||
result.put("data", "Hello, Basic Test!");
|
||||
result.put("time", System.currentTimeMillis());
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
package com.test.musichouduan.controller;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaCheckLogin;
|
||||
import cn.dev33.satoken.annotation.SaCheckRole;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import com.test.musichouduan.dto.CommentDTO;
|
||||
import com.test.musichouduan.dto.PageResult;
|
||||
import com.test.musichouduan.dto.Result;
|
||||
import com.test.musichouduan.service.CommentService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
/**
|
||||
* 评论控制器
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/api/comment")
|
||||
public class CommentController {
|
||||
|
||||
@Autowired
|
||||
private CommentService commentService;
|
||||
|
||||
/**
|
||||
* 根据ID获取评论
|
||||
*
|
||||
* @param id 评论ID
|
||||
* @return 响应结果
|
||||
*/
|
||||
@GetMapping("/{id}")
|
||||
public Result<CommentDTO> getCommentById(@PathVariable Long id) {
|
||||
CommentDTO commentDTO = commentService.getCommentById(id);
|
||||
return Result.success(commentDTO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页获取评论列表
|
||||
*
|
||||
* @param targetId 目标ID
|
||||
* @param targetType 目标类型
|
||||
* @param page 页码
|
||||
* @param size 每页大小
|
||||
* @return 响应结果
|
||||
*/
|
||||
@GetMapping("/list")
|
||||
public Result<PageResult<CommentDTO>> getCommentList(
|
||||
@RequestParam Long targetId,
|
||||
@RequestParam Integer targetType,
|
||||
@RequestParam(defaultValue = "1") Integer page,
|
||||
@RequestParam(defaultValue = "10") Integer size) {
|
||||
PageResult<CommentDTO> pageResult = commentService.getCommentList(targetId, targetType, page, size);
|
||||
return Result.success(pageResult);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加评论
|
||||
*
|
||||
* @param commentDTO 评论DTO
|
||||
* @return 响应结果
|
||||
*/
|
||||
@SaCheckLogin
|
||||
@PostMapping("/add")
|
||||
public Result<CommentDTO> addComment(@RequestBody CommentDTO commentDTO) {
|
||||
CommentDTO result = commentService.addComment(commentDTO);
|
||||
return Result.success("评论成功", result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除评论
|
||||
*
|
||||
* @param id 评论ID
|
||||
* @return 响应结果
|
||||
*/
|
||||
@SaCheckLogin
|
||||
@DeleteMapping("/{id}")
|
||||
public Result<Boolean> deleteComment(@PathVariable Long id) {
|
||||
boolean result = commentService.deleteComment(id);
|
||||
return Result.success("删除成功", result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户的评论列表
|
||||
*
|
||||
* @param page 页码
|
||||
* @param size 每页大小
|
||||
* @return 响应结果
|
||||
*/
|
||||
@SaCheckLogin
|
||||
@GetMapping("/user")
|
||||
public Result<PageResult<CommentDTO>> getUserCommentList(
|
||||
@RequestParam(defaultValue = "1") Integer page,
|
||||
@RequestParam(defaultValue = "10") Integer size) {
|
||||
// 获取当前登录用户ID
|
||||
Long userId = StpUtil.getLoginIdAsLong();
|
||||
PageResult<CommentDTO> pageResult = commentService.getUserCommentList(userId, page, size);
|
||||
return Result.success(pageResult);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.test.musichouduan.controller;
|
||||
|
||||
import com.test.musichouduan.dto.Result;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
* 回退控制器
|
||||
* 处理所有未匹配的请求
|
||||
* 使用Order注解指定优先级,数值越大优先级越低
|
||||
*/
|
||||
@RestController
|
||||
@Order(Integer.MAX_VALUE)
|
||||
public class FallbackController {
|
||||
|
||||
/**
|
||||
* 处理所有非API请求
|
||||
*
|
||||
* @param request HTTP请求
|
||||
* @param response HTTP响应
|
||||
* @return 响应结果
|
||||
*/
|
||||
@RequestMapping({"/1", "/2", "/3", "/4", "/5", "/6", "/7", "/8", "/9"})
|
||||
public Result<String> handleFallback(HttpServletRequest request, HttpServletResponse response) {
|
||||
// 设置HTTP状态码为404
|
||||
response.setStatus(HttpStatus.NOT_FOUND.value());
|
||||
|
||||
// 返回错误信息
|
||||
return Result.error(HttpStatus.NOT_FOUND.value(), "资源不存在: " + request.getRequestURI());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package com.test.musichouduan.controller;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaCheckLogin;
|
||||
import com.test.musichouduan.dto.Result;
|
||||
import com.test.musichouduan.service.FileService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
/**
|
||||
* 文件控制器
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/api/file")
|
||||
public class FileController {
|
||||
|
||||
@Autowired
|
||||
private FileService fileService;
|
||||
|
||||
/**
|
||||
* 上传图片
|
||||
*
|
||||
* @param file 图片文件
|
||||
* @return 响应结果
|
||||
*/
|
||||
@SaCheckLogin
|
||||
@PostMapping("/upload/image")
|
||||
public Result<String> uploadImage(@RequestParam("file") MultipartFile file) {
|
||||
String url = fileService.uploadImage(file);
|
||||
return Result.success("上传成功", url);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传音乐
|
||||
*
|
||||
* @param file 音乐文件
|
||||
* @return 响应结果
|
||||
*/
|
||||
@SaCheckLogin
|
||||
@PostMapping("/upload/music")
|
||||
public Result<String> uploadMusic(@RequestParam("file") MultipartFile file) {
|
||||
String url = fileService.uploadMusic(file);
|
||||
return Result.success("上传成功", url);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传歌词
|
||||
*
|
||||
* @param file 歌词文件
|
||||
* @return 响应结果
|
||||
*/
|
||||
@SaCheckLogin
|
||||
@PostMapping("/upload/lyric")
|
||||
public Result<String> uploadLyric(@RequestParam("file") MultipartFile file) {
|
||||
String lyric = fileService.uploadLyric(file);
|
||||
return Result.success("上传成功", lyric);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
*
|
||||
* @param url 文件URL
|
||||
* @return 响应结果
|
||||
*/
|
||||
@SaCheckLogin
|
||||
@DeleteMapping("/delete")
|
||||
public Result<Boolean> deleteFile(@RequestParam String url) {
|
||||
boolean result = fileService.deleteFile(url);
|
||||
return Result.success("删除成功", result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.test.musichouduan.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 简单的Hello控制器
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/api")
|
||||
public class HelloController {
|
||||
|
||||
/**
|
||||
* 欢迎接口
|
||||
*/
|
||||
@GetMapping("/hello")
|
||||
public Map<String, Object> hello() {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("code", 200);
|
||||
result.put("message", "Hello, Music API!");
|
||||
result.put("data", "欢迎使用音乐应用API");
|
||||
result.put("timestamp", System.currentTimeMillis());
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试热部署接口
|
||||
*/
|
||||
@GetMapping("/test")
|
||||
public Map<String, Object> test() {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("code", 200);
|
||||
result.put("message", "测试成功");
|
||||
result.put("data", "这是一个测试接口,可以修改此处来测试热部署");
|
||||
result.put("timestamp", System.currentTimeMillis());
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,216 @@
|
||||
package com.test.musichouduan.controller;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaCheckLogin;
|
||||
import cn.dev33.satoken.annotation.SaCheckRole;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import com.test.musichouduan.dto.MusicDTO;
|
||||
import com.test.musichouduan.dto.PageResult;
|
||||
import com.test.musichouduan.dto.Result;
|
||||
import com.test.musichouduan.service.MusicService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 音乐控制器
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/api/music")
|
||||
public class MusicController {
|
||||
|
||||
@Autowired
|
||||
private MusicService musicService;
|
||||
|
||||
/**
|
||||
* 根据ID获取音乐
|
||||
*
|
||||
* @param id 音乐ID
|
||||
* @return 响应结果
|
||||
*/
|
||||
@GetMapping("/{id}")
|
||||
public Result<MusicDTO> getMusicById(@PathVariable Long id) {
|
||||
MusicDTO musicDTO = musicService.getMusicById(id);
|
||||
if (musicDTO == null) {
|
||||
// 如果音乐不存在,返回空数据而不是错误
|
||||
return Result.success(null);
|
||||
}
|
||||
return Result.success(musicDTO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页获取音乐列表
|
||||
*
|
||||
* @param page 页码
|
||||
* @param size 每页大小
|
||||
* @param keyword 关键字
|
||||
* @param typeId 类型ID
|
||||
* @return 响应结果
|
||||
*/
|
||||
@GetMapping("/list")
|
||||
public Result<PageResult<MusicDTO>> getMusicList(
|
||||
@RequestParam(defaultValue = "1") Integer page,
|
||||
@RequestParam(defaultValue = "10") Integer size,
|
||||
@RequestParam(required = false) String keyword,
|
||||
@RequestParam(required = false) Long typeId) {
|
||||
PageResult<MusicDTO> pageResult = musicService.getMusicList(page, size, keyword, typeId);
|
||||
return Result.success(pageResult);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据歌手ID获取音乐列表
|
||||
*
|
||||
* @param singerId 歌手ID
|
||||
* @param page 页码
|
||||
* @param size 每页大小
|
||||
* @return 响应结果
|
||||
*/
|
||||
@GetMapping("/singer/{singerId}")
|
||||
public Result<PageResult<MusicDTO>> getMusicListBySinger(
|
||||
@PathVariable Long singerId,
|
||||
@RequestParam(defaultValue = "1") Integer page,
|
||||
@RequestParam(defaultValue = "10") Integer size) {
|
||||
PageResult<MusicDTO> pageResult = musicService.getMusicListBySinger(singerId, page, size);
|
||||
return Result.success(pageResult);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取最新音乐列表
|
||||
*
|
||||
* @param limit 限制数量
|
||||
* @return 响应结果
|
||||
*/
|
||||
@GetMapping("/latest")
|
||||
public Result<List<MusicDTO>> getLatestMusic(@RequestParam(defaultValue = "10") Integer limit) {
|
||||
List<MusicDTO> musicList = musicService.getLatestMusic(limit);
|
||||
return Result.success(musicList);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取热门音乐列表
|
||||
*
|
||||
* @param limit 限制数量
|
||||
* @return 响应结果
|
||||
*/
|
||||
@GetMapping("/hot")
|
||||
public Result<List<MusicDTO>> getHotMusic(@RequestParam(defaultValue = "10") Integer limit) {
|
||||
List<MusicDTO> musicList = musicService.getHotMusic(limit);
|
||||
return Result.success(musicList);
|
||||
}
|
||||
|
||||
/**
|
||||
* 搜索音乐
|
||||
*
|
||||
* @param keyword 关键字
|
||||
* @param page 页码
|
||||
* @param size 每页大小
|
||||
* @return 响应结果
|
||||
*/
|
||||
@GetMapping("/search")
|
||||
public Result<PageResult<MusicDTO>> searchMusic(
|
||||
@RequestParam String keyword,
|
||||
@RequestParam(defaultValue = "1") Integer page,
|
||||
@RequestParam(defaultValue = "10") Integer size) {
|
||||
PageResult<MusicDTO> pageResult = musicService.getMusicList(page, size, keyword, null);
|
||||
return Result.success(pageResult);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加音乐
|
||||
*
|
||||
* @param musicDTO 音乐DTO
|
||||
* @param file 音乐文件
|
||||
* @param cover 封面文件
|
||||
* @return 响应结果
|
||||
*/
|
||||
@SaCheckRole("admin")
|
||||
@PostMapping("/add")
|
||||
public Result<MusicDTO> addMusic(
|
||||
@RequestPart("music") MusicDTO musicDTO,
|
||||
@RequestPart(value = "file", required = false) MultipartFile file,
|
||||
@RequestPart(value = "cover", required = false) MultipartFile cover) {
|
||||
MusicDTO result = musicService.addMusic(musicDTO, file, cover);
|
||||
return Result.success("添加成功", result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新音乐
|
||||
*
|
||||
* @param id 音乐ID
|
||||
* @param musicDTO 音乐DTO
|
||||
* @param file 音乐文件
|
||||
* @param cover 封面文件
|
||||
* @return 响应结果
|
||||
*/
|
||||
@SaCheckRole("admin")
|
||||
@PutMapping("/{id}")
|
||||
public Result<MusicDTO> updateMusic(
|
||||
@PathVariable Long id,
|
||||
@RequestPart("music") MusicDTO musicDTO,
|
||||
@RequestPart(value = "file", required = false) MultipartFile file,
|
||||
@RequestPart(value = "cover", required = false) MultipartFile cover,
|
||||
@RequestPart(value = "lyric", required = false) MultipartFile lyric) {
|
||||
MusicDTO result = musicService.updateMusic(id, musicDTO, file, cover, lyric);
|
||||
return Result.success("更新成功", result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除音乐
|
||||
*
|
||||
* @param id 音乐ID
|
||||
* @return 响应结果
|
||||
*/
|
||||
@SaCheckRole("admin")
|
||||
@DeleteMapping("/{id}")
|
||||
public Result<Boolean> deleteMusic(@PathVariable Long id) {
|
||||
boolean result = musicService.deleteMusic(id);
|
||||
return Result.success("删除成功", result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 收藏/取消收藏音乐
|
||||
*
|
||||
* @param id 音乐ID
|
||||
* @return 响应结果
|
||||
*/
|
||||
@SaCheckLogin
|
||||
@PostMapping("/{id}/collect")
|
||||
public Result<Boolean> toggleCollectMusic(@PathVariable Long id) {
|
||||
// 获取当前登录用户ID
|
||||
Long userId = StpUtil.getLoginIdAsLong();
|
||||
boolean isCollected = musicService.toggleCollectMusic(id, userId);
|
||||
String message = isCollected ? "收藏成功" : "取消收藏成功";
|
||||
return Result.success(message, isCollected);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户收藏的音乐列表
|
||||
*
|
||||
* @param page 页码
|
||||
* @param size 每页大小
|
||||
* @return 响应结果
|
||||
*/
|
||||
@SaCheckLogin
|
||||
@GetMapping("/collect")
|
||||
public Result<PageResult<MusicDTO>> getCollectedMusic(
|
||||
@RequestParam(defaultValue = "1") Integer page,
|
||||
@RequestParam(defaultValue = "10") Integer size) {
|
||||
// 获取当前登录用户ID
|
||||
Long userId = StpUtil.getLoginIdAsLong();
|
||||
PageResult<MusicDTO> pageResult = musicService.getCollectedMusic(userId, page, size);
|
||||
return Result.success(pageResult);
|
||||
}
|
||||
|
||||
/**
|
||||
* 播放音乐
|
||||
*
|
||||
* @param id 音乐ID
|
||||
* @return 响应结果
|
||||
*/
|
||||
@PostMapping("/{id}/play")
|
||||
public Result<MusicDTO> playMusic(@PathVariable Long id) {
|
||||
MusicDTO musicDTO = musicService.playMusic(id);
|
||||
return Result.success(musicDTO);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,256 @@
|
||||
package com.test.musichouduan.controller;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaCheckLogin;
|
||||
import cn.dev33.satoken.annotation.SaCheckRole;
|
||||
import com.test.musichouduan.dto.PageResult;
|
||||
import com.test.musichouduan.dto.PlaylistDTO;
|
||||
import com.test.musichouduan.dto.Result;
|
||||
import com.test.musichouduan.service.PlaylistService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 歌单控制器
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/api/playlist")
|
||||
public class PlaylistController {
|
||||
|
||||
@Autowired
|
||||
private PlaylistService playlistService;
|
||||
|
||||
/**
|
||||
* 根据ID获取歌单
|
||||
*
|
||||
* @param id 歌单ID
|
||||
* @return 响应结果
|
||||
*/
|
||||
@GetMapping("/{id}")
|
||||
public Result<PlaylistDTO> getPlaylistById(@PathVariable Long id) {
|
||||
PlaylistDTO playlistDTO = playlistService.getPlaylistById(id);
|
||||
return Result.success(playlistDTO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页获取歌单列表
|
||||
*
|
||||
* @param page 页码
|
||||
* @param size 每页大小
|
||||
* @param keyword 关键字
|
||||
* @param typeId 类型ID
|
||||
* @return 响应结果
|
||||
*/
|
||||
@GetMapping("/list")
|
||||
public Result<PageResult<PlaylistDTO>> getPlaylistList(
|
||||
@RequestParam(defaultValue = "1") Integer page,
|
||||
@RequestParam(defaultValue = "10") Integer size,
|
||||
@RequestParam(required = false) String keyword,
|
||||
@RequestParam(required = false) Long typeId) {
|
||||
PageResult<PlaylistDTO> pageResult = playlistService.getPlaylistList(page, size, keyword, typeId);
|
||||
return Result.success(pageResult);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取最新歌单列表
|
||||
*
|
||||
* @param limit 限制数量
|
||||
* @return 响应结果
|
||||
*/
|
||||
@GetMapping("/latest")
|
||||
public Result<List<PlaylistDTO>> getLatestPlaylist(@RequestParam(defaultValue = "10") Integer limit) {
|
||||
List<PlaylistDTO> playlistList = playlistService.getLatestPlaylist(limit);
|
||||
return Result.success(playlistList);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取热门歌单列表
|
||||
*
|
||||
* @param limit 限制数量
|
||||
* @return 响应结果
|
||||
*/
|
||||
@GetMapping("/hot")
|
||||
public Result<List<PlaylistDTO>> getHotPlaylist(@RequestParam(defaultValue = "10") Integer limit) {
|
||||
List<PlaylistDTO> playlistList = playlistService.getHotPlaylist(limit);
|
||||
return Result.success(playlistList);
|
||||
}
|
||||
|
||||
/**
|
||||
* 搜索歌单
|
||||
*
|
||||
* @param keyword 关键字
|
||||
* @param page 页码
|
||||
* @param size 每页大小
|
||||
* @return 响应结果
|
||||
*/
|
||||
@GetMapping("/search")
|
||||
public Result<PageResult<PlaylistDTO>> searchPlaylist(
|
||||
@RequestParam String keyword,
|
||||
@RequestParam(defaultValue = "1") Integer page,
|
||||
@RequestParam(defaultValue = "10") Integer size) {
|
||||
PageResult<PlaylistDTO> pageResult = playlistService.getPlaylistList(page, size, keyword, null);
|
||||
return Result.success(pageResult);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取官方歌单列表
|
||||
*
|
||||
* @param page 页码
|
||||
* @param size 每页大小
|
||||
* @return 响应结果
|
||||
*/
|
||||
@GetMapping("/official")
|
||||
public Result<PageResult<PlaylistDTO>> getOfficialPlaylist(
|
||||
@RequestParam(defaultValue = "1") Integer page,
|
||||
@RequestParam(defaultValue = "10") Integer size) {
|
||||
PageResult<PlaylistDTO> pageResult = playlistService.getOfficialPlaylist(page, size);
|
||||
return Result.success(pageResult);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加歌单
|
||||
*
|
||||
* @param playlistDTO 歌单DTO
|
||||
* @param cover 封面文件
|
||||
* @return 响应结果
|
||||
*/
|
||||
@SaCheckLogin
|
||||
@PostMapping("/add")
|
||||
public Result<PlaylistDTO> addPlaylist(
|
||||
@RequestPart("playlist") PlaylistDTO playlistDTO,
|
||||
@RequestPart(value = "cover", required = false) MultipartFile cover) {
|
||||
PlaylistDTO result = playlistService.addPlaylist(playlistDTO, cover);
|
||||
return Result.success("添加成功", result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新歌单
|
||||
*
|
||||
* @param id 歌单ID
|
||||
* @param playlistDTO 歌单DTO
|
||||
* @param cover 封面文件
|
||||
* @return 响应结果
|
||||
*/
|
||||
@SaCheckLogin
|
||||
@PutMapping("/{id}")
|
||||
public Result<PlaylistDTO> updatePlaylist(
|
||||
@PathVariable Long id,
|
||||
@RequestPart("playlist") PlaylistDTO playlistDTO,
|
||||
@RequestPart(value = "cover", required = false) MultipartFile cover) {
|
||||
PlaylistDTO result = playlistService.updatePlaylist(id, playlistDTO, cover);
|
||||
return Result.success("更新成功", result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除歌单
|
||||
*
|
||||
* @param id 歌单ID
|
||||
* @return 响应结果
|
||||
*/
|
||||
@SaCheckLogin
|
||||
@DeleteMapping("/{id}")
|
||||
public Result<Boolean> deletePlaylist(@PathVariable Long id) {
|
||||
boolean result = playlistService.deletePlaylist(id);
|
||||
return Result.success("删除成功", result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 收藏歌单
|
||||
*
|
||||
* @param id 歌单ID
|
||||
* @return 响应结果
|
||||
*/
|
||||
@SaCheckLogin
|
||||
@PostMapping("/{id}/collect")
|
||||
public Result<Boolean> collectPlaylist(@PathVariable Long id) {
|
||||
boolean result = playlistService.collectPlaylist(id);
|
||||
return Result.success("收藏成功", result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消收藏歌单
|
||||
*
|
||||
* @param id 歌单ID
|
||||
* @return 响应结果
|
||||
*/
|
||||
@SaCheckLogin
|
||||
@DeleteMapping("/{id}/collect")
|
||||
public Result<Boolean> uncollectPlaylist(@PathVariable Long id) {
|
||||
boolean result = playlistService.uncollectPlaylist(id);
|
||||
return Result.success("取消收藏成功", result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户收藏的歌单列表
|
||||
*
|
||||
* @param page 页码
|
||||
* @param size 每页大小
|
||||
* @return 响应结果
|
||||
*/
|
||||
@SaCheckLogin
|
||||
@GetMapping("/collect")
|
||||
public Result<PageResult<PlaylistDTO>> getCollectedPlaylist(
|
||||
@RequestParam(defaultValue = "1") Integer page,
|
||||
@RequestParam(defaultValue = "10") Integer size) {
|
||||
PageResult<PlaylistDTO> pageResult = playlistService.getCollectedPlaylist(page, size);
|
||||
return Result.success(pageResult);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户创建的歌单列表
|
||||
*
|
||||
* @param page 页码
|
||||
* @param size 每页大小
|
||||
* @return 响应结果
|
||||
*/
|
||||
@SaCheckLogin
|
||||
@GetMapping("/user")
|
||||
public Result<PageResult<PlaylistDTO>> getUserPlaylist(
|
||||
@RequestParam(defaultValue = "1") Integer page,
|
||||
@RequestParam(defaultValue = "10") Integer size) {
|
||||
PageResult<PlaylistDTO> pageResult = playlistService.getUserPlaylist(page, size);
|
||||
return Result.success(pageResult);
|
||||
}
|
||||
|
||||
/**
|
||||
* 向歌单添加音乐
|
||||
*
|
||||
* @param id 歌单ID
|
||||
* @param musicId 音乐ID
|
||||
* @return 响应结果
|
||||
*/
|
||||
@SaCheckLogin
|
||||
@PostMapping("/{id}/music/{musicId}")
|
||||
public Result<Boolean> addMusicToPlaylist(@PathVariable Long id, @PathVariable Long musicId) {
|
||||
boolean result = playlistService.addMusicToPlaylist(id, musicId);
|
||||
return Result.success("添加成功", result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从歌单移除音乐
|
||||
*
|
||||
* @param id 歌单ID
|
||||
* @param musicId 音乐ID
|
||||
* @return 响应结果
|
||||
*/
|
||||
@SaCheckLogin
|
||||
@DeleteMapping("/{id}/music/{musicId}")
|
||||
public Result<Boolean> removeMusicFromPlaylist(@PathVariable Long id, @PathVariable Long musicId) {
|
||||
boolean result = playlistService.removeMusicFromPlaylist(id, musicId);
|
||||
return Result.success("移除成功", result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 播放歌单
|
||||
*
|
||||
* @param id 歌单ID
|
||||
* @return 响应结果
|
||||
*/
|
||||
@PostMapping("/{id}/play")
|
||||
public Result<PlaylistDTO> playPlaylist(@PathVariable Long id) {
|
||||
PlaylistDTO playlistDTO = playlistService.playPlaylist(id);
|
||||
return Result.success(playlistDTO);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,215 @@
|
||||
package com.test.musichouduan.controller;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaCheckLogin;
|
||||
import cn.dev33.satoken.annotation.SaCheckRole;
|
||||
import com.test.musichouduan.dto.PageResult;
|
||||
import com.test.musichouduan.dto.Result;
|
||||
import com.test.musichouduan.dto.SingerDTO;
|
||||
import com.test.musichouduan.service.SingerService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 歌手控制器
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/api/singer")
|
||||
public class SingerController {
|
||||
|
||||
@Autowired
|
||||
private SingerService singerService;
|
||||
|
||||
/**
|
||||
* 根据ID获取歌手
|
||||
*
|
||||
* @param id 歌手ID
|
||||
* @return 响应结果
|
||||
*/
|
||||
@GetMapping("/{id}")
|
||||
public Result<SingerDTO> getSingerById(@PathVariable Long id) {
|
||||
SingerDTO singerDTO = singerService.getSingerById(id);
|
||||
return Result.success(singerDTO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页获取歌手列表
|
||||
*
|
||||
* @param page 页码
|
||||
* @param size 每页大小
|
||||
* @param keyword 关键字
|
||||
* @param typeId 类型ID
|
||||
* @return 响应结果
|
||||
*/
|
||||
@GetMapping("/list")
|
||||
public Result<PageResult<SingerDTO>> getSingerList(
|
||||
@RequestParam(defaultValue = "1") Integer page,
|
||||
@RequestParam(defaultValue = "10") Integer size,
|
||||
@RequestParam(required = false) String keyword,
|
||||
@RequestParam(required = false) Long typeId) {
|
||||
PageResult<SingerDTO> pageResult = singerService.getSingerList(page, size, keyword, typeId);
|
||||
return Result.success(pageResult);
|
||||
}
|
||||
|
||||
/**
|
||||
* 搜索歌手
|
||||
*
|
||||
* @param keyword 关键字
|
||||
* @param page 页码
|
||||
* @param size 每页大小
|
||||
* @return 响应结果
|
||||
*/
|
||||
@GetMapping("/search")
|
||||
public Result<PageResult<SingerDTO>> searchSinger(
|
||||
@RequestParam String keyword,
|
||||
@RequestParam(defaultValue = "1") Integer page,
|
||||
@RequestParam(defaultValue = "10") Integer size) {
|
||||
PageResult<SingerDTO> pageResult = singerService.getSingerList(page, size, keyword, null);
|
||||
return Result.success(pageResult);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取最新歌手列表
|
||||
*
|
||||
* @param limit 限制数量
|
||||
* @return 响应结果
|
||||
*/
|
||||
@GetMapping("/latest")
|
||||
public Result<List<SingerDTO>> getLatestSinger(@RequestParam(defaultValue = "10") Integer limit) {
|
||||
List<SingerDTO> singerList = singerService.getLatestSinger(limit);
|
||||
return Result.success(singerList);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取热门歌手列表
|
||||
*
|
||||
* @param limit 限制数量
|
||||
* @return 响应结果
|
||||
*/
|
||||
@GetMapping("/hot-list")
|
||||
public Result<List<SingerDTO>> getHotSinger(@RequestParam(defaultValue = "10") Integer limit) {
|
||||
List<SingerDTO> singerList = singerService.getHotSinger(limit);
|
||||
return Result.success(singerList);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加歌手
|
||||
*
|
||||
* @param singerDTO 歌手DTO
|
||||
* @param avatar 头像文件
|
||||
* @return 响应结果
|
||||
*/
|
||||
@SaCheckRole("admin")
|
||||
@PostMapping("/add")
|
||||
public Result<SingerDTO> addSinger(
|
||||
@RequestPart("singer") SingerDTO singerDTO,
|
||||
@RequestPart(value = "avatar", required = false) MultipartFile avatar) {
|
||||
SingerDTO result = singerService.addSinger(singerDTO, avatar);
|
||||
return Result.success("添加成功", result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新歌手
|
||||
*
|
||||
* @param id 歌手ID
|
||||
* @param singerDTO 歌手DTO
|
||||
* @param avatar 头像文件
|
||||
* @return 响应结果
|
||||
*/
|
||||
@SaCheckRole("admin")
|
||||
@PutMapping("/{id}")
|
||||
public Result<SingerDTO> updateSinger(
|
||||
@PathVariable Long id,
|
||||
@RequestPart("singer") SingerDTO singerDTO,
|
||||
@RequestPart(value = "avatar", required = false) MultipartFile avatar) {
|
||||
SingerDTO result = singerService.updateSinger(id, singerDTO, avatar);
|
||||
return Result.success("更新成功", result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除歌手
|
||||
*
|
||||
* @param id 歌手ID
|
||||
* @return 响应结果
|
||||
*/
|
||||
@SaCheckRole("admin")
|
||||
@DeleteMapping("/{id}")
|
||||
public Result<Boolean> deleteSinger(@PathVariable Long id) {
|
||||
boolean result = singerService.deleteSinger(id);
|
||||
return Result.success("删除成功", result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 收藏歌手
|
||||
*
|
||||
* @param id 歌手ID
|
||||
* @return 响应结果
|
||||
*/
|
||||
@SaCheckLogin
|
||||
@PostMapping("/{id}/collect")
|
||||
public Result<Boolean> collectSinger(@PathVariable Long id) {
|
||||
boolean result = singerService.collectSinger(id);
|
||||
return Result.success("收藏成功", result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消收藏歌手
|
||||
*
|
||||
* @param id 歌手ID
|
||||
* @return 响应结果
|
||||
*/
|
||||
@SaCheckLogin
|
||||
@DeleteMapping("/{id}/collect")
|
||||
public Result<Boolean> uncollectSinger(@PathVariable Long id) {
|
||||
boolean result = singerService.uncollectSinger(id);
|
||||
return Result.success("取消收藏成功", result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户收藏的歌手列表
|
||||
*
|
||||
* @param page 页码
|
||||
* @param size 每页大小
|
||||
* @return 响应结果
|
||||
*/
|
||||
@SaCheckLogin
|
||||
@GetMapping("/collect")
|
||||
public Result<PageResult<SingerDTO>> getCollectedSinger(
|
||||
@RequestParam(defaultValue = "1") Integer page,
|
||||
@RequestParam(defaultValue = "10") Integer size) {
|
||||
PageResult<SingerDTO> pageResult = singerService.getCollectedSinger(page, size);
|
||||
return Result.success(pageResult);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加音乐到歌手
|
||||
* @param singerId 歌手ID
|
||||
* @param musicId 音乐ID
|
||||
* @return 响应结果
|
||||
*/
|
||||
@SaCheckRole("admin")
|
||||
@PostMapping("/{singerId}/music/{musicId}")
|
||||
public Result<Boolean> addMusicToSinger(
|
||||
@PathVariable Long singerId,
|
||||
@PathVariable Long musicId) {
|
||||
boolean result = singerService.addMusicToSinger(singerId, musicId);
|
||||
return Result.success("添加成功", result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从歌手移除音乐
|
||||
* @param singerId 歌手ID
|
||||
* @param musicId 音乐ID
|
||||
* @return 响应结果
|
||||
*/
|
||||
@SaCheckRole("admin")
|
||||
@DeleteMapping("/{singerId}/music/{musicId}")
|
||||
public Result<Boolean> removeMusicFromSinger(
|
||||
@PathVariable Long singerId,
|
||||
@PathVariable Long musicId) {
|
||||
boolean result = singerService.removeMusicFromSinger(singerId, musicId);
|
||||
return Result.success("移除成功", result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package com.test.musichouduan.controller;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaCheckRole;
|
||||
import com.test.musichouduan.dto.Result;
|
||||
import com.test.musichouduan.service.StatService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 统计控制器
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/api/stat")
|
||||
@SaCheckRole("admin")
|
||||
public class StatController {
|
||||
|
||||
@Autowired
|
||||
private StatService statService;
|
||||
|
||||
/**
|
||||
* 获取总体统计数据
|
||||
*
|
||||
* @return 响应结果
|
||||
*/
|
||||
@GetMapping("/total")
|
||||
public Result<Map<String, Object>> getTotalStats() {
|
||||
Map<String, Object> stats = statService.getTotalStats();
|
||||
return Result.success(stats);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户增长趋势
|
||||
*
|
||||
* @param days 天数
|
||||
* @return 响应结果
|
||||
*/
|
||||
@GetMapping("/user/growth")
|
||||
public Result<Map<String, Object>> getUserGrowthTrend(@RequestParam(defaultValue = "7") Integer days) {
|
||||
Map<String, Object> stats = statService.getUserGrowthTrend(days);
|
||||
return Result.success(stats);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取音乐类型分布
|
||||
*
|
||||
* @return 响应结果
|
||||
*/
|
||||
@GetMapping("/music/type")
|
||||
public Result<Map<String, Object>> getMusicTypeDistribution() {
|
||||
Map<String, Object> stats = statService.getMusicTypeDistribution();
|
||||
return Result.success(stats);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取播放量统计
|
||||
*
|
||||
* @param days 天数
|
||||
* @return 响应结果
|
||||
*/
|
||||
@GetMapping("/play/count")
|
||||
public Result<Map<String, Object>> getPlayCountStats(@RequestParam(defaultValue = "7") Integer days) {
|
||||
Map<String, Object> stats = statService.getPlayCountStats(days);
|
||||
return Result.success(stats);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取最新用户列表
|
||||
*
|
||||
* @param limit 限制数量
|
||||
* @return 响应结果
|
||||
*/
|
||||
@GetMapping("/user/latest")
|
||||
public Result<Map<String, Object>> getLatestUsers(@RequestParam(defaultValue = "5") Integer limit) {
|
||||
Map<String, Object> stats = statService.getLatestUsers(limit);
|
||||
return Result.success(stats);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取最新音乐列表
|
||||
*
|
||||
* @param limit 限制数量
|
||||
* @return 响应结果
|
||||
*/
|
||||
@GetMapping("/music/latest")
|
||||
public Result<Map<String, Object>> getLatestMusics(@RequestParam(defaultValue = "5") Integer limit) {
|
||||
Map<String, Object> stats = statService.getLatestMusics(limit);
|
||||
return Result.success(stats);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,227 @@
|
||||
package com.test.musichouduan.controller;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaCheckRole;
|
||||
import com.test.musichouduan.dto.*;
|
||||
import com.test.musichouduan.service.TypeService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 类型控制器
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/api/type")
|
||||
public class TypeController {
|
||||
|
||||
@Autowired
|
||||
private TypeService typeService;
|
||||
|
||||
/**
|
||||
* 获取音乐类型列表
|
||||
*
|
||||
* @return 响应结果
|
||||
*/
|
||||
@GetMapping("/music")
|
||||
public Result<List<MusicTypeDTO>> getMusicTypeList() {
|
||||
List<MusicTypeDTO> typeList = typeService.getMusicTypeList();
|
||||
return Result.success(typeList);
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页获取音乐类型列表
|
||||
*
|
||||
* @param page 页码
|
||||
* @param size 每页大小
|
||||
* @param keyword 关键字
|
||||
* @return 响应结果
|
||||
*/
|
||||
@SaCheckRole("admin")
|
||||
@GetMapping("/music/page")
|
||||
public Result<PageResult<MusicTypeDTO>> getMusicTypePage(
|
||||
@RequestParam(defaultValue = "1") Integer page,
|
||||
@RequestParam(defaultValue = "10") Integer size,
|
||||
@RequestParam(required = false) String keyword) {
|
||||
PageResult<MusicTypeDTO> pageResult = typeService.getMusicTypePage(page, size, keyword);
|
||||
return Result.success(pageResult);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加音乐类型
|
||||
*
|
||||
* @param musicTypeDTO 音乐类型DTO
|
||||
* @return 响应结果
|
||||
*/
|
||||
@SaCheckRole("admin")
|
||||
@PostMapping("/music/add")
|
||||
public Result<MusicTypeDTO> addMusicType(@RequestBody MusicTypeDTO musicTypeDTO) {
|
||||
MusicTypeDTO result = typeService.addMusicType(musicTypeDTO);
|
||||
return Result.success("添加成功", result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新音乐类型
|
||||
*
|
||||
* @param id 音乐类型ID
|
||||
* @param musicTypeDTO 音乐类型DTO
|
||||
* @return 响应结果
|
||||
*/
|
||||
@SaCheckRole("admin")
|
||||
@PutMapping("/music/{id}")
|
||||
public Result<MusicTypeDTO> updateMusicType(@PathVariable Long id, @RequestBody MusicTypeDTO musicTypeDTO) {
|
||||
MusicTypeDTO result = typeService.updateMusicType(id, musicTypeDTO);
|
||||
return Result.success("更新成功", result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除音乐类型
|
||||
*
|
||||
* @param id 音乐类型ID
|
||||
* @return 响应结果
|
||||
*/
|
||||
@SaCheckRole("admin")
|
||||
@DeleteMapping("/music/{id}")
|
||||
public Result<Boolean> deleteMusicType(@PathVariable Long id) {
|
||||
boolean result = typeService.deleteMusicType(id);
|
||||
return Result.success("删除成功", result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取歌手类型列表
|
||||
*
|
||||
* @return 响应结果
|
||||
*/
|
||||
@GetMapping("/singer")
|
||||
public Result<List<SingerTypeDTO>> getSingerTypeList() {
|
||||
List<SingerTypeDTO> typeList = typeService.getSingerTypeList();
|
||||
return Result.success(typeList);
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页获取歌手类型列表
|
||||
*
|
||||
* @param page 页码
|
||||
* @param size 每页大小
|
||||
* @param keyword 关键字
|
||||
* @return 响应结果
|
||||
*/
|
||||
@SaCheckRole("admin")
|
||||
@GetMapping("/singer/page")
|
||||
public Result<PageResult<SingerTypeDTO>> getSingerTypePage(
|
||||
@RequestParam(defaultValue = "1") Integer page,
|
||||
@RequestParam(defaultValue = "10") Integer size,
|
||||
@RequestParam(required = false) String keyword) {
|
||||
PageResult<SingerTypeDTO> pageResult = typeService.getSingerTypePage(page, size, keyword);
|
||||
return Result.success(pageResult);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加歌手类型
|
||||
*
|
||||
* @param singerTypeDTO 歌手类型DTO
|
||||
* @return 响应结果
|
||||
*/
|
||||
@SaCheckRole("admin")
|
||||
@PostMapping("/singer/add")
|
||||
public Result<SingerTypeDTO> addSingerType(@RequestBody SingerTypeDTO singerTypeDTO) {
|
||||
SingerTypeDTO result = typeService.addSingerType(singerTypeDTO);
|
||||
return Result.success("添加成功", result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新歌手类型
|
||||
*
|
||||
* @param id 歌手类型ID
|
||||
* @param singerTypeDTO 歌手类型DTO
|
||||
* @return 响应结果
|
||||
*/
|
||||
@SaCheckRole("admin")
|
||||
@PutMapping("/singer/{id}")
|
||||
public Result<SingerTypeDTO> updateSingerType(@PathVariable Long id, @RequestBody SingerTypeDTO singerTypeDTO) {
|
||||
SingerTypeDTO result = typeService.updateSingerType(id, singerTypeDTO);
|
||||
return Result.success("更新成功", result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除歌手类型
|
||||
*
|
||||
* @param id 歌手类型ID
|
||||
* @return 响应结果
|
||||
*/
|
||||
@SaCheckRole("admin")
|
||||
@DeleteMapping("/singer/{id}")
|
||||
public Result<Boolean> deleteSingerType(@PathVariable Long id) {
|
||||
boolean result = typeService.deleteSingerType(id);
|
||||
return Result.success("删除成功", result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取歌单类型列表
|
||||
*
|
||||
* @return 响应结果
|
||||
*/
|
||||
@GetMapping("/playlist")
|
||||
public Result<List<PlaylistTypeDTO>> getPlaylistTypeList() {
|
||||
List<PlaylistTypeDTO> typeList = typeService.getPlaylistTypeList();
|
||||
return Result.success(typeList);
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页获取歌单类型列表
|
||||
*
|
||||
* @param page 页码
|
||||
* @param size 每页大小
|
||||
* @param keyword 关键字
|
||||
* @return 响应结果
|
||||
*/
|
||||
@SaCheckRole("admin")
|
||||
@GetMapping("/playlist/page")
|
||||
public Result<PageResult<PlaylistTypeDTO>> getPlaylistTypePage(
|
||||
@RequestParam(defaultValue = "1") Integer page,
|
||||
@RequestParam(defaultValue = "10") Integer size,
|
||||
@RequestParam(required = false) String keyword) {
|
||||
PageResult<PlaylistTypeDTO> pageResult = typeService.getPlaylistTypePage(page, size, keyword);
|
||||
return Result.success(pageResult);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加歌单类型
|
||||
*
|
||||
* @param playlistTypeDTO 歌单类型DTO
|
||||
* @return 响应结果
|
||||
*/
|
||||
@SaCheckRole("admin")
|
||||
@PostMapping("/playlist/add")
|
||||
public Result<PlaylistTypeDTO> addPlaylistType(@RequestBody PlaylistTypeDTO playlistTypeDTO) {
|
||||
PlaylistTypeDTO result = typeService.addPlaylistType(playlistTypeDTO);
|
||||
return Result.success("添加成功", result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新歌单类型
|
||||
*
|
||||
* @param id 歌单类型ID
|
||||
* @param playlistTypeDTO 歌单类型DTO
|
||||
* @return 响应结果
|
||||
*/
|
||||
@SaCheckRole("admin")
|
||||
@PutMapping("/playlist/{id}")
|
||||
public Result<PlaylistTypeDTO> updatePlaylistType(@PathVariable Long id, @RequestBody PlaylistTypeDTO playlistTypeDTO) {
|
||||
PlaylistTypeDTO result = typeService.updatePlaylistType(id, playlistTypeDTO);
|
||||
return Result.success("更新成功", result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除歌单类型
|
||||
*
|
||||
* @param id 歌单类型ID
|
||||
* @return 响应结果
|
||||
*/
|
||||
@SaCheckRole("admin")
|
||||
@DeleteMapping("/playlist/{id}")
|
||||
public Result<Boolean> deletePlaylistType(@PathVariable Long id) {
|
||||
boolean result = typeService.deletePlaylistType(id);
|
||||
return Result.success("删除成功", result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,227 @@
|
||||
package com.test.musichouduan.controller;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaCheckLogin;
|
||||
import cn.dev33.satoken.annotation.SaCheckRole;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import com.test.musichouduan.dto.*;
|
||||
import com.test.musichouduan.service.FileService;
|
||||
import com.test.musichouduan.service.UserService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
/**
|
||||
* 用户控制器
|
||||
*/
|
||||
@Tag(name = "用户接口", description = "用户的注册、登录、信息管理等操作")
|
||||
@RestController
|
||||
@RequestMapping("/api/user")
|
||||
public class UserController {
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@Autowired
|
||||
private FileService fileService;
|
||||
|
||||
/**
|
||||
* 用户注册
|
||||
*
|
||||
* @param request 注册请求
|
||||
* @return 响应结果
|
||||
*/
|
||||
@Operation(summary = "用户注册", description = "新用户注册接口")
|
||||
@PostMapping("/register")
|
||||
public Result<UserDTO> register(@Parameter(description = "注册信息") @Validated @RequestBody RegisterRequest request) {
|
||||
UserDTO userDTO = userService.register(request);
|
||||
return Result.success("注册成功", userDTO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户登录
|
||||
*
|
||||
* @param request 登录请求
|
||||
* @return 响应结果
|
||||
*/
|
||||
@Operation(summary = "用户登录", description = "用户登录接口,登录成功后返回用户信息和令牌")
|
||||
@PostMapping("/login")
|
||||
public Result<LoginResultDTO> login(@Parameter(description = "登录信息") @Validated @RequestBody LoginRequest request) {
|
||||
LoginResultDTO resultDTO = userService.login(request);
|
||||
return Result.success("登录成功", resultDTO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户登出
|
||||
*
|
||||
* @return 响应结果
|
||||
*/
|
||||
@Operation(summary = "用户登出", description = "用户登出接口,需要登录状态")
|
||||
@SaCheckLogin
|
||||
@PostMapping("/logout")
|
||||
public Result<String> logout() {
|
||||
StpUtil.logout();
|
||||
return Result.success("登出成功", null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前登录用户
|
||||
*
|
||||
* @return 响应结果
|
||||
*/
|
||||
@Operation(summary = "获取当前登录用户", description = "获取当前登录用户的信息,需要登录状态")
|
||||
@SaCheckLogin
|
||||
@GetMapping("/current")
|
||||
public Result<UserDTO> getCurrentUser() {
|
||||
UserDTO userDTO = userService.getCurrentUser();
|
||||
return Result.success(userDTO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新用户信息
|
||||
*
|
||||
* @param request 用户信息更新请求
|
||||
* @return 响应结果
|
||||
*/
|
||||
@Operation(summary = "更新用户信息", description = "更新当前登录用户的信息,需要登录状态")
|
||||
@SaCheckLogin
|
||||
@PutMapping("/update")
|
||||
public Result<UserDTO> updateUser(@Parameter(description = "用户信息更新请求") @Validated @RequestBody UserUpdateRequest request) {
|
||||
UserDTO userDTO = userService.updateUser(request);
|
||||
return Result.success("更新成功", userDTO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改密码
|
||||
*
|
||||
* @param request 修改密码请求
|
||||
* @return 响应结果
|
||||
*/
|
||||
@Operation(summary = "修改密码", description = "修改当前登录用户的密码,需要登录状态")
|
||||
@SaCheckLogin
|
||||
@PutMapping("/password")
|
||||
public Result<Boolean> updatePassword(@Parameter(description = "密码修改请求") @Validated @RequestBody PasswordUpdateRequest request) {
|
||||
boolean result = userService.updatePassword(request);
|
||||
return Result.success("修改成功", result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户统计数据
|
||||
*
|
||||
* @return 响应结果
|
||||
*/
|
||||
@Operation(summary = "获取用户统计数据", description = "获取当前登录用户的统计数据,包括收藏数量等,需要登录状态")
|
||||
@SaCheckLogin
|
||||
@GetMapping("/stats")
|
||||
public Result<UserDTO> getUserStats() {
|
||||
UserDTO userDTO = userService.getUserStats(StpUtil.getLoginIdAsLong());
|
||||
return Result.success(userDTO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页获取用户列表
|
||||
*
|
||||
* @param page 页码
|
||||
* @param size 每页大小
|
||||
* @param keyword 关键字
|
||||
* @return 响应结果
|
||||
*/
|
||||
@Operation(summary = "分页获取用户列表", description = "分页获取用户列表,支持关键字搜索,需要管理员权限")
|
||||
@SaCheckRole("admin")
|
||||
@GetMapping("/list")
|
||||
public Result<PageResult<UserDTO>> getUserList(
|
||||
@Parameter(description = "页码", example = "1") @RequestParam(defaultValue = "1") Integer page,
|
||||
@Parameter(description = "每页大小", example = "10") @RequestParam(defaultValue = "10") Integer size,
|
||||
@Parameter(description = "搜索关键字", example = "用户名") @RequestParam(required = false) String keyword) {
|
||||
PageResult<UserDTO> pageResult = userService.getUserList(page, size, keyword);
|
||||
return Result.success(pageResult);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据ID获取用户
|
||||
*
|
||||
* @param id 用户ID
|
||||
* @return 响应结果
|
||||
*/
|
||||
@Operation(summary = "根据ID获取用户", description = "根据用户ID获取用户信息,需要管理员权限")
|
||||
@SaCheckRole("admin")
|
||||
@GetMapping("/{id}")
|
||||
public Result<UserDTO> getUserById(@Parameter(description = "用户ID", example = "1") @PathVariable Long id) {
|
||||
UserDTO userDTO = userService.getUserById(id);
|
||||
return Result.success(userDTO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除用户
|
||||
*
|
||||
* @param id 用户ID
|
||||
* @return 响应结果
|
||||
*/
|
||||
@Operation(summary = "删除用户", description = "根据用户ID删除用户,需要管理员权限")
|
||||
@SaCheckRole("admin")
|
||||
@DeleteMapping("/{id}")
|
||||
public Result<Boolean> deleteUser(@Parameter(description = "用户ID", example = "1") @PathVariable Long id) {
|
||||
boolean result = userService.deleteUser(id);
|
||||
return Result.success("删除成功", result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新用户状态
|
||||
*
|
||||
* @param id 用户ID
|
||||
* @param status 状态
|
||||
* @return 响应结果
|
||||
*/
|
||||
@Operation(summary = "更新用户状态", description = "更新用户状态,0-禁用,1-启用,需要管理员权限")
|
||||
@SaCheckRole("admin")
|
||||
@PutMapping("/{id}/status")
|
||||
public Result<Boolean> updateUserStatus(
|
||||
@Parameter(description = "用户ID", example = "1") @PathVariable Long id,
|
||||
@Parameter(description = "状态,0-禁用,1-启用", example = "1") @RequestParam Integer status) {
|
||||
boolean result = userService.updateUserStatus(id, status);
|
||||
return Result.success("更新成功", result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 管理员更新用户信息
|
||||
*
|
||||
* @param id 用户ID
|
||||
* @param request 用户信息更新请求
|
||||
* @return 响应结果
|
||||
*/
|
||||
@Operation(summary = "管理员更新用户信息", description = "管理员更新用户信息,需要管理员权限")
|
||||
@SaCheckRole("admin")
|
||||
@PutMapping("/{id}")
|
||||
public Result<UserDTO> adminUpdateUser(
|
||||
@Parameter(description = "用户ID", example = "1") @PathVariable Long id,
|
||||
@Parameter(description = "用户信息更新请求") @Validated @RequestBody UserUpdateRequest request) {
|
||||
UserDTO userDTO = userService.adminUpdateUser(id, request);
|
||||
return Result.success("更新成功", userDTO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传用户头像
|
||||
*
|
||||
* @param file 头像文件
|
||||
* @return 响应结果
|
||||
*/
|
||||
@Operation(summary = "上传用户头像", description = "上传当前登录用户的头像,需要登录状态")
|
||||
@SaCheckLogin
|
||||
@PostMapping("/upload/avatar")
|
||||
public Result<String> uploadAvatar(@Parameter(description = "头像文件") @RequestParam("file") MultipartFile file) {
|
||||
// 上传头像
|
||||
String avatarUrl = fileService.uploadImage(file);
|
||||
|
||||
// 用户ID已经通过 SaCheckLogin 注解验证
|
||||
|
||||
// 更新用户头像
|
||||
UserUpdateRequest request = new UserUpdateRequest();
|
||||
request.setAvatar(avatarUrl);
|
||||
userService.updateUser(request);
|
||||
|
||||
return Result.success("上传成功", avatarUrl);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package com.test.musichouduan.controller.admin;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaCheckRole;
|
||||
import com.test.musichouduan.dto.CommentDTO;
|
||||
import com.test.musichouduan.dto.PageResult;
|
||||
import com.test.musichouduan.dto.Result;
|
||||
import com.test.musichouduan.service.CommentService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
/**
|
||||
* 管理员评论控制器
|
||||
*/
|
||||
@Tag(name = "管理员评论接口", description = "提供管理员评论管理功能")
|
||||
@RestController
|
||||
@RequestMapping("/api/admin/comment")
|
||||
@SaCheckRole("admin")
|
||||
public class AdminCommentController {
|
||||
|
||||
@Autowired
|
||||
private CommentService commentService;
|
||||
|
||||
/**
|
||||
* 分页获取所有评论
|
||||
*
|
||||
* @param page 页码
|
||||
* @param size 每页大小
|
||||
* @param keyword 关键字
|
||||
* @param targetType 目标类型
|
||||
* @param status 状态
|
||||
* @return 响应结果
|
||||
*/
|
||||
@Operation(summary = "分页获取所有评论", description = "管理员分页获取所有评论")
|
||||
@GetMapping("/list")
|
||||
public Result<PageResult<CommentDTO>> getCommentList(
|
||||
@Parameter(description = "页码", example = "1") @RequestParam(defaultValue = "1") Integer page,
|
||||
@Parameter(description = "每页大小", example = "10") @RequestParam(defaultValue = "10") Integer size,
|
||||
@Parameter(description = "关键字") @RequestParam(required = false) String keyword,
|
||||
@Parameter(description = "目标类型:1-音乐,2-歌单,3-歌手") @RequestParam(required = false) Integer targetType,
|
||||
@Parameter(description = "状态:0-禁用,1-正常") @RequestParam(required = false) Integer status) {
|
||||
PageResult<CommentDTO> pageResult = commentService.getAdminCommentList(page, size, keyword, targetType, status);
|
||||
return Result.success(pageResult);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新评论状态
|
||||
*
|
||||
* @param id 评论ID
|
||||
* @param status 状态:0-禁用,1-正常
|
||||
* @return 响应结果
|
||||
*/
|
||||
@Operation(summary = "更新评论状态", description = "管理员更新评论状态")
|
||||
@PutMapping("/{id}/status")
|
||||
public Result<Boolean> updateCommentStatus(
|
||||
@Parameter(description = "评论ID") @PathVariable Long id,
|
||||
@Parameter(description = "状态:0-禁用,1-正常", example = "1") @RequestParam Integer status) {
|
||||
boolean result = commentService.updateCommentStatus(id, status);
|
||||
return Result.success("更新成功", result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除评论
|
||||
*
|
||||
* @param id 评论ID
|
||||
* @return 响应结果
|
||||
*/
|
||||
@Operation(summary = "删除评论", description = "管理员删除评论")
|
||||
@DeleteMapping("/{id}")
|
||||
public Result<Boolean> deleteComment(@Parameter(description = "评论ID") @PathVariable Long id) {
|
||||
boolean result = commentService.adminDeleteComment(id);
|
||||
return Result.success("删除成功", result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,240 @@
|
||||
package com.test.musichouduan.controller.admin;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaCheckRole;
|
||||
import com.test.musichouduan.dto.Result;
|
||||
import com.test.musichouduan.dto.admin.DashboardDataDTO;
|
||||
import com.test.musichouduan.dto.admin.MusicAdminDTO;
|
||||
import com.test.musichouduan.dto.admin.StatItemDTO;
|
||||
import com.test.musichouduan.dto.admin.UserAdminDTO;
|
||||
import com.test.musichouduan.entity.Music;
|
||||
import com.test.musichouduan.entity.PlaylistType;
|
||||
import com.test.musichouduan.entity.User;
|
||||
import com.test.musichouduan.repository.MusicRepository;
|
||||
import com.test.musichouduan.repository.PlayHistoryRepository;
|
||||
import com.test.musichouduan.repository.PlaylistRepository;
|
||||
import com.test.musichouduan.repository.SingerRepository;
|
||||
import com.test.musichouduan.repository.UserRepository;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 后台管理控制台控制器
|
||||
*/
|
||||
@Tag(name = "后台管理控制台接口", description = "提供后台管理控制台所需的统计数据")
|
||||
@RestController
|
||||
@RequestMapping("/api/admin/dashboard")
|
||||
@SaCheckRole("admin")
|
||||
public class AdminDashboardController {
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
@Autowired
|
||||
private MusicRepository musicRepository;
|
||||
|
||||
@Autowired
|
||||
private PlaylistRepository playlistRepository;
|
||||
|
||||
@Autowired
|
||||
private SingerRepository singerRepository;
|
||||
|
||||
@Autowired
|
||||
private PlayHistoryRepository playHistoryRepository;
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager entityManager;
|
||||
|
||||
/**
|
||||
* 获取控制台统计数据
|
||||
*
|
||||
* @return 统计数据
|
||||
*/
|
||||
@Operation(summary = "获取控制台统计数据", description = "获取用户数量、音乐数量、歌单数量、歌手数量等统计数据")
|
||||
@GetMapping("/stats")
|
||||
public Result<DashboardDataDTO> getStats() {
|
||||
try {
|
||||
DashboardDataDTO data = new DashboardDataDTO();
|
||||
|
||||
// 统计基础数据
|
||||
long userCount = userRepository.count();
|
||||
long musicCount = musicRepository.count();
|
||||
long playlistCount = playlistRepository.count();
|
||||
long singerCount = singerRepository.count();
|
||||
long playCount = musicRepository.sumPlayCount();
|
||||
|
||||
data.setUserCount(userCount);
|
||||
data.setMusicCount(musicCount);
|
||||
data.setPlaylistCount(playlistCount);
|
||||
data.setSingerCount(singerCount);
|
||||
data.setPlayCount(playCount);
|
||||
|
||||
// 获取最近7天的用户增长数据
|
||||
data.setUserGrowth(getUserGrowthData());
|
||||
|
||||
// 获取音乐类型分布数据
|
||||
data.setMusicTypeDistribution(getTypeDistribution());
|
||||
|
||||
// 获取最近7天的播放量数据
|
||||
data.setPlayCountData(getPlayCountData());
|
||||
|
||||
// 获取最新用户
|
||||
List<User> users = userRepository.findLatestUsers(PageRequest.of(0, 5));
|
||||
List<UserAdminDTO> latestUsers = users.stream()
|
||||
.map(UserAdminDTO::fromEntity)
|
||||
.collect(Collectors.toList());
|
||||
data.setLatestUsers(latestUsers);
|
||||
|
||||
// 获取最新音乐
|
||||
List<Music> musics = musicRepository.findLatest(PageRequest.of(0, 5));
|
||||
List<MusicAdminDTO> latestMusics = musics.stream()
|
||||
.map(MusicAdminDTO::fromEntity)
|
||||
.collect(Collectors.toList());
|
||||
data.setLatestMusics(latestMusics);
|
||||
|
||||
return Result.success(data);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return Result.error("获取控制台统计数据失败");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取最近7天的用户增长数据
|
||||
*
|
||||
* @return 用户增长数据
|
||||
*/
|
||||
private List<StatItemDTO> getUserGrowthData() {
|
||||
List<StatItemDTO> result = new ArrayList<>();
|
||||
LocalDate today = LocalDate.now();
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM-dd");
|
||||
|
||||
// 获取最近7天的日期
|
||||
for (int i = 6; i >= 0; i--) {
|
||||
LocalDate date = today.minusDays(i);
|
||||
LocalDateTime startOfDay = date.atStartOfDay();
|
||||
LocalDateTime endOfDay = date.plusDays(1).atStartOfDay().minusNanos(1);
|
||||
|
||||
// 统计当天注册的用户数量
|
||||
long count = userRepository.countByCreateTimeBetween(startOfDay, endOfDay);
|
||||
|
||||
StatItemDTO item = new StatItemDTO();
|
||||
item.setName(date.format(formatter));
|
||||
item.setValue(count);
|
||||
result.add(item);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取音乐、歌单、歌手类型分布数据
|
||||
*
|
||||
* @return 类型分布数据
|
||||
*/
|
||||
private List<StatItemDTO> getTypeDistribution() {
|
||||
List<StatItemDTO> result = new ArrayList<>();
|
||||
|
||||
// 音乐类型分布
|
||||
StatItemDTO musicTypeStat = new StatItemDTO();
|
||||
musicTypeStat.setName("音乐类型");
|
||||
|
||||
try {
|
||||
List<Object[]> musicTypeData = musicRepository.countByMusicType();
|
||||
List<StatItemDTO> musicTypeChildren = musicTypeData.stream().map(item -> {
|
||||
StatItemDTO child = new StatItemDTO();
|
||||
child.setName((String) item[0]);
|
||||
child.setValue(((Number) item[1]).longValue());
|
||||
return child;
|
||||
}).collect(Collectors.toList());
|
||||
musicTypeStat.setChildren(musicTypeChildren);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
// 如果查询失败,返回空列表
|
||||
musicTypeStat.setChildren(new ArrayList<>());
|
||||
}
|
||||
|
||||
result.add(musicTypeStat);
|
||||
|
||||
// 歌单类型分布
|
||||
StatItemDTO playlistTypeStat = new StatItemDTO();
|
||||
playlistTypeStat.setName("歌单类型");
|
||||
|
||||
// 使用原生 SQL 查询或者手动聚合数据
|
||||
List<PlaylistType> playlistTypes = entityManager.createQuery("SELECT DISTINCT pt FROM PlaylistType pt", PlaylistType.class).getResultList();
|
||||
List<StatItemDTO> playlistTypeChildren = new ArrayList<>();
|
||||
|
||||
for (PlaylistType type : playlistTypes) {
|
||||
Long count = playlistRepository.countByType(type);
|
||||
StatItemDTO child = new StatItemDTO();
|
||||
child.setName(type.getName());
|
||||
child.setValue(count);
|
||||
playlistTypeChildren.add(child);
|
||||
}
|
||||
|
||||
playlistTypeStat.setChildren(playlistTypeChildren);
|
||||
result.add(playlistTypeStat);
|
||||
|
||||
// 歌手类型分布
|
||||
StatItemDTO singerTypeStat = new StatItemDTO();
|
||||
singerTypeStat.setName("歌手类型");
|
||||
|
||||
// 使用原生 SQL 查询或者手动聚合数据
|
||||
List<com.test.musichouduan.entity.SingerType> singerTypes = entityManager.createQuery("SELECT DISTINCT st FROM SingerType st", com.test.musichouduan.entity.SingerType.class).getResultList();
|
||||
List<StatItemDTO> singerTypeChildren = new ArrayList<>();
|
||||
|
||||
for (com.test.musichouduan.entity.SingerType type : singerTypes) {
|
||||
Long count = singerRepository.countByType(type);
|
||||
StatItemDTO child = new StatItemDTO();
|
||||
child.setName(type.getName());
|
||||
child.setValue(count);
|
||||
singerTypeChildren.add(child);
|
||||
}
|
||||
|
||||
singerTypeStat.setChildren(singerTypeChildren);
|
||||
result.add(singerTypeStat);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取最近7天的播放量数据
|
||||
*
|
||||
* @return 播放量数据
|
||||
*/
|
||||
private List<StatItemDTO> getPlayCountData() {
|
||||
List<StatItemDTO> result = new ArrayList<>();
|
||||
LocalDate today = LocalDate.now();
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM-dd");
|
||||
|
||||
// 获取最近7天的日期
|
||||
for (int i = 6; i >= 0; i--) {
|
||||
LocalDate date = today.minusDays(i);
|
||||
LocalDateTime startOfDay = date.atStartOfDay();
|
||||
LocalDateTime endOfDay = date.plusDays(1).atStartOfDay().minusNanos(1);
|
||||
|
||||
// 统计当天的播放量
|
||||
long count = playHistoryRepository.countByPlayTimeBetween(startOfDay, endOfDay);
|
||||
|
||||
StatItemDTO item = new StatItemDTO();
|
||||
item.setName(date.format(formatter));
|
||||
item.setValue(count);
|
||||
result.add(item);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user