Пример #1
0
        public HttpResponseMessage GetCategory()
        {
            var dbContext = new PostsContext();
            var categories = dbContext.Categories.AsQueryable();

            return this.Request.CreateResponse(HttpStatusCode.OK, categories);
        }
Пример #2
0
        public HttpResponseMessage GetPost()
        {
            var dbContext = new PostsContext();
            var posts = dbContext.Posts.AsQueryable();

            return this.Request.CreateResponse(HttpStatusCode.OK, posts);
        }
Пример #3
0
        public HttpResponseMessage PostCategory(Category category)
        {
            var dbContext = new PostsContext();
            dbContext.Categories.Add(category);
            dbContext.SaveChanges();

            return this.Request.CreateResponse(HttpStatusCode.Created);
        }
Пример #4
0
        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);
        }
Пример #5
0
        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);
        }