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); }