public async Task <int> Handle(StoreProductsCommand command, CancellationToken cancellationToken) { var validator = new SmartProductDtoValidator(_context, command); await validator.ValidateAndThrowAsync(command, cancellationToken : cancellationToken); var product = _context.Products .Include(x => x.Manufacturer) .Include(x => x.Category) .FirstOrDefault(x => command.ManufacturerId.Equals(x.ManufacturerId) && x.BarCode == command.BarCode); if (product != null) { _mapper.Map(command, product); _context.Update(product); await _context.SaveChangesAsync(cancellationToken); return(product.Id); } product = _mapper.Map <Product>(command); await _context.AddAsync(product, cancellationToken); await _context.SaveChangesAsync(cancellationToken); return(product.Id); }
public async Task <ManufacturerViewModel> Handle(StoreManufacturerCommand request, CancellationToken cancellationToken) { var anyManufacturers = _context.Set <Manufacturer>().Any(x => x.Name == request.ManufacturerDto.Name); if (anyManufacturers) { throw new DuplicateNameException(); } var validator = new StoreManufacturerCommandValidator(); await validator.ValidateAndThrowAsync(request, cancellationToken : cancellationToken); var mappedManufacturer = _mapper.Map <Manufacturer>(request.ManufacturerDto); await _context.AddAsync(mappedManufacturer, cancellationToken); await _context.SaveChangesAsync(cancellationToken); return(new ManufacturerViewModel { Id = mappedManufacturer.Id, Name = request.ManufacturerDto.Name, Description = request.ManufacturerDto.Description, }); }
public void Seed(SmartStoreContext context, DbOptions dbOptions, ILogger <SmartStoreContextSeeder> logger) { if (!dbOptions.Seed) { logger.LogInformation("Seed has not been activated."); return; } var policy = CreatePolicy(logger, nameof(SmartStoreContextSeeder)); policy.ExecuteAsync(async() => { await using (context) { await context.Database.MigrateAsync(); var manufacturers = GetPredefinedManufacturers(); if (!context.Manufacturers.Any()) { await context.AddRangeAsync(manufacturers); } await context.SaveChangesAsync(); var categories = GetPredefinedCategories(); if (!context.Categories.Any()) { await context.AddRangeAsync(categories); } await context.SaveChangesAsync(); var products = GetPredefinedProduct(); if (!context.Products.Any()) { await context.AddRangeAsync(products); await context.SaveChangesAsync(); LinkProductsWithManufacturersAndCategories(ref products, categories, manufacturers); await context.SaveChangesAsync(); } } }).Wait(); }
public async Task <int> Handle(StoreCategoryCommand request, CancellationToken cancellationToken) { var category = new Category { Name = request.Name, ParentCategoryId = request.ParentCategoryId, CreatedOnUtc = DateTime.UtcNow, UpdatedOnUtc = DateTime.UtcNow, Description = request.Description }; await _context.AddAsync(category, cancellationToken); await _context.SaveChangesAsync(cancellationToken); return(category.Id); }