diff --git a/src/api_productions.ts b/src/api_productions.ts index fd7222d..1fe81fe 100644 --- a/src/api_productions.ts +++ b/src/api_productions.ts @@ -98,7 +98,6 @@ async function handleAnswerRequest( ); } endpointDescription.audio.ssrcs = []; - //endpointDescription.video.streams = []; const parsedAnswer = parse(answer); const answerMediaDescription = parsedAnswer.media[0]; @@ -179,7 +178,7 @@ function getProduction(name: string): Production { const production: Production | undefined = productionManager.getProduction(name); if (!production) { - throw new Error('Trying to join production that does not exist'); + throw new Error('Trying to get production that does not exist'); } return production; } @@ -275,6 +274,56 @@ const apiProductions: FastifyPluginCallback = ( } ); + fastify.get<{ + Reply: Production[] | string; + }>( + '/productions', + { + schema: { + // description: 'Retrieves all Productions.', + response: { + 200: Type.Array(Production) + } + } + }, + async (request, reply) => { + try { + const productions: Production[] = productionManager.getProductions(); + console.log(productions); + reply.code(200).send(productions); + } catch (err) { + reply + .code(500) + .send('Exception thrown when trying to get productions: ' + err); + } + } + ); + + fastify.get<{ + Params: { name: string }; + Reply: Production | string; + }>( + '/productions/:name', + { + schema: { + // description: 'Retrieves a Production.', + response: { + 200: Production + } + } + }, + async (request, reply) => { + try { + const production: Production = getProduction(request.params.name); + reply.code(200).send(production); + } catch (err) { + reply + .code(500) + .send('Exception thrown when trying to get productions: ' + err); + } + } + ); + fastify.get<{ Params: { name: string }; Reply: Line[] | string; @@ -284,7 +333,7 @@ const apiProductions: FastifyPluginCallback = ( schema: { // description: 'Retrieves lines for a Production.', response: { - 200: Type.Object({ Line }) + 200: Type.Array(Line) } } }, @@ -301,22 +350,22 @@ const apiProductions: FastifyPluginCallback = ( ); fastify.get<{ - Params: { name: string; lineName: string }; + Params: { name: string; linename: string }; Reply: Line | string; }>( - '/productions/:name/lines/:lineName', + '/productions/:name/lines/:linename', { schema: { // description: 'Retrieves an active Production line.', response: { - 200: Type.Object({ Line }) + 200: Line } } }, async (request, reply) => { try { const production: Production = getProduction(request.params.name); - const line: Line = getLine(production.lines, request.params.lineName); + const line: Line = getLine(production.lines, request.params.linename); reply.code(200).send(line); } catch (err) { reply @@ -327,10 +376,10 @@ const apiProductions: FastifyPluginCallback = ( ); fastify.post<{ - Params: { name: string; lineName: string; username: string }; + Params: { name: string; linename: string; username: string }; Reply: { [key: string]: string | string[] } | string; }>( - '/productions/:name/lines/:lineName/:username', + '/productions/:name/lines/:linename/:username', { schema: { // description: 'Join a Production.', @@ -344,12 +393,18 @@ const apiProductions: FastifyPluginCallback = ( async (request, reply) => { try { const production: Production = getProduction(request.params.name); - const line: Line = getLine(production.lines, request.params.lineName); + const line: Line = getLine(production.lines, request.params.linename); const activeLines = await getActiveLines(smb, smbServerUrl); if (!activeLines.includes(line.id)) { const newLineId = await smb.allocateConference(smbServerUrl); - productionManager.setLineId(production.name, line.name, newLineId); + if ( + !productionManager.setLineId(production.name, line.name, newLineId) + ) { + throw new Error( + `Failed to set line id for line ${line.name} in production ${production.name}` + ); + } } const endpoint = await createEndpoint( @@ -392,10 +447,10 @@ const apiProductions: FastifyPluginCallback = ( ); fastify.patch<{ - Params: { name: string; lineName: string; username: string }; + Params: { name: string; linename: string; username: string }; Body: string; }>( - '/productions/:name/lines/:lineName/:username', + '/productions/:name/lines/:linename/:username', { schema: { //description: 'Join a Production line.', @@ -407,7 +462,7 @@ const apiProductions: FastifyPluginCallback = ( async (request, reply) => { try { const production: Production = getProduction(request.params.name); - const line: Line = getLine(production.lines, request.params.lineName); + const line: Line = getLine(production.lines, request.params.linename); const connectionEndpointDescription = line.connections[request.params.username]; @@ -433,6 +488,33 @@ const apiProductions: FastifyPluginCallback = ( } ); + fastify.delete<{ + Params: { name: string }; + Reply: string; + }>( + '/productions/:name', + { + schema: { + // description: 'Deletes a Production.', + response: { + 204: Type.String() + } + } + }, + async (request, reply) => { + try { + if (!productionManager.deleteProduction(request.params.name)) { + throw new Error('Could not delete production'); + } + reply.code(204).send(`Deleted ${request.params.name}`); + } catch (err) { + reply + .code(500) + .send('Exception thrown when trying to get line: ' + err); + } + } + ); + next(); }; diff --git a/src/production_manager.ts b/src/production_manager.ts index 959134d..8c5f96a 100644 --- a/src/production_manager.ts +++ b/src/production_manager.ts @@ -61,15 +61,37 @@ export class ProductionManager { } } - setLineId(productionName: string, lineName: string, lineId: string): void { - const matchedProduction = this.productions.find( + deleteProduction(productionName: string): string | undefined { + const matchedProductionIndex: number = this.productions.findIndex( (production) => production.name === productionName ); + if (matchedProductionIndex != -1) { + if (this.productions.splice(matchedProductionIndex, 1)) { + return productionName; + } else { + return undefined; + } + } else { + return undefined; + } + } + + setLineId( + productionName: string, + lineName: string, + lineId: string + ): Line | undefined { + const matchedProduction = this.getProduction(productionName); if (matchedProduction) { const line = this.getLine(matchedProduction.lines, lineName); if (line) { line.id = lineId; + return line; + } else { + return undefined; } + } else { + return undefined; } }