mirror of
https://github.com/DrizzleTime/Foxel.git
synced 2026-06-08 00:49:54 +08:00
feat(album): add cover picture functionality to albums and enhance album management API
This commit is contained in:
@@ -55,7 +55,7 @@ public class AlbumController(IAlbumService albumService) : BaseApiController
|
||||
if (userId == null)
|
||||
return Error<AlbumResponse>("无法识别用户信息", 401);
|
||||
|
||||
var album = await albumService.CreateAlbumAsync(request.Name, request.Description, userId.Value);
|
||||
var album = await albumService.CreateAlbumAsync(request.Name, request.Description, userId.Value, request.CoverPictureId);
|
||||
return Success(album, "相册创建成功");
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -74,8 +74,7 @@ public class AlbumController(IAlbumService albumService) : BaseApiController
|
||||
if (currentUserId == null)
|
||||
return Error<AlbumResponse>("无法识别用户信息", 401);
|
||||
|
||||
var album = await albumService.UpdateAlbumAsync(request.Id, request.Name, request.Description,
|
||||
currentUserId);
|
||||
var album = await albumService.UpdateAlbumAsync(request.Id, request.Name, request.Description, currentUserId, request.CoverPictureId);
|
||||
return Success(album, "相册更新成功");
|
||||
}
|
||||
catch (UnauthorizedAccessException)
|
||||
@@ -192,4 +191,5 @@ public class AlbumController(IAlbumService albumService) : BaseApiController
|
||||
return Error<bool>($"从相册移除图片失败: {ex.Message}", 500);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
216
Api/Management/AlbumManagementController.cs
Normal file
216
Api/Management/AlbumManagementController.cs
Normal file
@@ -0,0 +1,216 @@
|
||||
using Foxel.Controllers;
|
||||
using Foxel.Models;
|
||||
using Foxel.Models.Request.Album;
|
||||
using Foxel.Models.Response.Album;
|
||||
using Foxel.Models.Response.Picture;
|
||||
using Foxel.Services.Management;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Foxel.Api.Management
|
||||
{
|
||||
[Authorize(Roles = "Administrator")]
|
||||
[Route("api/management/album")]
|
||||
public class AlbumManagementController(IAlbumManagementService albumManagementService) : BaseApiController
|
||||
{
|
||||
[HttpGet("get_albums")]
|
||||
public async Task<ActionResult<PaginatedResult<AlbumResponse>>> GetAlbums(
|
||||
[FromQuery] int page = 1,
|
||||
[FromQuery] int pageSize = 10,
|
||||
[FromQuery] string? searchQuery = null,
|
||||
[FromQuery] int? userId = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await albumManagementService.GetAlbumsAsync(page, pageSize, searchQuery, userId);
|
||||
return PaginatedSuccess(result.Data, result.TotalCount, result.Page, result.PageSize);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return PaginatedError<AlbumResponse>($"获取相册列表失败: {ex.Message}", 500);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet("get_album/{id}")]
|
||||
public async Task<ActionResult<BaseResult<AlbumResponse>>> GetAlbumById(int id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var album = await albumManagementService.GetAlbumByIdAsync(id);
|
||||
return Success(album, "相册获取成功");
|
||||
}
|
||||
catch (KeyNotFoundException knfex)
|
||||
{
|
||||
return Error<AlbumResponse>(knfex.Message, 404);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Error<AlbumResponse>($"获取相册失败: {ex.Message}", 500);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost("create_album")]
|
||||
public async Task<ActionResult<BaseResult<AlbumResponse>>> CreateAlbum([FromBody] AlbumCreateRequest request)
|
||||
{
|
||||
try
|
||||
{
|
||||
var userId = GetCurrentUserId();
|
||||
if (userId is null) return Error<AlbumResponse>("用户未登录或无法识别用户", 401);
|
||||
var album = await albumManagementService.CreateAlbumAsync(request, (int)userId);
|
||||
return Success(album, "相册创建成功");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Error<AlbumResponse>($"创建相册失败: {ex.Message}", 500);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost("update_album/{id}")]
|
||||
public async Task<ActionResult<BaseResult<AlbumResponse>>> UpdateAlbum(int id,
|
||||
[FromBody] AlbumUpdateRequest request)
|
||||
{
|
||||
try
|
||||
{
|
||||
var album = await albumManagementService.UpdateAlbumAsync(id, request);
|
||||
return Success(album, "相册更新成功");
|
||||
}
|
||||
catch (KeyNotFoundException knfex)
|
||||
{
|
||||
return Error<AlbumResponse>(knfex.Message, 404);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Error<AlbumResponse>($"更新相册失败: {ex.Message}", 500);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost("delete_album")]
|
||||
public async Task<ActionResult<BaseResult<bool>>> DeleteAlbum([FromBody] int id) // Or [FromQuery] int id
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await albumManagementService.DeleteAlbumAsync(id);
|
||||
return Success(result, "相册删除成功");
|
||||
}
|
||||
catch (KeyNotFoundException knfex)
|
||||
{
|
||||
return Error<bool>(knfex.Message, 404);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Error<bool>($"删除相册失败: {ex.Message}", 500);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost("batch_delete_albums")]
|
||||
public async Task<ActionResult<BaseResult<BatchDeleteResult>>> BatchDeleteAlbums([FromBody] List<int> ids)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (ids == null || ids.Count == 0)
|
||||
{
|
||||
return Error<BatchDeleteResult>("未提供相册ID");
|
||||
}
|
||||
|
||||
var result = await albumManagementService.BatchDeleteAlbumsAsync(ids);
|
||||
return Success(result, $"成功删除 {result.SuccessCount} 个相册,失败 {result.FailedCount} 个");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Error<BatchDeleteResult>($"批量删除相册失败: {ex.Message}", 500);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet("get_albums_by_user/{userId}")]
|
||||
public async Task<ActionResult<PaginatedResult<AlbumResponse>>> GetAlbumsByUserId(
|
||||
int userId, [FromQuery] int page = 1, [FromQuery] int pageSize = 10)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await albumManagementService.GetAlbumsByUserIdAsync(userId, page, pageSize);
|
||||
return PaginatedSuccess(result.Data, result.TotalCount, result.Page, result.PageSize);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return PaginatedError<AlbumResponse>($"获取用户相册列表失败: {ex.Message}", 500);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost("{albumId}/picture/{pictureId}/add")]
|
||||
public async Task<ActionResult<BaseResult<bool>>> AddPictureToAlbum(int albumId, int pictureId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await albumManagementService.AddPictureToAlbumAsync(albumId, pictureId);
|
||||
return Success(result, "图片已成功添加到相册");
|
||||
}
|
||||
catch (KeyNotFoundException knfex)
|
||||
{
|
||||
return Error<bool>(knfex.Message, 404);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Error<bool>($"添加图片到相册失败: {ex.Message}", 500);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost("{albumId}/picture/{pictureId}/remove")]
|
||||
public async Task<ActionResult<BaseResult<bool>>> RemovePictureFromAlbum(int albumId, int pictureId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await albumManagementService.RemovePictureFromAlbumAsync(albumId, pictureId);
|
||||
return Success(result, "图片已成功从相册移除");
|
||||
}
|
||||
catch (KeyNotFoundException knfex)
|
||||
{
|
||||
return Error<bool>(knfex.Message, 404);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Error<bool>($"从相册移除图片失败: {ex.Message}", 500);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet("{albumId}/pictures")]
|
||||
public async Task<ActionResult<PaginatedResult<PictureResponse>>> GetPicturesInAlbum(
|
||||
int albumId, [FromQuery] int page = 1, [FromQuery] int pageSize = 10)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await albumManagementService.GetPicturesInAlbumAsync(albumId, page, pageSize);
|
||||
return PaginatedSuccess(result.Data, result.TotalCount, result.Page, result.PageSize);
|
||||
}
|
||||
catch (KeyNotFoundException knfex)
|
||||
{
|
||||
return PaginatedError<PictureResponse>(knfex.Message, 404);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return PaginatedError<PictureResponse>($"获取相册内图片失败: {ex.Message}", 500);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost("{albumId}/set_cover/{pictureId}")]
|
||||
public async Task<ActionResult<BaseResult<bool>>> SetAlbumCover(int albumId, int pictureId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await albumManagementService.SetAlbumCoverAsync(albumId, pictureId);
|
||||
return Success(result, "相册封面设置成功");
|
||||
}
|
||||
catch (KeyNotFoundException knfex)
|
||||
{
|
||||
return Error<bool>(knfex.Message, 404);
|
||||
}
|
||||
catch (InvalidOperationException ioex)
|
||||
{
|
||||
return Error<bool>(ioex.Message, 400);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Error<bool>($"设置相册封面失败: {ex.Message}", 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user