Skip to content

Commit

Permalink
Have configured cache with redis, added handler to each response, fix…
Browse files Browse the repository at this point in the history
…ed json settings, changed docker-compose.yml
  • Loading branch information
shishnk committed Apr 7, 2024
1 parent ae135c1 commit ab52bda
Show file tree
Hide file tree
Showing 37 changed files with 341 additions and 108 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Reflection;
using DatabaseApp.Application.Common.Behaviors;
using DatabaseApp.Application.Common.Mapping;
using DatabaseApp.Application.Users.Queries.GetAllUsers;
using FluentValidation;
using Mapster;
using MapsterMapper;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
using System.Text.Json;
using DatabaseApp.Application.Users.Queries.GetAllUsers;
using MediatR;
using TelegramBotApp.Domain.Responses;
using Microsoft.Extensions.DependencyInjection;
using TelegramBotApp.Messaging.IntegrationContext;
using TelegramBotApp.Messaging.IntegrationContext.UserIntegrationEvents;
using TelegramBotApp.Messaging.IntegrationResponseContext.IntegrationResponses;

namespace DatabaseApp.IntegrationEvents.IntegrationEventHandlers;

public class GetAllUsersRequestIntegrationEventHandler(ISender mediator)
public class GetAllUsersRequestIntegrationEventHandler(IServiceScopeFactory factory)
: IIntegrationEventHandler<GetAllUsersRequestIntegrationEvent>
{
public async Task<UniversalResponse?> Handle(GetAllUsersRequestIntegrationEvent @event)
public async Task<IResponseMessage?> Handle(GetAllUsersRequestIntegrationEvent @event)
{
using var scope = factory.CreateScope();
var mediator = scope.ServiceProvider.GetRequiredService<IMediator>();
var users = await mediator.Send(new GetAllUsersQuery());
return new(JsonSerializer.Serialize(users.Select(u => u.TelegramId)));
return new AllUsersResponse { UserTelegramIds = users.Select(u => u.TelegramId).ToList() };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

<ItemGroup>
<ProjectReference Include="..\..\Core\DatabaseApp.Application\DatabaseApp.Application.csproj" />
<ProjectReference Include="..\..\Infrastructure\DatabaseApp.IntegrationEvents\DatabaseApp.IntegrationEvents.csproj" />
<ProjectReference Include="..\..\Infrastructure\DatabaseApp.Persistence\DatabaseApp.Persistence.csproj" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System.Reflection;
using DatabaseApp.Application;
using DatabaseApp.IntegrationEvents;
using DatabaseApp.Persistence;
using DatabaseApp.Persistence.DatabaseContext;
using DatabaseApp.WebApi.Middleware;
using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Any;
using Serilog;
using TelegramBotApp.Messaging;

var builder = WebApplication.CreateBuilder(args);

Expand All @@ -28,7 +30,7 @@
builder.Services.AddExceptionHandler<GlobalExceptionHandler>();
builder.Services.AddProblemDetails();
builder.Services.AddControllers();
builder.Services.AddApplication().AddPersistence(builder.Configuration);
builder.Services.AddApplication().AddPersistence(builder.Configuration).AddMessaging(builder.Configuration);

var app = builder.Build();

Expand All @@ -54,6 +56,7 @@
});
}

app.SubscribeToEvents();
app.UseHttpsRedirection();
app.UseExceptionHandler();
app.UseSerilogRequestLogging();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

<ItemGroup>
<ProjectReference Include="..\..\Infrastructure\TelegramBotApp.Messaging\TelegramBotApp.Messaging.csproj" />
<ProjectReference Include="..\TelegramBotApp.Domain\TelegramBotApp.Domain.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using FluentResults;
using Telegram.Bot;
using Telegram.Bot.Exceptions;
using Telegram.Bot.Polling;
using Telegram.Bot.Types;
using TelegramBotApp.Application.Factories;
using TelegramBotApp.Domain.Models;
using TelegramBotApp.Messaging;

namespace TelegramBotApp.Application.TelegramBotContext;
Expand Down Expand Up @@ -83,6 +83,7 @@ ApiRequestException apiRequestException
};

