Skip to content

Commit

Permalink
Merge pull request #10 from LibardiFelipe/teste
Browse files Browse the repository at this point in the history
Teste
  • Loading branch information
LibardiFelipe committed Oct 14, 2022
2 parents 6435ca6 + 26f96ad commit e768b33
Show file tree
Hide file tree
Showing 55 changed files with 1,223 additions and 173 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace TemplateBase.Application.Commands.Login
{
public class AuthenticateCommandHandler : CommandHandler,
IRequestHandler<AuthenticationCommand, Result>
IRequestHandler<UserLoginCommand, Result>
{
private readonly IAuthenticationService _authService;

Expand All @@ -18,7 +18,7 @@ public AuthenticateCommandHandler(IAuthenticationService authService)
_authService = authService;
}

public async Task<Result> Handle(AuthenticationCommand request, CancellationToken cancellationToken)
public async Task<Result> Handle(UserLoginCommand request, CancellationToken cancellationToken)
{
if (request.IsInvalid())
return new Result(DefaultMessages.Handler_ComandoInvalido, false, request.Notifications);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace TemplateBase.Application.Commands.Login
{
public class AuthenticationCommand : Command
public class UserLoginCommand : Command
{
public string Email { get; set; }
public string Password { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using TemplateBase.Application.Commands.Base;

namespace TemplateBase.Application.Commands.TemplatesEmail
{
public class CreateTemplateEmailCommand : Command
{
public string Name { get; set; }
public string Body { get; set; }

public override void Validate()
{
}
}
}
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
@@ -0,0 +1,62 @@
using MediatR;
using System.Threading;
using System.Threading.Tasks;
using TemplateBase.Application.Commands.Base;
using TemplateBase.Application.Models;
using TemplateBase.Domain.Resources;
using TemplateBase.Domain.Services.Contracts;

namespace TemplateBase.Application.Commands.TemplatesEmail
{
public class TemplateEmailCommandHandler : CommandHandler,
IRequestHandler<CreateTemplateEmailCommand, Result>,
IRequestHandler<UpdateTemplateEmailCommand, Result>,
IRequestHandler<DeleteEmailTemplateCommand, Result>
{
private readonly ITemplateEmailService _emailTemplateService;

public TemplateEmailCommandHandler(ITemplateEmailService emailTemplateService)
{
_emailTemplateService = emailTemplateService;
}

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

var entity = await _emailTemplateService.CreateTemplateEmailAsync(request.Name, request.Body, cancellationToken);

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

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

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

var entity = await _emailTemplateService.UpdateTemplateEmailAsync(request.Id, request.Name, request.Body, cancellationToken);

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

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
@@ -0,0 +1,16 @@
using System;
using TemplateBase.Application.Commands.Base;

namespace TemplateBase.Application.Commands.TemplatesEmail
{
public class UpdateTemplateEmailCommand : Command
{
public Guid Id { get; set; }
public string Name { get; set; }
public string Body { get; set; }

public override void Validate()
{
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
using System;
using Microsoft.AspNetCore.Http;
using System;
using TemplateBase.Application.Commands.Base;

namespace TemplateBase.Application.Commands.Persons
{
public class CreateUserCommand : Command
public class RegisterUserCommand : Command
{
public string Name { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public string ProfilePictureUrl { get; set; }
public DateTime BirthDate { get; set; }
public IFormFile ProfilePicture { get; set; }

public override void Validate()
{
Expand Down
33 changes: 24 additions & 9 deletions TemplateBase.Application/Commands/Users/UserCommandHandler.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using MediatR;
using System.Threading;
using System.Threading.Tasks;
using TemplateBase.Application.Commands.Auth;
using TemplateBase.Application.Commands.Base;
using TemplateBase.Application.Models;
using TemplateBase.Domain.Resources;
Expand All @@ -9,25 +10,39 @@
namespace TemplateBase.Application.Commands.Persons
{
public class UserCommandHandler : CommandHandler,
IRequestHandler<CreateUserCommand, Result>
IRequestHandler<RegisterUserCommand, Result>,
IRequestHandler<VerifyUserCommand, Result>
{
private readonly IUserService _personService;
private readonly IUserService _userService;

public UserCommandHandler(IUserService personService)
public UserCommandHandler(IUserService userService)
{
_personService = personService;
_userService = userService;
}

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

var entity = await _personService.CreateUserAsync(request.Name, request.Email,
request.Password, request.ProfilePictureUrl, request.BirthDate, cancellationToken);
var entity = await _userService.RegisterUserAsync(request.Name, request.Email,
request.Password, request.BirthDate, request.ProfilePicture, cancellationToken);

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

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

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

var entity = await _userService.VerifyUserAsync(request.Hash, cancellationToken);

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

return new Result(DefaultMessages.Handler_ComandoExecutado, true, entity);
}
Expand Down
23 changes: 23 additions & 0 deletions TemplateBase.Application/Commands/Users/VerifyUserCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using TemplateBase.Application.Commands.Base;

namespace TemplateBase.Application.Commands.Auth
{
public class VerifyUserCommand : Command
{
public VerifyUserCommand()
{

}

public VerifyUserCommand(string hash)
{
Hash = hash;
}

public string Hash { get; set; }

public override void Validate()
{
}
}
}
17 changes: 16 additions & 1 deletion TemplateBase.Application/Queries/QueryHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
using System.Threading;
using System.Threading.Tasks;
using TemplateBase.Application.Models;
using TemplateBase.Application.Queries.TemplatesEmail;
using TemplateBase.Application.Queries.Users;
using TemplateBase.Domain.Contracts;
using TemplateBase.Domain.Entities;
using TemplateBase.Domain.Resources;

namespace TemplateBase.Application.Queries
{
public class QueryHandler : IRequestHandler<UserQuery, Result>
public class QueryHandler :
IRequestHandler<UserQuery, Result>,
IRequestHandler<TemplateEmailQuery, Result>
{
private readonly IUnitOfWork _unitOfWork;

Expand All @@ -29,5 +32,17 @@ public async Task<Result> Handle(UserQuery request, CancellationToken cancellati

return new Result(DefaultMessages.Handler_QueryExecutada, true, result);
}

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

var repo = _unitOfWork.Repository<TemplateEmail>();
var spec = request.ToSpecification();
var result = await repo.GetAllAsync(spec, cancellationToken);

return new Result(DefaultMessages.Handler_QueryExecutada, true, result);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using TemplateBase.Application.Queries.Base;
using TemplateBase.Domain.Contracts;
using TemplateBase.Domain.Entities;
using TemplateBase.Domain.Specifications;

namespace TemplateBase.Application.Queries.TemplatesEmail
{
public class TemplateEmailQuery : Query<TemplateEmail>
{
#region Construtores
public TemplateEmailQuery() { }
public TemplateEmailQuery(string id) : base(id) { }
#endregion

#region Specification
public override ISpecification<TemplateEmail> ToSpecification()
{
var spec = new TemplateEmailSpec();

if (_id.HasValue)
spec.FilterById(_id.Value);

return spec;
}
#endregion

#region Validações
public override void Validate()
{
/*
* Caso queira trabalhar com failfast validations,
* aqui dentro é o local onde essas validações
* deverão ser feitas.
*/
}
#endregion
}
}
10 changes: 5 additions & 5 deletions TemplateBase.Application/Queries/Users/UserQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class UserQuery : Query<User>
#region Membros privados
private string _name;
private string _email;
private EUserPermission? _permission = null;
private EUserType? _type = null;
private DateTime? _birthDate = null;
#endregion

Expand All @@ -34,9 +34,9 @@ public UserQuery FilterByEmail(string value)
return this;
}

public UserQuery FilterByPermission(EUserPermission? value)
public UserQuery FilterByType(EUserType? value)
{
_permission = value;
_type = value;
return this;
}

Expand All @@ -61,8 +61,8 @@ public override ISpecification<User> ToSpecification()
if (!string.IsNullOrWhiteSpace(_email))
spec.FilterByEmail(_email);

if (_permission.HasValue)
spec.FilterByPermission(_permission.Value);
if (_type.HasValue)
spec.FilterByType(_type.Value);

if (_birthDate.HasValue)
spec.FilterByBirthDate(_birthDate.Value);
Expand Down
2 changes: 1 addition & 1 deletion TemplateBase.Domain/Classes/AuthData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class AuthData
{
public string Name { get; set; }
public string Email { get; set; }
public EUserPermission Permission { get; set; }
public EUserType Type { get; set; }
public string Token { get; set; }
}
}
33 changes: 33 additions & 0 deletions TemplateBase.Domain/Classes/Email.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Collections.Generic;

namespace TemplateBase.Domain.Classes
{
public class Email
{
private List<string> _addressees = new List<string>();

public string Subject { get; set; }
public string Body { get; set; }
public IReadOnlyCollection<string> Addressees { get { return _addressees; } }

public void InjectToBody(Dictionary<string, string> values)
{
if (string.IsNullOrWhiteSpace(Body))
return;

foreach (var item in values)
Body = Body.Replace(item.Key, item.Value);
}

public void AddAddressee(string email)
{
_addressees.Add(email.Trim());
}

public void AddAddressee(List<string> emails)
{
foreach (var email in emails)
_addressees.Add(email.Trim());
}
}
}
Loading

0 comments on commit e768b33

Please sign in to comment.