Exemplo n.º 1
0
        public IHttpActionResult Post([FromBody] BankUser value)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                using (BankContext db = new BankContext())
                {
                    string userId = RequestContext.Principal.Identity.GetUserId();
                    value.UserIdentityId = userId;
                    if (!db.BankUsers.Any(u => u.UserIdentityId == userId))
                    {
                        db.BankUsers.Add(value);
                        db.SaveChanges();
                        var createdUser = db.BankUsers.FirstOrDefault(u => u.UserIdentityId == userId);
                        //send hellomail
                        return(Ok(createdUser));
                    }
                }
            }
            catch (Exception e)
            {
                ExceptionProcessor.ProcessException(e);
            }


            return(BadRequest());
        }
Exemplo n.º 2
0
        public IList <PayInfo> GetByCard(int cardId)
        {
            IList <PayInfo> userPays = new List <PayInfo>();

            try
            {
                using (BankContext db = new BankContext())
                {
                    var userPaysEnum = db.BankUsers.Include(u => u.Pays).FirstOrDefault(u =>
                                                                                        u.UserIdentityId == UserId)?.Pays.Where(p => p.CardId == cardId);
                    if (!userPaysEnum.Any())
                    {
                        CreateFirstData(db, cardId);
                        userPaysEnum = db.BankUsers.Include(u => u.Pays).FirstOrDefault(u =>
                                                                                        u.UserIdentityId == UserId)?.Pays.Where(p => p.CardId == cardId);
                    }

                    return(userPaysEnum.ToList());
                }
            }
            catch (Exception e)
            {
                ExceptionProcessor.ProcessException(e);
            }

            return(userPays);
        }
Exemplo n.º 3
0
        public IList <PayInfo> Get()
        {
            IList <PayInfo> userPays = new List <PayInfo>();

            try
            {
                using (BankContext db = new BankContext())
                {
                    var userCardIDs = db.BankUsers.Include(u => u.Cards).FirstOrDefault(u =>
                                                                                        u.UserIdentityId == UserId).Cards.Where(c => c.IsActive).Select(c => c.Id).ToArray();

                    foreach (var userCardID in userCardIDs)
                    {
                        var cardPays = db.BankUsers.Include(u => u.Pays).FirstOrDefault(u =>
                                                                                        u.UserIdentityId == UserId)?.Pays.Where(p => p.CardId == userCardID);
                        if (!cardPays.Any())
                        {
                            CreateFirstData(db, userCardID);
                        }
                    }

                    userPays = db.BankUsers.Include(u => u.Pays).FirstOrDefault(u =>
                                                                                u.UserIdentityId == UserId)?.Pays.Where(p => userCardIDs.Contains(p.CardId)).ToList();
                }
            }
            catch (Exception e)
            {
                ExceptionProcessor.ProcessException(e);
            }
            return(userPays);
        }
Exemplo n.º 4
0
        public IHttpActionResult Put(int id, [FromBody] CardInfo value)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var controllerValidate = ValidateCard(value);

            if (controllerValidate.Count > 0)
            {
                foreach (var errorText in controllerValidate)
                {
                    ModelState.AddModelError("", errorText);
                }
                return(BadRequest(ModelState));
            }
            CardInfo retCard = null;

            try
            {
                using (BankContext db = new BankContext())
                {
                    var currentUser = db.BankUsers.Include(c => c.Cards).FirstOrDefault(u =>
                                                                                        u.UserIdentityId == UserId && u.Cards.Any(c => c.Id == id));

                    if (currentUser != null)
                    {
                        var cardForModify = currentUser.Cards.FirstOrDefault(c => c.Id == id);
                        if (cardForModify != null)
                        {
                            foreach (PropertyInfo propertyInfo in cardForModify.GetType().GetProperties()
                                     .Where(p => p.Name != nameof(CardInfo.Id) && p.Name != nameof(CardInfo.Balance)))
                            {
                                if (propertyInfo.GetValue(value, null) != null)
                                {
                                    propertyInfo.SetValue(cardForModify, propertyInfo.GetValue(value, null), null);
                                }
                            }

                            db.SaveChanges();
                        }
                        retCard = currentUser.Cards.FirstOrDefault(c => c.Id == id);
                    }
                }
            }
            catch (Exception e)
            {
                ExceptionProcessor.ProcessException(e);
            }
            if (retCard != null)
            {
                return(Ok(retCard));
            }
            else
            {
                return(BadRequest());
            }
        }
