Example #1
0
        public IActionResult AddUser(IFormCollection fc)
        {
            string            name     = fc["name"];
            string            account  = fc["account"];
            string            password = fc["password"];
            BlockchainContext db       = new BlockchainContext();
            User user = new User
            {
                Name     = name,
                Account  = account,
                Password = password,
            };

            db.User.Add(user);
            int number = db.SaveChanges();

            if (number == 1)
            {
                return(RedirectToAction("Index"));
            }
            else
            {
                return(View("Add"));
            }
        }
Example #2
0
        public (bool success, string error) Write(List <Transaction> transactions)
        {
            using (var transaction = _context.Database.BeginTransaction())
            {
                foreach (var blockData in transactions)
                {
                    try
                    {
                        var currentData = _context.Transactions.FirstOrDefault(d =>
                                                                               d.Index == blockData.Index &&
                                                                               d.Location == blockData.Location &&
                                                                               !d.Stale);

                        if (currentData != null)
                        {
                            currentData.Stale = true;
                            _context.Update(currentData);
                        }

                        blockData.DateCreated = DateTimeOffset.Now;
                        blockData.Stale       = false;

                        _context.Add(blockData);

                        _context.SaveChanges();
                    }
                    catch (Exception e)
                    {
                        transaction.Rollback();
                        return(false, e.InnerException.Message);
                    }
                }

                transaction.Commit();



                return(true, null);
            }
        }
Example #3
0
        public IActionResult GameEditPost(IFormCollection fc)
        {
            int               id          = int.Parse(fc["id"]);
            string            name        = fc["name"];
            string            description = fc["description"];
            BlockchainContext db          = new BlockchainContext();
            var               res         = (from x in db.Game where x.Id == id select x).FirstOrDefault();

            res.Name        = name;
            res.Description = description;
            db.SaveChanges();
            return(Redirect("/Game"));
        }
        public void UpdateOrAddTransaction(Transaction transaction)
        {
            bool editMode = _dbContext.Transactions.Any(t => t.Id == transaction.Id);

            if (editMode)
            {
                _dbContext.Update(transaction);
            }
            else
            {
                _dbContext.Add(transaction);
            }

            _dbContext.SaveChanges();
        }
Example #5
0
        public IActionResult Delete(int id)
        {
            BlockchainContext db = new BlockchainContext();
            var res = (from x in db.Game where x.Id == id select x).FirstOrDefault();

            if (res == null)
            {
                return(Content("Miss data"));
            }
            else
            {
                db.Game.Remove(res);
                db.SaveChanges();
                return(Content("ok"));
            }
        }
Example #6
0
        public IActionResult UserEditPost(IFormCollection fc)
        {
            int    id       = int.Parse(fc["id"]);
            string name     = fc["name"];
            string account  = fc["account"];
            string password = fc["password"];


            BlockchainContext db = new BlockchainContext();
            var res = (from x in db.User where x.Id == id select x).FirstOrDefault();


            res.Name     = name;
            res.Account  = account;
            res.Password = password;

            db.SaveChanges();
            return(Redirect("/User"));
        }
        public void ReplaceChain(List <Block> newChain)
        {
            var blockCount = _dbContext.Blocks.Count();

            if (newChain.Count < blockCount)
            {
                Console.WriteLine("Invalid New Chain Length");
                return;
            }

            else if (!IsValidChain(newChain))
            {
                Console.WriteLine("Invalid New Chain");
                return;
            }

            _dbContext.Blocks.RemoveRange();
            _dbContext.Blocks.AddRange(newChain);
            _dbContext.SaveChanges();
        }
Example #8
0
        public IActionResult AddGame(IFormCollection fc)
        {
            string            name        = fc["name"];
            string            description = fc["description"];
            BlockchainContext db          = new BlockchainContext();
            Game game = new Game
            {
                Name        = name,
                Description = description
            };

            db.Game.Add(game);
            int number = db.SaveChanges();

            if (number == 1)
            {
                return(RedirectToAction("Index"));
            }
            else
            {
                return(View("Add"));
            }
        }