2020-03-26 19:04:51 +01:00
|
|
|
|
using AutoMapper;
|
2020-03-26 12:25:22 +01:00
|
|
|
|
using MediatR;
|
|
|
|
|
using ModernKeePass.Application.Common.Interfaces;
|
|
|
|
|
using ModernKeePass.Application.Group.Models;
|
|
|
|
|
using ModernKeePass.Domain.Exceptions;
|
|
|
|
|
|
|
|
|
|
namespace ModernKeePass.Application.Group.Commands.CreateGroup
|
|
|
|
|
{
|
|
|
|
|
public class CreateGroupCommand : IRequest<GroupVm>
|
|
|
|
|
{
|
|
|
|
|
public GroupVm ParentGroup { get; set; }
|
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
public bool IsRecycleBin { get; set; }
|
|
|
|
|
|
2020-03-26 19:04:51 +01:00
|
|
|
|
public class CreateGroupCommandHandler : IRequestHandler<CreateGroupCommand, GroupVm>
|
2020-03-26 12:25:22 +01:00
|
|
|
|
{
|
|
|
|
|
private readonly IDatabaseProxy _database;
|
|
|
|
|
private readonly IMapper _mapper;
|
|
|
|
|
|
2020-03-26 19:04:51 +01:00
|
|
|
|
public CreateGroupCommandHandler(IDatabaseProxy database, IMapper mapper)
|
2020-03-26 12:25:22 +01:00
|
|
|
|
{
|
|
|
|
|
_database = database;
|
|
|
|
|
_mapper = mapper;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-26 19:04:51 +01:00
|
|
|
|
public GroupVm Handle(CreateGroupCommand message)
|
2020-03-26 12:25:22 +01:00
|
|
|
|
{
|
2020-03-26 19:04:51 +01:00
|
|
|
|
if (!_database.IsOpen) throw new DatabaseClosedException();
|
2020-03-26 12:25:22 +01:00
|
|
|
|
|
|
|
|
|
var group = _database.CreateGroup(message.ParentGroup.Id, message.Name, message.IsRecycleBin);
|
|
|
|
|
var groupVm = _mapper.Map<GroupVm>(group);
|
2020-04-01 12:48:36 +02:00
|
|
|
|
groupVm.ParentGroup = message.ParentGroup;
|
2020-03-26 12:25:22 +01:00
|
|
|
|
message.ParentGroup.SubGroups.Add(groupVm);
|
|
|
|
|
return groupVm;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|