Console.WriteLine(errorMessage);

return Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Telegram.Bot;
using Telegram.Bot.Polling;
using Telegram.Bot.Types.Enums;
using TelegramBotApp.Domain.Models;

namespace TelegramBotApp.Application.TelegramBotContext;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using FluentResults;
using TelegramBotApp.Messaging;
using TelegramBotApp.Messaging.IntegrationContext.WeatherForecastIntegrationEvents;
using TelegramBotApp.Messaging.IntegrationResponseContext.IntegrationResponses;

namespace TelegramBotApp.Application.TelegramCommands;

Expand Down Expand Up @@ -36,7 +37,7 @@ public async Task<Result<string>> Execute(string command, string value, IEventBu
{
if (command != Command) throw new InvalidOperationException($"Invalid command {command}");

var response = await bus.Publish(new WeatherForecastRequestIntegrationEvent(value), ReplyNames.UniversalReply,
var response = await bus.Publish(new WeatherForecastRequestIntegrationEvent(value), nameof(UniversalResponse),
cancellationToken);

return response != null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Telegram.Bot.Types;
using TelegramBotApp.Messaging;

namespace TelegramBotApp.Application.TelegramBotContext;
namespace TelegramBotApp.Domain.Models;

public interface IBotInitializer
{
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,12 @@
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Telegram.Bot" Version="20.0.0-alpha.1"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Infrastructure\TelegramBotApp.Messaging\TelegramBotApp.Messaging.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Text.Json;
using Microsoft.Extensions.Caching.Distributed;

namespace TelegramBotApp.Caching.Caching;

public class CacheService(IDistributedCache distributedCache) : ICacheService
{
public async Task<T?> GetAsync<T>(string key, CancellationToken cancellationToken = default) where T : class
{
var value = await distributedCache.GetStringAsync(key, cancellationToken);

return value == null ? null : JsonSerializer.Deserialize<T>(value);
}

public async Task SetAsync<T>(string key, T value, CancellationToken cancellationToken = default) where T : class =>
await distributedCache.SetStringAsync(key, JsonSerializer.Serialize(value), cancellationToken);

public async Task RemoveAsync(string key, CancellationToken cancellationToken = default) =>
await distributedCache.RemoveAsync(key, cancellationToken);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace TelegramBotApp.Caching.Caching;

public interface ICacheService
{
public Task<T?> GetAsync<T>(string key, CancellationToken cancellationToken = default) where T : class;
public Task SetAsync<T>(string key, T value, CancellationToken cancellationToken = default) where T : class;
public Task RemoveAsync(string key, CancellationToken cancellationToken = default);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Microsoft.Extensions.DependencyInjection;
using TelegramBotApp.Caching.Caching;

namespace TelegramBotApp.Caching;

public static class DependencyInjection
{
// ReSharper disable once UnusedMethodReturnValue.Global
public static IServiceCollection AddCaching(this IServiceCollection services)
{
services.AddSingleton<ICacheService, CacheService>();
return services;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="8.0.1" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using TelegramBotApp.Messaging.Connection;
using TelegramBotApp.Messaging.EventBusContext;
using TelegramBotApp.Messaging.IntegrationContext;
using TelegramBotApp.Messaging.IntegrationResponseContext.IntegrationResponseHandlers;
using TelegramBotApp.Messaging.Settings;

namespace TelegramBotApp.Messaging;
Expand Down Expand Up @@ -30,4 +31,16 @@ public static IServiceCollection AddMessaging(this IServiceCollection services,

return services;
}

// ReSharper disable once UnusedMethodReturnValue.Global
public static IServiceCollection AddResponseHandlers(this IServiceCollection services)
{
var interfaceType = typeof(IResponseHandler);
foreach (var type in
AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(s => s.GetTypes())
.Where(p => interfaceType.IsAssignableFrom(p) && p.IsClass)) services.AddTransient(type);

return services;
}
}
Loading

0 comments on commit ab52bda

Please sign in to comment.