generated from Java-2025Fall/final-vibevault-template
更新 src/main/java/com/vibevault/controller/PlaylistController.java
This commit is contained in:
parent
80bdeefa76
commit
633d89272b
@ -12,7 +12,7 @@ import java.util.List;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 歌单 REST 控制器
|
* 歌单 REST 控制器
|
||||||
*
|
*
|
||||||
* 需要实现以下端点:
|
* 需要实现以下端点:
|
||||||
* - GET /api/playlists - 获取所有歌单(公开)
|
* - GET /api/playlists - 获取所有歌单(公开)
|
||||||
* - GET /api/playlists/{id} - 获取指定歌单(公开)
|
* - GET /api/playlists/{id} - 获取指定歌单(公开)
|
||||||
@ -20,11 +20,11 @@ import java.util.List;
|
|||||||
* - POST /api/playlists/{id}/songs - 添加歌曲(需认证)
|
* - POST /api/playlists/{id}/songs - 添加歌曲(需认证)
|
||||||
* - DELETE /api/playlists/{playlistId}/songs/{songId} - 移除歌曲(需认证)
|
* - DELETE /api/playlists/{playlistId}/songs/{songId} - 移除歌曲(需认证)
|
||||||
* - DELETE /api/playlists/{id} - 删除歌单(需认证)
|
* - DELETE /api/playlists/{id} - 删除歌单(需认证)
|
||||||
*
|
*
|
||||||
* [Advanced] 额外端点:
|
* [Advanced] 额外端点:
|
||||||
* - GET /api/playlists/search?keyword=xxx - 搜索歌单
|
* - GET /api/playlists/search?keyword=xxx - 搜索歌单
|
||||||
* - POST /api/playlists/{id}/copy?newName=xxx - 复制歌单
|
* - POST /api/playlists/{id}/copy?newName=xxx - 复制歌单
|
||||||
*
|
*
|
||||||
* 提示:
|
* 提示:
|
||||||
* - 使用 Authentication 参数获取当前用户名:authentication.getName()
|
* - 使用 Authentication 参数获取当前用户名:authentication.getName()
|
||||||
* - 使用 @ResponseStatus 设置正确的 HTTP 状态码
|
* - 使用 @ResponseStatus 设置正确的 HTTP 状态码
|
||||||
@ -39,19 +39,69 @@ public class PlaylistController {
|
|||||||
this.playlistService = playlistService;
|
this.playlistService = playlistService;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: 实现 GET /api/playlists
|
// GET /api/playlists - 获取所有歌单(公开)
|
||||||
|
@GetMapping
|
||||||
|
public List<PlaylistDTO> getAllPlaylists() {
|
||||||
|
return playlistService.getAllPlaylists();
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: 实现 GET /api/playlists/{id}
|
// GET /api/playlists/{id} - 获取指定歌单(公开)
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
public PlaylistDTO getPlaylistById(@PathVariable Long id) {
|
||||||
|
return playlistService.getPlaylistById(id);
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: 实现 POST /api/playlists (状态码 201)
|
// POST /api/playlists - 创建歌单(需认证) 状态码 201
|
||||||
|
@PostMapping
|
||||||
|
@ResponseStatus(HttpStatus.CREATED)
|
||||||
|
public PlaylistDTO createPlaylist(@RequestBody PlaylistCreateDTO playlistCreateDTO,
|
||||||
|
Authentication authentication) {
|
||||||
|
String username = authentication.getName();
|
||||||
|
return playlistService.createPlaylist(playlistCreateDTO.name(), username);
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: 实现 POST /api/playlists/{id}/songs (状态码 201)
|
// POST /api/playlists/{id}/songs - 添加歌曲(需认证) 状态码 201
|
||||||
|
@PostMapping("/{id}/songs")
|
||||||
|
@ResponseStatus(HttpStatus.CREATED)
|
||||||
|
public PlaylistDTO addSongToPlaylist(@PathVariable Long id,
|
||||||
|
@RequestBody SongCreateDTO songCreateDTO,
|
||||||
|
Authentication authentication) {
|
||||||
|
String username = authentication.getName();
|
||||||
|
return playlistService.addSongToPlaylist(id, songCreateDTO, username);
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: 实现 DELETE /api/playlists/{playlistId}/songs/{songId} (状态码 204)
|
// DELETE /api/playlists/{playlistId}/songs/{songId} - 移除歌曲(需认证) 状态码 204
|
||||||
|
@DeleteMapping("/{playlistId}/songs/{songId}")
|
||||||
|
@ResponseStatus(HttpStatus.NO_CONTENT)
|
||||||
|
public void removeSongFromPlaylist(@PathVariable Long playlistId,
|
||||||
|
@PathVariable Long songId,
|
||||||
|
Authentication authentication) {
|
||||||
|
String username = authentication.getName();
|
||||||
|
playlistService.removeSongFromPlaylist(playlistId, songId, username);
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: 实现 DELETE /api/playlists/{id} (状态码 204)
|
// DELETE /api/playlists/{id} - 删除歌单(需认证) 状态码 204
|
||||||
|
@DeleteMapping("/{id}")
|
||||||
|
@ResponseStatus(HttpStatus.NO_CONTENT)
|
||||||
|
public void deletePlaylist(@PathVariable Long id,
|
||||||
|
Authentication authentication) {
|
||||||
|
String username = authentication.getName();
|
||||||
|
playlistService.deletePlaylist(id, username);
|
||||||
|
}
|
||||||
|
|
||||||
// TODO [Advanced]: 实现 GET /api/playlists/search?keyword=xxx
|
// [Advanced] GET /api/playlists/search?keyword=xxx - 搜索歌单
|
||||||
|
@GetMapping("/search")
|
||||||
|
public List<PlaylistDTO> searchPlaylists(@RequestParam String keyword) {
|
||||||
|
return playlistService.searchPlaylists(keyword);
|
||||||
|
}
|
||||||
|
|
||||||
// TODO [Advanced]: 实现 POST /api/playlists/{id}/copy?newName=xxx (状态码 201)
|
// [Advanced] POST /api/playlists/{id}/copy?newName=xxx - 复制歌单 状态码 201
|
||||||
}
|
@PostMapping("/{id}/copy")
|
||||||
|
@ResponseStatus(HttpStatus.CREATED)
|
||||||
|
public PlaylistDTO copyPlaylist(@PathVariable Long id,
|
||||||
|
@RequestParam String newName,
|
||||||
|
Authentication authentication) {
|
||||||
|
String username = authentication.getName();
|
||||||
|
return playlistService.copyPlaylist(id, newName, username);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user