/// <inheritdoc/> public async Task <StoreItem> UpdateStoreItemAsync(int storeItemId, UpdateStoreItemRequest updateStoreItemRequest) { var dbProduct = _productContext.Products.Where(p => p.Id == updateStoreItemRequest.ProductId).FirstOrDefault(); if (dbProduct == null) { throw new RequestedResourceHasConflictException(); } var dbAllowedSectionCatagegories = _categoryContext.Categories.Where(c => c.StoreHiveSectionId == updateStoreItemRequest.HiveSectionId && c.ProductCategoryId == dbProduct.Category.Id).FirstOrDefault(); if (dbAllowedSectionCatagegories == null) { throw new RequestedResourceHasConflictException(); } var dbStoreItems = await _context.Items.Where(i => i.Id == storeItemId).ToArrayAsync(); if (dbStoreItems.Length == 0) { throw new RequestedResourceNotFoundException(); } var dbStoreItem = dbStoreItems[0]; Mapper.Map(updateStoreItemRequest, dbStoreItem); await _context.SaveChangesAsync(); return(Mapper.Map <StoreItem>(dbStoreItem)); }
/// <inheritdoc/> public async Task <StoreItem> CreateStoreItemAsync(UpdateStoreItemRequest createRequest) { var dbProduct = _productContext.Products.Where(p => p.Id == createRequest.ProductId).FirstOrDefault(); if (dbProduct == null) { throw new RequestedResourceHasConflictException(); } var dbAllowedSectionCatagegories = _categoryContext.Categories.Where(c => c.StoreHiveSectionId == createRequest.HiveSectionId && c.ProductCategoryId == dbProduct.Category.Id).FirstOrDefault(); if (dbAllowedSectionCatagegories == null) { throw new RequestedResourceHasConflictException(); } var dbStoreItem = Mapper.Map <UpdateStoreItemRequest, DbStoreItem>(createRequest); _context.Items.Add(dbStoreItem); await _context.SaveChangesAsync(); return(Mapper.Map <StoreItem>(dbStoreItem)); }