Example #1
0
        public async Task <List <KeyVm> > GetUserPublicKeysAsync(long userId, long?time = 0, bool?direction = true)
        {
            using (MessengerDbContext context = contextFactory.Create())
            {
                IQueryable <Key> query = context.Keys
                                         .Where(opt => opt.UserId == userId);
                if (direction == false)
                {
                    query = query.Where(opt => opt.GenerationTimeSeconds > time)
                            .OrderBy(opt => opt.GenerationTimeSeconds)
                            .ThenByDescending(opt => opt.ExpirationTimeSeconds);
                }
                else
                {
                    if (time == 0)
                    {
                        time = DateTime.UtcNow.ToUnixTime();
                    }

                    query = query.Where(opt => opt.GenerationTimeSeconds < time)
                            .OrderByDescending(opt => opt.GenerationTimeSeconds)
                            .ThenByDescending(opt => opt.ExpirationTimeSeconds);
                }
                return(KeyConverter.GetKeysVm(await query.Take(30).ToListAsync().ConfigureAwait(false)));
            }
        }
Example #2
0
        public async Task <List <KeyVm> > AddNewUserKeysAsync(IEnumerable <KeyVm> keys, long userId)
        {
            try
            {
                using (MessengerDbContext context = contextFactory.Create())
                {
                    var publicKeys = KeyConverter.GetKeys(keys);
                    publicKeys.ForEach(key => key.UserId = userId);
                    await context.Keys.AddRangeAsync(publicKeys).ConfigureAwait(false);

                    await context.SaveChangesAsync().ConfigureAwait(false);

                    return(KeyConverter.GetKeysVm(publicKeys));
                }
            }
            catch (DbUpdateException ex)
            {
                if (ex.InnerException is PostgresException postgresException)
                {
                    if (postgresException.ConstraintName == "IX_Keys_KeyId_UserId")
                    {
                        throw new ObjectAlreadyExistsException();
                    }
                }
                throw new Exception("Error while saving key", ex);
            }
        }
Example #3
0
        public async Task <List <KeyVm> > GetUserPublicKeysAsync(long userId, IEnumerable <long> keysId)
        {
            using (MessengerDbContext context = contextFactory.Create())
            {
                var keysCondition = PredicateBuilder.New <Key>();
                keysCondition = keysId.Aggregate(keysCondition,
                                                 (current, value) => current.Or(opt => opt.KeyId == value && opt.UserId == userId).Expand());
                var publicKeys = await context.Keys.Where(keysCondition).ToListAsync().ConfigureAwait(false);

                return(KeyConverter.GetKeysVm(publicKeys));
            }
        }