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: add api subject recs #851

Merged
merged 1 commit into from
Dec 2, 2024
Merged
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
1 change: 1 addition & 0 deletions drizzle/orm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export type ISubjectRelation = typeof schema.chiiSubjectRelations.$inferSelect;
export type ISubjectTopic = typeof schema.chiiSubjectTopics.$inferSelect;
export type ISubjectPost = typeof schema.chiiSubjectPosts.$inferSelect;
export type ISubjectEpStatus = typeof schema.chiiEpStatus.$inferSelect;
export type ISubjectRec = typeof schema.chiiSubjectRec.$inferSelect;

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

Expand Down
12 changes: 6 additions & 6 deletions drizzle/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1008,15 +1008,15 @@ export const chiiSubjectPosts = mysqlTable(
export const chiiSubjectRec = mysqlTable(
'chii_subject_rec',
{
subjectId: mediumint('subject_id').notNull(),
recSubjectId: mediumint('rec_subject_id').notNull(),
mioSim: float('mio_sim').notNull(),
mioCount: mediumint('mio_count').notNull(),
subjectID: mediumint('subject_id').notNull(),
recSubjectID: mediumint('rec_subject_id').notNull(),
sim: float('mio_sim').notNull(),
count: mediumint('mio_count').notNull(),
},
(table) => {
return {
subjectId: index('subject_id').on(table.subjectId),
mioCount: index('mio_count').on(table.mioCount),
subjectId: index('subject_id').on(table.subjectID),
mioCount: index('mio_count').on(table.count),
};
},
);
Expand Down
10 changes: 10 additions & 0 deletions lib/types/res.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,16 @@ export const SubjectStaff = t.Object(
{ $id: 'SubjectStaff' },
);

export type ISubjectRec = Static<typeof SubjectRec>;
export const SubjectRec = t.Object(
{
subject: t.Ref(SlimSubject),
sim: t.Number(),
count: t.Integer(),
},
{ $id: 'SubjectRec', title: 'SubjectRec' },
);

export type ICharacterRelation = Static<typeof CharacterRelation>;
export const CharacterRelation = t.Object(
{
Expand Down
70 changes: 70 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.

7 changes: 7 additions & 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.

11 changes: 11 additions & 0 deletions routes/private/routes/subject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ describe('subject', () => {
expect(res.json()).toMatchSnapshot();
});

test('should get subject recs', async () => {
const app = createTestServer();
await app.register(setup);
const res = await app.inject({
method: 'get',
url: '/subjects/12/recs',
query: { limit: '2', offset: '0' },
});
expect(res.json()).toMatchSnapshot();
});

test('should get subject comments', async () => {
const app = createTestServer();
await app.register(setup);
Expand Down
69 changes: 69 additions & 0 deletions routes/private/routes/subject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@
};
}

function toSubjectRec(subject: orm.ISubject, rec: orm.ISubjectRec): res.ISubjectRec {
return {
subject: convert.toSlimSubject(subject),
sim: rec.sim,
count: rec.count,
};
}

Check warning on line 65 in routes/private/routes/subject.ts

View check run for this annotation

Codecov / codecov/patch

routes/private/routes/subject.ts#L59-L65

Added lines #L59 - L65 were not covered by tests

// eslint-disable-next-line @typescript-eslint/require-await
export async function setup(app: App) {
app.get(
Expand Down Expand Up @@ -381,6 +389,67 @@
},
);

app.get(
'/subjects/:subjectID/recs',
{
schema: {
summary: '获取条目的推荐',
operationId: 'getSubjectRecs',
tags: [Tag.Subject],
security: [{ [Security.CookiesSession]: [], [Security.HTTPBearer]: [] }],
params: t.Object({
subjectID: t.Integer(),
}),
querystring: t.Object({
limit: t.Optional(
t.Integer({ default: 10, minimum: 1, maximum: 10, description: 'max 10' }),
),
offset: t.Optional(t.Integer({ default: 0, minimum: 0, description: 'min 0' })),
}),
response: {
200: res.Paged(t.Ref(res.SubjectRec)),
},
},
},
async ({ auth, params: { subjectID }, query: { limit = 10, offset = 0 } }) => {
const subject = await fetcher.fetchSlimSubjectByID(subjectID, auth.allowNsfw);
if (!subject) {
throw new NotFoundError(`subject ${subjectID}`);
}

Check warning on line 418 in routes/private/routes/subject.ts

View check run for this annotation

Codecov / codecov/patch

routes/private/routes/subject.ts#L417-L418

Added lines #L417 - L418 were not covered by tests
const condition = op.and(
op.eq(schema.chiiSubjectRec.subjectID, subjectID),
op.ne(schema.chiiSubjects.ban, 1),
auth.allowNsfw ? undefined : op.eq(schema.chiiSubjects.nsfw, false),
);
const [{ count = 0 } = {}] = await db
.select({ count: op.count() })
.from(schema.chiiSubjectRec)
.innerJoin(
schema.chiiSubjects,
op.eq(schema.chiiSubjectRec.recSubjectID, schema.chiiSubjects.id),
)
.where(condition)
.execute();
const data = await db
.select()
.from(schema.chiiSubjectRec)
.innerJoin(
schema.chiiSubjects,
op.eq(schema.chiiSubjectRec.recSubjectID, schema.chiiSubjects.id),
)
.where(condition)
.orderBy(op.asc(schema.chiiSubjectRec.count))
.limit(limit)
.offset(offset)
.execute();
const recs = data.map((d) => toSubjectRec(d.chii_subjects, d.chii_subject_rec));
return {
data: recs,
total: count,
};
},
);

app.get(
'/subjects/:subjectID/comments',
{
Expand Down
1 change: 1 addition & 0 deletions routes/res.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export function addSchemas(app: App) {
app.addSchema(res.SubjectImages);
app.addSchema(res.SubjectPlatform);
app.addSchema(res.SubjectRating);
app.addSchema(res.SubjectRec);
app.addSchema(res.SubjectRelation);
app.addSchema(res.SubjectRelationType);
app.addSchema(res.SubjectStaff);
Expand Down
Loading