コード例 #1
0
ファイル: PostController.cs プロジェクト: n-yamaji/Kakashi
        // GET: /<controller>/
        public IActionResult Index()
        {
            投稿  post   = 投稿.新規("●●について", "まるまるまる");
            投稿者 author = 投稿者.新規作成("○○さん");

            return(View(PostViewModel.Create(post, author)));
        }
コード例 #2
0
        public IHttpActionResult GetWallPosts([FromUri] WallBindingModel bindingModel)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.BadRequest(this.ModelState));
            }

            if (bindingModel == null)
            {
                return(this.BadRequest("Invalid data - username cannot be null"));
            }

            var wantedUser = this.Data
                             .Users
                             .All()
                             .FirstOrDefault(u => u.UserName == bindingModel.Username);

            if (wantedUser == null)
            {
                return(this.NotFound());
            }

            var currentUserId = this.UserIdProvider.GetUserId();
            var currentUser   = this.Data.Users.Find(currentUserId);
            var posts         = this.Data
                                .Posts
                                .All()
                                .Where(p => p.WallOwnerId == wantedUser.Id)
                                .OrderByDescending(p => p.PostedOn)
                                .Skip(bindingModel.StartPostNumber)
                                .Take(bindingModel.PostsCount)
                                .Select(PostViewModel.Create(currentUser));

            return(this.Ok(posts));
        }
コード例 #3
0
        public IHttpActionResult GetNewsFeed([FromUri] NewsFeedBindingModel bindingModel)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.BadRequest(this.ModelState));
            }

            if (bindingModel == null)
            {
                return(this.BadRequest("Invalid data."));
            }

            var currentUserId = this.UserIdProvider.GetUserId();
            var currentUser   = this.Data.Users.Find(currentUserId);
            var posts         = this.Data
                                .Posts
                                .All()
                                .Where(p => p.Author.Friends.Any(f => f.Id == currentUserId) ||
                                       p.WallOwner.Friends.Any(f => f.Id == currentUserId))
                                .OrderByDescending(p => p.PostedOn)
                                .Skip(bindingModel.StartPostNumber)
                                .Take(bindingModel.PostsCount)
                                .Select(PostViewModel.Create(currentUser));

            return(this.Ok(posts));
        }
        public IHttpActionResult GetPostsById(int postId)
        {
            var currentUserId = this.UserIdProvider.GetUserId();
            var currentUser   = this.Data.Users.Find(currentUserId);
            var post          = this.Data
                                .Posts
                                .All()
                                .Where(p => p.Id == postId)
                                .Select(PostViewModel.Create(currentUser))
                                .FirstOrDefault();

            if (post == null)
            {
                return(this.NotFound());
            }

            var postAutor = this.Data.Users.Find(post.Author.Id);

            if ((postAutor.Friends.All(f => f.Id != currentUserId)) && post.Author.Id != currentUserId)
            {
                return(this.Unauthorized());
            }

            return(this.Ok(post));
        }
コード例 #5
0
        public IHttpActionResult GetWall(string username, [FromUri] GetWallBindingModel wall)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.BadRequest(this.ModelState));
            }

            var wallOwner = this.SocialNetworkData.Users.All()
                            .FirstOrDefault(u => u.UserName == username);

            if (wallOwner == null)
            {
                return(this.NotFound());
            }

            var loggedUserId = this.User.Identity.GetUserId();

            if (loggedUserId == null)
            {
                return(this.BadRequest("Invalid session token."));
            }

            var loggedUser = this.SocialNetworkData.Users.GetById(loggedUserId);

            //if (wallOwner.Id != loggedUserId)
            //{
            //    var isFriendOfWallOwner = wallOwner.Friends
            //       .Any(fr => fr.Id == loggedUserId);
            //    if (!isFriendOfWallOwner)
            //    {
            //        return this.BadRequest("Cannot view non-friend wall.");
            //    }
            //}

            var candidatePosts = wallOwner.WallPosts
                                 .OrderByDescending(p => p.Date)
                                 .AsQueryable();

            if (wall.StartPostId.HasValue)
            {
                candidatePosts = candidatePosts
                                 .SkipWhile(p => p.Id != wall.StartPostId)
                                 .Skip(1)
                                 .AsQueryable();
            }

            var pagePosts = candidatePosts
                            .Take(wall.PageSize)
                            .Select(p => PostViewModel.Create(p, loggedUser));

            return(this.Ok(pagePosts));
        }
