private Response UpdateBadge(int id) { Badge badge = null; try { badge = this.Bind <Badge>(); BadgeContext ctx = new BadgeContext(); ctx.update(badge); // 201 - created Nancy.Response response = new Nancy.Responses.JsonResponse <Badge>(badge, new DefaultJsonSerializer()); response.StatusCode = HttpStatusCode.OK; // uri string uri = this.Request.Url.SiteBase + this.Request.Path + "/" + badge.Id.ToString(); response.Headers["Location"] = uri; return(response); } catch (Exception e) { String operation = String.Format("BadgesModule.UpdateBadge({0})", (badge == null) ? "No Model Data" : badge.Title); var HandleException = (Response)operation; HandleException.StatusCode = HttpStatusCode.InternalServerError; return(HandleException); } }
public static Nancy.Response ErrorResponse(string url, string verb, HttpStatusCode code, string errorMessage) { ErrorBody e = new ErrorBody { Url = url, Operation = verb, Message = errorMessage }; // Build and return an object that the Nancy server knows about. Nancy.Response response = new Nancy.Responses.JsonResponse <ErrorBody>(e, new DefaultJsonSerializer()); response.StatusCode = code; return(response); }
Response GetUserDocs(Guid userId) { using (IDocumentRepository ctx = Program.NinjectKernel.Get <IDocumentRepository>()) { try { IEnumerable <Document> docs = ctx.GetAllByUserId(userId); Nancy.Response resp = new Nancy.Responses.JsonResponse <IEnumerable <Document> >(docs, new DefaultJsonSerializer()); return(resp); } catch { throw new Exception("Error on GetById"); } } }
private Response AddBadge() { Badge badge = null; try { // bind the request body to the object via a Nancy module. badge = this.Bind <Badge>(); // check exists. Return 409 if it does if (badge.Id > 0) { string errorMessage = String.Format("Use PUT to update an existing Badge with Id = {0}", badge.Id); var ErrorResponse = (Response)errorMessage; ErrorResponse.StatusCode = HttpStatusCode.Conflict; return(ErrorResponse); } BadgeContext ctx = new BadgeContext(); ctx.Add(badge); // 201 - created Nancy.Response response = new Nancy.Responses.JsonResponse <Badge>(badge, new DefaultJsonSerializer()); response.StatusCode = HttpStatusCode.Created; // uri string uri = this.Request.Url.SiteBase + this.Request.Path + "/" + badge.Id.ToString(); response.Headers["Location"] = uri; return(response); } catch (Exception e) { String operation = String.Format("BadgesModule.AddBadge({0})", (badge == null) ? "No Model Data" : badge.Title); var HandleException = (Response)operation; HandleException.StatusCode = HttpStatusCode.InternalServerError; return(HandleException); } }
private Response Search(string param) { try { BadgeContext ctx = new BadgeContext(); List <Badge> badge = ctx.SearchByName(param); Nancy.Response response = new Nancy.Responses.JsonResponse <List <Badge> >(badge, new DefaultJsonSerializer()); response.StatusCode = HttpStatusCode.OK; // uri string uri = this.Request.Url.SiteBase + this.Request.Path + "/"; response.Headers["Location"] = uri; return(response); } catch (Exception e) { String operation = String.Format("BadgesModule.SearchByName({0})", (param == "") ? "No Model Data" : param.ToString()); var HandleException = (Response)operation; HandleException.StatusCode = HttpStatusCode.InternalServerError; return(HandleException); } }
private Response GetAll() { try { BadgeContext ctx = new BadgeContext(); List <Badge> badges = ctx.GetAll(); Nancy.Response response = new Nancy.Responses.JsonResponse <List <Badge> >(badges, new DefaultJsonSerializer()); response.StatusCode = HttpStatusCode.OK; // uri string uri = this.Request.Url.SiteBase + this.Request.Path + "/"; response.Headers["Location"] = uri; return(response); } catch (Exception e) { String operation = String.Format("BadgesModule.GetAll()"); var HandleException = (Response)operation; HandleException.StatusCode = HttpStatusCode.InternalServerError; return(HandleException); } }
private Response GetById(int id) { try { BadgeContext ctx = new BadgeContext(); Badge badge = ctx.GetById(id); Nancy.Response response = new Nancy.Responses.JsonResponse <Badge>(badge, new DefaultJsonSerializer()); response.StatusCode = HttpStatusCode.OK; // uri string uri = this.Request.Url.SiteBase + this.Request.Path + "/" + badge.Id.ToString(); response.Headers["Location"] = uri; return(response); } catch (Exception e) { String operation = String.Format("BadgesModule.GetById({0})", (id == 0) ? "No Model Data" : id.ToString()); var HandleException = (Response)operation; HandleException.StatusCode = HttpStatusCode.InternalServerError; return(HandleException); } }