Skip to content

Commit

Permalink
feat: get delete api and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
oshinongit committed Dec 5, 2023
1 parent d97b15b commit 299f830
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 12 deletions.
102 changes: 92 additions & 10 deletions src/api_productions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ async function handleAnswerRequest(
);
}
endpointDescription.audio.ssrcs = [];
//endpointDescription.video.streams = [];

const parsedAnswer = parse(answer);
const answerMediaDescription = parsedAnswer.media[0];
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -275,6 +274,56 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
}
);

fastify.get<{
Reply: Production[] | string;
}>(
'/productions',
{
schema: {
// description: 'Retrieves all Productions.',
response: {
200: Type.Array(Type.Any())
}
}
},
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: Production } | string;
}>(
'/productions/:name',
{
schema: {
// description: 'Retrieves a Production.',
response: {
200: Type.Object({ production: Production })
}
}
},
async (request, reply) => {
try {
const production: Production = getProduction(request.params.name);
reply.code(200).send({ production: production });
} catch (err) {
reply
.code(500)
.send('Exception thrown when trying to get productions: ' + err);
}
}
);

fastify.get<{
Params: { name: string };
Reply: Line[] | string;
Expand All @@ -284,7 +333,7 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
schema: {
// description: 'Retrieves lines for a Production.',
response: {
200: Type.Object({ Line })
200: Type.Array(Type.Any())
}
}
},
Expand All @@ -302,22 +351,22 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (

fastify.get<{
Params: { name: string; lineName: string };
Reply: Line | string;
Reply: { line: Line } | string;
}>(
'/productions/:name/lines/:lineName',
'/productions/:name/lines/:linename',
{
schema: {
// description: 'Retrieves an active Production line.',
response: {
200: Type.Object({ Line })
200: Type.Object({ line: Line })
}
}
},
async (request, reply) => {
try {
const production: Production = getProduction(request.params.name);
const line: Line = getLine(production.lines, request.params.lineName);
reply.code(200).send(line);
reply.code(200).send({ line: line });
} catch (err) {
reply
.code(500)
Expand All @@ -330,7 +379,7 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
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.',
Expand All @@ -349,7 +398,13 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
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(
Expand Down Expand Up @@ -395,7 +450,7 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
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.',
Expand Down Expand Up @@ -433,6 +488,33 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
}
);

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

Expand Down
26 changes: 24 additions & 2 deletions src/production_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down

0 comments on commit 299f830

Please sign in to comment.