-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bf8878b
commit 14b4c96
Showing
19 changed files
with
252 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 3 additions & 41 deletions
44
src/CoolStore.Catalog/Data/Repository/ProductRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,12 @@ | ||
using CoolStore.Catalog.Domain; | ||
using Microsoft.EntityFrameworkCore; | ||
using Moduliths.Domain; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Moduliths.Infra.Data; | ||
|
||
namespace CoolStore.Catalog.Data.Repository | ||
{ | ||
public class ProductRepository : IProductRepository | ||
public class ProductRepository : RepositoryBase<Product, CatalogDbContext>, IProductRepository | ||
{ | ||
private readonly CatalogDbContext _dbContext; | ||
|
||
public ProductRepository(CatalogDbContext dbContext) | ||
{ | ||
_dbContext = dbContext ?? throw CoreException.NullArgument(nameof(dbContext)); | ||
} | ||
|
||
public void Add(Product entity) | ||
{ | ||
_dbContext.Products.Add(entity); | ||
} | ||
|
||
public async Task<IEnumerable<Product>> FindAllAsync(ISpecification<Product> specification) | ||
{ | ||
// TODO fix it | ||
//var query = specification.Includes | ||
// .Aggregate(_dbContext.Set<Product>().AsQueryable(), (current, include) => current.Include(include)); | ||
//return await query.Where(specification.Expression).AsNoTracking().ToListAsync(); | ||
|
||
return await _dbContext.Products.ToListAsync(); | ||
} | ||
|
||
public async Task<Product> FindOneAsync(ISpecification<Product> specification) | ||
{ | ||
return await _dbContext.Products.FirstOrDefaultAsync(specification.Expression); | ||
} | ||
|
||
public void Remove(Product entity) | ||
{ | ||
_dbContext.Products.Remove(entity); | ||
} | ||
|
||
public void Update(Product entity) | ||
public ProductRepository(CatalogDbContext dbContext) : base(dbContext) | ||
{ | ||
_dbContext.Products.Update(entity); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using Moduliths.Domain; | ||
|
||
namespace CoolStore.Catalog.Domain | ||
{ | ||
public class ProductByPriceSpec : SpecificationBase<Product> | ||
{ | ||
public ProductByPriceSpec(double price) | ||
: base(t => t.Price <= price && !t.IsDeleted) | ||
{ | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
...oolStore.Catalog/Usecases/GetProductsByPriceAndName/GetProductsByPriceAndNameValidator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using CoolStore.Protobuf.Catalogs.V1; | ||
using FluentValidation; | ||
|
||
namespace CoolStore.Catalog.Usecases.GetProductsByPriceAndName | ||
{ | ||
public class GetProductsByPriceAndNameValidator : AbstractValidator<GetProductsRequest> | ||
{ | ||
public GetProductsByPriceAndNameValidator() | ||
{ | ||
RuleFor(x => x.HighPrice) | ||
.NotNull() | ||
.NotEmpty() | ||
.GreaterThan(0) | ||
.WithMessage("[HighPrice] could not be null, empty and less than zero."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<TargetFramework>netstandard2.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="MediatR" Version="7.0.0" /> | ||
<PackageReference Include="MediatR" Version="8.0.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Moduliths.Domain; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace Moduliths.Infra.Data | ||
{ | ||
public class RepositoryBase<T, TDbContext> : IRepository<T> | ||
where T : class, IAggregateRoot | ||
where TDbContext : DbContext | ||
{ | ||
public RepositoryBase(TDbContext dbContext) | ||
{ | ||
DbContext = dbContext ?? throw CoreException.NullArgument(nameof(dbContext)); | ||
} | ||
|
||
public TDbContext DbContext { get; } | ||
|
||
public void Add(T entity) | ||
{ | ||
DbContext.Set<T>().Add(entity); | ||
} | ||
|
||
public void AddRange(IEnumerable<T> entities) | ||
{ | ||
DbContext.Set<T>().AddRange(entities); | ||
} | ||
|
||
public virtual IAsyncEnumerable<T> FindAllAsync(ISpecification<T> specification) | ||
{ | ||
return DbContext.Set<T>() | ||
.AsQueryable() | ||
.Where(specification.Expression) | ||
.AsNoTracking() | ||
.AsAsyncEnumerable(); | ||
} | ||
|
||
public Task<T> FindOneAsync(ISpecification<T> specification) | ||
{ | ||
return DbContext.Set<T>().FirstOrDefaultAsync(specification.Expression); | ||
} | ||
|
||
public void Remove(T entity) | ||
{ | ||
DbContext.Set<T>().Remove(entity); | ||
} | ||
|
||
public void RemoveRange(IEnumerable<T> entities) | ||
{ | ||
DbContext.Set<T>().RemoveRange(entities); | ||
} | ||
|
||
public void Update(T entity) | ||
{ | ||
DbContext.Set<T>().Update(entity); | ||
} | ||
|
||
public void UpdateRange(IEnumerable<T> entities) | ||
{ | ||
DbContext.Set<T>().UpdateRange(entities); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Moduliths.Infra | ||
{ | ||
public static class ServiceCollectionExtensions | ||
{ | ||
public static IServiceCollection AddServiceByIntefaceInAssembly<TRegisteredAssemblyType>(this IServiceCollection services, Type interfaceType) | ||
{ | ||
services.Scan(s => | ||
s.FromAssemblyOf<TRegisteredAssemblyType>() | ||
.AddClasses(c => c.AssignableTo(interfaceType)) | ||
.AsImplementedInterfaces() | ||
.WithScopedLifetime()); | ||
|
||
return services; | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/Moduliths.Infra/ValidationModel/RequestValidationBehavior.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using FluentValidation; | ||
using MediatR; | ||
|
||
namespace Moduliths.Infra.ValidationModel | ||
{ | ||
public class RequestValidationBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse> | ||
where TRequest : IRequest<TResponse> | ||
{ | ||
private readonly IValidator<TRequest> _validator; | ||
|
||
public RequestValidationBehavior(IValidator<TRequest> validator) | ||
{ | ||
_validator = validator; | ||
} | ||
|
||
public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next) | ||
{ | ||
await _validator.HandleValidation(request); | ||
return await next(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
namespace Moduliths.Infra.ValidationModel | ||
{ | ||
public class ValidationError | ||
{ | ||
public string Field { get; } | ||
|
||
public string Message { get; } | ||
|
||
public ValidationError(string field, string message) | ||
{ | ||
Field = field != string.Empty ? field : null; | ||
Message = message; | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/Moduliths.Infra/ValidationModel/ValidationException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System; | ||
|
||
namespace Moduliths.Infra.ValidationModel | ||
{ | ||
public class ValidationException : Exception | ||
{ | ||
public ValidationException(ValidationResultModel validationResultModel) | ||
{ | ||
ValidationResultModel = validationResultModel; | ||
} | ||
|
||
public ValidationResultModel ValidationResultModel { get; } | ||
} | ||
} |
Oops, something went wrong.