Пример #1
0
        public void UpdateWith(Account value)
        {
            if (value.InterestRate != 0.0M)
            {
                this.InterestRate = value.InterestRate;
            }

            if (!string.IsNullOrWhiteSpace(value.Description))
            {
                this.Description = value.Description;
            }

            if (value.Currency != null)
            {
                this.Currency = value.Currency;
            }

            if (value.Type != null)
            {
                this.Type = value.Type;
            }
        }
Пример #2
0
        public HttpResponseMessage CreateAccount([FromBody]InputAccountDto value)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
                () =>
                {
                    var db = new BankContext();
                    var user = this.ValidateAndGetLoggedUser(db);

                    var currency = db.Currencies.FirstOrDefault(c => c.Id == value.CurrencyId);
                    if (currency == null)
                    {
                        throw new ArgumentException("No such currency.");
                    }

                    var accountType = db.AccountTypes.FirstOrDefault(at => at.Id == value.TypeId);
                    if (accountType == null)
                    {
                        throw new ArgumentException("No such account type.");
                    }

                    if (ModelState.IsValid)
                    {
                        string iban = this.GenerateIban();
                        decimal interestRate = value.InterestRate ?? 7.8M;

                        var account = new Account
                        {
                            Iban = iban,
                            InterestRate = interestRate,
                            Balance = value.Balance.Value,
                            Description = value.Description,
                            Currency = currency,
                            Type = accountType,
                            User = user
                        };

                        db.Accounts.Add(account);
                        user.Accounts.Add(account);
                        db.SaveChanges();

                        var createdAccountDto = new OutputAccountDto()
                        {
                            Id = account.Id,
                            Iban = account.Iban,
                            InterestRate = account.InterestRate,
                            Balance = account.Balance,
                            Description = account.Description,
                            Currency = account.Currency,
                            Type = account.Type
                        };

                        var response = Request.CreateResponse(HttpStatusCode.Created, createdAccountDto);
                        return response;
                    }
                    else
                    {
                        throw new ApplicationException("Invalid model state.");
                    }
                });

            return responseMsg;
        }