public async Task <CreateShopItemResult> Handle(CreateShopItemCommand request, CancellationToken cancellationToken) { var entity = GetEntityFromRequest(request); foreach (var option in request.Options) { entity.AddOption(GetEntityFromRequest(option)); } var shop = _dbContext.Shops.FirstOrDefault(o => o.Id.Equals(request.ParentId)); if (shop != null) { shop.AddItem(entity); _dbContext.Shops.Update(shop); //_dbContext.ShopItems.Add(entity); } await _dbContext.SaveChangesAsync(cancellationToken); await _mediator.Publish(new ShopItemCreated() { ShopItemId = entity.Id, ShopId = shop.Id }, cancellationToken); return(new CreateShopItemResult() { Model = CreateShopItemViewModel.Create(entity, shop.Id) }); }
private ShopItemEntity GetEntityFromRequest(CreateShopItemCommand request) { var entity = new ShopItemEntity() { Description = request.Description, MaxAmount = request.MaxAmount, Name = request.Name, Price = request.Price }; return(entity); }