コード例 #1
0
        public IEnumerable <PostModel> GetAll()
        {
            PostsCategoriesContext context = new PostsCategoriesContext();

            var posts = context.Posts.Select(PostModel.FromPost);

            return(posts);
        }
コード例 #2
0
        public CategoryFullModel GetAllPosts(int categoryId)
        {
            PostsCategoriesContext context = new PostsCategoriesContext();

            var categoryInfo = context.Categories
                               .Where(x => x.Id == categoryId)
                               .Select(CategoryFullModel.FromCategory)
                               .FirstOrDefault();

            return(categoryInfo);
        }
コード例 #3
0
        public PostModel GetPost(int postId)
        {
            PostsCategoriesContext context = new PostsCategoriesContext();

            var post = context.Posts
                       .Where(x => x.Id == postId)
                       .Select(PostModel.FromPost)
                       .FirstOrDefault();

            return(post);
        }
コード例 #4
0
        public PostModel PostPost([FromBody] PostModel postDetails)
        {
            PostsCategoriesContext context = new PostsCategoriesContext();

            if (postDetails == null || postDetails.Category == null || postDetails.Category.Name == null ||
                postDetails.Category.Name.Length <= 1)
            {
                throw new ArgumentOutOfRangeException("Invalid post data.");
            }

            var category = context.Categories
                           .FirstOrDefault(x => x.Name.ToLower() == postDetails.Category.Name.ToLower());

            if (category == null)
            {
                category = new Category
                {
                    Name = postDetails.Category.Name.ToLower()
                };

                context.Categories.Add(category);
                context.SaveChanges();
            }

            Post newPost = new Post
            {
                Title    = postDetails.Title,
                Content  = postDetails.Content,
                Category = category
            };

            context.Posts.Add(newPost);
            context.SaveChanges();


            return(context.Posts
                   .Where(x => x.Id == newPost.Id)
                   .Select(PostModel.FromPost)
                   .FirstOrDefault());
        }
コード例 #5
0
        public IEnumerable <CategoryModel> GetAll()
        {
            PostsCategoriesContext context = new PostsCategoriesContext();

            return(context.Categories.Select(CategoryModel.FromCategory));
        }