Skip to content
This repository was archived by the owner on May 14, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions FileReceiver.sln
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FileReceiver.Bl.Abstract",
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FileReceiver.Bl.Impl", "src\FileReceiver.Bl.Impl\FileReceiver.Bl.Impl.csproj", "{9AC28975-E12B-449D-B340-782EBAB3E858}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FileReceiver.Tests.Bl", "tests\FileReceiver.Tests.Bl\FileReceiver.Tests.Bl.csproj", "{97FA2C2F-34CE-46F3-A426-C5BF1E6A3C54}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{165C050E-2183-4DB4-A63D-B362532F77F4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -35,6 +39,7 @@ Global
{8D9067D7-7807-4616-9C5C-800B53EC5010} = {9045B708-F21E-4EB8-ABB2-76EB6695F6A3}
{92A3C8FD-79EE-4157-9035-C2110F8953DB} = {8D9067D7-7807-4616-9C5C-800B53EC5010}
{9AC28975-E12B-449D-B340-782EBAB3E858} = {8D9067D7-7807-4616-9C5C-800B53EC5010}
{97FA2C2F-34CE-46F3-A426-C5BF1E6A3C54} = {165C050E-2183-4DB4-A63D-B362532F77F4}
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{70B0EF0C-D42D-4CF2-93E2-357FC9190A32}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
Expand Down Expand Up @@ -65,5 +70,9 @@ Global
{9AC28975-E12B-449D-B340-782EBAB3E858}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9AC28975-E12B-449D-B340-782EBAB3E858}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9AC28975-E12B-449D-B340-782EBAB3E858}.Release|Any CPU.Build.0 = Release|Any CPU
{97FA2C2F-34CE-46F3-A426-C5BF1E6A3C54}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{97FA2C2F-34CE-46F3-A426-C5BF1E6A3C54}.Debug|Any CPU.Build.0 = Debug|Any CPU
{97FA2C2F-34CE-46F3-A426-C5BF1E6A3C54}.Release|Any CPU.ActiveCfg = Release|Any CPU
{97FA2C2F-34CE-46F3-A426-C5BF1E6A3C54}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
Binary file added IssueReproduce.zip
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using FileReceiver.Bl.Abstract.Handlers;
using FileReceiver.Common.Models;
using FileReceiver.Common.Enums;

