public async Task<IHttpActionResult> PostWorkItem(CreateWorkItemViewModel workItemvm) { if(!ModelState.IsValid) { return BadRequest(ModelState); } var workItem = await _workItemManager.CreateWorkItem(workItemvm); if(workItem == null) { throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Could not match class to existing records")); } return CreatedAtRoute("DefaultApi", new { id = workItem.Id }, new WorkItemViewModel(workItem)); }
/// <summary> /// Creates a new WorkItem from the given model /// </summary> /// <param name="cwvm">Model containing the detail of the WorkItem to create</param> /// <returns>Null if the class or teacher is not found, Otherwise, returns the WorkItem created</returns> public async Task<WorkItem> CreateWorkItem(CreateWorkItemViewModel cwvm) { var @class = await Db.Classes.FindAsync(cwvm.ClassId); if(@class == null) { return null; } WorkItem workItem = new WorkItem { Title = cwvm.Title, Description = cwvm.Description, DueDate = cwvm.DueDate, MaxPoints = cwvm.MaxPoints, Type = cwvm.Type, Class = @class }; Db.WorkItems.Add(workItem); await Db.SaveChangesAsync(); return workItem; }