Ejemplo n.º 1
0
        public async Task <IActionResult> AddUser([Bind("Id,Name,BirthNumber,Adress,Email,Phone,AccountNumber,CardNumber,Money,Login,Pin,Role")] User user, string Turing, [Bind("AccGen")] bool AccGen = false, [Bind("CardGen")] bool CardGen = false)
        {
            if (!Turing.ToLower().Equals("human"))
            {
                ViewBag.ErrTuring = "We don't talk to machines, call a human being.";
                return(View(user));
            }

            if (user.Role == Role.User)
            {
                if (AccGen)
                {
                    user.AccountNumber = _context.GenerateAccountNumber();
                }
                if (CardGen)
                {
                    user.CardNumber = _context.GenerateCardNumber();
                }

                if (user.AccountNumber == null)
                {
                    ViewBag.ErrAcc = "Account number is required";
                    return(View(user));
                }

                if (!_context.IsAccountUnique((long)user.AccountNumber))
                {
                    ViewBag.ErrAcc = "Account number is not unique.";
                    return(View(user));
                }

                if (user.CardNumber == null)
                {
                    ViewBag.ErrAcc = "Card number is required";
                    return(View(user));
                }

                if (!_context.IsCardUnique((long)user.CardNumber))
                {
                    ViewBag.ErrCard = "Card number is not unique.";
                    return(View(user));
                }

                if (user.Money == null)
                {
                    user.Money = 0;
                }
            }


            if (ModelState.IsValid)
            {
                int pin = _context.GenerateLogin(user);

                _context.Add(user);
                await _context.SaveChangesAsync();

                MailClient.Singleton.SendAddUser(user, pin);

                return(RedirectToAction(nameof(Index)));
            }
            else
            {
                ViewBag.ErrMsg = "Values are not valid.";
                return(View(user));
            }
        }
Ejemplo n.º 2
0
        public async Task TestAccountGen()
        {
            long acc = _context.GenerateAccountNumber();

            Assert.IsFalse(await _context.User.AnyAsync(a => a.AccountNumber == acc));
        }