public HttpResponseMessage GetCategory() { var dbContext = new PostsContext(); var categories = dbContext.Categories.AsQueryable(); return this.Request.CreateResponse(HttpStatusCode.OK, categories); }
public HttpResponseMessage GetPost() { var dbContext = new PostsContext(); var posts = dbContext.Posts.AsQueryable(); return this.Request.CreateResponse(HttpStatusCode.OK, posts); }
public HttpResponseMessage PostCategory(Category category) { var dbContext = new PostsContext(); dbContext.Categories.Add(category); dbContext.SaveChanges(); return this.Request.CreateResponse(HttpStatusCode.Created); }
public HttpResponseMessage GetPostsByCategory(int categoryId) { var dbContext = new PostsContext(); var postsByCategory = (from p in dbContext.Posts where p.Category.Id == categoryId select p).AsQueryable(); return this.Request.CreateResponse(HttpStatusCode.OK, postsByCategory); }
public HttpResponseMessage CreatePost(Post post) { var dbContext = new PostsContext(); if (post.Category != null) { var postCat = dbContext.Categories.FirstOrDefault(c => c.Name == post.Category.Name); if (postCat != null) { post.Category = postCat; } } dbContext.Posts.Add(post); dbContext.SaveChanges(); return this.Request.CreateResponse(HttpStatusCode.Created); }