/// <inheritdoc/> public async Task <ProductStoreItem> CreateProductStoreItemAsync(UpdateProductStoreItemRequest createRequest) { var dbItems = await _context.Items.Where(i => i.ProductId == createRequest.ProductId && i.HiveSectionId == createRequest.HiveSectionId).ToArrayAsync(); if (dbItems.Length > 0) { throw new RequestedResourceHasConflictException("Product Id and/or Hive section Id"); } var dbItem = Mapper.Map <UpdateProductStoreItemRequest, DbProductStoreItem>(createRequest); _context.Items.Add(dbItem); await _context.SaveChangesAsync(); return(Mapper.Map <ProductStoreItem>(dbItem)); }
/// <inheritdoc/> public async Task <ProductStoreItem> UpdateProductStoreItemAsync(int storeItemId, UpdateProductStoreItemRequest updateRequest) { var dbItems = await _context.Items.Where(i => i.HiveSectionId == updateRequest.HiveSectionId && i.ProductId == updateRequest.ProductId && i.Id != storeItemId).ToArrayAsync(); if (dbItems.Length > 0) { throw new RequestedResourceHasConflictException("Hive section Id and/or Product Id"); } dbItems = await _context.Items.Where(i => i.Id == storeItemId).ToArrayAsync(); var dbItem = dbItems.FirstOrDefault(); if (dbItem == null) { throw new RequestedResourceNotFoundException(); } Mapper.Map(updateRequest, dbItem); await _context.SaveChangesAsync(); dbItems = await _context.Items.Where(i => i.Id == storeItemId).ToArrayAsync(); return(dbItems.Select(i => Mapper.Map <ProductStoreItem>(i)).FirstOrDefault()); }