refactor(picture): streamline picture response mapping and improve thumbnail generation logic

This commit is contained in:
shiyu
2025-06-10 10:46:27 +08:00
parent 48730bebea
commit b5931de344
3 changed files with 42 additions and 63 deletions
@@ -1,5 +1,6 @@
using Foxel.Models;
using Foxel.Models.Response.Picture;
using Foxel.Services.Mapping;
using Foxel.Services.Storage;
using Microsoft.EntityFrameworkCore;
@@ -8,6 +9,7 @@ namespace Foxel.Services.Management;
public class PictureManagementService(
IDbContextFactory<MyDbContext> contextFactory,
IStorageService storageService,
IMappingService mappingService,
ILogger<PictureManagementService> logger) : IPictureManagementService
{
public async Task<PaginatedResult<PictureResponse>> GetPicturesAsync(int page = 1, int pageSize = 10, string? searchQuery = null, int? userId = null)
@@ -44,26 +46,7 @@ public class PictureManagementService(
.ToListAsync();
// 转换为响应模型
var pictureResponses = pictures.Select(picture => new PictureResponse
{
Id = picture.Id,
Name = picture.Name,
Path = storageService.ExecuteAsync(picture.StorageModeId, provider =>
Task.FromResult(provider.GetUrl(picture.Id,picture.Path))).Result,
ThumbnailPath = storageService.ExecuteAsync(picture.StorageModeId, provider =>
Task.FromResult(provider.GetUrl(picture.Id,picture.ThumbnailPath ?? string.Empty))).Result,
Description = picture.Description,
CreatedAt = picture.CreatedAt,
TakenAt = picture.TakenAt,
ExifInfo = picture.ExifInfo,
UserId = picture.UserId,
Username = picture.User?.UserName,
Tags = picture.Tags?.Select(t => t.Name).ToList(),
AlbumId = picture.AlbumId,
AlbumName = picture.Album?.Name,
Permission = picture.Permission,
FavoriteCount = picture.Favorites?.Count ?? 0,
}).ToList();
var pictureResponses = pictures.Select(mappingService.MapPictureToResponse).ToList();
return new PaginatedResult<PictureResponse>
{