Skip to content

Commit

Permalink
Feature: Adicionado endpoint para deleção de template de email.
Browse files Browse the repository at this point in the history
  • Loading branch information
LibardiFelipe committed Oct 10, 2022
1 parent f7c006f commit 31951fd
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using TemplateBase.Application.Commands.Base;

namespace TemplateBase.Application.Commands.TemplatesEmail
{
public class DeleteEmailTemplateCommand : Command
{
public DeleteEmailTemplateCommand(Guid id)
{
Id = id;
}

public Guid Id { get; set; }

public override void Validate()
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ namespace TemplateBase.Application.Commands.TemplatesEmail
{
public class TemplateEmailCommandHandler : CommandHandler,
IRequestHandler<CreateTemplateEmailCommand, Result>,
IRequestHandler<UpdateTemplateEmailCommand, Result>
IRequestHandler<UpdateTemplateEmailCommand, Result>,
IRequestHandler<DeleteEmailTemplateCommand, Result>
{
private readonly ITemplateEmailService _emailTemplateService;

Expand Down Expand Up @@ -44,5 +45,18 @@ public async Task<Result> Handle(UpdateTemplateEmailCommand request, Cancellatio

return new Result(DefaultMessages.Handler_ComandoExecutado, true, entity);
}

public async Task<Result> Handle(DeleteEmailTemplateCommand request, CancellationToken cancellationToken)
{
if (request.IsInvalid())
return new Result(DefaultMessages.Handler_ComandoInvalido, false, request.Notifications);

var entity = await _emailTemplateService.DeleteTemplateEmailAsync(request.Id, cancellationToken);

if (_emailTemplateService.IsInvalid())
return new Result(DefaultMessages.Handler_FalhaAoExecutarComando, false, _emailTemplateService.GetNotifications());

return new Result(DefaultMessages.Handler_ComandoExecutado, true, entity);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace TemplateBase.Domain.Services.Contracts
public interface ITemplateEmailService : IService
{
Task<TemplateEmail> CreateTemplateEmailAsync(string name, string body, CancellationToken cancellationToken);
Task<TemplateEmail> UpdateTemplateEmailAsync(Guid id, string name, string body, CancellationToken cancellationToken);
Task<TemplateEmail> UpdateTemplateEmailAsync(Guid id, string name, string body, CancellationToken cancellationToken);
Task<bool> DeleteTemplateEmailAsync(Guid id, CancellationToken cancellationToken);
}
}
21 changes: 21 additions & 0 deletions TemplateBase.Domain/Services/TemplateEmailService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
using TemplateBase.Domain.Contracts;
using TemplateBase.Domain.Entities;
using TemplateBase.Domain.Resources;
Expand Down Expand Up @@ -64,7 +65,27 @@ public async Task<TemplateEmail> UpdateTemplateEmailAsync(Guid id, string name,
return null;
}

public async Task<bool> DeleteTemplateEmailAsync(Guid id, CancellationToken cancellationToken)
{
var repo = _uow.Repository<TemplateEmail>();

var entity = await repo.GetByIdAsync(id, cancellationToken);
if (entity is null)
{
AddNotification("Id", string.Format(DefaultMessages.EntidadeNaoEncontrado, "Template"));
return false;
}

repo.Delete(entity);
if (await _uow.CommitAsync(cancellationToken) > 0)
return true;

AddNotification("", DefaultMessages.Service_InternalError);
return false;
}

public IReadOnlyCollection<Notification> GetNotifications() => Notifications;
public bool IsInvalid() => Notifications.Any();

}
}
13 changes: 13 additions & 0 deletions TemplateBase.WebAPI/Controllers/TemplatesEmailController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
using TemplateBase.Application.Commands.TemplatesEmail;
using TemplateBase.Application.Queries.TemplatesEmail;
Expand Down Expand Up @@ -72,5 +73,17 @@ public async Task<IActionResult> GetByIdAsync([FromRoute] string id)
? Ok(response)
: BadRequest(response);
}

[HttpDelete("{id}")]
public async Task<IActionResult> DeleteByIdAsync([FromRoute] Guid id)
{
var command = new DeleteEmailTemplateCommand(id);
var result = await _mediator.Send(command);
var response = _mapper.Map<ResultViewModel>(result);

return response.Success
? Ok(response)
: BadRequest(response);
}
}
}

0 comments on commit 31951fd

Please sign in to comment.