2020-03-24 13:01:14 +01:00
|
|
|
|
using MediatR;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using ModernKeePass.Application.Common.Interfaces;
|
|
|
|
|
using ModernKeePass.Domain.Dtos;
|
|
|
|
|
using ModernKeePass.Domain.Exceptions;
|
|
|
|
|
|
|
|
|
|
namespace ModernKeePass.Application.Database.Commands.CreateDatabase
|
|
|
|
|
{
|
2020-04-01 19:37:30 +02:00
|
|
|
|
public class CreateDatabaseCommand : IRequest
|
2020-03-24 13:01:14 +01:00
|
|
|
|
{
|
2020-03-26 19:04:51 +01:00
|
|
|
|
public string FilePath { get; set; }
|
|
|
|
|
public string Password { get; set; }
|
|
|
|
|
public string KeyFilePath { get; set; }
|
2020-03-24 13:01:14 +01:00
|
|
|
|
|
2020-04-01 19:37:30 +02:00
|
|
|
|
public class CreateDatabaseCommandHandler : IAsyncRequestHandler<CreateDatabaseCommand>
|
2020-03-24 13:01:14 +01:00
|
|
|
|
{
|
|
|
|
|
private readonly IDatabaseProxy _database;
|
2020-04-06 20:20:45 +02:00
|
|
|
|
private readonly IFileProxy _file;
|
2020-03-24 13:01:14 +01:00
|
|
|
|
|
2020-04-06 20:20:45 +02:00
|
|
|
|
public CreateDatabaseCommandHandler(IDatabaseProxy database, IFileProxy file)
|
2020-03-24 13:01:14 +01:00
|
|
|
|
{
|
|
|
|
|
_database = database;
|
2020-04-06 20:20:45 +02:00
|
|
|
|
_file = file;
|
2020-03-24 13:01:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-01 19:37:30 +02:00
|
|
|
|
public async Task Handle(CreateDatabaseCommand message)
|
2020-03-24 13:01:14 +01:00
|
|
|
|
{
|
2020-03-26 19:04:51 +01:00
|
|
|
|
if (_database.IsOpen) throw new DatabaseOpenException();
|
2020-03-24 13:01:14 +01:00
|
|
|
|
|
2020-04-06 20:20:45 +02:00
|
|
|
|
var file = await _file.OpenBinaryFile(message.FilePath);
|
|
|
|
|
await _database.Create(file,
|
2020-03-26 19:04:51 +01:00
|
|
|
|
new Credentials
|
|
|
|
|
{
|
2020-04-06 20:20:45 +02:00
|
|
|
|
KeyFileContents = await _file.OpenBinaryFile(message.KeyFilePath),
|
2020-03-26 19:04:51 +01:00
|
|
|
|
Password = message.Password
|
|
|
|
|
});
|
2020-04-06 20:20:45 +02:00
|
|
|
|
_database.FileAccessToken = message.FilePath;
|
2020-03-24 13:01:14 +01:00
|
|
|
|
}
|
2020-03-24 17:31:34 +01:00
|
|
|
|
|
2020-03-24 13:01:14 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|