public async Task DeleteProduct(Guid productId) { if (!_permissionService.HasPermissionToChangeProducts()) { throw new InvalidOperationException("Нет прав доступа для изменения Товаров"); } var product = await _context.Products.FirstOrDefaultAsync(x => x.Id == productId); var productBooking = await _context.Bookings.FirstOrDefaultAsync(x => x.ProductId == productId); if (product != null) { _context.Remove(product); } if (productBooking != null) { _context.Remove(productBooking); } await _context.SaveChangesAsync(); }
public async Task <IActionResult> EditFilters(int id, [Bind("Id, FilterAddititons")] ProductExtViewModel product) { if (id != product.Id) { return(NotFound()); } List <Value> oldFilterValues = await _context.Values.Where(p => p.ProductId == id).ToListAsync(); if (oldFilterValues.Count != 0) { foreach (var item in product.FilterAddititons) { if (item.IsChecked) { if (oldFilterValues.FirstOrDefault(fv => fv.FilterAddId == item.Id && fv.ProductId == product.Id) == null) { _context.Update(new Value { FilterAddId = item.Id, ProductId = product.Id }); } } else { if (oldFilterValues.FirstOrDefault(fv => fv.FilterAddId == item.Id && fv.ProductId == product.Id) != null) { _context.Remove(oldFilterValues.First(fv => fv.FilterAddId == item.Id && fv.ProductId == product.Id)); } } } } else { foreach (var item in product.FilterAddititons) { if (item.IsChecked) { _context.Update(new Value { FilterAddId = item.Id, ProductId = product.Id }); } } } await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); }
public async Task DeleteCategory(Guid categoryId) { if (!_permissionService.HasPermissionToChangeCategories()) { throw new InvalidOperationException("Нет прав доступа для изменения категорий"); } var category = await _context.Categories.FirstOrDefaultAsync(x => x.Id == categoryId); var products = await _context.Products.Where(x => x.CategoryId == categoryId).ToArrayAsync(); var productIds = products.Select(x => x.Id); var bookings = await _context.Bookings.Where(x => productIds.Contains(x.ProductId)).ToArrayAsync(); if (category != null) { _context.Remove(category); _context.RemoveRange(products); _context.RemoveRange(bookings); await _context.SaveChangesAsync(); } }