Files
EmailBill/WebApi/Controllers/MessageRecordController.cs
孙诚 13bf23a48c
All checks were successful
Docker Build & Deploy / Build Docker Image (push) Successful in 22s
Docker Build & Deploy / Deploy to Production (push) Successful in 6s
实现消息记录功能,包括增删改查和标记已读,优化消息列表展示和未读消息计数
2025-12-29 14:18:09 +08:00

118 lines
3.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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}");
}
}
}