public async Task UpdateProgress(Guid id, UpdateProgressParam param, int userId) { var progress = await _catalogDb.UserVideoProgresses .FirstOrDefaultAsync(uvp => uvp.UserId == userId && uvp.Video.Id == id); if (param.Finished) { if (progress != null) { _catalogDb.Remove(progress); } } else { if (progress == null) { var video = await _catalogDb.Videos .FirstOrDefaultAsync(v => v.Id == id); progress = new UserVideoProgress { Video = video, UserId = userId }; await _catalogDb.AddAsync(progress); } progress.Progress = param.Progress; } await _catalogDb.SaveChangesAsync(); }
public async Task Handle(ProductCreateCommand command, CancellationToken cancellationToken) { await context.AddAsync(new Product { Name = command.Name, Description = command.Description, Price = command.Price }); await context.SaveChangesAsync(); }
public async Task Product_Sku_Can_Be_Verified_As_Unique_Async() { await _catalogDbContext.AddAsync(new Product() { Id = 1, Sku = "ABC-123", BrandId = 1, SupplierId = 1 }); await _catalogDbContext.SaveChangesAsync(); var result = await _productService.IsSkuUniqueAsync("ABC-123"); Assert.That(result, Is.False); }
public async Task Handle(ProductInStockUpdateStockCommand notification, CancellationToken cancellationToken) { logger.LogInformation("---ProductInStockUpdateStockCommand stared"); var products = notification.Items.Select(x => x.ProductId); var stocks = await dbContext.Stocks.Where(x => products.Contains(x.ProductInStockId)).ToListAsync(); logger.LogInformation("---Retrieved products from database"); foreach (var item in notification.Items) { var entry = stocks.SingleOrDefault(x => x.ProductInStockId == item.ProductId); switch (item.Action) { case Common.Enums.ProductInStockAction.Add: if (entry == null) { entry = new ProductInStock() { ProductId = item.ProductId }; await dbContext.AddAsync(entry); logger.LogInformation($"--- New stock record was created for {entry.ProductId} because didn't exists before"); } entry.Stock += item.Stock; logger.LogInformation($"---Product {entry.ProductId} - its stock was increased and its new stock is {entry.Stock}"); break; case Common.Enums.ProductInStockAction.Substract: if (entry == null || item.Stock > entry.Stock) { logger.LogError($"---Product {entry.ProductId} - doesn't have enough stock"); throw new Exception($"Product {entry.ProductId} - doesn't have enough stock"); } logger.LogInformation($"---Product {entry.ProductId} - its stock was subtracted and its new stock is {entry.Stock}"); entry.Stock -= item.Stock; break; } await dbContext.SaveChangesAsync(); logger.LogInformation("---ProductInStockUpdateStockCommand ended"); } }
public async Task Handle(ProductInStockUpdateStockCommand notification, CancellationToken cancellationToken) { _logger.LogInformation("--- ProductInStockUpdateStockCommand started"); var products = notification.Items.Select(x => x.ProductId); var stocks = await context.Stocks.Where(x => products.Contains(x.ProductId)).ToListAsync(); _logger.LogInformation("--- Retrieve products from database"); foreach (var item in notification.Items) { var entry = stocks.SingleOrDefault(x => x.ProductId == item.ProductId); if (item.Action == ProductInStockAction.Substract) { if (entry == null || item.Stock > entry.Stock) { _logger.LogError($"--- Product {entry.ProductId} -doens't have enough stock"); throw new ProductInStockUpdateStockCommandException($"Product {entry.ProductId} - doens't have enough stock"); } entry.Stock -= item.Stock; _logger.LogInformation($"--- Product {entry.ProductId} - its stock was subtracted and its new stock is {entry.Stock}"); } else { if (entry == null) { entry = new ProductInStock { ProductId = item.ProductId }; _logger.LogInformation($"--- New stock record was created for {entry.ProductId} because didn't exists before"); await context.AddAsync(entry); } _logger.LogInformation($"--- Add stock to product {entry.ProductId}"); entry.Stock += item.Stock; } } await context.SaveChangesAsync(); }
public async void Add(Product product) { await _context.AddAsync(product); }
public async Task Handle(ProductCreateCommand command, CancellationToken cancellationToken) { var product = command.MapTo <Product>(); await dbContext.AddAsync(product); }