-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from shishnk/telegram-api
Telegram api (fixed merge request)
- Loading branch information
Showing
75 changed files
with
1,173 additions
and
253 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...tegrationEventHandlers/UserIntegrationEventHandlers/CreatedUserIntegrationEventHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using DatabaseApp.Application.Users.Commands.CreateUser; | ||
using MediatR; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using TelegramBotApp.Caching.Caching; | ||
using TelegramBotApp.Messaging.IntegrationContext; | ||
using TelegramBotApp.Messaging.IntegrationContext.UserIntegrationEvents; | ||
using TelegramBotApp.Messaging.IntegrationResponseContext.IntegrationResponses; | ||
|
||
namespace DatabaseApp.IntegrationEvents.IntegrationEventHandlers.UserIntegrationEventHandlers; | ||
|
||
public class CreatedUserIntegrationEventHandler(IServiceScopeFactory factory, ICacheService cacheService) | ||
: IIntegrationEventHandler<CreatedUserIntegrationEvent> | ||
{ | ||
public async Task<IResponseMessage> Handle(CreatedUserIntegrationEvent @event) | ||
{ | ||
using var scope = factory.CreateScope(); | ||
var mediator = scope.ServiceProvider.GetRequiredService<IMediator>(); | ||
var result = await mediator.Send(new CreateUserCommand | ||
{ | ||
TelegramId = @event.UserTelegramId, // TODO | ||
Username = @event.Username, | ||
MobileNumber = @event.MobileNumber, | ||
RegisteredAt = @event.RegisteredAt | ||
}); | ||
|
||
if (!result.IsSuccess) return UniversalResponse.Empty; | ||
|
||
var allUsers = await cacheService.GetAsync<List<long>>("allUsers"); | ||
await cacheService.RemoveAsync("allUsers"); | ||
|
||
allUsers ??= []; | ||
allUsers.Add(@event.UserTelegramId); | ||
|
||
await cacheService.SetAsync("allUsers", allUsers); | ||
|
||
return UniversalResponse.Empty; | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
...ers/UserSubscriptionEventHandlers/CacheRequestUserSubscriptionsIntegrationEventHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using DatabaseApp.Application.Users.Queries.GetAllUsers; | ||
using DatabaseApp.Application.UserWeatherSubscriptions.Queries.GetWeatherSubscriptions; | ||
using MediatR; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using TelegramBotApp.Caching.Caching; | ||
using TelegramBotApp.Messaging.Common; | ||
using TelegramBotApp.Messaging.IntegrationContext; | ||
using TelegramBotApp.Messaging.IntegrationContext.UserIntegrationEvents; | ||
using TelegramBotApp.Messaging.IntegrationResponseContext.IntegrationResponses; | ||
|
||
namespace DatabaseApp.IntegrationEvents.IntegrationEventHandlers.UserSubscriptionEventHandlers; | ||
|
||
public class CacheRequestUserSubscriptionsIntegrationEventHandler( | ||
IServiceScopeFactory factory, | ||
ICacheService cacheService) | ||
: IIntegrationEventHandler<CacheRequestUserSubscriptionsIntegrationEvent> | ||
{ | ||
public async Task<IResponseMessage> Handle(CacheRequestUserSubscriptionsIntegrationEvent @event) | ||
{ | ||
using var scope = factory.CreateScope(); | ||
var mediator = scope.ServiceProvider.GetRequiredService<IMediator>(); | ||
List<UserSubscriptionInfo> subscriptionInfos = []; | ||
var userTelegramIds = await cacheService.GetAsync<List<long>>("allUsers"); | ||
|
||
if (userTelegramIds is null) | ||
{ | ||
var userDtos = await mediator.Send(new GetAllUsersQuery()); | ||
userTelegramIds = userDtos.Select(dto => dto.TelegramId).ToList(); | ||
await cacheService.SetAsync("allUsers", userTelegramIds); | ||
} | ||
|
||
foreach (var telegramId in userTelegramIds) | ||
{ | ||
var userSubscription = await mediator.Send(new GetUserWeatherSubscriptionsQuery | ||
{ | ||
UserTelegramId = telegramId | ||
}); | ||
|
||
if (userSubscription.Count == 0) continue; | ||
|
||
subscriptionInfos.AddRange(userSubscription.Select(subscriptionDto => new UserSubscriptionInfo | ||
{ | ||
TelegramId = subscriptionDto.UserTelegramId, | ||
ResendInterval = subscriptionDto.ResendInterval, | ||
Location = subscriptionDto.Location | ||
})); | ||
} | ||
|
||
await cacheService.RemoveAsync("allSubscriptions"); | ||
await cacheService.SetAsync("allSubscriptions", subscriptionInfos); | ||
|
||
return UniversalResponse.Empty; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...tHandlers/UserSubscriptionEventHandlers/CreatedUserSubscriptionIntegrationEventHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using DatabaseApp.Application.UserWeatherSubscriptions.Commands.CreateUserWeatherSubscription; | ||
using MediatR; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using TelegramBotApp.Caching.Caching; | ||
using TelegramBotApp.Messaging.Common; | ||
using TelegramBotApp.Messaging.IntegrationContext; | ||
using TelegramBotApp.Messaging.IntegrationContext.UserSubscriptionEvents; | ||
using TelegramBotApp.Messaging.IntegrationResponseContext.IntegrationResponses; | ||
|
||
namespace DatabaseApp.IntegrationEvents.IntegrationEventHandlers.UserSubscriptionEventHandlers; | ||
|
||
public class CreatedUserSubscriptionIntegrationEventHandler(IServiceScopeFactory factory, ICacheService cacheService) | ||
: IIntegrationEventHandler<CreatedUserSubscriptionIntegrationEvent> | ||
{ | ||
public async Task<IResponseMessage> Handle(CreatedUserSubscriptionIntegrationEvent @event) | ||
{ | ||
using var scope = factory.CreateScope(); | ||
var mediator = scope.ServiceProvider.GetRequiredService<IMediator>(); | ||
|
||
var result = await mediator.Send(new CreateUserWeatherSubscriptionCommand | ||
{ | ||
Location = @event.City, | ||
ResendInterval = @event.ResendInterval, | ||
TelegramUserId = @event.TelegramId | ||
}); | ||
|
||
if (result.IsFailed) return new UniversalResponse(result.Errors.First().Message); | ||
|
||
var allSubscriptions = await cacheService.GetAsync<List<UserSubscriptionInfo>>("allSubscriptions"); | ||
await cacheService.RemoveAsync("allSubscriptions"); | ||
|
||
allSubscriptions ??= []; | ||
|
||
allSubscriptions.Add(new() | ||
{ | ||
ResendInterval = @event.ResendInterval, | ||
Location = @event.City, | ||
TelegramId = @event.TelegramId | ||
}); | ||
await cacheService.SetAsync("allSubscriptions", allSubscriptions); | ||
|
||
return new UniversalResponse("Subscription created successfully"); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...tHandlers/UserSubscriptionEventHandlers/DeletedUserSubscriptionIntegrationEventHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using DatabaseApp.Application.UserWeatherSubscriptions.Commands.DeleteUserWeatherSubscription; | ||
using MediatR; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using TelegramBotApp.Caching.Caching; | ||
using TelegramBotApp.Messaging.Common; | ||
using TelegramBotApp.Messaging.IntegrationContext; | ||
using TelegramBotApp.Messaging.IntegrationContext.UserSubscriptionEvents; | ||
using TelegramBotApp.Messaging.IntegrationResponseContext.IntegrationResponses; | ||
|
||
namespace DatabaseApp.IntegrationEvents.IntegrationEventHandlers.UserSubscriptionEventHandlers; | ||
|
||
public class DeletedUserSubscriptionIntegrationEventHandler(IServiceScopeFactory factory, ICacheService cacheService) | ||
: IIntegrationEventHandler<DeletedUserSubscriptionIntegrationEvent> | ||
{ | ||
public async Task<IResponseMessage> Handle(DeletedUserSubscriptionIntegrationEvent @event) | ||
{ | ||
using var scope = factory.CreateScope(); | ||
var mediator = scope.ServiceProvider.GetRequiredService<IMediator>(); | ||
|
||
var result = await mediator.Send(new DeleteUserWeatherSubscriptionCommand | ||
{ | ||
UserTelegramId = @event.TelegramUserId, | ||
Location = @event.Location | ||
}); | ||
|
||
if (result.IsFailed) return new UniversalResponse(result.Errors.First().Message); | ||
|
||
var allSubscriptions = await cacheService.GetAsync<List<UserSubscriptionInfo>>("allSubscriptions"); | ||
await cacheService.RemoveAsync("allSubscriptions"); | ||
|
||
allSubscriptions ??= []; | ||
|
||
allSubscriptions.RemoveAll(x => | ||
x.TelegramId == @event.TelegramUserId && x.Location == @event.Location); | ||
|
||
await cacheService.SetAsync("allSubscriptions", allSubscriptions); | ||
|
||
return new UniversalResponse("Subscription deleted successfully"); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...tHandlers/UserSubscriptionEventHandlers/UpdatedUserSubscriptionIntegrationEventHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using DatabaseApp.Application.UserWeatherSubscriptions.Commands.UpdateUserWeatherSubscription; | ||
using MediatR; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using TelegramBotApp.Caching.Caching; | ||
using TelegramBotApp.Messaging.Common; | ||
using TelegramBotApp.Messaging.IntegrationContext; | ||
using TelegramBotApp.Messaging.IntegrationContext.UserSubscriptionEvents; | ||
using TelegramBotApp.Messaging.IntegrationResponseContext.IntegrationResponses; | ||
|
||
namespace DatabaseApp.IntegrationEvents.IntegrationEventHandlers.UserSubscriptionEventHandlers; | ||
|
||
public class UpdatedUserSubscriptionIntegrationEventHandler(IServiceScopeFactory factory, ICacheService cacheService) | ||
: IIntegrationEventHandler<UpdatedUserSubscriptionIntegrationEvent> | ||
{ | ||
public async Task<IResponseMessage> Handle(UpdatedUserSubscriptionIntegrationEvent @event) | ||
{ | ||
using var scope = factory.CreateScope(); | ||
var mediator = scope.ServiceProvider.GetRequiredService<IMediator>(); | ||
|
||
var result = await mediator.Send(new UpdateUserWeatherSubscriptionCommand | ||
{ | ||
Location = @event.Location, | ||
ResendInterval = @event.ResendInterval, | ||
UserTelegramId = @event.TelegramUserId | ||
}); | ||
|
||
if (result.IsFailed) return new UniversalResponse(result.Errors.First().Message); | ||
|
||
var allSubscriptions = await cacheService.GetAsync<List<UserSubscriptionInfo>>("allSubscriptions"); | ||
await cacheService.RemoveAsync("allSubscriptions"); | ||
|
||
allSubscriptions ??= []; | ||
|
||
allSubscriptions.RemoveAll(x => | ||
x.TelegramId == @event.TelegramUserId && x.Location == @event.Location); | ||
|
||
allSubscriptions.Add(new() | ||
{ | ||
ResendInterval = @event.ResendInterval, | ||
Location = @event.Location, | ||
TelegramId = @event.TelegramUserId | ||
}); | ||
await cacheService.SetAsync("allSubscriptions", allSubscriptions); | ||
|
||
return new UniversalResponse("Subscription updated successfully"); | ||
} | ||
} |
Oops, something went wrong.