protected override async Task Execute(CreateBudgetsForAllActiveUsersRequest parameter, ClaimsPrincipal principal, CancellationToken cancellationToken) { var currentYear = DateTime.Now.Year; IEnumerable <User> users = await _userRepository.GetAllUsers(_ => _.State == UserState.Active, cancellationToken); var budgetType = BudgetType.Types.Single(_ => _.Id == parameter.BudgetType); if (budgetType.SinglePerUser) { var budgets = (await _budgetRepository.GetBudgetsByYear(DateTime.Now.Year, cancellationToken)).Where(_ => _.BudgetType == budgetType.Id).Select(_ => _.UserId).ToHashSet(); users = users.Where(_ => !budgets.Contains(_.Id)); } foreach (var user in users) { var budget = new Budget() { UserId = user.Id, Year = currentYear, Amount = parameter.Amount, BudgetType = parameter.BudgetType, Title = parameter.Title }; _budgetRepository.AddBudget(budget); } await _unitOfWork.SaveChanges(cancellationToken); }
protected override async Task Execute(CreateBudgetRequest parameter, ClaimsPrincipal principal, CancellationToken cancellationToken) { var currentYear = DateTime.Now.Year; var userId = parameter.UserId; var user = await _userRepository.GetUser(userId, cancellationToken); if (user == null || user.State != UserState.Active) { throw new OperationErrorException(StatusCodes.Status400BadRequest, $"No active user with id {userId} found"); } var budgets = await _budgetRepository.GetBudgets(userId, currentYear, cancellationToken); var budgetType = BudgetType.Types.Single(_ => _.Id == parameter.BudgetType); if (budgetType.SinglePerUser && budgets.Any(b => b.BudgetType == parameter.BudgetType)) { throw new OperationErrorException(StatusCodes.Status400BadRequest, $"User {user.LastName} {user.FirstName} already has a budget of type {budgetType.Name} assigned for this year"); } var budget = new Budget { UserId = user.Id, Year = currentYear, Amount = parameter.Amount, BudgetType = parameter.BudgetType, Title = parameter.Title }; _budgetRepository.AddBudget(budget); await _unitOfWork.SaveChanges(cancellationToken); }
public void CreateBudgetForNewUser(string userId) { Budget newUserBudget = new Budget() { Sum = 0, ApplicationUserRef = userId }; _budgetRepo.AddBudget(newUserBudget); }
public async Task AddBudget(BudgetForm budgetForm) { var budget = new Budget { TotalToBeBudgeted = budgetForm.TotalToBeBudgeted, Description = budgetForm.Description, Name = budgetForm.Name, StartDate = budgetForm.StartDate, EndDate = budgetForm.EndDate }; await _budgetRepository.AddBudget(budget); }
public ActionResult CreateBudget(BudgetDetails budgetDetailsViewModel, string selectedCategory) { if (ModelState.IsValid) { using (budgetRepository) { string[] dateRange; if (budgetDetailsViewModel.BudgetPeriod.Contains('-')) { dateRange = budgetDetailsViewModel.BudgetPeriod.Split('-'); } else { dateRange = new string[] { budgetDetailsViewModel.BudgetPeriod, budgetDetailsViewModel.BudgetPeriod }; } BudgetEntity.BudgetDetails createBudgetData = new BudgetEntity.BudgetDetails { BudgetName = budgetDetailsViewModel.BudgetName, BudgetDescription = budgetDetailsViewModel.BudgetDescription, MinimumAmount = budgetDetailsViewModel.MinimumAmountAllocated, MaximumAmount = budgetDetailsViewModel.MaximumAmountAllocated, StartDate = Convert.ToDateTime(dateRange[0].TrimEnd()), EndDate = Convert.ToDateTime(dateRange[1].TrimStart()), CreatedBy = SessionUserId, Category = selectedCategory }; if (budgetRepository.AddBudget(createBudgetData)) { return(RedirectToAction("CreateBudget")); } else { ModelState.AddModelError("", "Error in creating budget. Try again!"); } } } else { ModelState.AddModelError("", "The submitted form contain errors. Please correct them and submit again!"); } budgetDetailsViewModel.Categories = GetAvailableBudgetCategory(budgetRepository); return(View("CreateBudget", budgetDetailsViewModel)); }
public async Task <IActionResult> AddBudget([FromBody] Budget budget) { if (budget == null || !ModelState.IsValid) { return(BadRequest(new ErrorModel <object>(ProjectCodes.Form_Generic_Error))); } try { budget = TimeUtils.GetBudgetUtc(budget); if (await _budgetRepository.Exists(budget.UserId, budget.FromDate, budget.ToDate)) { return(BadRequest(new ErrorModel <object>(ProjectCodes.Budget_Already_Present))); } var result = await _budgetRepository.AddBudget(budget); return(Ok(result)); } catch (Exception e) { Console.WriteLine(e.ToString()); return(BadRequest(new ErrorModel <object>(ProjectCodes.Generic_Error))); } }