finalizare 1.0
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
namespace Application.Endpoints.Chats.GetChats;
|
||||
|
||||
public class GetConversationsCommand
|
||||
{
|
||||
public Guid IdUser1 { get; set; } = Guid.Empty;
|
||||
public Guid IdUser2 { get; set; } = Guid.Empty;
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
using Application.Services.Database.MongoDB;
|
||||
using Application.Services.Database.PostgreSQL;
|
||||
using Domain.Entities;
|
||||
|
||||
namespace Application.Endpoints.Chats.GetChats;
|
||||
|
||||
public class GetConversationsHandler(
|
||||
IChatMongoDbService chatMongoDbService,
|
||||
IPatientRepository patientRepository,
|
||||
IDoctorRepository doctorRepository)
|
||||
{
|
||||
public async Task<BaseResponse> HandleGetConversation(GetConversationsCommand getConversationCommand,
|
||||
CancellationToken token)
|
||||
{
|
||||
var validation = new GetConversationsValidator(doctorRepository, patientRepository);
|
||||
var validationResult = await validation.ValidateAsync(getConversationCommand, 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(getConversationCommand.IdUser1, getConversationCommand.IdUser2);
|
||||
|
||||
var criteria = new List<(string, string)>();
|
||||
criteria.Add(("_id", chatId));
|
||||
|
||||
Chat? chat = null;
|
||||
|
||||
var documents = await chatMongoDbService.FindAsync<Chat>(criteria, token);
|
||||
if (!documents.Any())
|
||||
{
|
||||
chat = new Chat();
|
||||
chat.SetId(chatId);
|
||||
|
||||
await chatMongoDbService.AddAsync(chat, token);
|
||||
}
|
||||
else
|
||||
{
|
||||
chat = documents[0];
|
||||
}
|
||||
|
||||
return new BaseResponse
|
||||
{
|
||||
StatusCode = HttpStatusCodes.OK,
|
||||
Message = "Fetching messages.",
|
||||
Data = chat
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
using Application.Services.Database.PostgreSQL;
|
||||
using FluentValidation;
|
||||
|
||||
namespace Application.Endpoints.Chats.GetChats;
|
||||
|
||||
public class GetConversationsValidator : AbstractValidator<GetConversationsCommand>
|
||||
{
|
||||
private readonly IPatientRepository _patientRepository;
|
||||
private readonly IDoctorRepository _doctorRepository;
|
||||
|
||||
public GetConversationsValidator(IDoctorRepository doctorRepository, IPatientRepository patientRepository)
|
||||
{
|
||||
RuleFor(x => x.IdUser1)
|
||||
.NotEmpty().WithMessage("IdUser1 is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString());
|
||||
|
||||
RuleFor(x => x.IdUser2)
|
||||
.NotEmpty().WithMessage("IdUser2 is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString());
|
||||
|
||||
RuleFor(x => x)
|
||||
.MustAsync(CheckForUsersExistence).WithMessage("One of the users is not existing.")
|
||||
.WithErrorCode(HttpStatusCodes.BadRequest.ToString());
|
||||
|
||||
_patientRepository = patientRepository;
|
||||
_doctorRepository = doctorRepository;
|
||||
}
|
||||
|
||||
private async Task<bool> CheckForUsersExistence(GetConversationsCommand request, CancellationToken token)
|
||||
{
|
||||
var firstCheck = await _patientRepository.GetByIdAsync(request.IdUser1, token) != null &&
|
||||
await _doctorRepository.GetByIdAsync(request.IdUser2, token) != null;
|
||||
|
||||
var secondCheck = await _patientRepository.GetByIdAsync(request.IdUser2, token) != null &&
|
||||
await _doctorRepository.GetByIdAsync(request.IdUser1, token) != null;
|
||||
|
||||
return firstCheck || secondCheck;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user