added mass deactivate subscription for user.

This commit is contained in:
Ruberoid 2025-07-21 16:40:02 +03:00
parent a35e616753
commit 85bc493d73
8 changed files with 34 additions and 1 deletions

View File

@ -24,4 +24,7 @@ public interface ITextSubscriptionsController
[Patch(WebRoutes.TextSubscriptions.Disable)] [Patch(WebRoutes.TextSubscriptions.Disable)]
Task Disable([Path] long id, CancellationToken cancellationToken = default); Task Disable([Path] long id, CancellationToken cancellationToken = default);
[Patch(WebRoutes.TextSubscriptions.DeactivateAllSubscriptionsById)]
Task DeactivateAllSubscriptionsById([Path] long userId, CancellationToken cancellationToken = default);
} }

View File

@ -13,6 +13,8 @@ public static class WebRoutes
public const string Activate = ById + "/" + "activate"; public const string Activate = ById + "/" + "activate";
public const string Disable = ById + "/" + "disable"; public const string Disable = ById + "/" + "disable";
public const string DeactivateAllSubscriptionsById = "deactivate-all-subscriptions/{userId}";
public const string ById = "{id}"; public const string ById = "{id}";
} }

View File

@ -7,6 +7,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" /> <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" />
<PackageReference Include="Nocr.TelegramListener.Async.Api.Contracts" /> <PackageReference Include="Nocr.TelegramListener.Async.Api.Contracts" />
<PackageReference Include="RestEase" />
<PackageReference Include="Rebus" /> <PackageReference Include="Rebus" />
<PackageReference Include="Rebus.ServiceProvider" /> <PackageReference Include="Rebus.ServiceProvider" />
</ItemGroup> </ItemGroup>

View File

@ -13,4 +13,6 @@ public interface ITextSubscriptionRepository
Task<TextSubscription?> GetById(long id, CancellationToken cancellationToken = default); Task<TextSubscription?> GetById(long id, CancellationToken cancellationToken = default);
Task Update(TextSubscription subscription, CancellationToken cancellationToken = default); Task Update(TextSubscription subscription, CancellationToken cancellationToken = default);
Task DeactivateAllSubscriptionsById(long userId, CancellationToken cancellationToken = default);
} }

View File

@ -19,4 +19,6 @@ public interface ITextSubscriptionsService
Task<IReadOnlyCollection<TextSubscriptionData>> Get(CancellationToken cancellationToken = default); Task<IReadOnlyCollection<TextSubscriptionData>> Get(CancellationToken cancellationToken = default);
Task<IReadOnlyCollection<TextSubscriptionData>> GetByUserId(long userId, CancellationToken cancellationToken = default); Task<IReadOnlyCollection<TextSubscriptionData>> GetByUserId(long userId, CancellationToken cancellationToken = default);
Task DeactivateAllSubscriptionsById(long userId, CancellationToken cancellationToken = default);
} }

View File

@ -1,3 +1,4 @@
using Microsoft.Extensions.Logging;
using Nocr.TextMatcher.Api.Contracts.TextMatches.Dto; using Nocr.TextMatcher.Api.Contracts.TextMatches.Dto;
using Nocr.TextMatcher.AppServices.TextSubscriptions.Repositories; using Nocr.TextMatcher.AppServices.TextSubscriptions.Repositories;
using Nocr.TextMatcher.Async.Api.Contracts; using Nocr.TextMatcher.Async.Api.Contracts;
@ -12,13 +13,15 @@ public sealed class TextSubscriptionsService : ITextSubscriptionsService
private readonly IBus _bus; private readonly IBus _bus;
private readonly ITextSubscriptionRepository _repository; private readonly ITextSubscriptionRepository _repository;
private readonly ICurrentDateProvider _dateProvider; private readonly ICurrentDateProvider _dateProvider;
private readonly ILogger<TextSubscriptionsService> _logger;
public TextSubscriptionsService(IBus bus, ITextSubscriptionRepository repository, public TextSubscriptionsService(IBus bus, ITextSubscriptionRepository repository,
ICurrentDateProvider dateProvider) ICurrentDateProvider dateProvider, ILogger<TextSubscriptionsService> logger)
{ {
_bus = bus ?? throw new ArgumentNullException(nameof(bus)); _bus = bus ?? throw new ArgumentNullException(nameof(bus));
_repository = repository ?? throw new ArgumentNullException(nameof(repository)); _repository = repository ?? throw new ArgumentNullException(nameof(repository));
_dateProvider = dateProvider ?? throw new ArgumentNullException(nameof(dateProvider)); _dateProvider = dateProvider ?? throw new ArgumentNullException(nameof(dateProvider));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
} }
public async Task<long> Create(long userId, string chatUsername, string template, TextSubscriptionRule rule, public async Task<long> Create(long userId, string chatUsername, string template, TextSubscriptionRule rule,
@ -91,6 +94,13 @@ public sealed class TextSubscriptionsService : ITextSubscriptionsService
return matches.Select(MapToTextMatchData).ToArray(); return matches.Select(MapToTextMatchData).ToArray();
} }
public async Task DeactivateAllSubscriptionsById(long userId, CancellationToken cancellationToken = default)
{
_logger.LogInformation("Deactivating all subscriptions for user {UserId}", userId);
await _repository.DeactivateAllSubscriptionsById(userId, cancellationToken);
_logger.LogInformation("Successfully deactivated all subscriptions for user {UserId}", userId);
}
private TextSubscriptionData MapToTextMatchData(TextSubscription textSubscription) private TextSubscriptionData MapToTextMatchData(TextSubscription textSubscription)
{ {
return new TextSubscriptionData return new TextSubscriptionData

View File

@ -54,4 +54,10 @@ public class TextSubscriptionsController : ControllerBase
{ {
return _textSubscriptionsService.Disable(id, cancellationToken); return _textSubscriptionsService.Disable(id, cancellationToken);
} }
[HttpPatch(WebRoutes.TextSubscriptions.DeactivateAllSubscriptionsById)]
public Task DeactivateAllSubscriptionsById([FromRoute] long userId, CancellationToken cancellationToken = default)
{
return _textSubscriptionsService.DeactivateAllSubscriptionsById(userId, cancellationToken);
}
} }

View File

@ -46,4 +46,11 @@ public sealed class TextSubscriptionRepository : ITextSubscriptionRepository
_db.TextSubscriptions.Update(subscription); _db.TextSubscriptions.Update(subscription);
await _db.SaveChangesAsync(cancellationToken); await _db.SaveChangesAsync(cancellationToken);
} }
public async Task DeactivateAllSubscriptionsById(long userId, CancellationToken cancellationToken = default)
{
await _db.TextSubscriptions
.Where(x => x.UserId == userId && x.Active)
.ExecuteUpdateAsync(x => x.SetProperty(s => s.Active, false), cancellationToken);
}
} }