Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: wiki subject batch edit episodes api #942

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions routes/__snapshots__/index.test.ts.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions routes/private/routes/wiki/subject/ep.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe('edit subject ', () => {
expect(res.json()).toMatchInlineSnapshot(`
Object {
"date": "",
"disc": 0,
"duration": "",
"ep": 6,
"id": 8,
Expand Down
50 changes: 34 additions & 16 deletions routes/private/routes/wiki/subject/ep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const EpisodeWikiInfo = t.Object(
nameCN: t.String(),
type: res.Ref(res.EpisodeType),
ep: t.Number(),
disc: t.Optional(t.Number()),
duration: t.String({ examples: ['24:53', '24m52s'] }),
date: t.Optional(
t.String({
Expand Down Expand Up @@ -108,8 +109,9 @@ export async function setup(app: App) {
name: lo.unescape(ep.name),
nameCN: lo.unescape(ep.nameCN),
ep: ep.sort,
disc: ep.epDisc,
date: ep.date,
type: 0,
type: ep.type,
duration: ep.duration,
summary: ep.summary,
};
Expand Down Expand Up @@ -185,22 +187,11 @@ export async function setup(app: App) {
matchExpected(ep, expected);
}

validateDateDuration(body.date, body.duration);
if (body.date) {
if (!datePattern.test(body.date)) {
throw new BadRequestError(`${body.date} is not valid date`);
}

ep.date = body.date;
}

if (body.duration) {
const duration = parseDuration(body.duration);
if (Number.isNaN(duration)) {
throw new BadRequestError(
`${body.duration} is not valid duration, use string like 'hh:mm:dd' or '1h10m20s'`,
);
}

ep.duration = body.duration;
}

Expand All @@ -216,6 +207,18 @@ export async function setup(app: App) {
ep.summary = body.summary;
}

if (body.ep !== undefined) {
ep.sort = body.ep;
}

if (body.disc !== undefined) {
ep.epDisc = body.disc;
}

if (body.type !== undefined) {
ep.type = body.type;
}

const now = new Date();

await db.transaction(async (t) => {
Expand All @@ -229,9 +232,9 @@ export async function setup(app: App) {
ep_duration: ep.duration,
ep_name: ep.name,
ep_name_cn: ep.nameCN,
ep_sort: '0',
ep_disc: '0',
ep_type: '0',
ep_sort: ep.sort.toString(),
ep_disc: ep.epDisc.toString(),
ep_type: ep.type.toString(),
},
},
],
Expand All @@ -247,3 +250,18 @@ export async function setup(app: App) {
},
);
}

export function validateDateDuration(date: string | undefined, duration: string | undefined) {
if (date && !datePattern.test(date)) {
throw new BadRequestError(`${date} is not valid date`);
}

if (duration) {
const durationN = parseDuration(duration);
if (Number.isNaN(durationN)) {
throw new BadRequestError(
`${duration} is not valid duration, use string like 'hh:mm:dd' or '1h10m20s'`,
);
}
}
}
38 changes: 21 additions & 17 deletions routes/private/routes/wiki/subject/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { requireLogin } from '@app/routes/hooks/pre-handler.ts';
import type { App } from '@app/routes/type.ts';
import { getSubjectPlatforms } from '@app/vendor/index.ts';

import * as epRoutes from './ep.ts';
import * as imageRoutes from './image.ts';
import * as manageRoutes from './mgr.ts';

Expand Down Expand Up @@ -621,23 +622,26 @@ export async function setup(app: App) {

const now = new Date();
const discDefault = s.typeID === SubjectType.Music ? 1 : 0;
const newEpisodes = episodes.map((ep) => ({
subjectID: subjectID,
sort: ep.ep,
type: ep.type ?? 0,
disc: ep.disc ?? discDefault,
name: ep.name ?? '',
nameCN: ep.nameCN ?? '',
rate: 0,
duration: ep.duration ?? '',
airdate: ep.date ?? '',
online: '',
comment: 0,
resources: 0,
desc: ep.summary ?? '',
createdAt: now.getTime() / 1000,
updatedAt: now.getTime() / 1000,
}));
const newEpisodes = episodes.map((ep) => {
epRoutes.validateDateDuration(ep.date, ep.duration);
return {
subjectID: subjectID,
sort: ep.ep,
type: ep.type ?? 0,
disc: ep.disc ?? discDefault,
name: ep.name ?? '',
nameCN: ep.nameCN ?? '',
rate: 0,
duration: ep.duration ?? '',
airdate: ep.date ?? '',
online: '',
comment: 0,
resources: 0,
desc: ep.summary ?? '',
createdAt: now.getTime() / 1000,
updatedAt: now.getTime() / 1000,
};
});

const episodeIDs = await db.transaction(async (txn) => {
const [{ insertId: firstEpID }] = await txn.insert(schema.chiiEpisodes).values(newEpisodes);
Expand Down