public async STT.Task <ViewModels.Scenario> UpdateAsync(Guid id, ViewModels.Scenario scenario, CancellationToken ct) { if (!(await _authorizationService.AuthorizeAsync(_user, null, new ContentDeveloperRequirement())).Succeeded) { throw new ForbiddenException(); } var scenarioToUpdate = await _context.Scenarios.SingleOrDefaultAsync(v => v.Id == id, ct); if (scenarioToUpdate == null) { throw new EntityNotFoundException <SAVM.Scenario>(); } scenario.DateCreated = scenarioToUpdate.DateCreated; scenario.CreatedBy = scenarioToUpdate.CreatedBy; scenario.DateModified = DateTime.UtcNow; scenario.ModifiedBy = _user.GetId(); _mapper.Map(scenario, scenarioToUpdate); _context.Scenarios.Update(scenarioToUpdate); await _context.SaveChangesAsync(ct); var updatedScenario = _mapper.Map(scenarioToUpdate, scenario); _engineHub.Clients.All.SendAsync(EngineMethods.ScenarioUpdated, updatedScenario); return(updatedScenario); }
public async STT.Task <ViewModels.Scenario> CreateAsync(ViewModels.Scenario scenario, CancellationToken ct) { if (!(await _authorizationService.AuthorizeAsync(_user, null, new ContentDeveloperRequirement())).Succeeded) { throw new ForbiddenException(); } scenario.DateCreated = DateTime.UtcNow; scenario.CreatedBy = _user.GetId(); var scenarioEntity = _mapper.Map <ScenarioEntity>(scenario); //TODO: add permissions // var ScenarioAdminPermission = await _context.Permissions // .Where(p => p.Key == PlayerClaimTypes.ScenarioAdmin.ToString()) // .FirstOrDefaultAsync(); // if (ScenarioAdminPermission == null) // throw new EntityNotFoundException<Permission>($"{PlayerClaimTypes.ScenarioAdmin.ToString()} Permission not found."); _context.Scenarios.Add(scenarioEntity); await _context.SaveChangesAsync(ct); scenario = await GetAsync(scenarioEntity.Id, ct); _engineHub.Clients.All.SendAsync(EngineMethods.ScenarioCreated, scenario); return(scenario); }