Skip to content

Commit

Permalink
feat: inline constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
Amitpnk committed Oct 13, 2024
1 parent 0e9ce08 commit 7b4dc05
Show file tree
Hide file tree
Showing 26 changed files with 140 additions and 305 deletions.
18 changes: 6 additions & 12 deletions src/CleanArch.Api/Account/AccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,35 @@ namespace CleanArch.Api.Account;

[Route("api/[controller]")]
[ApiController]
public class AccountController : ControllerBase
public class AccountController(IAuthenticationService authenticationService) : ControllerBase
{
private readonly IAuthenticationService _authenticationService;
public AccountController(IAuthenticationService authenticationService)
{
_authenticationService = authenticationService;
}

[HttpPost("authenticate")]
public async Task<IActionResult> AuthenticateAsync(AuthenticationRequest request)
{
return Ok(await _authenticationService.AuthenticateAsync(request));
return Ok(await authenticationService.AuthenticateAsync(request));
}
[HttpPost("register")]
public async Task<IActionResult> RegisterAsync(RegistrationRequest request)
{
var origin = Request.Headers["origin"];
return Ok(await _authenticationService.RegisterAsync(request, origin));
return Ok(await authenticationService.RegisterAsync(request, origin));
}
[HttpGet("confirm-email")]
public async Task<IActionResult> ConfirmEmailAsync([FromQuery] string userId, [FromQuery] string code)
{
return Ok(await _authenticationService.ConfirmEmailAsync(userId, code));
return Ok(await authenticationService.ConfirmEmailAsync(userId, code));
}
[HttpPost("forgot-password")]
public async Task<IActionResult> ForgotPassword(ForgotPasswordRequest model)
{
await _authenticationService.ForgotPassword(model, Request.Headers["origin"]);
await authenticationService.ForgotPassword(model, Request.Headers["origin"]);
return Ok();
}
[HttpPost("reset-password")]
public async Task<IActionResult> ResetPassword(ResetPasswordRequest model)
{

return Ok(await _authenticationService.ResetPassword(model));
return Ok(await authenticationService.ResetPassword(model));
}

}
1 change: 0 additions & 1 deletion src/CleanArch.Application/CleanArch.Application.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

<ItemGroup>
<PackageReference Include="AutoMapper" Version="13.0.1" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" />
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="11.1.0" />

<PackageReference Include="FluentValidation" Version="11.10.0" />
Expand Down
8 changes: 1 addition & 7 deletions src/CleanArch.Application/Exceptions/BadRequestException.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
namespace CleanArch.Application.Exceptions;

public class BadRequestException : ApplicationException
{
public BadRequestException(string message) : base(message)
{

}
}
public class BadRequestException(string message) : ApplicationException(message);
8 changes: 1 addition & 7 deletions src/CleanArch.Application/Exceptions/NotFoundException.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
namespace CleanArch.Application.Exceptions;

public class NotFoundException : ApplicationException
{
public NotFoundException(string name, object key)
: base($"{name} ({key}) is not found")
{
}
}
public class NotFoundException(string name, object key) : ApplicationException($"{name} ({key}) is not found");
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,17 @@

namespace CleanArch.Application.Features.Categories.Commands.CreateCategory;

