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 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); }