Skip to content

Commit 7b4dc05

Browse files
committed
feat: inline constructor
1 parent 0e9ce08 commit 7b4dc05

File tree

26 files changed

+140
-305
lines changed

26 files changed

+140
-305
lines changed

src/CleanArch.Api/Account/AccountController.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,35 @@ namespace CleanArch.Api.Account;
66

77
[Route("api/[controller]")]
88
[ApiController]
9-
public class AccountController : ControllerBase
9+
public class AccountController(IAuthenticationService authenticationService) : ControllerBase
1010
{
11-
private readonly IAuthenticationService _authenticationService;
12-
public AccountController(IAuthenticationService authenticationService)
13-
{
14-
_authenticationService = authenticationService;
15-
}
16-
1711
[HttpPost("authenticate")]
1812
public async Task<IActionResult> AuthenticateAsync(AuthenticationRequest request)
1913
{
20-
return Ok(await _authenticationService.AuthenticateAsync(request));
14+
return Ok(await authenticationService.AuthenticateAsync(request));
2115
}
2216
[HttpPost("register")]
2317
public async Task<IActionResult> RegisterAsync(RegistrationRequest request)
2418
{
2519
var origin = Request.Headers["origin"];
26-
return Ok(await _authenticationService.RegisterAsync(request, origin));
20+
return Ok(await authenticationService.RegisterAsync(request, origin));
2721
}
2822
[HttpGet("confirm-email")]
2923
public async Task<IActionResult> ConfirmEmailAsync([FromQuery] string userId, [FromQuery] string code)
3024
{
31-
return Ok(await _authenticationService.ConfirmEmailAsync(userId, code));
25+
return Ok(await authenticationService.ConfirmEmailAsync(userId, code));
3226
}
3327
[HttpPost("forgot-password")]
3428
public async Task<IActionResult> ForgotPassword(ForgotPasswordRequest model)
3529
{
36-
await _authenticationService.ForgotPassword(model, Request.Headers["origin"]);
30+
await authenticationService.ForgotPassword(model, Request.Headers["origin"]);
3731
return Ok();
3832
}
3933
[HttpPost("reset-password")]
4034
public async Task<IActionResult> ResetPassword(ResetPasswordRequest model)
4135
{
4236

43-
return Ok(await _authenticationService.ResetPassword(model));
37+
return Ok(await authenticationService.ResetPassword(model));
4438
}
4539

4640
}

src/CleanArch.Application/CleanArch.Application.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
<ItemGroup>
1010
<PackageReference Include="AutoMapper" Version="13.0.1" />
11-
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" />
1211
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="11.1.0" />
1312

1413
<PackageReference Include="FluentValidation" Version="11.10.0" />
Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
11
namespace CleanArch.Application.Exceptions;
22

3-
public class BadRequestException : ApplicationException
4-
{
5-
public BadRequestException(string message) : base(message)
6-
{
7-
8-
}
9-
}
3+
public class BadRequestException(string message) : ApplicationException(message);
Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
11
namespace CleanArch.Application.Exceptions;
22

3-
public class NotFoundException : ApplicationException
4-
{
5-
public NotFoundException(string name, object key)
6-
: base($"{name} ({key}) is not found")
7-
{
8-
}
9-
}
3+
public class NotFoundException(string name, object key) : ApplicationException($"{name} ({key}) is not found");

src/CleanArch.Application/Features/Categories/Commands/CreateCategory/CreateCategoryCommandHandler.cs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,17 @@
55

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

