generated from Java-2025Fall/final-vibevault-template
更新 src/main/java/com/vibevault/service/PlaylistServiceImpl.java
This commit is contained in:
parent
87546e6f19
commit
0b73de2ee6
@ -14,6 +14,7 @@ import org.springframework.stereotype.Service;
|
|||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 歌单服务实现
|
* 歌单服务实现
|
||||||
@ -37,60 +38,134 @@ public class PlaylistServiceImpl implements PlaylistService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<PlaylistDTO> getAllPlaylists() {
|
public List<PlaylistDTO> getAllPlaylists() {
|
||||||
// TODO: 实现获取所有歌单
|
// 获取所有歌单并转换为DTO
|
||||||
throw new UnsupportedOperationException("待实现");
|
return playlistRepository.findAll().stream()
|
||||||
|
.map(this::toDTO)
|
||||||
|
.collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PlaylistDTO getPlaylistById(Long id) {
|
public PlaylistDTO getPlaylistById(Long id) {
|
||||||
// TODO: 实现根据 ID 获取歌单,不存在时抛出 ResourceNotFoundException
|
// 根据ID查询歌单,不存在则抛异常
|
||||||
throw new UnsupportedOperationException("待实现");
|
Playlist playlist = playlistRepository.findById(id)
|
||||||
|
.orElseThrow(() -> new ResourceNotFoundException("歌单不存在,ID: " + id));
|
||||||
|
return toDTO(playlist);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional
|
@Transactional
|
||||||
public PlaylistDTO createPlaylist(String name, String ownerUsername) {
|
public PlaylistDTO createPlaylist(String name, String ownerUsername) {
|
||||||
// TODO: 实现创建歌单
|
// 1. 查询歌单所有者
|
||||||
throw new UnsupportedOperationException("待实现");
|
User owner = userRepository.findByUsername(ownerUsername)
|
||||||
|
.orElseThrow(() -> new ResourceNotFoundException("用户不存在,用户名: " + ownerUsername));
|
||||||
|
|
||||||
|
// 2. 创建歌单实体
|
||||||
|
Playlist playlist = new Playlist();
|
||||||
|
playlist.setName(name);
|
||||||
|
playlist.setOwner(owner);
|
||||||
|
|
||||||
|
// 3. 保存歌单并返回DTO
|
||||||
|
Playlist savedPlaylist = playlistRepository.save(playlist);
|
||||||
|
return toDTO(savedPlaylist);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional
|
@Transactional
|
||||||
public PlaylistDTO addSongToPlaylist(Long playlistId, SongCreateDTO song, String username) {
|
public PlaylistDTO addSongToPlaylist(Long playlistId, SongCreateDTO songDTO, String username) {
|
||||||
// TODO: 实现添加歌曲到歌单
|
// 1. 查询歌单
|
||||||
// [Challenge] 需要检查用户是否有权限操作此歌单
|
Playlist playlist = playlistRepository.findById(playlistId)
|
||||||
throw new UnsupportedOperationException("待实现");
|
.orElseThrow(() -> new ResourceNotFoundException("歌单不存在,ID: " + playlistId));
|
||||||
|
|
||||||
|
// 2. 检查用户权限
|
||||||
|
checkPermission(playlist, username);
|
||||||
|
|
||||||
|
// 3. 创建歌曲实体
|
||||||
|
Song song = new Song();
|
||||||
|
song.setTitle(songDTO.getTitle());
|
||||||
|
song.setArtist(songDTO.getArtist());
|
||||||
|
song.setDurationInSeconds(songDTO.getDurationInSeconds());
|
||||||
|
song.setPlaylist(playlist);
|
||||||
|
|
||||||
|
// 4. 维护双向关系并保存
|
||||||
|
playlist.getSongs().add(song);
|
||||||
|
Playlist updatedPlaylist = playlistRepository.save(playlist);
|
||||||
|
|
||||||
|
return toDTO(updatedPlaylist);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional
|
@Transactional
|
||||||
public void removeSongFromPlaylist(Long playlistId, Long songId, String username) {
|
public void removeSongFromPlaylist(Long playlistId, Long songId, String username) {
|
||||||
// TODO: 实现从歌单移除歌曲
|
// 1. 查询歌单
|
||||||
// [Challenge] 需要检查用户是否有权限操作此歌单
|
Playlist playlist = playlistRepository.findById(playlistId)
|
||||||
throw new UnsupportedOperationException("待实现");
|
.orElseThrow(() -> new ResourceNotFoundException("歌单不存在,ID: " + playlistId));
|
||||||
|
|
||||||
|
// 2. 检查用户权限
|
||||||
|
checkPermission(playlist, username);
|
||||||
|
|
||||||
|
// 3. 查询歌曲并移除
|
||||||
|
Song song = playlist.getSongs().stream()
|
||||||
|
.filter(s -> s.getId().equals(songId))
|
||||||
|
.findFirst()
|
||||||
|
.orElseThrow(() -> new ResourceNotFoundException("歌曲不存在,ID: " + songId));
|
||||||
|
|
||||||
|
playlist.getSongs().remove(song);
|
||||||
|
playlistRepository.save(playlist);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional
|
@Transactional
|
||||||
public void deletePlaylist(Long playlistId, String username) {
|
public void deletePlaylist(Long playlistId, String username) {
|
||||||
// TODO: 实现删除歌单
|
// 1. 查询歌单
|
||||||
// [Challenge] 需要检查用户是否有权限操作此歌单
|
Playlist playlist = playlistRepository.findById(playlistId)
|
||||||
throw new UnsupportedOperationException("待实现");
|
.orElseThrow(() -> new ResourceNotFoundException("歌单不存在,ID: " + playlistId));
|
||||||
|
|
||||||
|
// 2. 检查用户权限
|
||||||
|
checkPermission(playlist, username);
|
||||||
|
|
||||||
|
// 3. 删除歌单
|
||||||
|
playlistRepository.delete(playlist);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ========== Advanced 方法 ==========
|
// ========== Advanced 方法 ==========
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<PlaylistDTO> searchPlaylists(String keyword) {
|
public List<PlaylistDTO> searchPlaylists(String keyword) {
|
||||||
// TODO [Advanced]: 实现按关键字搜索歌单
|
// 按名称模糊搜索歌单
|
||||||
throw new UnsupportedOperationException("待实现");
|
return playlistRepository.findByNameContainingIgnoreCase(keyword).stream()
|
||||||
|
.map(this::toDTO)
|
||||||
|
.collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional
|
@Transactional
|
||||||
public PlaylistDTO copyPlaylist(Long playlistId, String newName, String username) {
|
public PlaylistDTO copyPlaylist(Long playlistId, String newName, String username) {
|
||||||
// TODO [Advanced]: 实现复制歌单
|
// 1. 查询原歌单
|
||||||
throw new UnsupportedOperationException("待实现");
|
Playlist original = playlistRepository.findById(playlistId)
|
||||||
|
.orElseThrow(() -> new ResourceNotFoundException("原歌单不存在,ID: " + playlistId));
|
||||||
|
|
||||||
|
// 2. 查询目标用户(新所有者)
|
||||||
|
User newOwner = userRepository.findByUsername(username)
|
||||||
|
.orElseThrow(() -> new ResourceNotFoundException("用户不存在,用户名: " + username));
|
||||||
|
|
||||||
|
// 3. 创建新歌单
|
||||||
|
Playlist copy = new Playlist();
|
||||||
|
copy.setName(newName);
|
||||||
|
copy.setOwner(newOwner);
|
||||||
|
|
||||||
|
// 4. 复制歌曲
|
||||||
|
original.getSongs().forEach(song -> {
|
||||||
|
Song newSong = new Song();
|
||||||
|
newSong.setTitle(song.getTitle());
|
||||||
|
newSong.setArtist(song.getArtist());
|
||||||
|
newSong.setDurationInSeconds(song.getDurationInSeconds());
|
||||||
|
newSong.setPlaylist(copy);
|
||||||
|
copy.getSongs().add(newSong);
|
||||||
|
});
|
||||||
|
|
||||||
|
// 5. 保存并返回DTO
|
||||||
|
Playlist savedCopy = playlistRepository.save(copy);
|
||||||
|
return toDTO(savedCopy);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ========== 辅助方法 ==========
|
// ========== 辅助方法 ==========
|
||||||
@ -99,16 +174,28 @@ public class PlaylistServiceImpl implements PlaylistService {
|
|||||||
* 将 Playlist 实体转换为 DTO
|
* 将 Playlist 实体转换为 DTO
|
||||||
*/
|
*/
|
||||||
private PlaylistDTO toDTO(Playlist playlist) {
|
private PlaylistDTO toDTO(Playlist playlist) {
|
||||||
// TODO: 实现实体到 DTO 的转换
|
List<SongDTO> songDTOs = playlist.getSongs().stream()
|
||||||
throw new UnsupportedOperationException("待实现");
|
.map(this::toSongDTO)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
|
return new PlaylistDTO(
|
||||||
|
playlist.getId(),
|
||||||
|
playlist.getName(),
|
||||||
|
playlist.getOwner().getUsername(),
|
||||||
|
songDTOs
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将 Song 实体转换为 DTO
|
* 将 Song 实体转换为 DTO
|
||||||
*/
|
*/
|
||||||
private SongDTO toSongDTO(Song song) {
|
private SongDTO toSongDTO(Song song) {
|
||||||
// TODO: 实现实体到 DTO 的转换
|
return new SongDTO(
|
||||||
throw new UnsupportedOperationException("待实现");
|
song.getId(),
|
||||||
|
song.getTitle(),
|
||||||
|
song.getArtist(),
|
||||||
|
song.getDurationInSeconds()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -116,7 +203,17 @@ public class PlaylistServiceImpl implements PlaylistService {
|
|||||||
* 规则:歌单所有者或管理员可以操作
|
* 规则:歌单所有者或管理员可以操作
|
||||||
*/
|
*/
|
||||||
private void checkPermission(Playlist playlist, String username) {
|
private void checkPermission(Playlist playlist, String username) {
|
||||||
// TODO [Challenge]: 实现权限检查
|
// 查询当前操作的用户
|
||||||
// 如果无权限,抛出 UnauthorizedException
|
User operator = userRepository.findByUsername(username)
|
||||||
|
.orElseThrow(() -> new ResourceNotFoundException("操作用户不存在,用户名: " + username));
|
||||||
|
|
||||||
|
// 权限规则:所有者 或 管理员(假设角色为"ROLE_ADMIN")
|
||||||
|
boolean isOwner = playlist.getOwner().getUsername().equals(username);
|
||||||
|
boolean isAdmin = "ROLE_ADMIN".equals(operator.getRole());
|
||||||
|
|
||||||
|
if (!isOwner && !isAdmin) {
|
||||||
|
throw new UnauthorizedException("无权限操作此歌单");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Loading…
Reference in New Issue
Block a user