Nancy.Response UpdateItem(string id) { ItemModel item = null; // capture actual string posted in case the bind fails (as it will if the JSON is bad) // need to do it now as the bind operation will remove the data String rawBody = this.GetRawBody(); try { item = this.Bind <ItemModel>(); ItemMapper itm_mpr = new ItemMapper(); item.Id = id; ItemModel res = itm_mpr.GetById(id); if (res == null) { return(ErrorBuilder.ErrorResponse(this.Request.Url.ToString(), "GET", HttpStatusCode.NotFound, String.Format("An Item with Id = {0} does not exist", id))); } itm_mpr.update(item); Nancy.Response response = Response.AsJson(item); response.StatusCode = HttpStatusCode.OK; string uri = this.Request.Url.SiteBase + this.Request.Path + "/" + item.Id; response.Headers["Location"] = uri; return(response); } catch (Exception e) { String operation = String.Format("ItemModule.UpdateBadge({0})", (item == null) ? "No Model Data" : item.Url); return(HandleException(e, operation)); } }
Nancy.Response DeleteItem(string id) { try { ItemMapper itm_mpr = new ItemMapper(); ItemModel item = itm_mpr.GetById(id); if (item == null) { return(ErrorBuilder.ErrorResponse(this.Request.Url.ToString(), "GET", HttpStatusCode.NotFound, String.Format("An Item with Id = {0} does not exist", id))); } itm_mpr.delete(item); return(HttpStatusCode.OK); } catch (Exception e) { return(HandleException(e, String.Format("\nItemModule.Delete({0})", id))); } }
private object GetById(string id) { try { // create a connection to the PetaPoco orm and try to fetch and object with the given Id ItemMapper itm_mpr = new ItemMapper(); ItemModel item = itm_mpr.GetById(id); if (item == null) // a null return means no object found // return a reponse conforming to REST conventions: a 404 error { return(ErrorBuilder.ErrorResponse(this.Request.Url.ToString(), "GET", HttpStatusCode.NotFound, String.Format("An Item with Id = {0} does not exist", id))); } else { // success. The Nancy server will automatically serialise this to JSON return(Response.AsJson(item)); } } catch (Exception e) { return(HandleException(e, String.Format("ItemModule.GetById({0})", id))); } }