コード例 #1
0
        public async Task <IActionResult> Create(string title, string content)
        {
            string userId = _userManager.GetUserId(User);
            HttpResponseMessage response = await PostEndpoints.CreatePost(userId, title, content);

            return(RedirectToAction("Index", "Post"));
        }
コード例 #2
0
ファイル: PostTests.cs プロジェクト: musl0074/UstabilKodeMVC
        public void Get()
        {
            int validID = GetValidPostID();

            var post = PostEndpoints.GetPost(validID).Result;

            Assert.IsTrue(post != null);
        }
コード例 #3
0
        public async Task <IActionResult> Edit(int id)
        {
            var post = await PostEndpoints.GetPost(id);

            if (_userManager.GetUserId(User) != post.UserID) // Wrong user, trying to edit another post
            {
            }

            return(View(post));
        }
コード例 #4
0
ファイル: PostTests.cs プロジェクト: musl0074/UstabilKodeMVC
        public void Delete()
        {
            int validID = GetValidPostID();


            var response = PostEndpoints.DeletePost(validID).Result;


            Assert.IsTrue(response.StatusCode == System.Net.HttpStatusCode.OK);
        }
コード例 #5
0
        public async Task <IActionResult> Edit(Post post)
        {
            var postToUpdate = await PostEndpoints.GetPost(post.ID);

            postToUpdate.Title      = post.Title;
            postToUpdate.Content    = post.Content;
            postToUpdate.RowVersion = post.RowVersion;

            var result = await PostEndpoints.UpdatePost(postToUpdate);

            return(RedirectToAction("Get", "Post", new { id = postToUpdate.ID }));
        }
コード例 #6
0
ファイル: PostTests.cs プロジェクト: musl0074/UstabilKodeMVC
        private int GetValidPostID()
        {
            int validID = 0;

            for (int i = 1; i < 100; i++)
            {
                Post post = PostEndpoints.GetPost(i).Result;

                if (post != null && post.ID != 0)
                {
                    validID = post.ID;
                    break;
                }
            }

            return(validID);
        }
コード例 #7
0
        public async Task <IActionResult> Get(int id)
        {
            var post = await PostEndpoints.GetPost(id);

            var username    = _userManager.Users.Where((user) => user.Id == post.UserID).FirstOrDefault().UserName;
            var currentUser = await _userManager.GetUserAsync(User);

            var username_comments = new List <string>();

            foreach (var comment in post.Comments)
            {
                var comment_username = _userManager.Users.Where((user) => user.Id == comment.UserID).FirstOrDefault().UserName;
                username_comments.Add(comment_username);
            }

            return(View(new PostUsername()
            {
                Post = post, Username = username, CurrentUser = currentUser, Username_comments = username_comments
            }));
        }
コード例 #8
0
        public async Task <IActionResult> Delete(string id)
        {
            var user = await _userManager.FindByIdAsync(id);

            var posts = await PostEndpoints.GetPosts();

            // Delete all comments from the user
            foreach (var post in posts)
            {
                foreach (var comment in post.Comments)
                {
                    if (comment.UserID == user.Id)
                    {
                        await CommentEndpoints.DeleteComment(comment.ID);
                    }
                }
            }

            // Delete all posts by user
            foreach (var post in posts)
            {
                if (post.UserID == user.Id)
                {
                    // First delete all comments on that post
                    foreach (var comment in post.Comments)
                    {
                        await CommentEndpoints.DeleteComment(comment.ID);
                    }

                    // Then delete the post
                    await PostEndpoints.DeletePost(post.ID);
                }
            }

            // Delete the user from the database
            var result = await _userManager.DeleteAsync(user);

            return(RedirectToAction("Index", "User"));
        }
コード例 #9
0
        public async Task <IActionResult> Index()
        {
            var posts = await PostEndpoints.GetPosts();

            var userNames = new List <string>();

            List <IdentityUser> users = _userManager.Users.ToList();

            for (int i = 0; i < posts.Count; i++)
            {
                foreach (var user in users)
                {
                    if (user.Id == posts[i].UserID)
                    {
                        userNames.Add(user.UserName);
                    }
                }
            }

            return(View(new PostsUsers()
            {
                Posts = posts, Usernames = userNames
            }));
        }
コード例 #10
0
        public async Task <IActionResult> Delete(int id)
        {
            var result = await PostEndpoints.DeletePost(id);

            return(RedirectToAction("Index", "Post"));
        }
コード例 #11
0
ファイル: PostTests.cs プロジェクト: musl0074/UstabilKodeMVC
        public void Post()
        {
            var response = PostEndpoints.CreatePost("admin", "UnitTest", "UnitTest").Result;

            Assert.IsTrue(response.StatusCode == System.Net.HttpStatusCode.Created);
        }
コード例 #12
0
ファイル: PostTests.cs プロジェクト: musl0074/UstabilKodeMVC
        public void GetAll()
        {
            var posts = PostEndpoints.GetPosts().Result;

            Assert.IsTrue(posts.Count != 0);
        }