Exemplo n.º 5
0
        public IHttpActionResult Post([FromBody] CardInfo value)
        {
            CardInfo retCard = null;

            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                var controllerValidate = ValidateCard(value);
                if (controllerValidate.Count > 0)
                {
                    foreach (var errorText in controllerValidate)
                    {
                        ModelState.AddModelError("", errorText);
                    }
                    return(BadRequest(ModelState));
                }



                using (BankContext db = new BankContext())
                {
                    var currentUser = db.BankUsers.Include(u => u.Cards).FirstOrDefault(u =>
                                                                                        u.UserIdentityId == UserId);
                    if (currentUser != null)
                    {
                        currentUser.Cards.Add(value);
                        db.SaveChanges();
                        retCard = currentUser.Cards.FirstOrDefault(c => c.Id == value.Id);
                    }
                }
            }
            catch (Exception e)
            {
                ExceptionProcessor.ProcessException(e);
            }


            if (retCard != null)
            {
                return(Ok(retCard));
            }
            else
            {
                return(BadRequest());
            }
        }
Exemplo n.º 6
0
 //TODO test
 protected void AttachDBInternal(DbConnection connection, ExternalDatastore externalDB)
 {
     if (connection != null)
     {
         var commandText = "ATTACH DATABASE \"" + externalDB.DbPath + "\" AS " + externalDB.Alias + ";";
         try
         {
             connection.ExecuteNonQuery(commandText, (object[])null, CurrentTransaction);
         }
         catch (Exception e)
         {
             throw ExceptionProcessor.ProcessException(e, connection, commandText, CurrentTransaction);
         }
     }
 }
Exemplo n.º 7
0
        public BankUser Get()
        {
            try
            {
                using (BankContext db = new BankContext())
                {
                    string userId     = RequestContext.Principal.Identity.GetUserId();
                    var    findedUser = db.BankUsers.FirstOrDefault(u => u.UserIdentityId == userId);
                    return(findedUser);
                }
            }
            catch (Exception e)
            {
                ExceptionProcessor.ProcessException(e);
            }

            return(null);
        }
Exemplo n.º 8
0
 public IHttpActionResult Delete(int id)
 {
     try
     {
         using (BankContext db = new BankContext())
         {
             var userForDelete = db.BankUsers.FirstOrDefault(u => u.Id == id);
             db.BankUsers.Remove(userForDelete);
             db.SaveChanges();
         }
         return(Ok());
     }
     catch (Exception e)
     {
         ExceptionProcessor.ProcessException(e);
     }
     return(InternalServerError());
 }
Exemplo n.º 9
0
 //TODO test
 protected void DetachDBInternal(ExternalDatastore externalDB)
 {
     lock (PersistentConnectionSyncLock)
     {
         var connection = PersistentConnection;
         if (connection != null)
         {
             var commandText = "DETACH DATABASE \"" + externalDB.Alias + "\";";
             try
             {
                 connection.ExecuteNonQuery(commandText, (object[])null, CurrentTransaction);
             }
             catch (Exception e)
             {
                 throw ExceptionProcessor.ProcessException(e, connection, commandText, CurrentTransaction);
             }
         }
     }
 }
Exemplo n.º 10
0
        public CardInfo Get(int id)
        {
            CardInfo retCard = null;

            try
            {
                using (BankContext db = new BankContext())
                {
                    retCard = db.BankUsers
                              .Include(u => u.Cards)
                              .FirstOrDefault(u => u.UserIdentityId == RequestContext.Principal.Identity.GetUserId())?.Cards
                              .FirstOrDefault(c => c.Id == id && c.IsActive);
                }
            }
            catch (Exception e)
            {
                ExceptionProcessor.ProcessException(e);
            }
            return(retCard);
        }
