public async Task <ActionResult> Delete(int order_id, int dish_id) { try { // Searching for record in the Order_Dish table var response = await _repository.GetById(order_id, dish_id); string format1 = "Record in the Order_Dish table with key=(Dish_ID={0},Order_ID={1}) deleted succesfully\n"; string format2 = "Record in the Order table with Order_ID={0} deleted because orders should contain at least one dish (the last dish was removed)\n"; // Getting number of dishes in the order for that ingredient if (await _repository.getNumberOfDishes(order_id) == 1) { // Deleting record from Order_Dish table and Order // Due to foreign key constraints we can simply delete the order from the Order table var order_response = await _orderRepository.GetById(order_id); await _orderRepository.DeleteById(order_id); // In case we delete the last order contained in a transaction, we delete the transaction as well if (await _orderRepository.numOrderByTransaction(order_response.Transaction_ID) == 1) { await _transationRepository.DeleteById(order_response.Transaction_ID); return(Ok(string.Format("Records in the Order, Order_Dish and Transaction deleted successfully due to database constraints\n"))); } return(Ok(string.Format(format2, order_id))); } else { // Deleting record from Order_Dish table await _repository.DeleteById(order_id, dish_id); return(Ok(string.Format(format1, dish_id, order_id))); } } catch (Npgsql.PostgresException ex) { // Postgres threw an exception return(BadRequest(ex.Message.ToString())); } catch { // Unknown error return(BadRequest("Error: Order_Dish Record could not be deleted\n")); } }
public async Task <ActionResult> Delete(int id) { try { // Searching for record in the Dish table (if its not found an exception is thrown) var response = await _repository.GetById(id); var order_list = await _orderDishRepository.getOrderList(id); foreach (Order order in order_list) // Orders that contain the dish to be deleted { // If the removed dish is the only one contained in an order, then delete the order as well if (await _orderDishRepository.getNumberOfDishes(order.Order_ID) == 1) { // It is the last dish contained in this order // Remove the Order, but check if it is the last order in the transaction if (await _orderRepository.numOrderByTransaction(order.Transaction_ID) == 1) // If the order is the only one in the transaction { // Removinng both the order and transaction await _orderRepository.DeleteById(order.Order_ID); await _transactionRepository.DeleteById(order.Transaction_ID); // NOTE Elements in the Order_Dish table will also be deleted (cascading) which is what we want. } else // Transaction contains more than 1 orders still { await _orderRepository.DeleteById(order.Order_ID); // Updating the transaction await _transactionRepository.updateAmount(order.Transaction_ID, await _transactionRepository.getAmount(order.Transaction_ID) - response.Price); // NOTE Elemets in the Order_Dish table will also be deleted (cascading) which is what we want. } } else { // If it is not the last dish we do the following // Update the transaction related to the order (which the dish is in) await _transactionRepository.updateAmount(order.Transaction_ID, await _transactionRepository.getAmount(order.Transaction_ID) - response.Price); } } // After validating the database constraints, delete the dish await _repository.DeleteById(id); string format = "Dish record with key={0} and related records deleted succesfully due to database constraints\n"; return(Ok(string.Format(format, id))); } catch (Npgsql.PostgresException ex) { // Postgres threw an exceptions return(BadRequest(ex.Message.ToString())); } catch { // Unknown errors return(BadRequest("Error: Dish record could not be deleted\n")); } }