Пример #1
0
        public async Task <string> RemovePurchaseTypeAsync(ShopPurchaseTypeDeleteRequest model, ApiDbContext apiDbContext)
        {
            try
            {
                Shop shop = await apiDbContext.Shops.FindAsync(model.ShopId);

                if (shop == null)
                {
                    throw new Exception($"Tienda con id {model.ShopId} no encontrada");
                }

                PurchaseType purchaseType = await apiDbContext.PurchaseTypes.FindAsync(model.PurchaseTypeId);

                if (purchaseType == null)
                {
                    throw new Exception($"Tipo de pedido con id {model.PurchaseTypeId} no encontrado");
                }

                Shop_PurchaseType rel = shop.Shop_PurchaseTypes.FirstOrDefault(sp => sp.PurchaseTypeId == model.PurchaseTypeId && sp.ShopId == model.ShopId);
                if (rel == null)
                {
                    throw new Exception($"Relación entre {shop.Name} y {purchaseType.Name} no encontrada");
                }

                shop.Shop_PurchaseTypes.Remove(rel);

                await apiDbContext.SaveChangesAsync();

                return(purchaseType.Code);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
Пример #2
0
        public async Task <IActionResult> RemovePurchaseType(ShopPurchaseTypeDeleteRequest model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    throw new Exception("Petición de eliminar inválida");
                }

                return(Ok(await _shopService.RemovePurchaseTypeAsync(model, _apiDbContext)));
            }
            catch (Exception e)
            {
                return(StatusCode(500, e.Message));
            }
        }