Exemplo n.º 11
0
        public PayInfo Get(int id)
        {
            PayInfo userPay = null;

            try
            {
                using (BankContext db = new BankContext())
                {
                    userPay = db.BankUsers.Include(u => u.Pays).FirstOrDefault(u =>
                                                                               u.UserIdentityId == UserId)?.Pays.FirstOrDefault(p => p.Id == id);
                }
            }
            catch (Exception e)
            {
                ExceptionProcessor.ProcessException(e);
            }


            return(userPay);
        }
Exemplo n.º 12
0
 public void Delete(int id)
 {
     try
     {
         using (BankContext db = new BankContext())
         {
             var currentUser = db.BankUsers.Include(c => c.Cards).FirstOrDefault(u =>
                                                                                 u.UserIdentityId == UserId && u.Cards.Any(c => c.Id == id));
             if (currentUser != null)
             {
                 //currentUser.Cards.Remove(currentUser.Cards.FirstOrDefault(c => c.Id == id));
                 currentUser.Cards.FirstOrDefault(c => c.Id == id).IsActive = false;
                 db.SaveChanges();
             }
         }
     }
     catch (Exception e)
     {
         ExceptionProcessor.ProcessException(e);
     }
 }
Exemplo n.º 13
0
        public IEnumerable <CardInfo> Get()
        {
            IList <CardInfo> userCards = new List <CardInfo>();

            try
            {
                using (BankContext db = new BankContext())
                {
                    userCards = db.BankUsers
                                .Include(u => u.Cards)
                                .FirstOrDefault(u => u.UserIdentityId == UserId)
                                ?.Cards.Where(c => c.IsActive)
                                .ToList();
                }
            }
            catch (Exception e)
            {
                ExceptionProcessor.ProcessException(e);
            }

            return(userCards);
        }
Exemplo n.º 14
0
        private List <string> ValidateCard(CardInfo cardInfo)
        {
            var results = new List <string>();

            try
            {
                using (BankContext db = new BankContext())
                {
                    if (db.BankUsers
                        .Include(u => u.Cards)
                        .Any(u => u.UserIdentityId != UserId &&
                             u.Cards.Any(c => c.CardNumber == cardInfo.CardNumber)))
                    {
                        results.Add("This card is not yours");
                    }
                }
            }
            catch (Exception e)
            {
                ExceptionProcessor.ProcessException(e);
            }
            return(results);
        }
Exemplo n.º 15
0
        public IList <BankBranch> Get()
        {
            IList <BankBranch> retValues = new List <BankBranch>();

            try
            {
                using (BankContext db = new BankContext())
                {
                    retValues = db.BankBranches.ToList();
                    if (!retValues.Any())
                    {
                        CreateFirstData(db);
                        retValues = db.BankBranches.ToList();
                    }
                }
            }
            catch (Exception e)
            {
                ExceptionProcessor.ProcessException(e);
            }


            return(retValues);
        }
Exemplo n.º 16
0
 public IHttpActionResult Put(int id, [FromBody] BankUser value)
 {
     if (!ModelState.IsValid)
     {
         return(BadRequest(ModelState));
     }
     try
     {
         using (BankContext db = new BankContext())
         {
             string userId        = RequestContext.Principal.Identity.GetUserId();
             var    userForModify = db.BankUsers.FirstOrDefault(u => u.Id == id && u.UserIdentityId == userId);
             if (userForModify != null)
             {
                 foreach (PropertyInfo propertyInfo in userForModify.GetType().GetProperties()
                          .Where(p => p.Name != nameof(BankUser.Id)))
                 {
                     if (propertyInfo.GetValue(value, null) == null)
                     {
                         propertyInfo.SetValue(value, propertyInfo.GetValue(userForModify, null), null);
                     }
                 }
                 db.Entry(userForModify).CurrentValues.SetValues(value);
                 db.Entry(userForModify).State = EntityState.Modified;
                 db.SaveChanges();
                 var createdUser = db.BankUsers.FirstOrDefault(u => u.UserIdentityId == userId);
                 return(Ok(createdUser));
             }
         }
     }
     catch (Exception e)
     {
         ExceptionProcessor.ProcessException(e);
     }
     return(BadRequest());
 }