public IActionResult Create(ExpenseLimit expenseLimit)
 {
     if (ModelState.IsValid)
     {
         if (string.IsNullOrEmpty(User.Identity.Name))
         {
             userName = "******";
         }
         else
         {
             userName = User.Identity.Name;
         }
         IEnumerable <ExpenseLimit> expenseLimits = _expenseLimitRepository.GetExpenseLimitsAll(userName);
         var result = expenseLimits.SingleOrDefault(el => el.ExpenseType == expenseLimit.ExpenseType);
         if (result == null)
         {
             expenseLimit.UserName = userName;
             ExpenseLimit newexpenseLimit = _expenseLimitRepository.Add(expenseLimit);
             return(RedirectToAction("Index"));
         }
         else
         {
             ModelState.AddModelError("", $"Limit already exists for {expenseLimit.ExpenseType}");
         }
     }
     return(View());
 }
Esempio n. 2
0
        public Message Create(ExpenseLimit expenseLimit)
        {
            Message msg = new Message();

            try
            {
                var model = expenseLimit.ToDalEntity();
                _expenseLimitRepository.Add(model);
                _expenseLimitRepository.SaveChanges();
                msg.StatusCode = 200;
                msg.Status     = "Expense limit has been added successfully. You will be notified when you expend more than your Limit";
            }
            catch (Exception ex)
            {
                msg.StatusCode = 400;
                msg.Status     = "An error occured while creating expense limit.";
            }

            return(msg);
        }