Files
EmailBill/WebApi/Controllers/MessageRecordController.cs
孙诚 557d44aed8
All checks were successful
Docker Build & Deploy / Build Docker Image (push) Successful in 23s
Docker Build & Deploy / Deploy to Production (push) Successful in 6s
Docker Build & Deploy / Cleanup Dangling Images (push) Successful in 1s
优化代码
2026-01-04 16:43:32 +08:00

118 lines
3.2 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 count.Ok();
}
catch (Exception ex)
{
logger.LogError(ex, "获取未读消息数量失败");
return $"获取未读消息数量失败: {ex.Message}".Fail<long>();
}
}
/// <summary>
/// 标记已读
/// </summary>
[HttpPost]
public async Task<BaseResponse<bool>> MarkAsRead([FromQuery] long id)
{
try
{
var result = await messageService.MarkAsReadAsync(id);
return result.Ok();
}
catch (Exception ex)
{
logger.LogError(ex, "标记消息已读失败ID: {Id}", id);
return $"标记消息已读失败: {ex.Message}".Fail<bool>();
}
}
/// <summary>
/// 全部标记已读
/// </summary>
[HttpPost]
public async Task<BaseResponse<bool>> MarkAllAsRead()
{
try
{
var result = await messageService.MarkAllAsReadAsync();
return result.Ok();
}
catch (Exception ex)
{
logger.LogError(ex, "全部标记已读失败");
return $"全部标记已读失败: {ex.Message}".Fail<bool>();
}
}
/// <summary>
/// 删除消息
/// </summary>
[HttpPost]
public async Task<BaseResponse<bool>> Delete([FromQuery] long id)
{
try
{
var result = await messageService.DeleteAsync(id);
return result.Ok();
}
catch (Exception ex)
{
logger.LogError(ex, "删除消息失败ID: {Id}", id);
return $"删除消息失败: {ex.Message}".Fail<bool>();
}
}
/// <summary>
/// 新增消息 (测试用)
/// </summary>
[HttpPost]
public async Task<BaseResponse<bool>> Add([FromBody] MessageRecord message)
{
try
{
var result = await messageService.AddAsync(message);
return result.Ok();
}
catch (Exception ex)
{
logger.LogError(ex, "新增消息失败");
return $"新增消息失败: {ex.Message}".Fail<bool>();
}
}
}