finalizare 1.0
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
using Application.Services.Database.MongoDB;
|
||||
using Application.Services.Database.PostgreSQL;
|
||||
using Domain.Entities;
|
||||
|
||||
namespace Application.Endpoints.Chats.SendMessage;
|
||||
|
||||
public class SendMessageHandler(
|
||||
IChatMongoDbService chatMongoDbService,
|
||||
IPatientRepository patientRepository,
|
||||
IDoctorRepository doctorRepository)
|
||||
{
|
||||
public async Task<BaseResponse> Handle(SendMessageCommand request, CancellationToken token)
|
||||
{
|
||||
var validation = new SendMessageValidator(doctorRepository, patientRepository);
|
||||
var validationResult = await validation.ValidateAsync(request, token);
|
||||
|
||||
if (!validationResult.IsValid)
|
||||
{
|
||||
var firstError = validationResult.Errors.FirstOrDefault();
|
||||
var errorCode = int.TryParse(firstError.ErrorCode, out var code) ? code : -1;
|
||||
var errorMessage = firstError.ErrorMessage;
|
||||
|
||||
return new BaseResponse
|
||||
{
|
||||
StatusCode = errorCode,
|
||||
Message = errorMessage,
|
||||
Data = null
|
||||
};
|
||||
}
|
||||
|
||||
var chatId = IdentifierGenerator.GenerateId(request.Sender, request.Receiver);
|
||||
|
||||
var criteria = new List<(string, string)>();
|
||||
criteria.Add(("_id", chatId));
|
||||
|
||||
var documents = await chatMongoDbService.FindAsync<Chat>(criteria, token);
|
||||
if (!documents.Any())
|
||||
return new BaseResponse
|
||||
{
|
||||
StatusCode = HttpStatusCodes.NotFound,
|
||||
Message = "Access to medical history not found.",
|
||||
Data = null
|
||||
};
|
||||
|
||||
var chat = documents[0].Messages;
|
||||
chat.Add(new Message(request.Sender, request.Message));
|
||||
|
||||
var newChat = new Chat();
|
||||
newChat.SetId(chatId);
|
||||
newChat.SetMessages(chat);
|
||||
|
||||
await chatMongoDbService.ModifyAsync("_id", chatId, newChat, token);
|
||||
|
||||
return new BaseResponse
|
||||
{
|
||||
StatusCode = HttpStatusCodes.NoContent,
|
||||
Message = null,
|
||||
Data = null
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user