コード例 #6
0
        public IHttpActionResult PostOnWall(AddPostBindingModel postModel)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.BadRequest(this.ModelState));
            }

            var wallOwner = this.SocialNetworkData.Users.All()
                            .FirstOrDefault(u => u.UserName == postModel.Username);

            if (wallOwner == null)
            {
                return(this.NotFound());
            }

            var loggedUserId = this.User.Identity.GetUserId();

            var loggedUser = this.SocialNetworkData.Users
                             .GetById(loggedUserId);

            if (loggedUserId == null)
            {
                return(this.BadRequest("Invalid session token."));
            }

            if (wallOwner.Id != loggedUserId)
            {
                var isFriendOfWallOwner = wallOwner.Friends
                                          .Any(fr => fr.Id == loggedUserId);
                if (!isFriendOfWallOwner)
                {
                    return(this.BadRequest("Only friends can post on user's wall."));
                }
            }

            var newPost = new Post()
            {
                Content     = postModel.PostContent,
                WallOwner   = wallOwner,
                WallOwnerId = wallOwner.Id,
                Author      = loggedUser,
                AuthorId    = loggedUserId,
                Date        = DateTime.Now
            };

            this.SocialNetworkData.Posts.Add(newPost);
            this.SocialNetworkData.SaveChanges();

            return(this.Ok(PostViewModel.Create(newPost, loggedUser)));
        }
コード例 #7
0
        public Task <PostViewModel> Execute(string slug, int version)
        {
            var post = posts.GetVersion(slug, version);

            if (post == null)
            {
                return(Task.FromResult <PostViewModel>(null));
            }

            var errors = GetErrorsInPost(post);

            var viewModel = PostViewModel.Create(post);

            viewModel.Errors = errors;

            return(Task.FromResult(viewModel));
        }
コード例 #8
0
        public IHttpActionResult GetNewsFeed([FromUri] NewsFeedBindingModel feedModel)
        {
            var userId = this.User.Identity.GetUserId();

            if (userId == null)
            {
                return(this.BadRequest());
            }

            if (feedModel == null)
            {
                return(this.BadRequest("No page size specified."));
            }

            if (!this.ModelState.IsValid)
            {
                return(this.BadRequest(this.ModelState));
            }

            var user           = this.SocialNetworkData.Users.GetById(userId);
            var candidatePosts = this.SocialNetworkData.Posts.All()
                                 .Where(p => p.Author.Friends.Any(fr => fr.Id == userId) ||
                                        p.WallOwner.Friends.Any(fr => fr.Id == userId))
                                 .OrderByDescending(p => p.Date)
                                 .AsEnumerable();

            if (feedModel.StartPostId.HasValue)
            {
                candidatePosts = candidatePosts
                                 .SkipWhile(p => p.Id != feedModel.StartPostId)
                                 .Skip(1);
            }

            var pagePosts = candidatePosts
                            .Take(feedModel.PageSize)
                            .Select(p => PostViewModel.Create(p, user));

            if (!pagePosts.Any())
            {
                return(this.Ok(Enumerable.Empty <string>()));
            }

            return(this.Ok(pagePosts));
        }
コード例 #9
0
        public Task <PostViewModel> Execute()
        {
            var post    = new Post();
            var builder = new StringBuilder();

            builder.AppendLine("class Person")
            .AppendLine("{")
            .AppendLine("    public Person(string name)")
            .AppendLine("    {")
            .AppendLine("        Name = name;")
            .AppendLine("    }")
            .AppendLine()
            .AppendLine("    public string Name { get; private set; }")
            .AppendLine()
            .AppendLine("    public string Greet()")
            .AppendLine("    {")
            .AppendLine("        if (Name == null)")
            .AppendLine("            return \"Hello, stranger!\";")
            .AppendLine()
            .AppendLine("        return string.Format(\"Hello, {0}!\", Name);")
            .AppendLine("    }")
            .AppendLine("}");

            post.Classes = builder.ToString();

            builder.Clear()
            .AppendLine("var person = new Person(name: null);")
            .AppendLine()
            .AppendLine("return person.Greet();");

            post.Content = builder.ToString();

            var result = PostViewModel.Create(post);

            result.Errors = validator.GetCompilationErrors(post);

            return(Task.FromResult(result));
        }
コード例 #10
0
        public IHttpActionResult Get(int id)
        {
            var existingPost = this.SocialNetworkData.Posts.All()
                               .FirstOrDefault(p => p.Id == id);

            if (existingPost == null)
            {
                return(this.NotFound());
            }

            var userId = this.User.Identity.GetUserId();

            if (userId == null)
            {
                return(this.BadRequest("Invalid session token."));
            }

            var user = this.SocialNetworkData.Users.GetById(userId);

            var post = PostViewModel.Create(existingPost, user);

            return(this.Ok(post));
        }
コード例 #11
0
        public ActionResult GetHomePosts(string loaded)
        {
            var user = _repository.ApplicationUserRepository.GetUserById(User.Identity.GetUserId());

            if (user == null)
            {
                return(HttpNotFound());
            }

            var loadedPosts = new List <Post>();

            try
            {
                var ids = loaded.Split('_');
                foreach (var id in ids)
                {
                    try
                    {
                        var temp = Convert.ToInt32(id);
                        loadedPosts.Add(_repository.PostRepository.GetPost(temp));
                    }
                    catch
                    {
                        // ignored
                    }
                }
            }
            catch
            {
                // ignored
            }

            var posts = _repository.PostRepository.GetHomePosts(user.Id).Except(loadedPosts).Take(10);
            var list  = posts.Select(post => PostViewModel.Create(post, user)).ToList();

            return(PartialView("_PostList", list));
        }