// // GET: /Manage/Grant/Create public ActionResult Create() { ViewData["Classifications"] = _uow.Classifications.All .ToSelectListWithEmptyOption(c => c.Id.ToString(), c => c.Name.ToString()); var vm = new GrantBindingModel(); return View(vm); }
public async Task<ActionResult> Create(GrantBindingModel form) { try { // TODO: Add insert logic here if (ModelState.IsValid) { //var issuer = _uow.Organizations.All.OfType<FundingAgency>().First(a => a.Id == form.IssuerId); //var grant = AutoMapper.Mapper.Map<Grant>(form); var clas = _uow.Classifications.FindById(form.ClassificationId); _uow.Grants.Add(Grant.NewGrant(form.Code, form.Name, form.Description, form.Objective, form.Scope, form.IsActive, form.Total, clas)); await _uow.SaveAsync(); } return RedirectToAction<GrantController>(c => c.Index()).WithSuccess("Created"); } catch { return View(); } }
public async Task<ActionResult> Edit(Guid id, GrantBindingModel form) { try { if (ModelState.IsValid) { var clas = await _uow.Classifications.FindAsyncById(form.ClassificationId); var grant = await _uow.Grants.FindAsyncById(id); grant.Code = form.Code; grant.Name = form.Name; grant.Description = form.Description; grant.Objective = form.Objective; grant.Scope = form.Scope; grant.IsActive = form.IsActive; grant.Total = form.Total; grant.Classification = clas; await _uow.SaveAsync(); } return RedirectToAction<GrantController>(c => c.Index()).WithSuccess("Updated"); } catch (Exception ex) { return RedirectToAction<GrantController>(c => c.Index()).WithError("Updated failed " + ex.Message); } }
// PUT api/Grant/5 public IHttpActionResult PutGrant(Guid id, GrantBindingModel model) { if (!ModelState.IsValid) { return BadRequest(ModelState); } var item = _uow.Grants.FindById(id); if (item == null) { return NotFound(); } item.Code = model.Code; item.Name = model.Name; item.Description = model.Description; item.MaxAward = model.MaxAward; item.MaxDuration = model.MaxDuration; item.Total = model.Total; item.LastUpdated = DateTime.Now; try { _uow.Save(); } catch (DbUpdateConcurrencyException) { throw; } return StatusCode(HttpStatusCode.NoContent); }
//[Route("api/grants/{id}/budgets")] //public IHttpActionResult GetBudgets(Guid id) //{ // var grant = _uow.Grants.GetAll().Include(g => g.Budgets).Where(g => g.Id == id).FirstOrDefault(); // if (grant == null) // { // return NotFound(); // } // var data = grant.Budgets.Select(b => new BudgetViewModel // { // Id = b.Id, // Name = b.Name, // Description = b.Description, // Year = b.FinancialYear, // Amount = b.Amount, // Supplier = _uow.Organizations.FindById(b.SupplierId).Name // }); // return Ok(data); //} //[Route("api/grants/{grantId}/budgets/{budgetId}")] //public IHttpActionResult GetBudgets(Guid grantId, Guid budgetId) //{ // var grant = _uow.Grants.GetAll().Include(g => g.Budgets).Where(g => g.Id == grantId).FirstOrDefault(); // if (grant == null) // { // return NotFound(); // } // var data = grant.Budgets.Where(b => b.Id == budgetId).Select(b => new BudgetBindingModel // { // Name = b.Name, // Description = b.Description, // FinancialYear = b.FinancialYear, // Amount = b.Amount, // SupplierId = b.SupplierId // }).FirstOrDefault(); // return Ok(data); //} //[Route("api/grants/{grantId}/budgets/{budgetId}")] //public IHttpActionResult PutGetBudgets(Guid grantId, Guid budgetId, BudgetBindingModel model) //{ // var grant = _uow.Grants.GetAll().Include(g => g.Budgets).Where(g => g.Id == grantId).FirstOrDefault(); // if (grant == null) // { // return NotFound(); // } // var data = grant.Budgets.Where(b => b.Id == budgetId).FirstOrDefault(); // var org = _uow.Organizations.FindById(model.SupplierId); // data.Name = model.Name; // data.Description = model.Description; // data.FinancialYear = model.FinancialYear; // data.Amount = model.Amount; // data.Supplier = org; // grant.LastUpdated = DateTime.Now; // _uow.Commit(); // return Ok(); //} //[Route("api/grants/{id}/budgets")] //public IHttpActionResult Post(Guid id, BudgetBindingModel model) //{ // //validation // if (!ModelState.IsValid) // { // return BadRequest(ModelState); // } // Grant grant = _uow.Grants.FindById(id); // //var org = _uow.Organizations.FindById(model.Supplier); // if (grant == null) // { // return NotFound(); // } // var supplier = _uow.Organizations.FindById(model.SupplierId); // grant.Budgets.Add(new Budget // { // Id = Guid.NewGuid(), // Name = model.Name, // Description = model.Description, // FinancialYear = model.FinancialYear, // Amount = model.Amount, // Supplier = supplier // }); // grant.LastUpdated = DateTime.Now; // _uow.Commit(); // return Ok(); //} public IHttpActionResult Post(GrantBindingModel model) { //validation if (!ModelState.IsValid) { return BadRequest(ModelState); } var grant = new Grant { Id = Guid.NewGuid(), Code = model.Code, Name = model.Name, Description = model.Description, Objective = model.Objective, MaxDuration = model.MaxDuration, MaxAward = model.MaxAward, Total = model.Total, //Agency = _uow.Organizations.FindById(model.Issuer), Created = DateTime.Now }; //item.Init(grant.Code, grant.Name, grant.Description, grant.Total, grant.MaxAward); _uow.Grants.Add(grant); _uow.Save(); return Ok(grant); }