67 lines
2.1 KiB
C#
67 lines
2.1 KiB
C#
using GymLogAI.Application.Interfaces;
|
|
using GymLogAI.Core.Abstractions;
|
|
using GymLogAI.Core.Entities;
|
|
using GymLogAI.Core.Enums;
|
|
|
|
namespace GymLogAI.Application.Handlers;
|
|
|
|
public sealed class ImportTelegramMessageHandler(
|
|
IUserRepository userRepository,
|
|
ITelegramMessageRepository telegramMessageRepository,
|
|
IClock clock,
|
|
IIdGenerator idGenerator)
|
|
{
|
|
public async Task<Guid> HandleAsync(ImportTelegramMessageCommand command, CancellationToken cancellationToken)
|
|
{
|
|
var user = await userRepository.GetByIdAsync(command.UserId, cancellationToken);
|
|
if (user is null)
|
|
{
|
|
user = new User
|
|
{
|
|
Id = command.UserId,
|
|
TelegramUserId = command.TelegramUserId,
|
|
Username = command.Username,
|
|
FirstName = command.FirstName,
|
|
LastName = command.LastName,
|
|
CreatedAtUtc = clock.UtcNow
|
|
};
|
|
|
|
await userRepository.AddAsync(user, cancellationToken);
|
|
}
|
|
else
|
|
{
|
|
user.TelegramUserId = command.TelegramUserId;
|
|
user.Username = command.Username ?? user.Username;
|
|
user.FirstName = command.FirstName ?? user.FirstName;
|
|
user.LastName = command.LastName ?? user.LastName;
|
|
await userRepository.UpdateAsync(user, cancellationToken);
|
|
}
|
|
|
|
var message = new TelegramMessage
|
|
{
|
|
Id = idGenerator.New(),
|
|
UserId = user.Id,
|
|
TelegramChatId = command.TelegramChatId,
|
|
TelegramMessageId = command.TelegramMessageId,
|
|
Text = command.Text,
|
|
SentAtUtc = command.SentAtUtc,
|
|
ImportedAtUtc = clock.UtcNow,
|
|
ParsingStatus = ParsingStatus.Pending
|
|
};
|
|
|
|
await telegramMessageRepository.AddAsync(message, cancellationToken);
|
|
return message.Id;
|
|
}
|
|
}
|
|
|
|
public sealed record ImportTelegramMessageCommand(
|
|
Guid UserId,
|
|
long TelegramUserId,
|
|
long TelegramChatId,
|
|
int TelegramMessageId,
|
|
string Text,
|
|
DateTime SentAtUtc,
|
|
string? Username,
|
|
string? FirstName,
|
|
string? LastName);
|