public IActionResult GetWalletAvg(string walletId)
        {
            WalletAvgResponse response = new WalletAvgResponse();

            try
            {
                response = WalletAccess.GetDebitAvg(Guid.Parse(walletId));

                if (response.ErrorCode == MyJijoWalletData.ErrorCode.NoError)
                {
                    return(Ok(response.DebitsAvg));
                }
                if (response.ErrorCode == MyJijoWalletData.ErrorCode.Validation)
                {
                    return(BadRequest(response));
                }
                else
                {
                    throw new Exception(response.ErrorMessage);
                }
            }
            catch (Exception ex)
            {
                response.ErrorCode    = MyJijoWalletData.ErrorCode.Other;
                response.ErrorMessage = ex.Message;
                return(StatusCode(StatusCodes.Status500InternalServerError, response));
            }
        }
Beispiel #2
0
        public void GetDebitAvgShouldSucceed()
        {
            var wallet = "8920d0ff-4d19-4a3f-8d11-63c0ceb39922";

            WalletAvgResponse response = WalletAccess.GetDebitAvg(Guid.Parse(wallet));

            Assert.IsTrue(response.ErrorCode == 0);
        }
        public static WalletAvgResponse GetDebitAvg(Guid walletId)
        {
            Exception           exception = null;
            WalletAvgResponse   response  = new WalletAvgResponse();
            MyJijoWalletContext db        = new MyJijoWalletContext();

            try
            {
                #region Validate
                //wallets exists
                if (!db.Wallets.Where(w => w.Id == walletId).Any())
                {
                    throw new WalletValException("Wallet does not exist");
                }
                #endregion Validate

                response.DebitsAvg = db.Transactions.Where(t => t.Wallet.Id == walletId && t.TransactionType.IsDebit).Average(a => a.Amount);
            }
            catch (Exception ex)
            {
                if (ex is WalletValException)
                {
                    response.ErrorCode = ErrorCode.Validation;
                }
                else
                {
                    response.ErrorCode = ErrorCode.Other;
                }

                response.ErrorMessage = ex.Message;
                exception             = ex;
            }
            finally
            {
                db.SaveChanges();
                LogAccess.LogTransaction(walletId, response, "GetDebitAvg", exception);
            }

            return(response);
        }