public async Task <ActionResult> Create([Bind(Include = "ExpenseTime, Cost, Comment, CategoryId")] ExpenditureCreateEditViewModel model)
        {
            var id = User.Identity.GetUserId();

            if (ModelState.IsValid)
            {
                var expenditure = new ExpenditureCreateEditDTO
                {
                    ExpenseTime = model.ExpenseTime,
                    Cost        = model.Cost,
                    Comment     = model.Comment,
                    CategoryId  = model.CategoryId,
                    UserId      = id
                };
                var result = await _expenditures.Add(expenditure);

                if (result.Succeeded)
                {
                    return(RedirectToAction("UserInfo", "User", new { id = id }));
                }
                ModelState.AddModelError(result.Property, result.Message);
            }
            await PopulateCategoriesData(model.CategoryId);

            return(View(model));
        }
Esempio n. 2
0
        public async Task <OperationDetails> Add(ExpenditureCreateEditDTO expenditure)
        {
            if (expenditure == null)
            {
                return(new OperationDetails(false, "Ошибка! Укажите расход для добавление!", ""));
            }
            if (expenditure.ExpenseTime == null)
            {
                return(new OperationDetails(false, "Ошибка! Укажите дату и время расхода!", ""));
            }
            if (string.IsNullOrWhiteSpace(expenditure.Comment))
            {
                return(new OperationDetails(false, "Ошибка! Добавте комментарий на расход!", ""));
            }
            if (expenditure.Cost == null)
            {
                return(new OperationDetails(false, "Ошибка! Укажите сумму расхода!", ""));
            }
            if (expenditure.CategoryId == null)
            {
                return(new OperationDetails(false, "Ошидка! Укажите категорию расхода!", ""));
            }
            if (expenditure.UserId == null)
            {
                return(new OperationDetails(false, "Ошидка! Укажите пользователя!", ""));
            }
            var category = await _db.Categories.GetCategory(expenditure.CategoryId);

            if (category == null)
            {
                return(new OperationDetails(false, "Ошибка! Выбранная категория не сушествует!", ""));
            }
            var user = await _db.UserManager.FindByIdAsync(expenditure.UserId);

            if (user == null)
            {
                return(new OperationDetails(false, "Ошибка! Пользователь не был найден!", ""));
            }
            if (user.Deposit < expenditure.Cost)
            {
                return(new OperationDetails(false, "Ошибка! У вас не достаточно средств на счету!", ""));
            }
            user.Deposit -= (decimal)expenditure.Cost;
            await _db.SaveChanges();

            var item = new Expenditure
            {
                ExpenseTime       = (DateTime)expenditure.ExpenseTime,
                Comment           = expenditure.Comment,
                Cost              = (decimal)expenditure.Cost,
                CategoryId        = (int)expenditure.CategoryId,
                ApplicationUserId = user.Id
            };
            await _db.Expenditures.Add(item);

            return(new OperationDetails(true, "Расход был успешно добавлен!", ""));
        }
Esempio n. 3
0
        public async Task <OperationDetails> Update(ExpenditureCreateEditDTO expenditure)
        {
            if (expenditure == null)
            {
                return(new OperationDetails(false, "Ошибка! Укажите расход для редактирование!", ""));
            }
            if (expenditure.ExpenseTime == null)
            {
                return(new OperationDetails(false, "Ошибка! Укажите дату и время расхода!", ""));
            }
            if (string.IsNullOrWhiteSpace(expenditure.Comment))
            {
                return(new OperationDetails(false, "Ошибка! Добавте комментарий на расход!", ""));
            }
            if (expenditure.Cost == null)
            {
                return(new OperationDetails(false, "Ошибка! Укажите сумму расхода!", ""));
            }
            if (expenditure.CategoryId == null)
            {
                return(new OperationDetails(false, "Ошидка! Укажите категорию расхода!", ""));
            }
            var category = await _db.Categories.GetCategory(expenditure.CategoryId);

            if (category == null)
            {
                return(new OperationDetails(false, "Ошибка! Выбранная категория не сушествует!", ""));
            }
            var item = await _db.Expenditures.GetExpenditure(expenditure.Id);

            if (item == null)
            {
                return(new OperationDetails(false, "Ошибка! расход не был найден!", ""));
            }
            item.ExpenseTime = (DateTime)expenditure.ExpenseTime;
            item.Cost        = (decimal)expenditure.Cost;
            item.Comment     = expenditure.Comment;
            item.CategoryId  = (int)expenditure.CategoryId;
            await _db.Expenditures.Update(item);

            return(new OperationDetails(true, "Расход был успешно обнавлён!", ""));
        }
Esempio n. 4
0
        public async Task <ExpenditureCreateEditDTO> GetExpenditureForEdit(int?id)
        {
            if (id == null)
            {
                throw new ArgumentNullException(nameof(id));
            }
            var item = await _db.Expenditures.GetExpenditure(id);

            if (item == null)
            {
                throw new NullReferenceException();
            }
            var expenditure = new ExpenditureCreateEditDTO
            {
                Id          = item.Id,
                ExpenseTime = item.ExpenseTime,
                Cost        = item.Cost,
                Comment     = item.Comment,
                CategoryId  = item.CategoryId,
                UserId      = item.ApplicationUserId
            };

            return(expenditure);
        }
        public async Task <ActionResult> Edit(ExpenditureCreateEditViewModel model)
        {
            if (ModelState.IsValid)
            {
                var expenditure = new ExpenditureCreateEditDTO
                {
                    Id          = model.Id,
                    ExpenseTime = model.ExpenseTime,
                    Cost        = model.Cost,
                    Comment     = model.Comment,
                    CategoryId  = model.CategoryId
                };
                var result = await _expenditures.Update(expenditure);

                if (result.Succeeded)
                {
                    return(RedirectToAction("Index"));
                }
                ModelState.AddModelError(result.Property, result.Message);
            }
            await PopulateCategoriesData(model.CategoryId);

            return(View(model));
        }