Ejemplo n.º 1
0
        public TransactionDetailStruct CreateTransaction([FromBody] TransactionDetailStruct newTransaction)
        {
            // Create the transaction
            RestRequest req = new RestRequest("api/transaction/create", Method.POST);

            req.AddJsonBody(newTransaction);
            IRestResponse res = client.Execute(req);

            if (res.StatusCode == HttpStatusCode.BadRequest)
            {
                // If the server returns a BAD REQUEST status code, throw a HttpResponseException
                var response = new HttpResponseMessage(HttpStatusCode.BadRequest)
                {
                    Content      = new StringContent(res.Content + ", " + res.StatusDescription),
                    ReasonPhrase = res.StatusDescription
                };
                throw new HttpResponseException(response);
            }
            TransactionDetailStruct result = JsonConvert.DeserializeObject <TransactionDetailStruct>(res.Content);

            // Process the transaction
            IRestResponse processRes = client.Execute(processTransactionsRequest);

            return(result);
        }
        public TransactionDetailStruct CreateTransaction([FromBody] TransactionDetailStruct newTransaction)
        {
            if (newTransaction.amount < 0)
            {
                var response = new HttpResponseMessage(HttpStatusCode.BadRequest)
                {
                    Content      = new StringContent("Could not create new transaction"),
                    ReasonPhrase = "Transaction amount cannot be less than 0"
                };
                throw new HttpResponseException(response);
            }

            if (newTransaction.receiverID == newTransaction.senderID)
            {
                var response = new HttpResponseMessage(HttpStatusCode.BadRequest)
                {
                    Content      = new StringContent("Could not create new transaction"),
                    ReasonPhrase = "Sender and Receiver ID cannot be the same"
                };
                throw new HttpResponseException(response);
            }
            TransactionDetailStruct result = new TransactionDetailStruct();

            result.transactionID = access.CreateTransaction();
            access.SelectTransaction(result.transactionID);
            access.SetSendr(newTransaction.senderID);
            access.SetRecvr(newTransaction.receiverID);
            access.SetAmount(newTransaction.amount);

            result.senderID   = access.GetSendrAcct();
            result.receiverID = access.GetRecvrAcct();
            result.amount     = access.GetAmount();
            return(result);
        }
        public TransactionDetailStruct GetTransaction(uint transactionID)
        {
            TransactionDetailStruct result = new TransactionDetailStruct();

            access.SelectTransaction(transactionID);
            result.transactionID = transactionID;
            result.senderID      = access.GetSendrAcct();
            result.receiverID    = access.GetRecvrAcct();
            result.amount        = access.GetAmount();
            return(result);
        }
        public List <TransactionDetailStruct> GetAllTransactions()
        {
            List <uint> transactionIDs            = access.GetTransactions();
            List <TransactionDetailStruct> result = new List <TransactionDetailStruct>();

            foreach (uint ID in transactionIDs)
            {
                TransactionDetailStruct entry = new TransactionDetailStruct();

                access.SelectTransaction(ID);
                entry.transactionID = ID;
                entry.senderID      = access.GetSendrAcct();
                entry.receiverID    = access.GetRecvrAcct();
                entry.amount        = access.GetAmount();
                result.Add(entry);
            }
            return(result);
        }