Skip to content

Commit e1af4c1

Browse files
fix naming prefix.
pass missing cancellationtoken.
1 parent 50929cc commit e1af4c1

File tree

4 files changed

+29
-25
lines changed

4 files changed

+29
-25
lines changed

src/Infrastructure/Persistance/CommandHandlers/Products/AddProductCommandHandler.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,21 @@ namespace CleanTemplate.Persistance.CommandHandlers.Products
1313
{
1414
public class AddProductCommandHandler : IRequestHandler<AddProductCommand, int>
1515
{
16-
private readonly CleanArchWriteDbContext dbContext;
17-
private readonly ILogger<AddProductCommandHandler> logger;
16+
private readonly CleanArchWriteDbContext _dbContext;
17+
private readonly ILogger<AddProductCommandHandler> _logger;
1818

1919
public AddProductCommandHandler(CleanArchWriteDbContext dbContext, ILogger<AddProductCommandHandler> logger)
2020
{
21-
this.dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
22-
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
21+
_dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
22+
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
2323
}
2424

2525
public async Task<int> Handle(AddProductCommand request, CancellationToken cancellationToken)
2626
{
27-
var existingProduct = await dbContext.Set<Product>().FirstOrDefaultAsync(a => a.Name == request.Name, cancellationToken);
27+
if (request is null)
28+
throw new ArgumentNullException(nameof(request));
29+
30+
var existingProduct = await _dbContext.Set<Product>().FirstOrDefaultAsync(a => a.Name == request.Name, cancellationToken);
2831

2932
if (existingProduct != null)
3033
{
@@ -37,10 +40,10 @@ public async Task<int> Handle(AddProductCommand request, CancellationToken cance
3740
Price = request.Price
3841
};
3942

40-
await dbContext.Set<Product>().AddAsync(product, cancellationToken);
41-
await dbContext.SaveChangesAsync(cancellationToken);
43+
await _dbContext.Set<Product>().AddAsync(product, cancellationToken);
44+
await _dbContext.SaveChangesAsync(cancellationToken);
4245

43-
logger.LogInformation("Product Inserted", product);
46+
_logger.LogInformation("Product Inserted", product);
4447

4548
return product.Id;
4649
}

src/Infrastructure/Persistance/CommandHandlers/Users/LoginCommandHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ public async Task<LoginResponse> Handle(LoginCommand request, CancellationToken
2525
{
2626
var user = await _userManager.FindByNameAsync(request.Username);
2727
if (user == null)
28-
throw new CleanArchAppException("نام کاربری یا رمز عبور اشتباه است");
28+
throw new CleanArchAppException("username or password is incorrect");
2929

3030
var isPasswordValid = await _userManager.CheckPasswordAsync(user, request.Password);
3131
if (!isPasswordValid)
32-
throw new CleanArchAppException("نام کاربری یا رمز عبور اشتباه است");
32+
throw new CleanArchAppException("username or password is incorrect");
3333

3434
var jwt = await _jwtService.GenerateAsync(user);
3535

src/Infrastructure/Persistance/QueryHandlers/Products/GetProductByIdQueryHandler.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,21 @@ namespace CleanTemplate.Persistance.QueryHandlers.Products
1212
{
1313
public class GetProductByIdQueryHandler : IRequestHandler<GetProductByIdQuery, ProductQueryModel>
1414
{
15-
private readonly CleanArchReadOnlyDbContext dbContext;
15+
private readonly CleanArchReadOnlyDbContext _dbContext;
1616

1717
public GetProductByIdQueryHandler(CleanArchReadOnlyDbContext dbContext)
1818
{
19-
this.dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
19+
_dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
2020
}
2121

2222
public async Task<ProductQueryModel> Handle(GetProductByIdQuery request, CancellationToken cancellationToken)
2323
{
24-
var existingProduct = await dbContext.Set<Product>().Where(a => a.Id == request.ProductId).Select(a =>
24+
var existingProduct = await _dbContext.Set<Product>().Where(a => a.Id == request.ProductId).Select(a =>
2525
new ProductQueryModel
2626
{
2727
Name = a.Name,
2828
Price = a.Price
29-
}).FirstOrDefaultAsync();
29+
}).FirstOrDefaultAsync(cancellationToken: cancellationToken);
3030

3131
return existingProduct;
3232
}

src/Infrastructure/Persistance/QueryHandlers/Products/ReadProductFromRedisQueryHandler.cs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,38 @@ namespace CleanTemplate.Persistance.QueryHandlers.Products
1313
{
1414
public class ReadProductFromRedisQueryHandler : IRequestHandler<ReadProductFromRedisQuery, ReadProductFromRedisResponse>
1515
{
16-
private readonly CleanArchReadOnlyDbContext dbContext;
17-
private readonly IStaticCacheManager staticCacheManager;
18-
private const string CachePrefix = "product_";
19-
private const int CacheExpiryTime = 2; //minitues
16+
private readonly CleanArchReadOnlyDbContext _dbContext;
17+
private readonly IStaticCacheManager _staticCacheManager;
18+
19+
private const string _cachePrefix = "product_";
20+
private const int _cacheExpiryTime = 2; //minitues
2021

2122
public ReadProductFromRedisQueryHandler(CleanArchReadOnlyDbContext dbContext,
2223
IStaticCacheManager staticCacheManager)
2324
{
24-
this.dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
25-
this.staticCacheManager = staticCacheManager ?? throw new ArgumentNullException(nameof(staticCacheManager));
25+
_dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
26+
_staticCacheManager = staticCacheManager ?? throw new ArgumentNullException(nameof(staticCacheManager));
2627
}
2728

2829
public async Task<ReadProductFromRedisResponse> Handle(ReadProductFromRedisQuery request, CancellationToken cancellationToken)
2930
{
3031
var productId = request.ProductId;
3132

32-
var result = await staticCacheManager.GetWithExpireTimeAsync(
33-
new CacheKey(CachePrefix + productId),
34-
CacheExpiryTime,
33+
var result = await _staticCacheManager.GetWithExpireTimeAsync(
34+
new CacheKey(_cachePrefix + productId),
35+
_cacheExpiryTime,
3536
async () => await GetProductAsync());
3637

3738
return result;
3839

3940
async Task<ReadProductFromRedisResponse> GetProductAsync()
4041
{
41-
var product = await dbContext.Set<Product>().Where(a => a.Id == productId).Select(a =>
42+
var product = await _dbContext.Set<Product>().Where(a => a.Id == productId).Select(a =>
4243
new ReadProductFromRedisResponse
4344
{
4445
Name = a.Name,
4546
Price = a.Price
46-
}).FirstOrDefaultAsync();
47+
}).FirstOrDefaultAsync(cancellationToken: cancellationToken);
4748

4849
return product;
4950
}

0 commit comments

Comments
 (0)