public async Task <IActionResult> Index(SpendingViewModel spendingViewModel) { if (ModelState.IsValid) { var s = spendingViewModel; bool CategoryInDayExists = _db.Spendings.Where(x => x.Date == DateTime.Parse(spendingViewModel.Date)).Select(x => x.Category).Any(x => x == spendingViewModel.Category); if (CategoryInDayExists) { var result = _db.Spendings.Where(x => x.Date == DateTime.Parse(spendingViewModel.Date) && x.Category == spendingViewModel.Category).FirstOrDefault(); result.SpendAmount += spendingViewModel.SpendAmount; await _db.SaveChangesAsync(); return(View()); } else { _db.Spendings.Add(new Spending { Date = DateTime.Parse(spendingViewModel.Date), Category = spendingViewModel.Category.ToLower(), SpendAmount = spendingViewModel.SpendAmount, UserId = _UserManager.GetUserId(HttpContext.User) }); await _db.SaveChangesAsync(); return(View()); } } return(View(spendingViewModel)); }
public IHttpActionResult GetById(int id) { Spending spending = _spendingService.GetById(id); SpendingViewModel spendingViewModel = new SpendingViewModel(); spendingViewModel = Mapper.Map <Spending, SpendingViewModel>(spending); return(HandleCallback(() => spendingViewModel)); }