Reviewed-on: #2 Co-authored-by: Sergey Nazarov <insight.appdev@gmail.com> Co-committed-by: Sergey Nazarov <insight.appdev@gmail.com>
107 lines
3.7 KiB
C#
107 lines
3.7 KiB
C#
using Nocr.TextMatcher.Api.Contracts.TextMatches.Dto;
|
|
using Nocr.TextMatcher.AppServices.TextMatches.Repositories;
|
|
using Nocr.TextMatcher.Async.Api.Contracts;
|
|
using Nocr.TextMatcher.Contracts;
|
|
using Nocr.TextMatcher.Core.Dates;
|
|
using Rebus.Bus;
|
|
|
|
namespace Nocr.TextMatcher.AppServices.TextMatches.Services;
|
|
|
|
public sealed class TextSubscriptionsService : ITextSubscriptionsService
|
|
{
|
|
private readonly IBus _bus;
|
|
private readonly ITextSubscriptionRepository _repository;
|
|
private readonly ICurrentDateProvider _dateProvider;
|
|
|
|
public TextSubscriptionsService(IBus bus, ITextSubscriptionRepository repository,
|
|
ICurrentDateProvider dateProvider)
|
|
{
|
|
_bus = bus ?? throw new ArgumentNullException(nameof(bus));
|
|
_repository = repository ?? throw new ArgumentNullException(nameof(repository));
|
|
_dateProvider = dateProvider ?? throw new ArgumentNullException(nameof(dateProvider));
|
|
}
|
|
|
|
public async Task<long> Create(long userId, string chatUsername, string template, TextSubscriptionRule rule,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
var textMatch = TextSubscription.Initialize(userId, chatUsername, template, rule, _dateProvider.UtcNow);
|
|
await _repository.Create(textMatch, cancellationToken);
|
|
|
|
var @event = new TextSubscriptionCreated
|
|
{
|
|
ChatUsername = textMatch.ChatUsername
|
|
};
|
|
|
|
await _bus.Advanced.Topics.Publish(Constants.RoutingKeys.Subscriptions, @event);
|
|
|
|
return textMatch.Id;
|
|
}
|
|
|
|
public async Task<TextSubscriptionData?> GetById(long id, CancellationToken cancellationToken)
|
|
{
|
|
var textMatch = await _repository.GetById(id, cancellationToken);
|
|
if (textMatch == null)
|
|
return null;
|
|
|
|
return MapToTextMatchData(textMatch);
|
|
}
|
|
|
|
public Task Delete(long id, CancellationToken cancellationToken = default)
|
|
{
|
|
return _repository.Delete(id, cancellationToken);
|
|
}
|
|
|
|
public async Task Activate(long id, CancellationToken cancellationToken = default)
|
|
{
|
|
var textMatch = await _repository.GetById(id, cancellationToken);
|
|
if (textMatch == null)
|
|
{
|
|
throw new InvalidOperationException($"Match with id {id} not found");
|
|
}
|
|
|
|
textMatch.Activate();
|
|
|
|
await _repository.Update(textMatch, cancellationToken);
|
|
}
|
|
|
|
public async Task Disable(long id, CancellationToken cancellationToken = default)
|
|
{
|
|
var textMatch = await _repository.GetById(id, cancellationToken);
|
|
if (textMatch == null)
|
|
{
|
|
throw new InvalidOperationException($"Match with id {id} not found");
|
|
}
|
|
|
|
textMatch.Disable();
|
|
|
|
await _repository.Update(textMatch, cancellationToken);
|
|
}
|
|
|
|
public async Task<IReadOnlyCollection<TextSubscriptionData>> Get(CancellationToken cancellationToken = default)
|
|
{
|
|
var matches = await _repository.Get(cancellationToken);
|
|
|
|
return matches.Select(MapToTextMatchData).ToArray();
|
|
}
|
|
|
|
public async Task<IReadOnlyCollection<TextSubscriptionData>> GetByUserId(long userId,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
var matches = await _repository.GetByUserId(userId, cancellationToken);
|
|
return matches.Select(MapToTextMatchData).ToArray();
|
|
}
|
|
|
|
private TextSubscriptionData MapToTextMatchData(TextSubscription textSubscription)
|
|
{
|
|
return new TextSubscriptionData
|
|
{
|
|
Id = textSubscription.Id,
|
|
ChatUsername = textSubscription.ChatUsername,
|
|
Active = textSubscription.Active,
|
|
Template = textSubscription.Template,
|
|
UserId = textSubscription.UserId,
|
|
Rule = textSubscription.Rule,
|
|
CreatedDateTime = textSubscription.CreatedDateTime
|
|
};
|
|
}
|
|
} |