Exemple #1
0
        public async Task<ActionResult<bool>> FollowUser(BlockPostData data)
        {
            var uid = User.GetNumericId();
            if (!uid.HasValue) return Unauthorized();

            var targetUserId = await _context.Users
                .Where(u => u.NormalizedUserName == data.Name.Normalize().ToUpper())
                .Select(u => u.Id)
                .FirstOrDefaultAsync();

            var existing = await _context.FollowedUsers
                .Where(bu => bu.FollowingUserId == uid)
                .Where(bu => bu.FollowedUserId == targetUserId)
                .FirstOrDefaultAsync();
            
            if (existing == null)
            {
                await _context.FollowedUsers.AddAsync(new UserFollow
                {
                    FollowingUserId = (long) uid ,
                    FollowedUserId = targetUserId
                });
                await _context.SaveChangesAsync();
                return true;
            }
            else
            {
                _context.FollowedUsers.Remove(existing);
                await _context.SaveChangesAsync();
                return false;
            }
        }
Exemple #2
0
        public async Task <ActionResult <bool> > BlockUser(BlockPostData data)
        {
            var uid = User.GetNumericId();

            if (!uid.HasValue)
            {
                return(Unauthorized());
            }

            var targetUserId = await _context.Users
                               .Where(u => u.NormalizedUserName == data.Name.Normalize().ToUpper())
                               .Select(u => u.Id)
                               .FirstOrDefaultAsync();

            var existing = await _context.BlacklistedUsers
                           .Where(bu => bu.BlockingUserId == targetUserId)
                           .Where(bu => bu.BlockedUserId == uid)
                           .FirstOrDefaultAsync();

            if (existing is null)
            {
                _context.BlacklistedUsers.Add(new UserBlock
                {
                    BlockingUserId = targetUserId,
                    BlockedUserId  = (long)uid
                });
                await _context.SaveChangesAsync();

                return(true);
            }
            else
            {
                _context.BlacklistedUsers.Remove(existing);
                await _context.SaveChangesAsync();

                return(false);
            }
        }