public async Task <IActionResult> PutUser(int id, User user) { if (id != user.Id) { return(BadRequest()); } _context.Entry(user).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!UserExists(id)) { return(NotFound()); } throw; } return(NoContent()); }
public async Task <ActionResult <SavedSystemDto> > PostSystem(NewSystemDto systemDto) { var system = _mapper.Map <Domain.ConfigModels.SystemModels.System>(systemDto); _context.Systems.Add(system); await _context.SaveChangesAsync(); var savedSystem = _mapper.Map <SavedSystemDto>(await _context.Systems.FirstAsync(c => c.Id.Equals(system.Id))); return(CreatedAtAction("GetSystem", new { id = savedSystem.Id }, savedSystem)); }
public async Task IncrementPlots() { await RefreshDataBase(); var plots = await DbContext.Plots.ToListAsync(); foreach (var plot in plots) { plot.Minutes++; } await DbContext.SaveChangesAsync(); }
public async Task <IActionResult> Add([FromForm] AddWorkerModel model) { if (!ModelState.IsValid) { return(BadRequest()); } await _context.Workers.AddAsync(new WorkerConfiguration { WorkerName = model.Name, ApiKey = Guid.NewGuid().ToString() }); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); }
public async Task <ActionResult <SavedMicroServiceDto> > PutMicroservice(int id, UpdatedMicroserviceDto microserviceDto) { if (!(MicroserviceExists(id) && (id == microserviceDto.Id))) { return(BadRequest("microservice with this id does not exist")); } _context.Entry(_mapper.Map <Microservice>(microserviceDto)).State = EntityState.Modified; await _context.SaveChangesAsync(); return(_mapper.Map <SavedMicroServiceDto>( await _context.MicroServices .Include(y => y.System) .FirstAsync(c => c.Id.Equals(id)))); }
/// <summary> /// <inheritdoc /> /// </summary> public async Task SeedAsync() { var clients = GetClients(); foreach (var client in clients) { await _context.AddOrUpdateAsync(client.ToEntity(), x => x.ClientId == client.ClientId); } var apiScopes = GetApiScopes(); foreach (var apiScope in apiScopes) { await _context.AddOrUpdateAsync(apiScope.ToEntity(), x => x.Name == apiScope.Name); } var identityResources = GetIdentityResources(); foreach (var identityResource in identityResources) { await _context.AddOrUpdateAsync(identityResource.ToEntity(), x => x.Name == identityResource.Name); } var apiResources = GetApiResources(); foreach (var apiResource in apiResources) { await _context.AddOrUpdateAsync(apiResource.ToEntity(), x => x.Name == apiResource.Name); } await _context.SaveChangesAsync(); }
public async Task <IActionResult> PutConfig(int id, UpdateConfigDto configDto) { if (!(ConfigExists(id) && configDto.Id.Equals(id))) { return(BadRequest("this configuration doesn't exist in this manner")); } var config = await _context.Configs .Where(x => x.Id.Equals(id)) .Include(x => x.ConfigContent) .Include(x => x.Author) .FirstAsync(); if (config.Author.Id.ToString() != ControllerContext.HttpContext.User.Identity.Name) { return(BadRequest("you are not the owner of this configuration")); } if (!config.ConfigContent.Id.Equals(configDto.ConfigContent.Id)) { return(BadRequest("mismatching config content ids")); } _context.Configs.Attach(config); config.ConfigContent = _mapper.Map <ConfigContent>(configDto.ConfigContent); try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ConfigExists(id)) { return(NotFound()); } throw; } return(Ok(new { ConfigId = config.Id, ConfigContentId = config.ConfigContent.Id, UpdatedContent = config.ConfigContent.Content })); }
public static async Task School(ConfigurationContext configurationContext) { if (await configurationContext.Tenants.FindAsync(Tenants[0].Tag) == null) { foreach (Tenant tenant in Tenants) { await configurationContext.AddAsync(tenant); await configurationContext.SaveChangesAsync(); } } }
public async Task <IActionResult> DbConfig([FromQuery] string k, [FromQuery] string v) { if (!string.IsNullOrWhiteSpace(k) && !string.IsNullOrWhiteSpace(v)) { var configurationValue = await _configurationContext.ConfigurationValue.FirstOrDefaultAsync(a => a.Key == k); if (configurationValue != null) { configurationValue.Value = v; await _configurationContext.SaveChangesAsync(); } } var data = await _configurationContext.ConfigurationValue.ToListAsync(); return(Ok(data)); }
public async Task <DeviceConfig> SaveDeviceAsync(DeviceConfig model) { Device dbDevice = null; var transaction = await context.Database.BeginTransactionAsync(); if (string.IsNullOrEmpty(model.DeviceId)) { dbDevice = new Device { Name = model.Name, IsEnabled = model.IsEnabled }; await context.Device.AddAsync(dbDevice); } else { dbDevice = await context.Device.FirstOrDefaultAsync(d => d.DeviceId == Guid.Parse(model.DeviceId)); if (dbDevice == null) { throw new ApplicationException($"Device with id {model.DeviceId.ToString()} was not found."); } dbDevice.Name = model.Name; dbDevice.IsEnabled = model.IsEnabled; context.Device.Update(dbDevice); } try { await context.SaveChangesAsync(); } catch (System.Exception) { transaction.Rollback(); throw; } context.DeviceSensor.RemoveRange(context.DeviceSensor.Where(ds => !model.Sensors.Any(s => s.DeviceSensorId == ds.DeviceSensorId))); await context.SaveChangesAsync(); foreach (var sensor in model.Sensors) { DeviceSensor dbDeviceSensor = null; if (sensor.DeviceSensorId.HasValue) { dbDeviceSensor = await context.DeviceSensor.FirstOrDefaultAsync(ds => ds.DeviceSensorId == sensor.DeviceSensorId); dbDeviceSensor.IsEnabled = sensor.IsEnabled; context.DeviceSensor.Update(dbDeviceSensor); } else { dbDeviceSensor = new DeviceSensor(); dbDeviceSensor.DeviceId = dbDevice.DeviceId; dbDeviceSensor.SensorId = sensor.SensorId; dbDeviceSensor.IsEnabled = sensor.IsEnabled; await context.DeviceSensor.AddAsync(dbDeviceSensor); } } context.DeviceEventType.RemoveRange(context.DeviceEventType.Where(de => !model.Events.Any(e => e.DeviceEventTypeId == de.DeviceEventTypeId))); await context.SaveChangesAsync(); foreach (var eventType in model.Events) { DeviceEventType dbDeviceEventType = null; if (eventType.DeviceEventTypeId.HasValue) { dbDeviceEventType = await context.DeviceEventType.FirstOrDefaultAsync(de => de.DeviceEventTypeId == eventType.DeviceEventTypeId); dbDeviceEventType.IsEnabled = eventType.IsEnabled; context.DeviceEventType.Update(dbDeviceEventType); } else { dbDeviceEventType = new DeviceEventType(); dbDeviceEventType.DeviceId = dbDevice.DeviceId; dbDeviceEventType.EventTypeId = eventType.EventTypeId; dbDeviceEventType.IsEnabled = eventType.IsEnabled; await context.DeviceEventType.AddAsync(dbDeviceEventType); } } context.DeviceState.RemoveRange(context.DeviceState.Where(ds => !model.States.Any(e => e.DeviceStateId == ds.DeviceStateId))); await context.SaveChangesAsync(); foreach (var state in model.States) { DeviceState dbDeviceState = null; if (state.DeviceStateId.HasValue) { dbDeviceState = await context.DeviceState.FirstOrDefaultAsync(ds => ds.DeviceStateId == state.DeviceStateId); dbDeviceState.IsEnabled = state.IsEnabled; context.DeviceState.Update(dbDeviceState); } else { dbDeviceState = new DeviceState(); dbDeviceState.DeviceId = dbDevice.DeviceId; dbDeviceState.StateId = state.StateId; dbDeviceState.IsEnabled = state.IsEnabled; await context.DeviceState.AddAsync(dbDeviceState); } } try { await context.SaveChangesAsync(); } catch (Exception) { transaction.Rollback(); throw; } transaction.Commit(); return(await GetDeviceByIdAsync(dbDevice.DeviceId.ToString())); }