Skip to content

Commit

Permalink
Add PUT /api/custom-shows/:id API endpoint. Closes #485 (#488)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbenincasa authored Jun 7, 2024
1 parent ba5b758 commit cb10ba9
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 21 deletions.
32 changes: 32 additions & 0 deletions server/src/api/customShowsApi.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
CreateCustomShowRequestSchema,
IdPathParamSchema,
UpdateCustomShowRequestSchema,
} from '@tunarr/types/api';
import { CustomProgramSchema, CustomShowSchema } from '@tunarr/types/schemas';
import { isNull, map, sumBy } from 'lodash-es';
Expand Down Expand Up @@ -71,6 +72,37 @@ export const customShowsApiV2: RouterPluginAsyncCallback = async (fastify) => {
},
);

fastify.put(
'/custom-shows/:id',
{
schema: {
params: IdPathParamSchema,
body: UpdateCustomShowRequestSchema,
response: {
200: CustomShowSchema,
404: z.void(),
},
},
},
async (req, res) => {
const customShow = await req.serverCtx.customShowDB.saveShow(
req.params.id,
req.body,
);

if (isNull(customShow)) {
return res.status(404).send();
}

return res.status(200).send({
id: customShow.uuid,
name: customShow.name,
contentCount: customShow.content.length,
totalDuration: sumBy(customShow.content, (c) => c.duration),
});
},
);

fastify.get(
'/custom-shows/:id/programs',
{
Expand Down
65 changes: 57 additions & 8 deletions server/src/dao/customShowDb.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { CustomProgram } from '@tunarr/types';
import { CreateCustomShowRequest } from '@tunarr/types/api';
import { filter, isUndefined, map } from 'lodash-es';
import {
CreateCustomShowRequest,
UpdateCustomShowRequest,
} from '@tunarr/types/api';
import { filter, isNil, map } from 'lodash-es';
import { MarkOptional } from 'ts-essentials';
import { Maybe } from '../types/util.js';
import { mapAsyncSeq } from '../util/index.js';
import { isNonEmptyString, mapAsyncSeq } from '../util/index.js';
import { ProgramConverter } from './converters/programConverters.js';
import { getEm } from './dataSource.js';
import { CustomShow } from './entities/CustomShow.js';
Expand Down Expand Up @@ -61,12 +63,59 @@ export class CustomShowDB {
}));
}

async saveShow(id: Maybe<string>, customShow: CustomShowUpdate) {
if (isUndefined(id)) {
throw Error('Mising custom show id');
async saveShow(id: string, updateRequest: UpdateCustomShowRequest) {
const em = getEm();
const show = await this.getShow(id);

if (isNil(show)) {
return null;
}

return getEm().repo(CustomShow).upsert(customShow);
if (updateRequest.programs) {
const programIndexById = createPendingProgramIndexMap(
updateRequest.programs,
);

const persisted = filter(
updateRequest.programs,
(p) => p.persisted && isNonEmptyString(p.id),
);

const upsertedPrograms = await upsertContentPrograms(
updateRequest.programs,
);

const persistedCustomShowContent = map(persisted, (p) =>
em.create(CustomShowContent, {
customShow: show.uuid,
content: p.id!,
index: programIndexById[p.id!],
}),
);
const newCustomShowContent = map(upsertedPrograms, (p) =>
em.create(CustomShowContent, {
customShow: show.uuid,
content: p.uuid,
index: programIndexById[p.uniqueId()],
}),
);

await em.transactional(async (em) => {
await em.nativeDelete(CustomShowContent, { customShow: show.uuid });
await em.persistAndFlush([
...persistedCustomShowContent,
...newCustomShowContent,
]);
});
}

if (updateRequest.name) {
em.assign(show, { name: updateRequest.name });
}

await em.flush();

return await em.refresh(show);
}

async createShow(createRequest: CreateCustomShowRequest) {
Expand Down
19 changes: 6 additions & 13 deletions server/src/dao/fillerDb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
} from '@tunarr/types/api';
import { filter, isNil, isString, map } from 'lodash-es';
import { ChannelCache } from '../stream/ChannelCache.js';
import { mapAsyncSeq } from '../util/index.js';
import { isNonEmptyString, mapAsyncSeq } from '../util/index.js';
import { ProgramConverter } from './converters/programConverters.js';
import { getEm } from './dataSource.js';
import { Channel as ChannelEntity } from './entities/Channel.js';
Expand Down Expand Up @@ -44,7 +44,10 @@ export class FillerDB {
updateRequest.programs,
);

const persisted = filter(updateRequest.programs, (p) => p.persisted);
const persisted = filter(
updateRequest.programs,
(p) => p.persisted && isNonEmptyString(p.id),
);

const upsertedPrograms = await upsertContentPrograms(
updateRequest.programs,
Expand All @@ -70,25 +73,15 @@ export class FillerDB {
...persistedCustomShowContent,
...newCustomShowContent,
]);

console.log('programs update');
}

if (updateRequest.name) {
console.log('assigning filter');
em.assign(filler, { name: updateRequest.name });
em.persist(filler);
}

console.log('flushing filler', filler);

await em.flush();

return em.findOne(
FillerShow,
{ uuid: filler.uuid },
{ populate: ['*', 'content.uuid'] },
);
return await em.refresh(filler);
}

async createFiller(createRequest: CreateFillerListRequest): Promise<string> {
Expand Down
7 changes: 7 additions & 0 deletions types/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ export type CreateCustomShowRequest = z.infer<
typeof CreateCustomShowRequestSchema
>;

export const UpdateCustomShowRequestSchema =
CreateCustomShowRequestSchema.partial();

export type UpdateCustomShowRequest = z.infer<
typeof UpdateCustomShowRequestSchema
>;

export const CreateFillerListRequestSchema = z.object({
name: z.string(),
programs: z.array(
Expand Down
13 changes: 13 additions & 0 deletions web/src/external/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
CreateCustomShowRequestSchema,
CreateFillerListRequestSchema,
UpdateChannelProgrammingRequestSchema,
UpdateCustomShowRequestSchema,
UpdateFillerListRequestSchema,
VersionApiResponseSchema,
} from '@tunarr/types/api';
Expand Down Expand Up @@ -165,6 +166,18 @@ export const api = makeApi([
})
.build(),
},
{
method: 'put',
path: '/api/custom-shows/:id',
alias: 'updateCustomShow',
response: CustomShowSchema,
parameters: parametersBuilder()
.addPaths({
id: z.string(),
})
.addBody(UpdateCustomShowRequestSchema)
.build(),
},
{
method: 'delete',
path: '/api/custom-shows/:id',
Expand Down

0 comments on commit cb10ba9

Please sign in to comment.