public async Task <IActionResult> Create([FromForm] GroupViewModel viewModel)
        {
            if (viewModel.Group == null)
            {
                return(NotFound());
            }
            else
            {
                ViewModel.Group = viewModel.Group;
            }

            //Unique title
            if (await this._context.Groups.AnyAsync(i => i.Title == ViewModel.Group.Title))
            {
                ModelState.AddModelError("Title", $"Title {ViewModel.Group.Title} already exists. Title must be unique!");
                return(View(ViewModel));
            }

            if (ModelState.IsValid)
            {
                //Gets the current user
                ViewModel.CurrentUser = await this._userManager.GetUserAsync(User);

                //Create new row in the UsersInGroups table where the current user is admin
                var adminGroup = new UserInGroup()
                {
                    UserId  = ViewModel.CurrentUser.Id,
                    User    = ViewModel.CurrentUser,
                    GroupId = ViewModel.Group.GroupId,
                    Group   = ViewModel.Group,
                    Admin   = true
                };

                await this._context.UsersInGroups.AddAsync(adminGroup);

                await this._context.Groups.AddAsync(ViewModel.Group);

                await _context.SaveChangesAsync();

                ViewModel = new GroupViewModel();
                return(RedirectToAction(nameof(MyGroups)));
            }
            return(View());
        }
Exemplo n.º 2
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));
        }
        public async Task <IActionResult> Create([FromForm] CommentTagFriendsViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                //Get current user
                var user = await this._userManager.GetUserAsync(User);

                ViewModel.CurrentUser = user;
                var comment = new Comment()
                {
                    Author      = user,
                    AuthorId    = user.Id,
                    DatePosted  = DateTime.Now,
                    Content     = viewModel.Comment.Content,
                    TaggedUsers = TagFriendEntities()
                };

                //Get the post
                var post = await this._context.Posts.FirstOrDefaultAsync(i => i.PostId == _postId);

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

                comment.CommentedPost   = post;
                comment.CommentedPostId = post.PostId;

                this._context.Comments.Add(comment);
                await _context.SaveChangesAsync();

                ViewModel = new CommentTagFriendsViewModel();
                return(RedirectToAction(nameof(Index)));
            }
            return(View(viewModel));
        }