예제 #1
0
        // GET: Posts/Details/5
        public async Task <IActionResult> Details(int?id)
        {
            var user = await this._userManager.GetUserAsync(User);

            if (id == null)
            {
                return(NotFound());
            }

            //Gets the post and its tag friend entities
            var post = await _context.Posts
                       .Include(i => i.TaggedUsers)
                       .Include(i => i.Author)
                       .FirstOrDefaultAsync(m => m.PostId == id);

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

            ViewModel = new PostTagFriendsViewModel()
            {
                CurrentUser = user,
                Post        = post,
                Tagged      = GetTaggedFriends(post.PostId, user.Id)
            };


            //Pass current postId to CommentsController
            TempData["postId"] = id;

            return(View(ViewModel));
        }
예제 #2
0
        //GET: Posts/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var post = await _context.Posts.FindAsync(id);

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

            var user = await this._userManager.GetUserAsync(User);

            ViewModel             = new PostTagFriendsViewModel();
            ViewModel.CurrentUser = user;
            ViewModel.Post        = post;
            ViewModel.Tagged      = GetTaggedFriends(post.PostId, user.Id);

            /*If this method is invoked before GetTaggedFriends,
             * there will add all of the current user`s friends.
             * Let`s get that user x is already tagged from the creation of the post.
             * It does not make sense the current user to be allowed to tag user x twice.*/
            ViewModel.UserFriends = GetUserFriends(user);

            return(View(ViewModel));
        }
예제 #3
0
        public async Task <IActionResult> Edit([FromForm] PostTagFriendsViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var user = await this._userManager.GetUserAsync(User);

                    ViewModel.CurrentUser = user;

                    var post = await this._context.Posts
                               .Include(i => i.TaggedUsers)
                               .FirstOrDefaultAsync(i => i.PostId == viewModel.Post.PostId);

                    post.Author   = user;
                    post.AuthorId = user.Id;
                    post.Content  = viewModel.Post.Content;

                    //Local tag friend entities
                    var tagFriendEntities = TagFriendEntities(post);
                    //Connected tag friend entities (In the db)
                    var postTagFriendEntities = post.TaggedUsers;
                    //If there is a mismatch between any record in the Local collection will be deleted from the Connected collection
                    RemoveTaggedFriendRecords(postTagFriendEntities, tagFriendEntities);
                    //If there is a mismatch between any record in the Connected collection will be added from the Local collection
                    AddLocalTaggedFriends(postTagFriendEntities, tagFriendEntities);

                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!PostExists(viewModel.Post.PostId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }

                ViewModel = new PostTagFriendsViewModel();
                return(RedirectToAction(nameof(UserPosts)));
            }

            return(View(viewModel));
        }
예제 #4
0
        // GET: Posts/Create
        public IActionResult Create(int?id)
        {
            var userId = this._userManager.GetUserId(User);
            var user   = this._context.Users.FirstOrDefault(i => i.Id == userId);

            ViewModel = new PostTagFriendsViewModel()
            {
                CurrentUser = user,
                UserFriends = GetUserFriends(user)
            };

            //Assign the group id if it is not null
            if (id != null)
            {
                GroupId = (int)id;
            }

            return(View(ViewModel));
        }
예제 #5
0
        public async Task <IActionResult> Create([FromForm] PostTagFriendsViewModel viewModel)
        {
            var user = await this._userManager.GetUserAsync(User);

            ViewModel.CurrentUser = user;
            //Creates post in the current user`s profile
            if (ModelState.IsValid)
            {
                var post = new Post()
                {
                    Author      = user,
                    AuthorId    = user.Id,
                    DatePosted  = DateTime.Now,
                    Content     = viewModel.Post.Content,
                    TaggedUsers = TagFriendEntities(),
                };

                //Assign groupId to the newly created post
                if (GroupId != 0)
                {
                    post.GroupId = GroupId;
                }

                _context.Posts.Add(post);
                await _context.SaveChangesAsync();

                ViewModel = new PostTagFriendsViewModel();

                //Redirect to the given group details view
                if (GroupId != 0)
                {
                    return(RedirectToAction("Details", "Groups", new { id = post.GroupId }));
                }

                return(RedirectToAction(nameof(UserPosts)));
            }

            return(View(viewModel));
        }