Esempio n. 1
0
        public IActionResult Index(User user)
        {
            // Check initial ModelState
            if (ModelState.IsValid)
            {
                // If a User exists with provided email
                if (_dbContext.Users.Any(u => u.Email == user.Email))
                {
                    // Manually add a ModelState error to the Email field, with provided
                    // error message
                    ModelState.AddModelError("Email", "Email already in use!");

                    // You may consider returning to the View at this point
                    return(View());
                }

                PasswordHasher <User> Hasher = new PasswordHasher <User>();
                user.Password = Hasher.HashPassword(user, user.Password);
                _dbContext.Add(user);
                _dbContext.SaveChanges();

                return(Redirect("/login"));
            }

            // other code
            return(View());
        }
Esempio n. 2
0
        private void UpdateAccount(AccountDomainModel item)
        {
            using (var ctx = new BankAccountDbContext())
            {
                var entity = ctx.AccountSet.SingleOrDefault(b => b.AggregateId == item.Id);
                if (entity == null)
                {
                    throw new AggregateNotFoundException("account");
                }

                var customerEntityId =
                    ctx.CustomerSet.SingleOrDefault(c => c.AggregateId == item.CustomerId);
                if (customerEntityId == null)
                {
                    throw new AggregateNotFoundException("Bank account");
                }

                entity.Version             = item.Version;
                entity.Currency            = item.Currency;
                entity.CustomerAggregateId = item.CustomerId;
                entity.AccountState        = item.State;

                ctx.Entry(entity).State = EntityState.Modified;
                ctx.SaveChanges();
            }
        }
 public void Create(CustomerEntity item)
 {
     using (var ctx = new BankAccountDbContext())
     {
         ctx.CustomerSet.Add(item);
         ctx.SaveChanges();
     }
 }
Esempio n. 4
0
 private void AddCustomer(CustomerDomainModel item)
 {
     using (var ctx = new BankAccountDbContext())
     {
         ctx.CustomerSet.Add(new CustomerEntity
         {
             AggregateId   = item.Id,
             Version       = item.Version,
             CustomerState = State.Open
         });
         ctx.SaveChanges();
     }
 }
        public void Update(Guid aggregateId, State customerState, int version)
        {
            using (var ctx = new BankAccountDbContext())
            {
                var entity = ctx.CustomerSet.SingleOrDefault(cust => cust.AggregateId == aggregateId);
                if (entity == null)
                {
                    throw new ArgumentNullException($"Customer entity not found");
                }

                entity.CustomerState = customerState;
                entity.Version       = version;

                ctx.Entry(entity).State = EntityState.Modified;
                ctx.SaveChanges();
            }
        }
Esempio n. 6
0
        private void AddAccount(AccountDomainModel item)
        {
            using (var ctx = new BankAccountDbContext())
            {
                var customerEntityId =
                    ctx.CustomerSet.SingleOrDefault(c => c.AggregateId == item.CustomerId);
                if (customerEntityId == null)
                {
                    throw new AggregateNotFoundException("Bank account");
                }

                ctx.AccountSet.Add(new AccountEntity
                {
                    AggregateId         = item.Id,
                    Version             = item.Version,
                    CustomerAggregateId = item.CustomerId,
                    Currency            = item.Currency,
                    AccountState        = State.Open
                });
                ctx.SaveChanges();
            }
        }