public async Task <string> AddAllowedPurchaseTypeAsync(ShopPurchaseTypePostRequest 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.Shop_PurchaseTypes.Add(new Shop_PurchaseType { ShopId = model.ShopId, PurchaseTypeId = model.PurchaseTypeId }); await apiDbContext.SaveChangesAsync(); return(purchaseType.Code); } catch (Exception e) { throw new Exception(e.Message); } }
public async Task <IActionResult> AddPurchaseType(ShopPurchaseTypePostRequest model) { try { if (!ModelState.IsValid) { throw new Exception("Petición de alta inválida"); } return(Ok(await _shopService.AddAllowedPurchaseTypeAsync(model, _apiDbContext))); } catch (Exception e) { return(StatusCode(500, e.Message)); } }