コード例 #1
0
        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"));
            }
        }
コード例 #2
0
 public async Task <List <Order> > Get(int dish_id)
 {
     // Get all orders containing the specified dish
     return(await _repository.getOrderList(dish_id));
 }