public async Task <RiggedFixture> AddRiggedFixtureAsync(RiggedFixture fixture) { try { fixture.Structure = await _context.Structures.FirstAsync(s => s.Id == fixture.Structure.Id); } catch (InvalidOperationException) { throw new KeyNotFoundException("Structure ID not found"); } try { fixture.Fixture = await _context.Fixtures.Include(f => f.Modes).FirstAsync(f => f.Id == fixture.Fixture.Id); } catch (InvalidOperationException) { throw new KeyNotFoundException("Fixture ID not found"); } if (fixture.Mode != null) { fixture.Mode = await _context.FixtureModes.FirstAsync(fm => fm.Id == fixture.Mode.Id); } else if (fixture.Fixture.Modes.Count > 0) { fixture.Mode = fixture.Fixture.Modes[0]; } await _context.RiggedFixtures.AddAsync(fixture); await _context.SaveChangesAsync(); await _structureService.UpdateLastModifiedAsync(fixture.Structure); return(fixture); }
public async Task <JsonResponse <RiggedFixture> > AddFixture(RiggedFixture fixture) { if (!await _authUtils.hasAccess(new Structure { Id = fixture.Structure.Id }, Context.User.FindFirst(ClaimTypes.NameIdentifier).Value)) { throw new HubException("401: Unauthorised"); } try { fixture = await _rFixtureService.AddRiggedFixtureAsync(fixture); } catch (Exception) { return(new JsonResponse <RiggedFixture> { success = false }); } await Clients.Group(connectionDrawing[Context.ConnectionId]).AddFixture( (await _structureService.GetViewAsync(fixture.Structure)).Id, fixture ); return(new JsonResponse <RiggedFixture> { success = true, data = fixture }); }
public async Task AddRiggedFixture_NoMode() { RiggedFixture rFixture = new RiggedFixture { Id = "newRFixture", Name = "newRFixture", Fixture = testFixture, Structure = testDrawings[0].Views[0].Structures[0], Position = new Point { x = 1, y = 1 }, Angle = 0, Channel = 0, Address = 0, Universe = 0, Notes = "", Colour = "" }; await _fixture.RunWithDatabaseAsync( async context => { context.Drawings.AddRange(testDrawings); await context.SaveChangesAsync(); }, context => initService(context).AddRiggedFixtureAsync(rFixture), context => context.RiggedFixtures.Where(rf => rf.Id == rFixture.Id).ToList() .Should().HaveCount(1) .And.AllBeEquivalentTo( rFixture, options => options.Excluding(rf => rf.Mode) ) ); }
public async Task UpdatePropsAsync() { RiggedFixture updated = new RiggedFixture { Id = testDrawings[0].Views[0].Structures[0].Fixtures[0].Id, Name = "Updated Name", Angle = 20, Channel = 20, Address = 20, Universe = 20, Notes = "Updated Notes", Colour = "Updated Colour" }; await _fixture.RunWithDatabaseAsync( async context => { context.Drawings.AddRange(testDrawings); await context.SaveChangesAsync(); }, context => initService(context).UpdatePropsAsync(updated), context => context.RiggedFixtures.First(rf => rf.Id == updated.Id) .Should().BeEquivalentTo( updated, options => options.Excluding(rf => rf.Structure) .Excluding(rf => rf.Position) .Excluding(rf => rf.Mode) ) ); }
public async Task AddRiggedFixture_FixtureNotExists() { RiggedFixture rFixture = new RiggedFixture { Id = "newRFixture", Name = "newRFixture", Fixture = new Fixture { Id = "doesn't exist" }, Structure = testDrawings[0].Views[0].Structures[0], Position = new Point { x = 1, y = 1 }, Angle = 0, Channel = 0, Address = 0, Universe = 0, Mode = testFixture.Modes[0], Notes = "", Colour = "" }; await _fixture.RunWithDatabaseAsync( async context => { context.Drawings.AddRange(testDrawings); await context.SaveChangesAsync(); }, context => initService(context).AddRiggedFixtureAsync(rFixture), async (act, context) => await (act.Should().ThrowAsync <KeyNotFoundException>()).WithMessage("Fixture ID not found") ); }
public async Task <Structure> GetStructureAsync(RiggedFixture fixture) { try { fixture = await _context.RiggedFixtures.AsNoTracking() .Include(rf => rf.Structure) .FirstAsync(rf => rf.Id == fixture.Id); } catch (InvalidOperationException) { throw new KeyNotFoundException("RiggedFixture ID not found"); } return(fixture.Structure); }
public async Task <View> GetViewAsync(RiggedFixture fixture) { try { fixture = await _context.RiggedFixtures .Include(rf => rf.Structure) .ThenInclude(s => s.View) .FirstAsync(rf => rf.Id == fixture.Id); } catch (InvalidOperationException) { throw new KeyNotFoundException("RiggedFixture ID not found"); } return(fixture.Structure.View); }
public async Task UpdatePropsAsync_RFixtureNotExists() { RiggedFixture updated = new RiggedFixture { Id = "doesn't exist", Name = "Updated Name" }; await _fixture.RunWithDatabaseAsync( async context => { context.Drawings.AddRange(testDrawings); await context.SaveChangesAsync(); }, context => initService(context).UpdatePropsAsync(updated), async (act, context) => await act.Should().ThrowAsync <KeyNotFoundException>() ); }
public async Task <bool> UpdateFixturePosition(RiggedFixture fixture) { if (!await _authUtils.hasAccess(fixture, Context.User.FindFirst(ClaimTypes.NameIdentifier).Value)) { throw new HubException("401: Unauthorised"); } try { fixture = await _rFixtureService.UpdatePositionAsync(fixture); } catch (Exception) { return(false); } await Clients.Group(connectionDrawing[Context.ConnectionId]).UpdateFixturePosition(fixture); return(true); }
public async Task UpdatePropsAsync_StructureNotUpdated() { RiggedFixture updated = new RiggedFixture { Id = testDrawings[0].Views[0].Structures[0].Fixtures[0].Id, Structure = testDrawings[1].Views[0].Structures[0] }; await _fixture.RunWithDatabaseAsync( async context => { context.Drawings.AddRange(testDrawings); await context.SaveChangesAsync(); }, context => initService(context).UpdatePropsAsync(updated), context => context.RiggedFixtures.Include(rf => rf.Structure).First(rf => rf.Id == updated.Id).Structure.Id .Should().Be(testDrawings[0].Views[0].Structures[0].Id) ); }
public async Task UpdatePositionAsync() { RiggedFixture updated = new RiggedFixture { Id = testDrawings[0].Views[0].Structures[0].Fixtures[0].Id, Position = new Point { x = 2, y = 2 } }; await _fixture.RunWithDatabaseAsync( async context => { context.Drawings.AddRange(testDrawings); await context.SaveChangesAsync(); }, context => initService(context).UpdatePositionAsync(updated), context => context.RiggedFixtures.First(rf => rf.Id == updated.Id).Position .Should().Be(updated.Position) ); }
public async Task <RiggedFixture> UpdatePositionAsync(RiggedFixture fixture) { RiggedFixture existing; try { existing = await _context.RiggedFixtures .Include(rf => rf.Structure) .ThenInclude(s => s.View) .FirstAsync(f => f.Id == fixture.Id); } catch (InvalidOperationException) { throw new KeyNotFoundException("Fixture ID not found"); } existing.Position = fixture.Position; await _context.SaveChangesAsync(); await _structureService.UpdateLastModifiedAsync(existing.Structure); return(existing); }
public async Task <JsonResponse <dynamic> > UpdateObjectProperty(string type, string modifiedField, Structure structure, RiggedFixture fixture, Label label) { if (type == "structure") { if (!await _authUtils.hasAccess(structure, Context.User.FindFirst(ClaimTypes.NameIdentifier).Value)) { throw new HubException("401: Unauthorised"); } Structure current = await _structureService.GetStructureAsync(structure.Id); var prevValue = current[modifiedField]; if (structure.Type != null && structure.Type.Id != "") { structure.Type = await _structureService.GetStructureTypeAsync(structure.Type.Id); } Structure updated; try { updated = await _structureService.UpdatePropsAsync(structure); } catch (Exception) { return(new JsonResponse <dynamic> { success = false }); } await Clients.Group(connectionDrawing[Context.ConnectionId]).UpdateObjectProperty( type, modifiedField, (await _structureService.GetViewAsync(updated)).Id, updated, null, null ); return(new JsonResponse <dynamic> { success = true, data = prevValue }); } else if (type == "fixture") { if (!await _authUtils.hasAccess(fixture, Context.User.FindFirst(ClaimTypes.NameIdentifier).Value)) { throw new HubException("401: Unauthorised"); } RiggedFixture current = await _rFixtureService.GetRiggedFixtureAsync(fixture.Id); var prevValue = current[modifiedField]; if (fixture.Mode != null && fixture.Mode.Id != "") { fixture.Mode = await _fixtureService.GetFixtureModeAsync(fixture.Mode.Id); } RiggedFixture updated; try { updated = await _rFixtureService.UpdatePropsAsync(fixture); } catch (Exception) { return(new JsonResponse <dynamic> { success = false }); } await Clients.Group(connectionDrawing[Context.ConnectionId]).UpdateObjectProperty( type, modifiedField, (await _rFixtureService.GetViewAsync(updated)).Id, await _rFixtureService.GetStructureAsync(updated), updated, null ); return(new JsonResponse <dynamic> { success = true, data = prevValue }); } else if (type == "label") { if (!await _authUtils.hasAccess(label, Context.User.FindFirst(ClaimTypes.NameIdentifier).Value)) { throw new HubException("401: Unauthorised"); } Label current = await _labelService.GetLabelAsync(label.Id); var prevValue = current[modifiedField]; Label updated; try { updated = await _labelService.UpdatePropsAsync(label); } catch (Exception) { return(new JsonResponse <dynamic> { success = false }); } await Clients.Group(connectionDrawing[Context.ConnectionId]).UpdateObjectProperty( type, modifiedField, (await _labelService.GetViewAsync(updated)).Id, null, null, updated ); return(new JsonResponse <dynamic> { success = true, data = prevValue }); } else { return(new JsonResponse <dynamic> { success = false }); } }
public async Task <bool> hasAccess(RiggedFixture fixture, string userId) { Drawing drawing = await _rFixtureService.GetDrawingAsync(fixture); return(await _drawingService.IsOwnerAsync(drawing.Id, userId) || await _drawingService.IsSharedWithAsync(drawing.Id, userId)); }