Exemplo n.º 1
0
        public async Task <ActionResult> Post([FromBody] Customer_Transaction customer_transaction)
        {
            try
            {
                // Making sure that the customer being referenced exists
                await _customerRepository.GetById(customer_transaction.User_ID);

                await _transactionRepository.GetById(customer_transaction.Transaction_ID);

                // Inserting record in the Customer_Transaction table
                await _repository.Insert(customer_transaction);

                return(Ok("Customer_Transaction Record inserted successfully\n"));
            }
            catch (Npgsql.PostgresException ex)
            {
                // Postgres threw an exception
                return(BadRequest(ex.Message.ToString()));
            }
            catch
            {
                // Unknown error
                return(BadRequest("Error: Customer_Transaction record was not inserted. Make sure the transactioon and customer exist.\n"));
            }
        }
Exemplo 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"));
            }
        }