Exemple #1
0
        public async Task <IActionResult> ReassignMoney(int sourceCategoryId)
        {
            ApplicationUser user = await this.userRepository.FindByNameAsync(this.User.Identity.Name);

            var model = new ReassignMoneyViewModel
            {
                SourceCategoryId = sourceCategoryId,
                Categories       = await this.categoryRepository.GetAsync(user.Id)
            };

            return(this.View(model));
        }
Exemple #2
0
        public async Task <IActionResult> ReassignMoney(ReassignMoneyViewModel model)
        {
            ApplicationUser user = await this.userRepository.FindByNameAsync(this.User.Identity.Name);

            if (model.Amount < 0)
            {
                this.ModelState.AddModelError(string.Empty, "The amount can not be negative");
            }

            if (this.ModelState.IsValid)
            {
                try
                {
                    if (model.Amount != 0)
                    {
                        if (model.Unassign)
                        {
                            await this.categoryRepository.UnassignMoneyAsync(model.SourceCategoryId, model.Amount, user.Id);
                        }
                        else
                        {
                            await this.categoryRepository.ReassignMoneyAsync(model.SourceCategoryId, model.DestinationCategoryId, model.Amount);
                        }
                    }

                    return(this.RedirectToAction("Index"));
                }
                catch (RepositoryException)
                {
                    this.ModelState.AddModelError(string.Empty, "Failed to assign money, an unexpected error occured.");
                }
            }

            model.Categories = await this.categoryRepository.GetAsync(user.Id);

            return(this.View(model));
        }