public class CreateCategoryCommandHandler : IRequestHandler<CreateCategoryCommand, CreateCategoryCommandResponse>
public class CreateCategoryCommandHandler(
IMapper mapper,
ICategoryRepository categoryRepository)
: IRequestHandler<CreateCategoryCommand, CreateCategoryCommandResponse>
{
private readonly ICategoryRepository _categoryRepository;
private readonly IMapper _mapper;

public CreateCategoryCommandHandler(IMapper mapper,
ICategoryRepository categoryRepository)
{
_mapper = mapper;
_categoryRepository = categoryRepository;
}

public async Task<CreateCategoryCommandResponse> Handle(CreateCategoryCommand request,
CancellationToken cancellationToken)
{
var createCategoryCommandResponse = new CreateCategoryCommandResponse();

var validator = new CreateCategoryCommandValidator(_categoryRepository);
var validator = new CreateCategoryCommandValidator(categoryRepository);
var validationResult = await validator.ValidateAsync(request);

if (validationResult.Errors.Count > 0)
Expand All @@ -37,8 +30,8 @@ public async Task<CreateCategoryCommandResponse> Handle(CreateCategoryCommand re
if (createCategoryCommandResponse.Success)
{
var category = new Category() { Name = request.Name };
category = await _categoryRepository.AddAsync(category);
createCategoryCommandResponse.Category = _mapper.Map<CreateCategoryDto>(category);
category = await categoryRepository.AddAsync(category);
createCategoryCommandResponse.Category = mapper.Map<CreateCategoryDto>(category);
}

return createCategoryCommandResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,21 @@

namespace CleanArch.Application.Features.Categories.Commands.DeleteCategory;

public class DeleteCategoryCommandHandler : IRequestHandler<DeleteCategoryCommand>
public class DeleteCategoryCommandHandler(IMapper mapper, IGenericRepositoryAsync<Category> categoryRepository)
: IRequestHandler<DeleteCategoryCommand>
{
private readonly IGenericRepositoryAsync<Category> _categoryRepository;
private readonly IMapper _mapper;

public DeleteCategoryCommandHandler(IMapper mapper, IGenericRepositoryAsync<Category> categoryRepository)
{
_mapper = mapper;
_categoryRepository = categoryRepository;
}
private readonly IMapper _mapper = mapper;

public async Task<Unit> Handle(DeleteCategoryCommand request, CancellationToken cancellationToken)
{
var categoryToDelete = await _categoryRepository.GetByIdAsync(request.CategoryId);
var categoryToDelete = await categoryRepository.GetByIdAsync(request.CategoryId);

if (categoryToDelete == null)
{
throw new NotFoundException(nameof(Category), request.CategoryId);
}

await _categoryRepository.DeleteAsync(categoryToDelete);
await categoryRepository.DeleteAsync(categoryToDelete);

return Unit.Value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,13 @@

namespace CleanArch.Application.Features.Categories.Commands.UpdateCategory;

public class UpdateCategoryCommandHandler : IRequestHandler<UpdateCategoryCommand>
public class UpdateCategoryCommandHandler(IMapper mapper, IGenericRepositoryAsync<Category> categoryRepository)
: IRequestHandler<UpdateCategoryCommand>
{
private readonly IGenericRepositoryAsync<Category> _categoryRepository;
private readonly IMapper _mapper;

public UpdateCategoryCommandHandler(IMapper mapper, IGenericRepositoryAsync<Category> categoryRepository)
{
_mapper = mapper;
_categoryRepository = categoryRepository;
}

public async Task<Unit> Handle(UpdateCategoryCommand request, CancellationToken cancellationToken)
{

var categoryToUpdate = await _categoryRepository.GetByIdAsync(request.CategoryId);
var categoryToUpdate = await categoryRepository.GetByIdAsync(request.CategoryId);

if (categoryToUpdate == null)
{
Expand All @@ -33,9 +25,9 @@ public async Task<Unit> Handle(UpdateCategoryCommand request, CancellationToken
if (validationResult.Errors.Count > 0)
throw new ValidationException(validationResult);

_mapper.Map(request, categoryToUpdate, typeof(UpdateCategoryCommand), typeof(Category));
mapper.Map(request, categoryToUpdate, typeof(UpdateCategoryCommand), typeof(Category));

await _categoryRepository.UpdateAsync(categoryToUpdate);
await categoryRepository.UpdateAsync(categoryToUpdate);

return Unit.Value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,12 @@

namespace CleanArch.Application.Features.Categories.Queries.GetCategoriesList;

public class GetCategoriesListQueryHandler : IRequestHandler<GetCategoriesListQuery, List<CategoryListVm>>
public class GetCategoriesListQueryHandler(IMapper mapper, IGenericRepositoryAsync<Category> categoryRepository)
: IRequestHandler<GetCategoriesListQuery, List<CategoryListVm>>
{
private readonly IGenericRepositoryAsync<Category> _categoryRepository;
private readonly IMapper _mapper;

public GetCategoriesListQueryHandler(IMapper mapper, IGenericRepositoryAsync<Category> categoryRepository)
{
_mapper = mapper;
_categoryRepository = categoryRepository;
}

public async Task<List<CategoryListVm>> Handle(GetCategoriesListQuery request, CancellationToken cancellationToken)
{
var allCategories = (await _categoryRepository.ListAllAsync()).OrderBy(x => x.Name);
return _mapper.Map<List<CategoryListVm>>(allCategories);
var allCategories = (await categoryRepository.ListAllAsync()).OrderBy(x => x.Name);
return mapper.Map<List<CategoryListVm>>(allCategories);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,12 @@

namespace CleanArch.Application.Features.Categories.Queries.GetCategoriesListWithEvents;

public class GetCategoriesListWithEventsQueryHandler : IRequestHandler<GetCategoriesListWithEventsQuery, List<CategoryEventListVm>>
public class GetCategoriesListWithEventsQueryHandler(IMapper mapper, ICategoryRepository categoryRepository)
: IRequestHandler<GetCategoriesListWithEventsQuery, List<CategoryEventListVm>>
{
private readonly IMapper _mapper;
private readonly ICategoryRepository _categoryRepository;

public GetCategoriesListWithEventsQueryHandler(IMapper mapper, ICategoryRepository categoryRepository)
{
_mapper = mapper;
_categoryRepository = categoryRepository;
}

public async Task<List<CategoryEventListVm>> Handle(GetCategoriesListWithEventsQuery request, CancellationToken cancellationToken)
{
var list = await _categoryRepository.GetCategoriesWithEvents(request.IncludeHistory);
return _mapper.Map<List<CategoryEventListVm>>(list);
var list = await categoryRepository.GetCategoriesWithEvents(request.IncludeHistory);
return mapper.Map<List<CategoryEventListVm>>(list);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,38 @@

namespace CleanArch.Application.Features.Events.Commands.CreateEvent;

public class CreateEventCommandHandler : IRequestHandler<CreateEventCommand, Guid>
public class CreateEventCommandHandler(
IMapper mapper,
IEventRepository eventRepository,
IEmailService emailService,
ILogger<CreateEventCommandHandler> logger)
: IRequestHandler<CreateEventCommand, Guid>
{
private readonly IEventRepository _eventRepository;
private readonly IMapper _mapper;
private readonly IEmailService _emailService;
private readonly ILogger<CreateEventCommandHandler> _logger;

public CreateEventCommandHandler(IMapper mapper, IEventRepository eventRepository, IEmailService emailService, ILogger<CreateEventCommandHandler> logger)
{
_mapper = mapper;
_eventRepository = eventRepository;
_emailService = emailService;
_logger = logger;
}

public async Task<Guid> Handle(CreateEventCommand request, CancellationToken cancellationToken)
{
var validator = new CreateEventCommandValidator(_eventRepository);
var validator = new CreateEventCommandValidator(eventRepository);
var validationResult = await validator.ValidateAsync(request);

if (validationResult.Errors.Count > 0)
{
throw new Exceptions.ValidationException(validationResult);
}

var @event = _mapper.Map<Event>(request);
var @event = mapper.Map<Event>(request);

@event = await _eventRepository.AddAsync(@event);
@event = await eventRepository.AddAsync(@event);

// Todo: Sending email notification to admin address
var email = new MailRequest() { ToEmail = "amit.naik8103@gmail.com", Body = $"A new event was created: {request}", Subject = "A new event was created" };

try
{
await _emailService.SendEmailAsync(email);
await emailService.SendEmailAsync(email);
}
catch (Exception ex)
{
//this shouldn't stop the API from doing else so this can be logged
_logger.LogError($"Mailing about event {@event.Id} failed due to an error with the mail service: {ex.Message}");
logger.LogError($"Mailing about event {@event.Id} failed due to an error with the mail service: {ex.Message}");
}

return @event.Id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,21 @@

namespace CleanArch.Application.Features.Events.Commands.DeleteEvent;

public class DeleteEventCommandHandler : IRequestHandler<DeleteEventCommand>
public class DeleteEventCommandHandler(IMapper mapper, IGenericRepositoryAsync<Event> eventRepository)
: IRequestHandler<DeleteEventCommand>
{
private readonly IGenericRepositoryAsync<Event> _eventRepository;
private readonly IMapper _mapper;

public DeleteEventCommandHandler(IMapper mapper, IGenericRepositoryAsync<Event> eventRepository)
{
_mapper = mapper;
_eventRepository = eventRepository;
}
private readonly IMapper _mapper = mapper;

public async Task<Unit> Handle(DeleteEventCommand request, CancellationToken cancellationToken)
{
var eventToDelete = await _eventRepository.GetByIdAsync(request.EventId);
var eventToDelete = await eventRepository.GetByIdAsync(request.EventId);

if (eventToDelete == null)
{
throw new NotFoundException(nameof(Event), request.EventId);
}

await _eventRepository.DeleteAsync(eventToDelete);
await eventRepository.DeleteAsync(eventToDelete);

return Unit.Value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,13 @@

namespace CleanArch.Application.Features.Events.Commands.UpdateEvent;

public class UpdateEventCommandHandler : IRequestHandler<UpdateEventCommand>
public class UpdateEventCommandHandler(IMapper mapper, IGenericRepositoryAsync<Event> eventRepository)
: IRequestHandler<UpdateEventCommand>
{
private readonly IGenericRepositoryAsync<Event> _eventRepository;
private readonly IMapper _mapper;

public UpdateEventCommandHandler(IMapper mapper, IGenericRepositoryAsync<Event> eventRepository)
{
_mapper = mapper;
_eventRepository = eventRepository;
}

public async Task<Unit> Handle(UpdateEventCommand request, CancellationToken cancellationToken)
{

var eventToUpdate = await _eventRepository.GetByIdAsync(request.EventId);
var eventToUpdate = await eventRepository.GetByIdAsync(request.EventId);

if (eventToUpdate == null)
{
Expand All @@ -33,9 +25,9 @@ public async Task<Unit> Handle(UpdateEventCommand request, CancellationToken can
if (validationResult.Errors.Count > 0)
throw new ValidationException(validationResult);

_mapper.Map(request, eventToUpdate, typeof(UpdateEventCommand), typeof(Event));
mapper.Map(request, eventToUpdate, typeof(UpdateEventCommand), typeof(Event));

await _eventRepository.UpdateAsync(eventToUpdate);
await eventRepository.UpdateAsync(eventToUpdate);

return Unit.Value;
}
Expand Down
Loading

0 comments on commit 7b4dc05

Please sign in to comment.