实现消息记录功能,包括增删改查和标记已读,优化消息列表展示和未读消息计数
This commit is contained in:
117
WebApi/Controllers/MessageRecordController.cs
Normal file
117
WebApi/Controllers/MessageRecordController.cs
Normal file
@@ -0,0 +1,117 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
|
||||
namespace WebApi.Controllers;
|
||||
|
||||
[Authorize]
|
||||
[ApiController]
|
||||
[Route("api/[controller]/[action]")]
|
||||
public class MessageRecordController(IMessageRecordService messageService, ILogger<MessageRecordController> logger) : ControllerBase
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取消息列表
|
||||
/// </summary>
|
||||
[HttpGet]
|
||||
public async Task<PagedResponse<MessageRecord>> GetList([FromQuery] int pageIndex = 1, [FromQuery] int pageSize = 20)
|
||||
{
|
||||
try
|
||||
{
|
||||
var (list, total) = await messageService.GetPagedListAsync(pageIndex, pageSize);
|
||||
return PagedResponse<MessageRecord>.Done(list.ToArray(), (int)total);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "获取消息列表失败");
|
||||
return PagedResponse<MessageRecord>.Fail($"获取消息列表失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取未读消息数量
|
||||
/// </summary>
|
||||
[HttpGet]
|
||||
public async Task<BaseResponse<long>> GetUnreadCount()
|
||||
{
|
||||
try
|
||||
{
|
||||
var count = await messageService.GetUnreadCountAsync();
|
||||
return BaseResponse<long>.Done(count);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "获取未读消息数量失败");
|
||||
return BaseResponse<long>.Fail($"获取未读消息数量失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 标记已读
|
||||
/// </summary>
|
||||
[HttpPost]
|
||||
public async Task<BaseResponse<bool>> MarkAsRead([FromQuery] long id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await messageService.MarkAsReadAsync(id);
|
||||
return BaseResponse<bool>.Done(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "标记消息已读失败,ID: {Id}", id);
|
||||
return BaseResponse<bool>.Fail($"标记消息已读失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 全部标记已读
|
||||
/// </summary>
|
||||
[HttpPost]
|
||||
public async Task<BaseResponse<bool>> MarkAllAsRead()
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await messageService.MarkAllAsReadAsync();
|
||||
return BaseResponse<bool>.Done(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "全部标记已读失败");
|
||||
return BaseResponse<bool>.Fail($"全部标记已读失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除消息
|
||||
/// </summary>
|
||||
[HttpPost]
|
||||
public async Task<BaseResponse<bool>> Delete([FromQuery] long id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await messageService.DeleteAsync(id);
|
||||
return BaseResponse<bool>.Done(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "删除消息失败,ID: {Id}", id);
|
||||
return BaseResponse<bool>.Fail($"删除消息失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 新增消息 (测试用)
|
||||
/// </summary>
|
||||
[HttpPost]
|
||||
public async Task<BaseResponse<bool>> Add([FromBody] MessageRecord message)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await messageService.AddAsync(message);
|
||||
return BaseResponse<bool>.Done(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "新增消息失败");
|
||||
return BaseResponse<bool>.Fail($"新增消息失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user