mirror of
https://github.com/wismna/ModernKeePass.git
synced 2025-10-03 15:40:18 -04:00
Attachment Add and Delete commands implemented
This commit is contained in:
@@ -88,7 +88,9 @@
|
||||
<Compile Include="Common\Interfaces\ISettingsProxy.cs" />
|
||||
<Compile Include="Common\Mappings\IMapFrom.cs" />
|
||||
<Compile Include="Common\Mappings\MappingProfile.cs" />
|
||||
<Compile Include="Entry\Commands\AddAttachment\AddAttachmentCommand.cs" />
|
||||
<Compile Include="Entry\Commands\AddHistory\AddHistoryCommand.cs" />
|
||||
<Compile Include="Entry\Commands\DeleteAttachment\DeleteAttachmentCommand.cs" />
|
||||
<Compile Include="Entry\Commands\DeleteHistory\DeleteHistoryCommand.cs" />
|
||||
<Compile Include="Entry\Commands\RestoreHistory\RestoreHistoryCommand.cs" />
|
||||
<Compile Include="Entry\Queries\GetEntry\GetEntryQuery.cs" />
|
||||
|
@@ -44,6 +44,8 @@ namespace ModernKeePass.Application.Common.Interfaces
|
||||
GroupEntity CreateGroup(string parentGroupId, string name, bool isRecycleBin = false);
|
||||
void SortEntries(string groupId);
|
||||
void SortSubGroups(string groupId);
|
||||
void AddAttachment(string entryId, string attachmentName, byte[] attachmentContent);
|
||||
void DeleteAttachment(string entryId, string attachmentName);
|
||||
|
||||
EntryEntity AddHistory(string entryId);
|
||||
EntryEntity RestoreFromHistory(string entryId, int historyIndex);
|
||||
|
@@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using MediatR;
|
||||
using ModernKeePass.Application.Common.Interfaces;
|
||||
using ModernKeePass.Application.Entry.Models;
|
||||
using ModernKeePass.Domain.Exceptions;
|
||||
|
||||
namespace ModernKeePass.Application.Entry.Commands.AddAttachment
|
||||
{
|
||||
public class AddAttachmentCommand : IRequest
|
||||
{
|
||||
public EntryVm Entry { get; set; }
|
||||
public string AttachmentName { get; set; }
|
||||
public byte[] AttachmentContent { get; set; }
|
||||
|
||||
public class AddAttachmentCommandHandler : IRequestHandler<AddAttachmentCommand>
|
||||
{
|
||||
private readonly IDatabaseProxy _database;
|
||||
|
||||
public AddAttachmentCommandHandler(IDatabaseProxy database)
|
||||
{
|
||||
_database = database;
|
||||
}
|
||||
|
||||
public void Handle(AddAttachmentCommand message)
|
||||
{
|
||||
if (!_database.IsOpen) throw new DatabaseClosedException();
|
||||
|
||||
if (message.Entry.Attachments.ContainsKey(message.AttachmentName)) throw new ArgumentException("AttachmentAlreadyExists", nameof(message.AttachmentName));
|
||||
_database.AddAttachment(message.Entry.Id, message.AttachmentName, message.AttachmentContent);
|
||||
message.Entry.Attachments.Add(message.AttachmentName, message.AttachmentContent);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,33 @@
|
||||
using System.Collections.Generic;
|
||||
using MediatR;
|
||||
using ModernKeePass.Application.Common.Interfaces;
|
||||
using ModernKeePass.Application.Entry.Models;
|
||||
using ModernKeePass.Domain.Exceptions;
|
||||
|
||||
namespace ModernKeePass.Application.Entry.Commands.DeleteAttachment
|
||||
{
|
||||
public class DeleteAttachmentCommand : IRequest
|
||||
{
|
||||
public EntryVm Entry { get; set; }
|
||||
public string AttachmentName { get; set; }
|
||||
|
||||
public class DeleteAttachmentCommandHandler : IRequestHandler<DeleteAttachmentCommand>
|
||||
{
|
||||
private readonly IDatabaseProxy _database;
|
||||
|
||||
public DeleteAttachmentCommandHandler(IDatabaseProxy database)
|
||||
{
|
||||
_database = database;
|
||||
}
|
||||
|
||||
public void Handle(DeleteAttachmentCommand message)
|
||||
{
|
||||
if (!_database.IsOpen) throw new DatabaseClosedException();
|
||||
|
||||
if (!message.Entry.Attachments.ContainsKey(message.AttachmentName)) throw new KeyNotFoundException("AttachmentDoesntExist");
|
||||
_database.DeleteAttachment(message.Entry.Id, message.AttachmentName);
|
||||
message.Entry.Attachments.Remove(message.AttachmentName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user