Esempio n. 1
0
        public async Task <bool> AddAsync(TokenDescriptor value, CancellationToken cancellationToken = default)
        {
            try
            {
                await using var dbContext = _dbContextFactory.CreateDbContext();

                var dbModel = _mapper.Map <TokenDbModel>(value);
                UpdateDefaultProperties(dbModel, dbContext);
                dbModel.CreatedTime = DateTimeOffset.UtcNow;
                dbModel.Creator     = dbContext.InstanceId;

                await dbContext.Tokens.AddAsync(dbModel, cancellationToken);

                return(await dbContext.SaveChangesAsync(cancellationToken) > 0);
            }
            catch (Exception ex)
            {
                throw new RepositoryException(I18n.AddOperationException, ex);
            }
        }
        public async Task EnsureValidationAsync(CancellationToken token)
        {
            using (var dbContext = _dbContextFactory.CreateDbContext())
            {
                try
                {
                    var versionInfo = await dbContext.Versions.OrderByDescending(x => x.Version)
                                      .FirstAsync(cancellationToken: token);

                    if (versionInfo.Version != _configuration.RequiredVersion)
                    {
                        throw new FunctionalityException($"Request database version {_configuration.RequiredVersion}, but current {versionInfo.Version}");
                    }
                }
                catch (Exception ex) when(!(ex is FunctionalityException))
                {
                    throw new FunctionalityException("Can't connect to database", ex);
                }
            }
        }