Esempio n. 1
0
        Nancy.Response AddItem()
        {
            // 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();

            ItemModel item = this.Bind <ItemModel>();

            // Reject request with an ID param
            if (item.Id != null)
            {
                return(ErrorBuilder.ErrorResponse(this.Request.Url.ToString(), "POST", HttpStatusCode.Conflict, String.Format("Use PUT to update an existing Item with Id = {0}", item.Id)));
            }

            // Save the item to the DB
            try {
                ItemMapper itm_mpr = new ItemMapper();
                item.CreateId();
                itm_mpr.Add(item);
                Nancy.Response response = Response.AsJson(item);
                response.StatusCode = HttpStatusCode.Created;

                string uri = this.Request.Url.SiteBase + this.Request.Path + "/" + item.Id;
                response.Headers["Location"] = uri;

                return(response);
            } catch (Exception e) {
                Console.WriteLine(rawBody);
                String operation = String.Format("ItemModule.AddItem({0})", (item == null) ? "No Model Data" : item.Url);
                return(HandleException(e, operation));
            }
        }