public async Task <ActionResult> Post([FromBody] Order_Dish order_dish)
        {
            try
            {
                // Checking if referenced order and dish exist (if not an exception is thrown)
                await _orderRepository.GetById(order_dish.Order_ID);

                await _dishRepository.GetById(order_dish.Dish_ID);

                // Inserting record in the Order_Dish table
                await _repository.Insert(order_dish);

                // if no exceptionn was thrown by insert above, the record was successfully inserted
                // We update the amount in the corresponding transaction
                Order order = await _orderRepository.GetById(order_dish.Order_ID);

                Dish dish = await _dishRepository.GetById(order_dish.Dish_ID);

                await _transationRepository.updateAmount(order.Transaction_ID, await _transationRepository.getAmount(order.Transaction_ID) + dish.Price);

                return(Ok("Order_Dish record inserted successfully\n"));
            }
            catch (Npgsql.PostgresException ex)
            {
                // Postgres threw an exception
                return(BadRequest(ex.Message.ToString()));
            }
            catch
            {
                // Unknown error
                return(BadRequest("Error: Order_Dish record was not inserted\n"));
            }
        }
Esempio n. 2
0
        public async Task <ActionResult> Post([FromBody] Order_No_Transaction order_no_transaction, int tableno, int dish_id, int?tran_id = null)
        {
            try
            {
                // Checking if tableno and dish_id, and user_id exist (otherwise an exception is thrown)
                await _tableRepository.GetById(tableno);

                await _dishRepository.GetById(dish_id);

                await _customerRepository.GetById(order_no_transaction.User_ID);

                int last_tran_inserted = -1;
                // Opening a transaction for the order
                if (tran_id == null)
                {
                    // Creating a default transaction
                    Transaction def_tran = new Transaction {
                        Amount = (decimal)0.0, Date_Time = order_no_transaction.Date_Time
                    };
                    // Inserting default transaction into the Transaction table
                    await _transactionRepository.Insert(def_tran);

                    // Inserting transaction into Customer_Transaction table
                    last_tran_inserted = await _transactionRepository.getLastTransactionInserted();

                    await _customer_TransactionRepository.Insert(new Customer_Transaction { User_ID = order_no_transaction.User_ID, Transaction_ID = last_tran_inserted });

                    // Inserting record in the Order table
                    await _repository.Insert(new Order { User_ID = order_no_transaction.User_ID, Transaction_ID = last_tran_inserted, Date_Time = order_no_transaction.Date_Time });
                }
                else // The id of an existing transaction was passed, so we just add orders to that transaction.
                {
                    // Checking if provided transaction exists (otherwise an exception is thrown)
                    await _transactionRepository.GetById((int)tran_id);

                    // Inserting record in the Order table
                    await _repository.Insert(new Order { User_ID = order_no_transaction.User_ID, Transaction_ID = (int)tran_id, Date_Time = order_no_transaction.Date_Time });
                }

                // Getting last inserted order from Order table
                int last_order_id = await _repository.getLastOrderInserted();

                await _in_store_orderRepository.Insert(new In_Store_Order { Order_ID = last_order_id, TableNo = tableno });

                // Inserting new order and dish into Order_Dish table
                await _orderDishRepository.Insert(new Order_Dish { Order_ID = last_order_id, Dish_ID = dish_id });

                // Updating total amount of transaction respectively and returning a success message
                if (tran_id == null) // If tran_id was not provided
                {
                    await _transactionRepository.updateAmount(last_tran_inserted, await _transactionRepository.getAmount(last_tran_inserted) + await _repository.getCost(last_order_id));

                    return(Ok("Records inserted successfully in the Order, In_Store_Order, Transaction, and Order_Dish Tables"));
                }
                else // tran_id was provided (we added the order to an existing transaction
                {
                    await _transactionRepository.updateAmount((int)tran_id, await _transactionRepository.getAmount((int)tran_id) + await _repository.getCost(last_order_id));

                    return(Ok("Records inserted successfully in the Order, In_Store_Order, and Order_Dish tables\n"));
                }
            }
            catch (InvalidCastException)
            {
                // Invalid parameters in URI
                return(BadRequest("Provided table number, transaction id, or dish_id do not exist. Please check inputs!\n"));
            }
            catch (Npgsql.PostgresException ex)
            {
                // Postgres threw an exception
                return(BadRequest(ex.Message.ToString()));
            }
            catch
            {
                // Unknown error
                return(BadRequest("Error: Order record was not inserted\n"));
            }
        }