Skip to content

Commit

Permalink
✨ Added editor config and fixed up warnings (#63)
Browse files Browse the repository at this point in the history
Added editor config and fixed up warnings
  • Loading branch information
danielmackay authored Apr 13, 2023
1 parent 6ab7cbe commit 5e034b8
Show file tree
Hide file tree
Showing 136 changed files with 21,735 additions and 20,514 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System.Collections.Generic;

namespace Northwind.Application.Categories.Queries.GetCategoriesList
namespace Northwind.Application.Categories.Queries.GetCategoriesList;

public class CategoriesListVm
{
public class CategoriesListVm
{
public IList<CategoryLookupDto> Categories { get; set; }
}
public IList<CategoryLookupDto> Categories { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@
using Northwind.Application.Common.Mappings;
using Northwind.Domain.Entities;

namespace Northwind.Application.Categories.Queries.GetCategoriesList
namespace Northwind.Application.Categories.Queries.GetCategoriesList;

public class CategoryLookupDto : IMapFrom<Category>
{
public class CategoryLookupDto : IMapFrom<Category>
{
public int Id { get; set; }
public string Name { get; set; }
public int Id { get; set; }
public string Name { get; set; }

public void Mapping(Profile profile)
{
profile.CreateMap<Category, CategoryLookupDto>()
.ForMember(d => d.Id, opt => opt.MapFrom(s => s.CategoryId))
.ForMember(d => d.Name, opt => opt.MapFrom(s => s.CategoryName));
}
public void Mapping(Profile profile)
{
profile.CreateMap<Category, CategoryLookupDto>()
.ForMember(d => d.Id, opt => opt.MapFrom(s => s.CategoryId))
.ForMember(d => d.Name, opt => opt.MapFrom(s => s.CategoryName));
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using MediatR;

namespace Northwind.Application.Categories.Queries.GetCategoriesList
namespace Northwind.Application.Categories.Queries.GetCategoriesList;

public class GetCategoriesListQuery : IRequest<CategoriesListVm>
{
public class GetCategoriesListQuery : IRequest<CategoriesListVm>
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,30 @@
using System.Threading;
using System.Threading.Tasks;

namespace Northwind.Application.Categories.Queries.GetCategoriesList
namespace Northwind.Application.Categories.Queries.GetCategoriesList;

public class GetCategoriesListQueryHandler : IRequestHandler<GetCategoriesListQuery, CategoriesListVm>
{
public class GetCategoriesListQueryHandler : IRequestHandler<GetCategoriesListQuery, CategoriesListVm>
private readonly INorthwindDbContext _context;
private readonly IMapper _mapper;

public GetCategoriesListQueryHandler(INorthwindDbContext context, IMapper mapper)
{
private readonly INorthwindDbContext _context;
private readonly IMapper _mapper;
_context = context;
_mapper = mapper;
}

public GetCategoriesListQueryHandler(INorthwindDbContext context, IMapper mapper)
{
_context = context;
_mapper = mapper;
}
public async Task<CategoriesListVm> Handle(GetCategoriesListQuery request, CancellationToken cancellationToken)
{
var categories = await _context.Categories
.ProjectTo<CategoryLookupDto>(_mapper.ConfigurationProvider)
.ToListAsync(cancellationToken);

public async Task<CategoriesListVm> Handle(GetCategoriesListQuery request, CancellationToken cancellationToken)
var vm = new CategoriesListVm
{
var categories = await _context.Categories
.ProjectTo<CategoryLookupDto>(_mapper.ConfigurationProvider)
.ToListAsync(cancellationToken);

var vm = new CategoriesListVm
{
Categories = categories
};
Categories = categories
};

return vm;
}
return vm;
}
}
33 changes: 16 additions & 17 deletions Src/Application/Common/Behaviours/LoggingBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,26 @@
using Microsoft.Extensions.Logging;
using Northwind.Application.Common.Interfaces;

namespace Northwind.Application.Common.Behaviours
namespace Northwind.Application.Common.Behaviours;

public class LoggingBehavior<TRequest> : IRequestPreProcessor<TRequest> where TRequest : notnull
{
public class LoggingBehavior<TRequest> : IRequestPreProcessor<TRequest> where TRequest : notnull
{
private readonly ILogger _logger;
private readonly ICurrentUserService _currentUserService;
private readonly ILogger _logger;
private readonly ICurrentUserService _currentUserService;

public LoggingBehavior(ILogger<TRequest> logger, ICurrentUserService currentUserService)
{
_logger = logger;
_currentUserService = currentUserService;
}
public LoggingBehavior(ILogger<TRequest> logger, ICurrentUserService currentUserService)
{
_logger = logger;
_currentUserService = currentUserService;
}

public Task Process(TRequest request, CancellationToken cancellationToken)
{
var name = typeof(TRequest).Name;
public Task Process(TRequest request, CancellationToken cancellationToken)
{
var name = typeof(TRequest).Name;

_logger.LogInformation("Northwind Request: {Name} {@UserId} {@Request}",
name, _currentUserService.GetUserId(), request);
_logger.LogInformation("Northwind Request: {Name} {@UserId} {@Request}",
name, _currentUserService.GetUserId(), request);

return Task.CompletedTask;
}
return Task.CompletedTask;
}
}
49 changes: 24 additions & 25 deletions Src/Application/Common/Behaviours/PerformanceBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,38 @@
using Microsoft.Extensions.Logging;
using Northwind.Application.Common.Interfaces;

namespace Northwind.Application.Common.Behaviours
{
public class PerformanceBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse> where TRequest : IRequest<TResponse>
{
private readonly Stopwatch _timer;
private readonly ILogger<TRequest> _logger;
private readonly ICurrentUserService _currentUserService;
namespace Northwind.Application.Common.Behaviours;

public PerformanceBehavior(ILogger<TRequest> logger, ICurrentUserService currentUserService)
{
_timer = new Stopwatch();
public class PerformanceBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse> where TRequest : IRequest<TResponse>
{
private readonly Stopwatch _timer;
private readonly ILogger<TRequest> _logger;
private readonly ICurrentUserService _currentUserService;

_logger = logger;
_currentUserService = currentUserService;
}
public PerformanceBehavior(ILogger<TRequest> logger, ICurrentUserService currentUserService)
{
_timer = new Stopwatch();

public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
{
_timer.Start();
_logger = logger;
_currentUserService = currentUserService;
}

var response = await next();
public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
{
_timer.Start();

_timer.Stop();
var response = await next();

if (_timer.ElapsedMilliseconds > 500)
{
var name = typeof(TRequest).Name;
_timer.Stop();

_logger.LogWarning("Northwind Long Running Request: {Name} ({ElapsedMilliseconds} milliseconds) {@UserId} {@Request}",
name, _timer.ElapsedMilliseconds, _currentUserService.GetUserId(), request);
}
if (_timer.ElapsedMilliseconds > 500)
{
var name = typeof(TRequest).Name;

return response;
_logger.LogWarning("Northwind Long Running Request: {Name} ({ElapsedMilliseconds} milliseconds) {@UserId} {@Request}",
name, _timer.ElapsedMilliseconds, _currentUserService.GetUserId(), request);
}

return response;
}
}
45 changes: 22 additions & 23 deletions Src/Application/Common/Behaviours/ValidationBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,33 @@
using MediatR;
using ValidationException = Northwind.Application.Common.Exceptions.ValidationException;

namespace Northwind.Application.Common.Behaviours
{
public class ValidationBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TRequest : IRequest<TResponse>
{
private readonly IEnumerable<IValidator<TRequest>> _validators;
namespace Northwind.Application.Common.Behaviours;

public ValidationBehavior(IEnumerable<IValidator<TRequest>> validators)
{
_validators = validators;
}
public class ValidationBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TRequest : IRequest<TResponse>
{
private readonly IEnumerable<IValidator<TRequest>> _validators;

public Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
{
var context = new ValidationContext<TRequest>(request);
public ValidationBehavior(IEnumerable<IValidator<TRequest>> validators)
{
_validators = validators;
}

var failures = _validators
.Select(v => v.Validate(context))
.SelectMany(result => result.Errors)
.Where(f => f != null)
.ToList();
public Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
{
var context = new ValidationContext<TRequest>(request);

if (failures.Count != 0)
{
throw new ValidationException(failures);
}
var failures = _validators
.Select(v => v.Validate(context))
.SelectMany(result => result.Errors)
.Where(f => f != null)
.ToList();

return next();
if (failures.Count != 0)
{
throw new ValidationException(failures);
}

return next();
}
}
11 changes: 5 additions & 6 deletions Src/Application/Common/Exceptions/DeleteFailureException.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using System;

namespace Northwind.Application.Common.Exceptions
namespace Northwind.Application.Common.Exceptions;

public class DeleteFailureException : Exception
{
public class DeleteFailureException : Exception
public DeleteFailureException(string name, object key, string message)
: base($"Deletion of entity \"{name}\" ({key}) failed. {message}")
{
public DeleteFailureException(string name, object key, string message)
: base($"Deletion of entity \"{name}\" ({key}) failed. {message}")
{
}
}
}
11 changes: 5 additions & 6 deletions Src/Application/Common/Exceptions/NotFoundException.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using System;

namespace Northwind.Application.Common.Exceptions
namespace Northwind.Application.Common.Exceptions;

public class NotFoundException : Exception
{
public class NotFoundException : Exception
public NotFoundException(string name, object key)
: base($"Entity \"{name}\" ({key}) was not found.")
{
public NotFoundException(string name, object key)
: base($"Entity \"{name}\" ({key}) was not found.")
{
}
}
}
45 changes: 22 additions & 23 deletions Src/Application/Common/Exceptions/ValidationException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,33 @@
using System.Linq;
using FluentValidation.Results;

namespace Northwind.Application.Common.Exceptions
namespace Northwind.Application.Common.Exceptions;

public class ValidationException : Exception
{
public class ValidationException : Exception
public ValidationException()
: base("One or more validation failures have occurred.")
{
public ValidationException()
: base("One or more validation failures have occurred.")
{
Failures = new Dictionary<string, string[]>();
}
Failures = new Dictionary<string, string[]>();
}

public ValidationException(List<ValidationFailure> failures)
: this()
{
var propertyNames = failures
.Select(e => e.PropertyName)
.Distinct();
public ValidationException(List<ValidationFailure> failures)
: this()
{
var propertyNames = failures
.Select(e => e.PropertyName)
.Distinct();

foreach (var propertyName in propertyNames)
{
var propertyFailures = failures
.Where(e => e.PropertyName == propertyName)
.Select(e => e.ErrorMessage)
.ToArray();
foreach (var propertyName in propertyNames)
{
var propertyFailures = failures
.Where(e => e.PropertyName == propertyName)
.Select(e => e.ErrorMessage)
.ToArray();

Failures.Add(propertyName, propertyFailures);
}
Failures.Add(propertyName, propertyFailures);
}

public IDictionary<string, string[]> Failures { get; }
}

public IDictionary<string, string[]> Failures { get; }
}
9 changes: 4 additions & 5 deletions Src/Application/Common/Interfaces/ICsvFileBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using System.Collections.Generic;
using Northwind.Application.Products.Queries.GetProductsFile;

namespace Northwind.Application.Common.Interfaces
namespace Northwind.Application.Common.Interfaces;

public interface ICsvFileBuilder
{
public interface ICsvFileBuilder
{
byte[] BuildProductsFile(IEnumerable<ProductRecordDto> records);
}
byte[] BuildProductsFile(IEnumerable<ProductRecordDto> records);
}
9 changes: 4 additions & 5 deletions Src/Application/Common/Interfaces/ICurrentUserService.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace Northwind.Application.Common.Interfaces
namespace Northwind.Application.Common.Interfaces;

public interface ICurrentUserService
{
public interface ICurrentUserService
{
string GetUserId();
}
string GetUserId();
}
Loading

0 comments on commit 5e034b8

Please sign in to comment.