Ejemplo n.º 1
0
        private bool AccountExists(InputAccount inputAccount)
        {
            var login    = inputAccount.Login;
            var password = inputAccount.Password;

            return(postgres.Account.Any(a => a.Login == login && a.Password == password));
        }
Ejemplo n.º 2
0
        static void Transfer()
        {
            decimal transferAcount = 500m;

            using (var context = new DonateContext())
            {
                using (DbContextTransaction tran = context.Database.BeginTransaction())
                {
                    try
                    {
                        string sql = @"update OutputAccount set Balance=Balance-@acount where id=@outputId";
                        context.Database.ExecuteSqlCommand(sql, new SqlParameter("@acount", transferAcount), new SqlParameter("@outputId", 1));
                        InputAccount input = context.InputAccount.Find(1);
                        input.Balance += transferAcount;
                        context.SaveChanges();
                        tran.Commit();
                        Console.WriteLine("操作成功");
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("操作失败");
                        tran.Rollback();
                    }
                }
            }
        }
Ejemplo n.º 3
0
        public async Task <Responce> TryCreateAccountAsync(InputAccount inputAccount)
        {
            var login = inputAccount.Login;

            try
            {
                var typeAccount = await postgres.TypeAccount.FirstOrDefaultAsync(w => w.NameTtype == inputAccount.Role);

                if (LoginExists(login))
                {
                    throw new InvalidOperationException($"Пользователь с логином {login} уже существует!");
                }

                if (typeAccount == null)
                {
                    throw new InvalidOperationException($"Тип учетной записи {inputAccount.Role} не найден в базе даных!");
                }

                var newAccount = AccountBulder.Create(typeAccount.IdTypeAccount, inputAccount);
                postgres.Account.Add(newAccount);
                await postgres.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                return(RespоnceManager.CreateError(ex));
            }
            return(RespоnceManager.CreateSucces($"Пользователь {login} успешно создан!"));
        }
Ejemplo n.º 4
0
 static void AddAccount()
 {
     using (var db = new DonateContext())
     {
         OutputAccount o = new OutputAccount();
         o.Name    = "甲";
         o.Balance = 1000;
         db.OutputAccount.Add(o);
         InputAccount input = new InputAccount();
         input.Name    = "乙";
         input.Balance = 1000;
         db.InputAccount.Add(input);
         db.SaveChanges();
     }
 }
Ejemplo n.º 5
0
        public Responce GetToken(InputAccount inputAccount)
        {
            var    login = inputAccount.Login;
            string access_token;

            try
            {
                if (!AccountExists(inputAccount))
                {
                    throw new InvalidOperationException("Invalid username or password!");
                }

                JWTManager tokenManager = new JWTManager(Configuration);
                var        typeAccount  = GetTypeAccount(login);
                access_token = tokenManager.BuldToken(typeAccount, login);
            }
            catch (Exception ex)
            {
                return(RespоnceManager.CreateError(ex));
            }
            return(RespоnceManager.CreateSucces(access_token));
        }
Ejemplo n.º 6
0
 public ActionResult <Responce> SignUpWithRole(InputAccount inputAccount)
 {
     return(new ObjectResult(accountService.TryCreateAccountAsync(inputAccount).Result));
 }
Ejemplo n.º 7
0
 public ActionResult <Responce> SignUp(InputAccount inputAccount)
 {
     //по умолчанию ставим роль "Пользователь"
     inputAccount.Role = Roles.Пользователь;
     return(new ObjectResult(accountService.TryCreateAccountAsync(inputAccount).Result));
 }
Ejemplo n.º 8
0
 public ActionResult <Responce> SignIn(InputAccount inputAccount)
 {
     return(new ObjectResult(accountService.GetToken(inputAccount)));
 }