8-
public class CreateCategoryCommandHandler : IRequestHandler<CreateCategoryCommand, CreateCategoryCommandResponse>
8+
public class CreateCategoryCommandHandler(
9+
IMapper mapper,
10+
ICategoryRepository categoryRepository)
11+
: IRequestHandler<CreateCategoryCommand, CreateCategoryCommandResponse>
912
{
10-
private readonly ICategoryRepository _categoryRepository;
11-
private readonly IMapper _mapper;
12-
13-
public CreateCategoryCommandHandler(IMapper mapper,
14-
ICategoryRepository categoryRepository)
15-
{
16-
_mapper = mapper;
17-
_categoryRepository = categoryRepository;
18-
}
19-
2013
public async Task<CreateCategoryCommandResponse> Handle(CreateCategoryCommand request,
2114
CancellationToken cancellationToken)
2215
{
2316
var createCategoryCommandResponse = new CreateCategoryCommandResponse();
2417

25-
var validator = new CreateCategoryCommandValidator(_categoryRepository);
18+
var validator = new CreateCategoryCommandValidator(categoryRepository);
2619
var validationResult = await validator.ValidateAsync(request);
2720

2821
if (validationResult.Errors.Count > 0)
@@ -37,8 +30,8 @@ public async Task<CreateCategoryCommandResponse> Handle(CreateCategoryCommand re
3730
if (createCategoryCommandResponse.Success)
3831
{
3932
var category = new Category() { Name = request.Name };
40-
category = await _categoryRepository.AddAsync(category);
41-
createCategoryCommandResponse.Category = _mapper.Map<CreateCategoryDto>(category);
33+
category = await categoryRepository.AddAsync(category);
34+
createCategoryCommandResponse.Category = mapper.Map<CreateCategoryDto>(category);
4235
}
4336

4437
return createCategoryCommandResponse;

src/CleanArch.Application/Features/Categories/Commands/DeleteCategory/DeleteCategoryCommandHandler.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,21 @@
66

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

9-
public class DeleteCategoryCommandHandler : IRequestHandler<DeleteCategoryCommand>
9+
public class DeleteCategoryCommandHandler(IMapper mapper, IGenericRepositoryAsync<Category> categoryRepository)
10+
: IRequestHandler<DeleteCategoryCommand>
1011
{
11-
private readonly IGenericRepositoryAsync<Category> _categoryRepository;
12-
private readonly IMapper _mapper;
13-
14-
public DeleteCategoryCommandHandler(IMapper mapper, IGenericRepositoryAsync<Category> categoryRepository)
15-
{
16-
_mapper = mapper;
17-
_categoryRepository = categoryRepository;
18-
}
12+
private readonly IMapper _mapper = mapper;
1913

2014
public async Task<Unit> Handle(DeleteCategoryCommand request, CancellationToken cancellationToken)
2115
{
22-
var categoryToDelete = await _categoryRepository.GetByIdAsync(request.CategoryId);
16+
var categoryToDelete = await categoryRepository.GetByIdAsync(request.CategoryId);
2317

2418
if (categoryToDelete == null)
2519
{
2620
throw new NotFoundException(nameof(Category), request.CategoryId);
2721
}
2822

29-
await _categoryRepository.DeleteAsync(categoryToDelete);
23+
await categoryRepository.DeleteAsync(categoryToDelete);
3024

3125
return Unit.Value;
3226
}

src/CleanArch.Application/Features/Categories/Commands/UpdateCategory/UpdateCategoryCommandHandler.cs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,13 @@
66

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

9-
public class UpdateCategoryCommandHandler : IRequestHandler<UpdateCategoryCommand>
9+
public class UpdateCategoryCommandHandler(IMapper mapper, IGenericRepositoryAsync<Category> categoryRepository)
10+
: IRequestHandler<UpdateCategoryCommand>
1011
{
11-
private readonly IGenericRepositoryAsync<Category> _categoryRepository;
12-
private readonly IMapper _mapper;
13-
14-
public UpdateCategoryCommandHandler(IMapper mapper, IGenericRepositoryAsync<Category> categoryRepository)
15-
{
16-
_mapper = mapper;
17-
_categoryRepository = categoryRepository;
18-
}
19-
2012
public async Task<Unit> Handle(UpdateCategoryCommand request, CancellationToken cancellationToken)
2113
{
2214

23-
var categoryToUpdate = await _categoryRepository.GetByIdAsync(request.CategoryId);
15+
var categoryToUpdate = await categoryRepository.GetByIdAsync(request.CategoryId);
2416

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

36-
_mapper.Map(request, categoryToUpdate, typeof(UpdateCategoryCommand), typeof(Category));
28+
mapper.Map(request, categoryToUpdate, typeof(UpdateCategoryCommand), typeof(Category));
3729

38-
await _categoryRepository.UpdateAsync(categoryToUpdate);
30+
await categoryRepository.UpdateAsync(categoryToUpdate);
3931

4032
return Unit.Value;
4133
}

src/CleanArch.Application/Features/Categories/Queries/GetCategoriesList/GetCategoriesListQueryHandler.cs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,12 @@
55

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

8-
public class GetCategoriesListQueryHandler : IRequestHandler<GetCategoriesListQuery, List<CategoryListVm>>
8+
public class GetCategoriesListQueryHandler(IMapper mapper, IGenericRepositoryAsync<Category> categoryRepository)
9+
: IRequestHandler<GetCategoriesListQuery, List<CategoryListVm>>
910
{
10-
private readonly IGenericRepositoryAsync<Category> _categoryRepository;
11-
private readonly IMapper _mapper;
12-
13-
public GetCategoriesListQueryHandler(IMapper mapper, IGenericRepositoryAsync<Category> categoryRepository)
14-
{
15-
_mapper = mapper;
16-
_categoryRepository = categoryRepository;
17-
}
18-
1911
public async Task<List<CategoryListVm>> Handle(GetCategoriesListQuery request, CancellationToken cancellationToken)
2012
{
21-
var allCategories = (await _categoryRepository.ListAllAsync()).OrderBy(x => x.Name);
22-
return _mapper.Map<List<CategoryListVm>>(allCategories);
13+
var allCategories = (await categoryRepository.ListAllAsync()).OrderBy(x => x.Name);
14+
return mapper.Map<List<CategoryListVm>>(allCategories);
2315
}
2416
}

src/CleanArch.Application/Features/Categories/Queries/GetCategoriesListWithEvents/GetCategoriesListWithEventsQueryHandler.cs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,12 @@
44

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

7-
public class GetCategoriesListWithEventsQueryHandler : IRequestHandler<GetCategoriesListWithEventsQuery, List<CategoryEventListVm>>
7+
public class GetCategoriesListWithEventsQueryHandler(IMapper mapper, ICategoryRepository categoryRepository)
8+
: IRequestHandler<GetCategoriesListWithEventsQuery, List<CategoryEventListVm>>
89
{
9-
private readonly IMapper _mapper;
10-
private readonly ICategoryRepository _categoryRepository;
11-
12-
public GetCategoriesListWithEventsQueryHandler(IMapper mapper, ICategoryRepository categoryRepository)
13-
{
14-
_mapper = mapper;
15-
_categoryRepository = categoryRepository;
16-
}
17-
1810
public async Task<List<CategoryEventListVm>> Handle(GetCategoriesListWithEventsQuery request, CancellationToken cancellationToken)
1911
{
20-
var list = await _categoryRepository.GetCategoriesWithEvents(request.IncludeHistory);
21-
return _mapper.Map<List<CategoryEventListVm>>(list);
12+
var list = await categoryRepository.GetCategoriesWithEvents(request.IncludeHistory);
13+
return mapper.Map<List<CategoryEventListVm>>(list);
2214
}
2315
}

src/CleanArch.Application/Features/Events/Commands/CreateEvent/CreateEventCommandHandler.cs

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,46 +8,38 @@
88

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

11-
public class CreateEventCommandHandler : IRequestHandler<CreateEventCommand, Guid>
11+
public class CreateEventCommandHandler(
12+
IMapper mapper,
13+
IEventRepository eventRepository,
14+
IEmailService emailService,
15+
ILogger<CreateEventCommandHandler> logger)
16+
: IRequestHandler<CreateEventCommand, Guid>
1217
{
13-
private readonly IEventRepository _eventRepository;
14-
private readonly IMapper _mapper;
15-
private readonly IEmailService _emailService;
16-
private readonly ILogger<CreateEventCommandHandler> _logger;
17-
18-
public CreateEventCommandHandler(IMapper mapper, IEventRepository eventRepository, IEmailService emailService, ILogger<CreateEventCommandHandler> logger)
19-
{
20-
_mapper = mapper;
21-
_eventRepository = eventRepository;
22-
_emailService = emailService;
23-
_logger = logger;
24-
}
25-
2618
public async Task<Guid> Handle(CreateEventCommand request, CancellationToken cancellationToken)
2719
{
28-
var validator = new CreateEventCommandValidator(_eventRepository);
20+
var validator = new CreateEventCommandValidator(eventRepository);
2921
var validationResult = await validator.ValidateAsync(request);
3022

3123
if (validationResult.Errors.Count > 0)
3224
{
3325
throw new Exceptions.ValidationException(validationResult);
3426
}
3527

36-
var @event = _mapper.Map<Event>(request);
28+
var @event = mapper.Map<Event>(request);
3729

38-
@event = await _eventRepository.AddAsync(@event);
30+
@event = await eventRepository.AddAsync(@event);
3931

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

4335
try
4436
{
45-
await _emailService.SendEmailAsync(email);
37+
await emailService.SendEmailAsync(email);
4638
}
4739
catch (Exception ex)
4840
{
4941
//this shouldn't stop the API from doing else so this can be logged
50-
_logger.LogError($"Mailing about event {@event.Id} failed due to an error with the mail service: {ex.Message}");
42+
logger.LogError($"Mailing about event {@event.Id} failed due to an error with the mail service: {ex.Message}");
5143
}
5244

5345
return @event.Id;

0 commit comments

Comments
 (0)