public HttpResponseMessage DeleteBlog(string id, HttpRequestMessage request) { HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.NoContent); try { IBlogsService blogsService = ObjectFactory.GetInstance <IBlogsService>(); var blog = blogsService.Get(String.Format("blogs/{0}", id)); if (blog != null) { blogsService.Delete(String.Format("blogs/{0}", id)); } else { response.StatusCode = HttpStatusCode.NotFound; } } catch (Exception) { response.StatusCode = HttpStatusCode.InternalServerError; } return(response); }
public HttpResponseMessage UpdateBlog(string id, HttpRequestMessage request) { HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.NoContent); try { IBlogsService blogsService = ObjectFactory.GetInstance <IBlogsService>(); var blog = blogsService.Get(String.Format("blogs/{0}", id)); if (blog != null) { XmlReader reader = XmlReader.Create(request.Content.ReadAsStreamAsync().Result); SyndicationFeed feed = SyndicationFeed.Load(reader); if (feed != null) { SyndicationItem item = feed.Items.FirstOrDefault(); if (item != null) { blog.name = item.Title.Text; blog.description = item.Summary.Text; blog.updated = item.LastUpdatedTime; blog.published = item.PublishDate; var author = item.Authors.FirstOrDefault(); if (author != null) { blog.author = author.Name; blogsService.Update(blog); } else { response.StatusCode = HttpStatusCode.BadRequest; } } else { response.StatusCode = HttpStatusCode.BadRequest; } } else { response.StatusCode = HttpStatusCode.BadRequest; } } else { response.StatusCode = HttpStatusCode.NotFound; } } catch (Exception) { response.StatusCode = HttpStatusCode.InternalServerError; } return(response); }
public HttpResponseMessage GetBlogTagCloud(string id, HttpRequestMessage request) { HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK); try { IBlogsService blogsService = ObjectFactory.GetInstance <IBlogsService>(); var blog = blogsService.Get(String.Format("blogs/{0}", id)); var tagCloud = blogsService.GetTagCloud(String.Format("blogs/{0}", id)); if (blog != null) { SyndicationFeed blogFeed = new SyndicationFeed { Title = new TextSyndicationContent("Blog tag cloud"), LastUpdatedTime = new DateTimeOffset(DateTime.Now) }; blogFeed.Links.Add(SyndicationLink.CreateSelfLink(request.RequestUri)); SyndicationItem item = new SyndicationItem(); List <SyndicationItem> itemList = new List <SyndicationItem> { item }; blogFeed.Items = itemList; item.Id = blog.Id; item.LastUpdatedTime = blog.updated; item.PublishDate = blog.published; item.Title = new TextSyndicationContent("Blog tag cloud"); item.Content = SyndicationContent.CreatePlaintextContent(BuildtagCloud(tagCloud)); item.Links.Add(SyndicationLink.CreateSelfLink(request.RequestUri)); SyndicationFeedFormatter formatter = null; if (this.ClientAcceptsMediaType("application/atom+xml", request)) { formatter = blogFeed.GetAtom10Formatter(); } else { if (this.ClientAcceptsMediaType("application/rss+xml", request)) { formatter = blogFeed.GetRss20Formatter(); } } response.Content = new ObjectContent(typeof(SyndicationFeedFormatter), formatter); } else { response.StatusCode = HttpStatusCode.NotFound; } } catch (Exception) { response.StatusCode = HttpStatusCode.InternalServerError; } return(response); }
public HttpResponseMessage GetBlog(string id, HttpRequestMessage request) { HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK); try { IBlogsService blogsService = ObjectFactory.GetInstance <IBlogsService>(); var blog = blogsService.Get(String.Format("blogs/{0}", id)); var etag = request.Headers.IfNoneMatch.FirstOrDefault(); if (etag != null && etag.Tag == blog.etag) { response.StatusCode = HttpStatusCode.NotModified; } else { if (blog != null) { if (this.ClientAcceptsMediaType("text/html", request)) { response.Content = new ObjectContent <string>(blog.ToHtml(), "text/html"); } else { SyndicationFeed blogFeed = new SyndicationFeed { Title = new TextSyndicationContent("Single Blog"), LastUpdatedTime = new DateTimeOffset(DateTime.Now) }; blogFeed.Links.Add(SyndicationLink.CreateSelfLink(request.RequestUri)); SyndicationItem item = new SyndicationItem(); List <SyndicationItem> itemList = new List <SyndicationItem> { item }; blogFeed.Items = itemList; item.Id = blog.Id; item.LastUpdatedTime = blog.updated; item.PublishDate = blog.published; item.Title = new TextSyndicationContent(blog.name); item.Summary = new TextSyndicationContent(blog.description); item.Links.Add(SyndicationLink.CreateSelfLink(request.RequestUri)); item.Links.Add(SyndicationLink.CreateAlternateLink(request.RequestUri, "text/html")); item.Links.Add(new SyndicationLink(new Uri(String.Format("{0}/{1}", this.serviceURI, blog.Id)), "edit", "Edit blog", "application/atom+xml;type=feed", 0)); item.Links.Add(new SyndicationLink(new Uri(String.Format("{0}/{1}/posts", this.serviceURI, blog.Id)), "posts", "Blog posts", "application/atom+xml;type=feed", 0)); item.Authors.Add(new SyndicationPerson(string.Empty, blog.author, string.Empty)); SyndicationFeedFormatter formatter = null; if (this.ClientAcceptsMediaType("application/atom+xml", request)) { formatter = blogFeed.GetAtom10Formatter(); } else { if (this.ClientAcceptsMediaType("application/rss+xml", request)) { formatter = blogFeed.GetRss20Formatter(); } } response.Content = new ObjectContent(typeof(SyndicationFeedFormatter), formatter); } } else { response.StatusCode = HttpStatusCode.NotFound; } } } catch (Exception) { response.StatusCode = HttpStatusCode.InternalServerError; } return(response); }