Skip to content

Commit

Permalink
feat: api for episode colleciton (#836)
Browse files Browse the repository at this point in the history
  • Loading branch information
everpcpc authored Nov 30, 2024
1 parent 3a54426 commit 74925c8
Show file tree
Hide file tree
Showing 13 changed files with 428 additions and 9 deletions.
1 change: 1 addition & 0 deletions drizzle/orm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type ISubject = typeof schema.chiiSubjects.$inferSelect;
export type ISubjectFields = typeof schema.chiiSubjectFields.$inferSelect;
export type ISubjectInterest = typeof schema.chiiSubjectInterests.$inferSelect;
export type ISubjectRelation = typeof schema.chiiSubjectRelations.$inferSelect;
export type ISubjectEpStatus = typeof schema.chiiEpStatus.$inferSelect;

export type IEpisode = typeof schema.chiiEpisodes.$inferSelect;

Expand Down
13 changes: 7 additions & 6 deletions drizzle/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,16 +222,17 @@ export const chiiEpRevisions = mysqlTable(
export const chiiEpStatus = mysqlTable(
'chii_ep_status',
{
epSttId: mediumint('ep_stt_id').autoincrement().notNull(),
epSttUid: mediumint('ep_stt_uid').notNull(),
epSttSid: mediumint('ep_stt_sid').notNull(),
epSttOnPrg: tinyint('ep_stt_on_prg').default(0).notNull(),
epSttStatus: mediumtext('ep_stt_status').notNull(),
id: mediumint('ep_stt_id').autoincrement().notNull(),
uid: mediumint('ep_stt_uid').notNull(),
sid: mediumint('ep_stt_sid').notNull(),
// 未使用
// onProgress: tinyint('ep_stt_on_prg').default(0).notNull(),
status: mediumtext('ep_stt_status').notNull(),
updatedAt: int('ep_stt_lasttouch').notNull(),
},
(table) => {
return {
epSttUniq: unique('ep_stt_uniq').on(table.epSttUid, table.epSttSid),
epSttUniq: unique('ep_stt_uniq').on(table.uid, table.sid),
};
},
);
Expand Down
12 changes: 12 additions & 0 deletions lib/subject/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ export enum CollectionType {
export const CollectionTypeValues = new Set([1, 2, 3, 4, 5]);
export const CollectionTypeProfileValues = new Set([1, 2]);

export enum EpisodeCollectionStatus {
None = 0, // 撤消/删除
Wish = 1, // 想看
Done = 2, // 看过
Dropped = 3, // 抛弃
}

export interface UserEpisodeCollection {
id: number;
type: EpisodeCollectionStatus;
}

export enum PersonType {
Character = 'crt',
Person = 'prsn',
Expand Down
21 changes: 20 additions & 1 deletion lib/types/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as php from '@trim21/php-serialize';
import type * as orm from '@app/drizzle/orm.ts';
import type * as ormold from '@app/lib/orm/index.ts';
import { avatar, personImages, subjectCover } from '@app/lib/response.ts';
import { CollectionType } from '@app/lib/subject/type';
import { CollectionType, type UserEpisodeCollection } from '@app/lib/subject/type';
import type * as res from '@app/lib/types/res.ts';
import {
findSubjectPlatform,
Expand Down Expand Up @@ -168,6 +168,7 @@ function toSubjectRating(fields: orm.ISubjectFields): res.ISubjectRating {
const total = ratingCount.reduce((a, b) => a + b, 0);
const totalScore = ratingCount.reduce((a, b, i) => a + b * (i + 1), 0);
const rating = {
rank: fields.fieldRank,
total: total,
score: total === 0 ? 0 : Math.round((totalScore * 100) / total) / 100,
count: ratingCount,
Expand Down Expand Up @@ -259,6 +260,24 @@ export function toEpisode(episode: orm.IEpisode): res.IEpisode {
};
}

export function toSubjectEpStatus(
status: orm.ISubjectEpStatus,
): Record<number, UserEpisodeCollection> {
const result: Record<number, UserEpisodeCollection> = {};
if (!status.status) {
return result;
}
const epStatusList = php.parse(status.status) as Record<number, { eid: string; type: number }>;
for (const [eid, x] of Object.entries(epStatusList)) {
const episodeId = Number.parseInt(eid);
if (Number.isNaN(episodeId)) {
continue;
}
result[episodeId] = { id: episodeId, type: x.type };
}
return result;
}

export function toSlimCharacter(character: orm.ICharacter): res.ISlimCharacter {
return {
id: character.id,
Expand Down
18 changes: 18 additions & 0 deletions lib/types/fetcher.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { db, op } from '@app/drizzle/db.ts';
import * as schema from '@app/drizzle/schema';
import type { UserEpisodeCollection } from '@app/lib/subject/type.ts';

import * as convert from './convert.ts';
import type * as res from './res.ts';
Expand Down Expand Up @@ -83,6 +84,23 @@ export async function fetchSubjectsByIDs(
return map;
}

export async function fetchSubjectEpStatus(
userID: number,
subjectID: number,
): Promise<Record<number, UserEpisodeCollection> | null> {
const data = await db
.select()
.from(schema.chiiEpStatus)
.where(
op.and(op.eq(schema.chiiEpStatus.uid, userID), op.eq(schema.chiiEpStatus.sid, subjectID)),
)
.execute();
for (const d of data) {
return convert.toSubjectEpStatus(d);
}
return null;
}

export async function fetchSlimCharacterByID(
id: number,
allowNsfw = false,
Expand Down
1 change: 1 addition & 0 deletions lib/types/res.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ export const SubjectPlatform = t.Object(
export type ISubjectRating = Static<typeof SubjectRating>;
export const SubjectRating = t.Object(
{
rank: t.Integer(),
count: t.Array(t.Integer()),
score: t.Number(),
total: t.Integer(),
Expand Down
139 changes: 139 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/__snapshots__/subject.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/__snapshots__/topic.test.ts.snap

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

Loading

0 comments on commit 74925c8

Please sign in to comment.