Nancy.Response UpdateNotification(string id) { NotificationModel notification = 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 { notification = this.Bind <NotificationModel>(); NotificationMapper not_mpr = new NotificationMapper(); notification.Id = id; NotificationModel res = not_mpr.GetById(id); if (res == null) { return(ErrorBuilder.ErrorResponse(this.Request.Url.ToString(), "GET", HttpStatusCode.NotFound, String.Format("A notification with Id = {0} does not exist", id))); } not_mpr.update(notification); Nancy.Response response = Response.AsJson(notification); response.StatusCode = HttpStatusCode.OK; string uri = this.Request.Url.SiteBase + this.Request.Path + "/" + notification.Id; response.Headers["Location"] = uri; return(response); } catch (Exception e) { String operation = String.Format("NotificationModule.UpdateBadge({0})", (notification == null) ? "No Model Data" : notification.Message); return(HandleException(e, operation)); } }
Nancy.Response DeleteNotification(string id) { try { NotificationMapper not_mpr = new NotificationMapper(); NotificationModel notification = not_mpr.GetById(id); if (notification == null) { return(ErrorBuilder.ErrorResponse(this.Request.Url.ToString(), "GET", HttpStatusCode.NotFound, String.Format("A notification with Id = {0} does not exist", id))); } not_mpr.delete(notification); return(HttpStatusCode.OK); } catch (Exception e) { return(HandleException(e, String.Format("\nNotificationModule.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 NotificationMapper not_mpr = new NotificationMapper(); NotificationModel notification = not_mpr.GetById(id); if (notification == 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("A notification with Id = {0} does not exist", id))); } else { // success. The Nancy server will automatically serialise this to JSON return(Response.AsJson(notification)); } } catch (Exception e) { return(HandleException(e, String.Format("NotificationModule.GetById({0})", id))); } }