public async Task <List <TokenVm> > GetAllUsersTokensAsync(IEnumerable <long> usersId, bool requireDeviceToken = true) { var tokensCondition = PredicateBuilder.New <Token>(); if (requireDeviceToken) { tokensCondition = usersId.Aggregate(tokensCondition, (current, value) => current.Or(opt => opt.UserId == value && opt.DeviceTokenId != null && opt.AccessTokenExpirationTime > DateTime.UtcNow.ToUnixTime()).Expand()); } else { tokensCondition = usersId.Aggregate(tokensCondition, (current, value) => current.Or(opt => opt.UserId == value && opt.AccessTokenExpirationTime > DateTime.UtcNow.ToUnixTime()).Expand()); } using (MessengerDbContext context = contextFactory.Create()) { var tokens = await context.Tokens .AsNoTracking() .Where(tokensCondition) .ToListAsync() .ConfigureAwait(false); return(TokenConverter.GetTokensVm(tokens)); } }
public async Task <List <TokenVm> > RemoveTokensAsync(long userId, string accessToken, List <long> tokensIds) { using (MessengerDbContext context = contextFactory.Create()) { var query = context.Tokens.Where(token => token.UserId == userId); if (tokensIds != null && tokensIds.Any()) { var tokensCondition = PredicateBuilder.New <Token>(); tokensCondition = tokensIds.Aggregate(tokensCondition, (current, value) => current.Or(opt => opt.Id == value).Expand()); query = query.Where(tokensCondition); } else { query = query.Where(token => token.AccessToken == accessToken); } var tokens = await query.ToListAsync().ConfigureAwait(false); context.Tokens.RemoveRange(tokens); await context.SaveChangesAsync().ConfigureAwait(false); return(TokenConverter.GetTokensVm(tokens)); } }