Files
IM_NEW/MessageService.WebApi/Application/Conversation/ConversationService.cs
T

56 lines
1.9 KiB
C#

using AutoMapper;
using IM.Commons;
using MessageService.Domain.IReposities;
using MessageService.WebApi.Application.Dtos;
namespace MessageService.WebApi.Application.Conversation
{
public class ConversationService
{
private readonly IConversationReposity reposity;
private readonly IMapper mapper;
public ConversationService(IConversationReposity reposity, IMapper mapper)
{
this.reposity = reposity;
this.mapper = mapper;
}
public async Task<Result<List<ConversationResponse>>> GetByOwnerIdAsync(Guid userId)
{
var list = await reposity.FindByUserIdAsync(userId);
return Result.Success(mapper.Map<List<ConversationResponse>>(list.ToList()));
}
public async Task<Result<ConversationResponse>> GetByIdAsync(Guid id, Guid userId)
{
var conversation = await reposity.FindByIdAsync(id);
if (conversation is null || conversation.UserId != userId)
{
return Result.Fail<ConversationResponse>(ResultCode.CONVERSATION_NOT_FOUND);
}
return Result.Success(mapper.Map<ConversationResponse>(conversation));
}
public async Task<Result<List<string>>> GetStreamkeysAsync(Guid userId)
{
var list = await reposity.FindAllStreamKeyAsync(userId);
return Result.Success(list.ToList());
}
public async Task<Result<object>> MarkAsReadAsync(Guid conversationId, Guid userId)
{
var conversation = await reposity.FindByIdAsync(conversationId);
if (conversation is null || conversation.UserId != userId)
{
return Result.Fail<object>(ResultCode.CONVERSATION_NOT_FOUND);
}
conversation.MarkAsRead(lastReadSequenceId: null);
return Result.Success();
}
}
}