Пример #1
0
        public async Task DeletePost(int id)
        {
            var postToDelete = await _context.Posts.SingleOrDefaultAsync(x => x.PostId == id);

            _context.Remove(postToDelete);
            await _context.SaveChangesAsync();
        }
        public async Task <IActionResult> Like(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Post     post     = null;
            PostLike postlike = null;

            try
            {
                var user = await _userManager.GetUserAsync(User);

                post = await _context.Posts
                       .AsNoTracking()
                       .FirstOrDefaultAsync(m => m.ID == id);

                postlike = new PostLike
                {
                    UserID = await _userManager.GetUserIdAsync(user),
                    PostID = post.ID
                };

                if (ModelState.IsValid)
                {
                    if (_context.PostLikes.Contains(postlike))
                    {
                        _context.Remove(postlike);
                        await _context.SaveChangesAsync();

                        return(RedirectToAction(nameof(Index)));
                    }
                    _context.Add(postlike);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
            }
            catch (DbUpdateException /* ex */)
            {
                //Log the error (uncomment ex variable name and write a log.
                ModelState.AddModelError("", "Unable to save changes. " +
                                         "Try again, and if the problem persists " +
                                         "see your system administrator.");
            }
            return(RedirectToAction(nameof(Index)));
        }
Пример #3
0
 public async Task DeleteUser(User user)
 {
     _context.Remove(user);
     await _context.SaveChangesAsync();
 }