//when you post using the above http address it will run this method. it will pass the object you are posting into the method public IActionResult Post([FromBody] TrackedTask trackedTask) { if (!ModelState.IsValid) //it will check the model state to make sure it's valid { return(BadRequest(ModelState)); //if invalid it will return badrequest } context.TrackedTask.Add(trackedTask); //if valid it will add the task to the context try { context.SaveChanges(); //to add save the changes to the context to the DB } catch (DbUpdateException) //if the try doesn't work, we'll catch the exception { if (TrackedTaskExists(trackedTask.Taskid)) //if the task exists, { return(new StatusCodeResult(StatusCodes.Status409Conflict)); //we'll return an error saying there is a conflict } else { throw; } } //return new OkObjectResult(trackedTask); //this one worked when the CreatedAtRoute was not working return(CreatedAtRoute("GetTrackedTask", new { id = trackedTask.Taskid }, trackedTask)); //this did not work until I named the route in the get method above }
public IActionResult Put(int id, [FromBody] TrackedTask trackedTask) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != trackedTask.Taskid) { return(BadRequest(ModelState)); } if (trackedTask.TaskOrderStatus == OrderStatus.Complete) //this checks the value of the enum to see if it is complete { trackedTask.CompletedOn = DateTime.Now; } context.TrackedTask.Update(trackedTask); try { context.SaveChanges(); } catch (DbUpdateException) { if (TrackedTaskExists(trackedTask.Taskid)) { return(new StatusCodeResult(StatusCodes.Status409Conflict)); } else { throw; } } return(Ok(trackedTask)); }
public bool StartTracking(Task task, OnTaskContinuationSetCallback callback, object userData) { var trackedTask = new TrackedTask { TaskReference = new WeakReference <Task>(task, trackResurrection: false), Callback = callback, UserData = userData }; lock (_trackedTasks) { _trackedTasks.Add(trackedTask); } return(true); }
public IActionResult Get([FromRoute] int id) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); //if the model state is bad it will return badrequest } try //here we will try to pull the one task by using a linq query matching the passed in id (from the http route) with the id of a single task id in the DB { TrackedTask trackedTask = context.TrackedTask.Single(m => m.Taskid == id); if (trackedTask == null) { return(NotFound()); //if it does not find the id, it will return not found } return(Ok(trackedTask)); //if it finds the task it will return it } catch (System.InvalidOperationException ex) //if the try doesn't work, it will catch the error { return(NotFound(ex)); //and then return the error } }
public IActionResult Delete(int id, [FromBody] TrackedTask trackedTask) { if (id != trackedTask.Taskid) { return(BadRequest(ModelState)); } context.TrackedTask.Remove(trackedTask); try { context.SaveChanges(); } catch { if (TrackedTaskExists(trackedTask.Taskid)) { return(new StatusCodeResult(StatusCodes.Status409Conflict)); } else { throw; } } return(Ok(trackedTask)); }