Exemplo n.º 1
0
        public HttpResponseMessage GetCategory()
        {
            var dbContext = new PostsContext();
            var categories = dbContext.Categories.AsQueryable();

            return this.Request.CreateResponse(HttpStatusCode.OK, categories);
        }
Exemplo n.º 2
0
        public HttpResponseMessage GetPost()
        {
            var dbContext = new PostsContext();
            var posts = dbContext.Posts.AsQueryable();

            return this.Request.CreateResponse(HttpStatusCode.OK, posts);
        }
Exemplo n.º 3
0
        public HttpResponseMessage PostCategory(Category category)
        {
            var dbContext = new PostsContext();
            dbContext.Categories.Add(category);
            dbContext.SaveChanges();

            return this.Request.CreateResponse(HttpStatusCode.Created);
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }