public async Task UpdateJsonConfig( int configId, string data, CancellationToken token = default, string type = JsonType) { ConfigCache cache = await CacheQuery(configId, type).SingleAsync(token); cache.UpdateValue(data); await _objectConfigContext.SaveChangesAsync(token); }
public async Task <Config> Handle(UpdateConfigCommand request, CancellationToken cancellationToken) { await using IDbContextTransaction transaction = _configContext.Database.BeginTransaction(); try { (Config config, ConfigElement root, ConfigElement[] all)configSource = await _configService.GetConfigElement( () => _configService.GetConfig(request, EnvironmentRole.Editor, cancellationToken), cancellationToken); Config config = new Config(request.ConfigCode, configSource.config.Environment, request.VersionFrom); ObjectConfigReader reader = new ObjectConfigReader(config); ConfigElement configElement = await reader.Parse(request.Data); Compare(configSource.root, configElement); await _configContext.SaveChangesAsync(cancellationToken); await _cacheService.UpdateJsonConfig(configSource.config.ConfigId, request.Data, cancellationToken); await transaction.CommitAsync(cancellationToken); return(configSource.config); } catch (Exception ex) { await transaction.RollbackAsync(cancellationToken); _logger.LogError(ex, "{request} data:{Data}", request, request.Data); throw; } }
public async Task <User> CreateUser(User user) { Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry <User> trackUser = _configContext.Users.Add(user); await _configContext.SaveChangesAsync(); return(trackUser.Entity); }
public async Task <UsersEnvironments> Handle(CreateEnvironmentsCommand request, CancellationToken cancellationToken) { User user = await _securityService.GetCurrentUser(); var result = await(from app in _configContext.Applications.Where(w => w.Code.Equals(request.ApplicationCode)) join users in _configContext.UsersApplications.Where(w => w.UserId.Equals(user.UserId) && w.AccessRole == ApplicationRole.Administrator) on app.ApplicationId equals users.ApplicationId into userApp from appAccess in userApp.DefaultIfEmpty() select new { app, appAccess }).FirstOrDefaultAsync(cancellationToken); request.ThrowNotFoundExceptionWhenValueIsNull(result); request.ThrowForbidenExceptionWhenValueIsNull(result.appAccess); Environment environment = new Environment(request.Name, request.EnvironmentCode, request.Description, result.app); UsersEnvironments env = new UsersEnvironments(user, environment, EnvironmentRole.Editor); _configContext.UsersEnvironments.Add(env); await _configContext.SaveChangesAsync(cancellationToken); return(env); }
public async Task <UsersApplications> Handle(CreateCommand request, CancellationToken cancellationToken) { await _securityService.CheckAccess(UserRole.Administrator); Application application = new Application(request.Name, request.Code, request.Description); UsersApplications userApp = new UsersApplications(await _securityService.GetCurrentUser(), application, ApplicationRole.Administrator); application.Users.Add(userApp); _configContext.Applications.Add(application); await _configContext.SaveChangesAsync(cancellationToken); return(userApp); }
public async Task <UsersEnvironments> Handle(UpdateEnvironmentCommand request, CancellationToken cancellationToken) { Data.User user = await _securityService.GetCurrentUser(); Environment result = await(from app in _configContext.Applications.Where(w => w.Code.Equals(request.ApplicationCode)) join env in _configContext.Environments.Include(i => i.Users).Where(w => w.Code.Equals(request.EnvironmentCode)) on app.ApplicationId equals env.ApplicationId join appUser in _configContext.UsersApplications.Where(w => w.UserId.Equals(user.UserId) && w.AccessRole == ApplicationRole.Administrator) on app.ApplicationId equals appUser.ApplicationId select env) .FirstOrDefaultAsync(cancellationToken); request.ThrowNotFoundExceptionWhenValueIsNull(result); if (request.EnvironmentDefinition != null) { result.Rename(request.EnvironmentDefinition.Name); result.NewDefinition(request.EnvironmentDefinition.Description); } /*if (request.Users != null) * { * foreach (var changeUser in request.Users.Value.Where(w => w.UserId != userApplication?.UserId)) * { * var foundUser = application.Users.FirstOrDefault(f => f.UserId == changeUser.UserId); * if (foundUser != null) * { * switch (changeUser.Operation) * { * case EntityOperation.Create: * _configContext.UsersApplications.Add( * new UsersApplications(changeUser.UserId, application.ApplicationId, changeUser.Role)); * break; * case EntityOperation.Update: * foundUser.AccessRole = changeUser.Role; * _configContext.UsersApplications.Update(foundUser); * break; * case EntityOperation.Delete: * _configContext.UsersApplications.Remove(foundUser); * break; * } * } * * } * }*/ await _configContext.SaveChangesAsync(cancellationToken); return(result.Users.FirstOrDefault()); }
public async Task <Config> Handle(CreateConfigCommand request, CancellationToken cancellationToken) { Environment env = await _environmentService.GetEnvironment(request, EnvironmentRole.Editor, cancellationToken); Config existConfig = await _configService.GetConfig(env.EnvironmentId, request, cancellationToken); if (existConfig != null && existConfig.VersionFrom == request.VersionFrom) { throw new EntityException($"{request} is exists"); } Config config; if (existConfig != null && existConfig.VersionTo == null) { config = new Config(request.ConfigCode, env, request.VersionFrom); existConfig.SetVersionTo(request.From); } else if (existConfig != null && existConfig.VersionTo != null) { config = new Config(request.ConfigCode, env, request.VersionFrom, existConfig.VersionTo); existConfig.SetVersionTo(request.From); } else { long minVer = (await _configContext.Configs .Where(w => w.EnvironmentId.Equals(env.EnvironmentId) && w.Code == request.ConfigCode) .GroupBy(e => 1) .Select(s => s.Min(m => m.VersionFrom)).ToListAsync(cancellationToken))[0]; if (minVer != 0 && minVer < request.VersionFrom) { throw new OperationException( "Incorrect config latest version in storage, 'VersionTo' must be null. Contact your system administrator"); } config = new Config(request.ConfigCode, env, request.VersionFrom, minVer); } ObjectConfigReader reader = new ObjectConfigReader(config); ConfigElement configElemnt = await reader.Parse(request.Data); _configContext.ConfigCache.Add(new ConfigCache(config, request.Data)); await _configContext.SaveChangesAsync(cancellationToken); return(config); }
public async Task <UsersApplications> Handle(UpdateCommand request, CancellationToken cancellationToken) { Application application = await _applicationRepository.Find(request.ApplicationCode); request.ThrowNotFoundExceptionWhenValueIsNull(application); UsersApplications userApplication = await _securityService.CheckEntityAcces(application, ApplicationRole.Administrator); if (request.ApplicationDefinition != null) { application.Rename(request.ApplicationDefinition.Name); application.NewDefinition(request.ApplicationDefinition.Description); } else if (request.Users != null) { foreach (User changeUser in request.Users.Value.Where(w => w.UserId != userApplication?.UserId)) { UsersApplications foundUser = application.Users.FirstOrDefault(f => f.UserId == changeUser.UserId); if (foundUser != null) { switch (changeUser.Operation) { case EntityOperation.Create: _configContext.UsersApplications.Add( new UsersApplications(changeUser.UserId, application.ApplicationId, changeUser.Role)); break; case EntityOperation.Update: foundUser.AccessRole = changeUser.Role; _configContext.UsersApplications.Update(foundUser); break; case EntityOperation.Delete: _configContext.UsersApplications.Remove(foundUser); break; } } } } await _configContext.SaveChangesAsync(); return(await _mediator.Send(new FindByCodeCommand(application.Code), cancellationToken)); }