namespace FileReceiver.Bl.Abstract.Factories
{
public interface IUpdateHandlerFactory
{
IUpdateHandler CreateUpdateHandler(TransactionModel transaction);
IUpdateHandler CreateUpdateHandler(TransactionType transactionType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Telegram.Bot" Version="16.0.0" />
<PackageReference Include="Telegram.Bot" Version="16.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Threading.Tasks;

using FileReceiver.Common.Enums;

namespace FileReceiver.Bl.Abstract.Services
{
public interface IFileReceivingSessionService
{
Task CreateFileReceivingSessionAsync(long userId);
Task SetFileSizeConstraintAsync(Guid sessionId, int bytes = 1_000_000);
Task SetFileNameConstraintAsync(Guid sessionId, string regexPatterns);
Task SetFileExtensionConstraintAsync(Guid sessionId, string extensions);
Task SetSessionMaxFilesConstraintAsync(long userId, Guid sessionId, int amount = 50);
Task SetFilesStorageAsync(long userId, FileStorageType storageType);
Task<string> ExecuteSessionAsync(long userId);
Task StopSessionAsync(long userId);
Task<FileReceivingSessionState> GetSessionStateAsync(Guid sessionId);
Task<Guid?> GetFirstActiveFileReceivingSessionIdByUserIdAsync(long userId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using AutoMapper;

using FileReceiver.Bl.Impl.AutoMapper.ValueResolvers;
using FileReceiver.Common.Models;
using FileReceiver.Dal.Entities;

namespace FileReceiver.Bl.Impl.AutoMapper.Profiles
{
public class FileReceivingSessionProfile : Profile
{
public FileReceivingSessionProfile()
{
CreateMap<FileReceivingSessionModel, FileReceivingSessionEntity>()
.ForMember(x => x.Constrains,
opt => opt.MapFrom(x => x.Constrains.ConstraintsAsJson));
CreateMap<FileReceivingSessionEntity, FileReceivingSessionModel>()
.ForMember(x => x.Constrains,
opt => opt.MapFrom(new FileReceiverSessionTypeResolver()));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using AutoMapper;

using FileReceiver.Common.Models;
using FileReceiver.Dal.Entities;

namespace FileReceiver.Bl.Impl.AutoMapper.ValueResolvers
{
public class FileReceiverSessionTypeResolver
: IValueResolver<FileReceivingSessionEntity, FileReceivingSessionModel, ConstraintsModel>
{
public ConstraintsModel Resolve(FileReceivingSessionEntity source, FileReceivingSessionModel destination,
ConstraintsModel destMember, ResolutionContext context)
{
return new ConstraintsModel(source.Constrains);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public ICallbackQueryHandler CreateCallbackHandler(CallbackQuery callbackQuery)
{
{ Data: { } data } when data.StartsWith("profile-")
=> _serviceProvider.GetService<EditProfileCallbackQueryHandler>(),
_ => throw new ArgumentOutOfRangeException(nameof(callbackQuery), callbackQuery, null)
{ Data: { } data } when data.StartsWith("fr-session")
=> _serviceProvider.GetService<FileReceivingSessionCallbackQueryHandler>(),
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public ICommandHandler CreateCommandHandler(string command)
"/abort" => _serviceProvider.GetService<CancelCommandHandler>(),
"/profile" => _serviceProvider.GetService<ProfileCommandHandler>(),
"/profile_edit" => _serviceProvider.GetService<ProfileEditCommandHandler>(),
"/start_receiving" => _serviceProvider.GetService<StartReceivingCommandHandler>(),
{ } when command.StartsWith("/") => _serviceProvider.GetService<DefaultCommandHandler>(),
_ => null
};
Expand Down
6 changes: 3 additions & 3 deletions src/FileReceiver.Bl.Impl/Factories/UpdateHandlerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using FileReceiver.Bl.Abstract.Handlers;
using FileReceiver.Bl.Impl.Handlers.TelegramUpdate;
using FileReceiver.Common.Enums;
using FileReceiver.Common.Models;

using Microsoft.Extensions.DependencyInjection;

Expand All @@ -19,13 +18,14 @@ public UpdateHandlerFactory(IServiceProvider serviceProvider)
_serviceProvider = serviceProvider;
}

public IUpdateHandler CreateUpdateHandler(TransactionModel transaction)
public IUpdateHandler CreateUpdateHandler(TransactionType transactionType)
{
return transaction.TransactionType switch
return transactionType switch
{
TransactionType.Unknown => _serviceProvider.GetService<DefaultUpdateHandler>(),
TransactionType.Registration => _serviceProvider.GetService<RegistrationUpdateHandler>(),
TransactionType.EditProfile => _serviceProvider.GetService<EditProfileUpdateHandler>(),
TransactionType.FileReceivingSessionCreating => _serviceProvider.GetService<FileReceivingSessionCreatingUpdateHandler>(),
_ => _serviceProvider.GetService<DefaultUpdateHandler>(),
};
}
Expand Down
5 changes: 5 additions & 0 deletions src/FileReceiver.Bl.Impl/FileReceiverBlInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ private static void AddServices(this IServiceCollection services)
services.AddTransient<IUpdateHandlerService, UpdateHandlerService>();
services.AddTransient<IBotMessagesService, BotMessagesService>();
services.AddTransient<IUserRegistrationService, UserRegistrationService>();
services.AddTransient<IFileReceivingSessionService, FileReceivingSessionService>();
}

private static void AddMapperConfiguration(this IServiceCollection services)
Expand All @@ -41,6 +42,7 @@ private static void AddMapperConfiguration(this IServiceCollection services)
{
new RegistrationProfile(),
new TransactionsProfile(),
new FileReceivingSessionProfile(),
}
));
}
Expand All @@ -53,18 +55,21 @@ private static void AddCommandHandlers(this IServiceCollection services)
services.AddTransient<DefaultCommandHandler, DefaultCommandHandler>();
services.AddTransient<ProfileCommandHandler, ProfileCommandHandler>();
services.AddTransient<ProfileEditCommandHandler, ProfileEditCommandHandler>();
services.AddTransient<StartReceivingCommandHandler, StartReceivingCommandHandler>();
}

private static void AddUpdateHandlers(this IServiceCollection services)
{
services.AddTransient<RegistrationUpdateHandler, RegistrationUpdateHandler>();
services.AddTransient<DefaultUpdateHandler, DefaultUpdateHandler>();
services.AddTransient<EditProfileUpdateHandler, EditProfileUpdateHandler>();
services.AddTransient<FileReceivingSessionCreatingUpdateHandler, FileReceivingSessionCreatingUpdateHandler>();
}

private static void AddCallbackQueryHandlers(this IServiceCollection services)
{
services.AddTransient<EditProfileCallbackQueryHandler, EditProfileCallbackQueryHandler>();
services.AddTransient<FileReceivingSessionCallbackQueryHandler, FileReceivingSessionCallbackQueryHandler>();
}

private static void AddFactories(this IServiceCollection services)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
using FileReceiver.Common.Models;
using FileReceiver.Dal.Abstract.Repositories;
using FileReceiver.Dal.Entities;
using FileReceiver.Dal.Entities.Enums;

using Telegram.Bot.Types;

namespace FileReceiver.Bl.Impl.Handlers.CallbackQuery
{
Expand All @@ -21,19 +18,17 @@ public class EditProfileCallbackQueryHandler : ICallbackQueryHandler
private readonly IUserRegistrationService _userRegistrationService;
private readonly ITransactionRepository _transactionRepository;
private readonly IMapper _mapper;
private readonly IUpdateHandlerFactory _updateHandlerFactory;

public EditProfileCallbackQueryHandler(
IBotMessagesService botMessagesService,
IUserRegistrationService userRegistrationService,
ITransactionRepository transactionRepository,
IMapper mapper, IUpdateHandlerFactory updateHandlerFactory)
IMapper mapper)
{
_botMessagesService = botMessagesService;
_userRegistrationService = userRegistrationService;
_transactionRepository = transactionRepository;
_mapper = mapper;
_updateHandlerFactory = updateHandlerFactory;
_userRegistrationService = userRegistrationService;
}

public async Task HandleCallback(Telegram.Bot.Types.CallbackQuery callbackQuery)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System.Threading.Tasks;

using FileReceiver.Bl.Abstract.Factories;
using FileReceiver.Bl.Abstract.Handlers;
using FileReceiver.Common.Enums;

using Telegram.Bot.Types;

namespace FileReceiver.Bl.Impl.Handlers.CallbackQuery
{
public class FileReceivingSessionCallbackQueryHandler : ICallbackQueryHandler
{
private readonly IUpdateHandlerFactory _updateHandlerFactory;

public FileReceivingSessionCallbackQueryHandler(
IUpdateHandlerFactory updateHandlerFactory)
{
_updateHandlerFactory = updateHandlerFactory;
}

public async Task HandleCallback(Telegram.Bot.Types.CallbackQuery callbackQuery)
{
switch (callbackQuery)
{
case { Data: "fr-session-storage-mega" }:
await SetFilesStorageAsync(callbackQuery, FileStorageType.Mega);
break;
case { Data: "fr-session-execute" }:
await ExecuteSessionAsync(callbackQuery);
break;
case { Data: "fr-session-cancel" }:
await StopSessionAsync(callbackQuery);
break;
}
}

private async Task SetFilesStorageAsync(Telegram.Bot.Types.CallbackQuery callbackQuery, FileStorageType storage)
{
await _updateHandlerFactory
.CreateUpdateHandler(TransactionType.FileReceivingSessionCreating)
.HandleUpdateAsync(new Update()
{
CallbackQuery = callbackQuery,
Message = new Message()
{
Text = storage.ToString(),
},
});
}

private async Task ExecuteSessionAsync(Telegram.Bot.Types.CallbackQuery callbackQuery)
{
await _updateHandlerFactory
.CreateUpdateHandler(TransactionType.FileReceivingSessionCreating)
.HandleUpdateAsync(new Update()
{
CallbackQuery = callbackQuery,
});
}

private async Task StopSessionAsync(Telegram.Bot.Types.CallbackQuery callbackQuery)
{
await _updateHandlerFactory
.CreateUpdateHandler(TransactionType.FileReceivingSessionCreating)
.HandleUpdateAsync(new Update()
{
CallbackQuery = callbackQuery,
});
}
}
}
17 changes: 5 additions & 12 deletions src/FileReceiver.Bl.Impl/Handlers/Commands/CancelCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,31 @@

using FileReceiver.Bl.Abstract.Handlers;
using FileReceiver.Bl.Abstract.Services;
using FileReceiver.Common.Extensions;
using FileReceiver.Dal.Abstract.Repositories;
using FileReceiver.Dal.Entities.Enums;

using Telegram.Bot.Types;

namespace FileReceiver.Bl.Impl.Handlers.Commands
{
public class CancelCommandHandler : ICommandHandler
{
private readonly IUserRepository _userRepository;
private readonly ITransactionRepository _transactionRepository;
private readonly IBotMessagesService _botMessagesService;

public CancelCommandHandler(
IUserRepository userRepository,
ITransactionRepository transactionRepository, IBotMessagesService botMessagesService)
ITransactionRepository transactionRepository,
IBotMessagesService botMessagesService)
{
_userRepository = userRepository;
_transactionRepository = transactionRepository;
_botMessagesService = botMessagesService;
}

public async Task HandleCommandAsync(Update update)
{
var userId = update.Message.From.Id;
var userId = update.GetTgUserId();

var activeTransaction = await _transactionRepository.GetLastActiveTransactionByUserId(userId);
if (activeTransaction != null)
{
activeTransaction.TransactionState = TransactionStateDb.Aborted;
await _transactionRepository.UpdateAsync(activeTransaction);
}
await _transactionRepository.AbortAllTransactionsForUser(userId);

await _botMessagesService.SendMenuAsync(userId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public DefaultCommandHandler(IBotMessagesService botMessagesService)

public async Task HandleCommandAsync(Update update)
{
var userId = update.Message.From.Id;
var userId = update.GetTgUserId();
await _botMessagesService.SendNotSupportedAsync(userId, update.Message.Text.GetCommandFromMessage());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using FileReceiver.Bl.Abstract.Handlers;
using FileReceiver.Bl.Abstract.Services;
using FileReceiver.Common.Extensions;
using FileReceiver.Dal.Abstract.Repositories;

using Telegram.Bot.Types;
Expand All @@ -22,8 +23,8 @@ public ProfileCommandHandler(IBotMessagesService botMessagesService,

public async Task HandleCommandAsync(Update update)
{
var userId = update.Message.From.Id;
if (!await _userRepository.CheckIfUserExists(userId))
var userId = update.GetTgUserId();
if (!await _userRepository.CheckIfUserExistsAsync(userId))
{
await _botMessagesService.SendErrorAsync(userId,
"You should register before you can use this command, to do this you can use command /register");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using FileReceiver.Bl.Abstract.Handlers;
using FileReceiver.Bl.Abstract.Services;
using FileReceiver.Common.Extensions;
using FileReceiver.Dal.Abstract.Repositories;

using Telegram.Bot.Types;
Expand All @@ -23,9 +24,9 @@ public ProfileEditCommandHandler(IBotMessagesService botMessagesService,

public async Task HandleCommandAsync(Update update)
{
var userId = update.Message.From.Id;
var userId = update.GetTgUserId();

if (!await _userRepository.CheckIfUserExists(userId))
if (!await _userRepository.CheckIfUserExistsAsync(userId))
{
await _botMessagesService.SendErrorAsync(userId,
"You should register before you can use this command, to do this you can use command /register");
Expand Down
Loading