Ejemplo n.º 1
0
        public IHttpActionResult Post(BudgetDetailModel budgetModel)
        {
            var account = _AccountRepo.GetAccount(User.Identity.GetAccountId());

            if (account.Budgets.Any(b => b.Name == budgetModel.Name))
            {
                return
                    (BadRequest(string.Format("There is already a budget with the category {0} in the collection.",
                                              budgetModel.Name)));
            }

            // creating a new budget; set the account id
            var budget = ModelFactory.Parse(budgetModel);

            budget.AccountId  = account.Id.ToString();
            budget.CreatedBy  = User.Identity.GetUserId();
            budget.ModifiedBy = budget.CreatedBy;

            account.Budgets.Add(budget);
            account.ModifiedOn = DateTime.UtcNow;
            account.ModifiedBy = budget.ModifiedBy;

            // insert into account collection
            var result = _AccountRepo.SaveAccount(account);

            if (!result)
            {
                Log.Error("Tandem.API.BudgetsController, Post", new Exception("Unknown error saving account."));
                return(InternalServerError());
            }

            var budgetSummary = ModelFactory.Create(budget);

            return(Created(budgetSummary.Url, budgetSummary));
        }
Ejemplo n.º 2
0
        public IHttpActionResult Patch(string id, BudgetDetailModel budgetModel, bool detail = false)
        {
            var userId  = User.Identity.GetUserId();
            var account = _AccountRepo.GetAccount(User.Identity.GetAccountId());

            // updating the budget; set the account id
            var budgetToUpdate = account.Budgets.FirstOrDefault(x => x.Id.ToString() == id);

            if (budgetToUpdate == null)
            {
                return(NotFound());
            }

            // let's not duplicate the category names
            if (!string.IsNullOrWhiteSpace(budgetModel.Name) && budgetModel.Name != budgetToUpdate.Name)
            {
                if (account.Budgets.Any(b => b.Name == budgetModel.Name && b.Id.ToString() != budgetModel.Id))
                {
                    return
                        (BadRequest(string.Format("There is already a budget with the category {0} in the collection.",
                                                  budgetModel.Name)));
                }

                budgetToUpdate.Name = budgetModel.Name;
            }

            if (budgetModel.Amount.HasValue && budgetModel.Amount.Value != budgetToUpdate.Amount)
            {
                budgetToUpdate.Amount = budgetModel.Amount.Value;
            }

            if (budgetModel.CriticalThreshold.HasValue)
            {
                budgetToUpdate.CriticalThreshold = budgetModel.CriticalThreshold.Value;
            }

            if (budgetModel.WarningThreshold.HasValue)
            {
                budgetToUpdate.WarningThreshold = budgetModel.WarningThreshold.Value;
            }

            if (budgetModel.TransactionLimit.HasValue)
            {
                budgetToUpdate.TransactionLimit = budgetModel.TransactionLimit.Value;
            }

            budgetToUpdate.ModifiedBy = userId;
            budgetToUpdate.ModifiedOn = DateTime.UtcNow;



            // budget is updated by ref in the account collection. Go ahead and save the account.
            account.ModifiedOn = DateTime.UtcNow;
            account.ModifiedBy = budgetToUpdate.ModifiedBy;
            var result = _AccountRepo.SaveAccount(account);

            if (!result)
            {
                Log.Error("Tandem.API.BudgetsController, Patch", new Exception("Unknown error updating account."));
                return(InternalServerError());
            }

            return(Ok(detail ? ModelFactory.CreateDetail(budgetToUpdate) : ModelFactory.Create(budgetToUpdate)));
        }