feat(singer): 添加歌手收藏时间显示功能
- 在SingerDTO中新增collectTime字段用于显示收藏时间 - 修改SingerServiceImpl中的分页查询逻辑以填充收藏时间信息 - 修复UserCollectPlaylist实体中createTime字段的数据库映射注解 - 扩展UserSingerRepository添加findByUserIdAndSingerId方法支持收藏记录查询
This commit is contained in:
@@ -70,6 +70,12 @@ public class SingerDTO {
|
||||
*/
|
||||
private Boolean isCollected;
|
||||
|
||||
/**
|
||||
* 收藏时间
|
||||
*/
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
private LocalDateTime collectTime;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
|
||||
@@ -36,6 +36,6 @@ public class UserCollectPlaylist {
|
||||
* 收藏时间
|
||||
*/
|
||||
@CreationTimestamp
|
||||
@Column(nullable = false, updatable = false)
|
||||
@Column(name = "collect_time", nullable = false, updatable = false)
|
||||
private LocalDateTime createTime;
|
||||
}
|
||||
|
||||
@@ -12,6 +12,8 @@ import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* 用户歌手关联数据访问层
|
||||
*/
|
||||
@@ -86,4 +88,13 @@ public interface UserSingerRepository extends JpaRepository<UserSinger, Long> {
|
||||
@Transactional
|
||||
@Query("DELETE FROM UserSinger us WHERE us.singer.id = :singerId")
|
||||
void deleteBySingerId(@Param("singerId") Long singerId);
|
||||
|
||||
/**
|
||||
* 根据用户ID和歌手ID查询收藏记录
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @param singerId 歌手ID
|
||||
* @return 收藏记录
|
||||
*/
|
||||
Optional<UserSinger> findByUserIdAndSingerId(Long userId, Long singerId);
|
||||
}
|
||||
|
||||
@@ -385,9 +385,15 @@ public class SingerServiceImpl implements SingerService {
|
||||
PageRequest.of(page - 1, size, Sort.by(Sort.Direction.DESC, "collectTime"))
|
||||
);
|
||||
|
||||
// 转换为DTO
|
||||
// 转换为DTO,并填充收藏时间
|
||||
List<SingerDTO> singerDTOs = singerPage.getContent().stream()
|
||||
.map(this::convertToDTO)
|
||||
.map(singer -> {
|
||||
SingerDTO dto = convertToDTO(singer);
|
||||
// 从收藏记录中获取收藏时间
|
||||
userSingerRepository.findByUserIdAndSingerId(userId, singer.getId())
|
||||
.ifPresent(us -> dto.setCollectTime(us.getCollectTime()));
|
||||
return dto;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return new PageResult<>(
|
||||
|
||||
Reference in New Issue
Block a user