public async Task<ActionResult> Create(NewBudgetModel model) { // check if model is valid if (!ModelState.IsValid) { this.AddError(SharedResource.ModelStateIsNotValid); return this.View(model); } // get Id of current logged UserProfile from HttpContext var userId = await this.CurrentProfileId(); // finding creator by his ID var creator = await this._budgetService.GetBudgetCreator(userId); // creating new Budget by filling it from model var budget = this.NewBudgetInstanceFromNewBudgetModel(model, creator); // write budget to DB try { await this._budgetService.CreateBudget(budget); } catch (ServiceValidationException exception) { ModelState.AddModelErrors(exception); return this.View(model); } this.AddSuccess(string.Format(BudgetResource.SuccessfullCreation, budget.Name)); return this.RedirectToAction(SharedConstant.Index); }
/// <summary> /// Creates new budget /// </summary> /// <returns>View with model</returns> public ActionResult Create() { var model = new NewBudgetModel(); return this.View(model); }
/// <summary> /// Creates instance of Budget from NewBudgetModel and UserProfile of creator /// </summary> /// <param name="model">NewBudgetModel instance</param> /// <param name="creator">Creator of the budget</param> /// <returns></returns> private Budget NewBudgetInstanceFromNewBudgetModel(NewBudgetModel model, UserProfile creator) { return new Budget { Name = model.Name, StartDate = model.StartDate, EndDate = model.EndDate, Limit = model.Limit, Description = model.Description ?? string.Empty, AccessRights = new List<BudgetAccessRight> { new BudgetAccessRight { Permission = PermissionEnum.Owner, UserProfile = creator } } }; }