Beispiel #1
0
        public string Authenticate(Credentials credentials)
        {
            MyJijoWalletContext db = new MyJijoWalletContext();

            //I know we should not save pass in db, i need a little more time to implement something better :)
            if (!db.Users.Where(x => x.UserName == credentials.UserName && x.Password == credentials.Password).Any())
            {
                return(null);
            }

            var tokenHandler = new JwtSecurityTokenHandler();
            var tokenKey     = Encoding.ASCII.GetBytes(this.Key);

            //token descriptor
            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject = new ClaimsIdentity(new Claim[]
                {
                    new Claim(ClaimTypes.Name, credentials.UserName)
                }),
                Expires            = DateTime.UtcNow.AddMinutes(1),
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(tokenKey), SecurityAlgorithms.HmacSha256Signature)
            };

            var token = tokenHandler.CreateToken(tokenDescriptor);

            return(tokenHandler.WriteToken(token));
        }
        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);
        }
Beispiel #3
0
        public static TransactionResponse AddTransaction(TransactionRequest request)
        {
            Exception           exception = null;
            TransactionResponse response  = new TransactionResponse();
            MyJijoWalletContext db        = new MyJijoWalletContext();

            try
            {
                #region Validate
                //client exists
                if (!db.Clients.Where(c => c.Id == request.Client).Any())
                {
                    throw new WalletValException("Client does not exist");
                }

                //wallets exists and it belongs to client
                Wallet wallet = db.Wallets.Where(w => w.Id == request.Wallet && w.Client.Id == request.Client).FirstOrDefault();
                if (wallet == null)
                {
                    throw new Exception("Wallet does not exist");
                }

                //transaction type code exists
                TransactionType traType = db.TransactionTypes.Where(tt => tt.ExternalCode == request.TransactionCode).FirstOrDefault();
                if (traType == null)
                {
                    throw new Exception("Transaction type code does not exist");
                }
                #endregion Validate

                Transaction tra = new Transaction();
                tra.Amount          = request.Amount;
                tra.Date            = DateTime.Now;
                tra.Id              = Guid.NewGuid();
                tra.TransactionType = traType;
                tra.Wallet          = wallet;

                db.Transactions.Add(tra);
                response.Amount = tra.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(request, response, "AddTransaction", exception);
            }

            return(response);
        }
Beispiel #4
0
 public void Setup()
 {
     dbConn = new MyJijoWalletContext();
 }