feat: 重构消息服务,替换消息记录服务为消息服务,更新相关依赖和逻辑
All checks were successful
Docker Build & Deploy / Build Docker Image (push) Successful in 20s
Docker Build & Deploy / Deploy to Production (push) Successful in 7s
Docker Build & Deploy / Cleanup Dangling Images (push) Successful in 1s
Docker Build & Deploy / WeChat Notification (push) Successful in 1s
All checks were successful
Docker Build & Deploy / Build Docker Image (push) Successful in 20s
Docker Build & Deploy / Deploy to Production (push) Successful in 7s
Docker Build & Deploy / Cleanup Dangling Images (push) Successful in 1s
Docker Build & Deploy / WeChat Notification (push) Successful in 1s
This commit is contained in:
80
Service/MessageService.cs
Normal file
80
Service/MessageService.cs
Normal file
@@ -0,0 +1,80 @@
|
||||
namespace Service;
|
||||
|
||||
public interface IMessageService
|
||||
{
|
||||
Task<(IEnumerable<MessageRecord> List, long Total)> GetPagedListAsync(int pageIndex, int pageSize);
|
||||
Task<MessageRecord?> GetByIdAsync(long id);
|
||||
Task<bool> AddAsync(MessageRecord message);
|
||||
Task<bool> AddAsync(string title, string content, MessageType type = MessageType.Text, string? url = null);
|
||||
Task<bool> MarkAsReadAsync(long id);
|
||||
Task<bool> MarkAllAsReadAsync();
|
||||
Task<bool> DeleteAsync(long id);
|
||||
Task<long> GetUnreadCountAsync();
|
||||
}
|
||||
|
||||
public class MessageService(IMessageRecordRepository messageRepo, INotificationService notificationService) : IMessageService
|
||||
{
|
||||
public async Task<(IEnumerable<MessageRecord> List, long Total)> GetPagedListAsync(int pageIndex, int pageSize)
|
||||
{
|
||||
return await messageRepo.GetPagedListAsync(pageIndex, pageSize);
|
||||
}
|
||||
|
||||
public async Task<MessageRecord?> GetByIdAsync(long id)
|
||||
{
|
||||
return await messageRepo.GetByIdAsync(id);
|
||||
}
|
||||
|
||||
public async Task<bool> AddAsync(MessageRecord message)
|
||||
{
|
||||
return await messageRepo.AddAsync(message);
|
||||
}
|
||||
|
||||
public async Task<bool> AddAsync(
|
||||
string title,
|
||||
string content,
|
||||
MessageType type = MessageType.Text,
|
||||
string? url = null
|
||||
)
|
||||
{
|
||||
var message = new MessageRecord
|
||||
{
|
||||
Title = title,
|
||||
Content = content,
|
||||
MessageType = type,
|
||||
Url = url,
|
||||
IsRead = false
|
||||
};
|
||||
var result = await messageRepo.AddAsync(message);
|
||||
if (result)
|
||||
{
|
||||
await notificationService.SendNotificationAsync(title, url);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<bool> MarkAsReadAsync(long id)
|
||||
{
|
||||
var message = await messageRepo.GetByIdAsync(id);
|
||||
if (message == null) return false;
|
||||
|
||||
message.IsRead = true;
|
||||
message.UpdateTime = DateTime.Now;
|
||||
return await messageRepo.UpdateAsync(message);
|
||||
}
|
||||
|
||||
public async Task<bool> MarkAllAsReadAsync()
|
||||
{
|
||||
return await messageRepo.MarkAllAsReadAsync();
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteAsync(long id)
|
||||
{
|
||||
return await messageRepo.DeleteAsync(id);
|
||||
}
|
||||
|
||||
public async Task<long> GetUnreadCountAsync()
|
||||
{
|
||||
var list = await messageRepo.GetAllAsync();
|
||||
return list.Count(x => !x.IsRead);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user