Skip to content

Commit

Permalink
feat: add subject and relation api (#784)
Browse files Browse the repository at this point in the history
  • Loading branch information
everpcpc authored Oct 27, 2024
1 parent 613a4a5 commit 1761c13
Show file tree
Hide file tree
Showing 11 changed files with 683 additions and 252 deletions.
6 changes: 4 additions & 2 deletions drizzle/orm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import type * as schema from './schema.ts';
export type IUser = typeof schema.chiiUser.$inferSelect;
export type IUserFields = typeof schema.chiiUserFields.$inferSelect;

export type IFriends = typeof schema.chiiFriends.$inferSelect;
export type IFriend = typeof schema.chiiFriends.$inferSelect;

export type ISubject = typeof schema.chiiSubjects.$inferSelect;
export type ISubjectFields = typeof schema.chiiSubjectFields.$inferSelect;
export type ISubjectInterests = typeof schema.chiiSubjectInterests.$inferSelect;
export type ISubjectInterest = typeof schema.chiiSubjectInterests.$inferSelect;

export type ISubjectRelation = typeof schema.chiiSubjectRelations.$inferSelect;

export type ICharacter = typeof schema.chiiCharacters.$inferSelect;
export type IPerson = typeof schema.chiiPersons.$inferSelect;
Expand Down
32 changes: 12 additions & 20 deletions drizzle/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1029,31 +1029,23 @@ export const chiiSubjectRec = mysqlTable(
export const chiiSubjectRelations = mysqlTable(
'chii_subject_relations',
{
rltSubjectId: mediumint('rlt_subject_id').notNull(),
rltSubjectTypeId: tinyint('rlt_subject_type_id').notNull(),
rltRelationType: smallint('rlt_relation_type').notNull(),
rltRelatedSubjectId: mediumint('rlt_related_subject_id').notNull(),
rltRelatedSubjectTypeId: tinyint('rlt_related_subject_type_id').notNull(),
rltViceVersa: tinyint('rlt_vice_versa').notNull(),
rltOrder: tinyint('rlt_order').notNull(),
id: mediumint('rlt_subject_id').notNull(),
type: tinyint('rlt_subject_type_id').notNull(),
relation: smallint('rlt_relation_type').notNull(),
relatedID: mediumint('rlt_related_subject_id').notNull(),
relatedType: tinyint('rlt_related_subject_type_id').notNull(),
viceVersa: tinyint('rlt_vice_versa').notNull(),
order: tinyint('rlt_order').notNull(),
},
(table) => {
return {
rltRelatedSubjectTypeId: index('rlt_related_subject_type_id').on(
table.rltRelatedSubjectTypeId,
table.rltOrder,
),
rltSubjectTypeId: index('rlt_subject_type_id').on(table.rltSubjectTypeId),
rltRelationType: index('rlt_relation_type').on(
table.rltRelationType,
table.rltSubjectId,
table.rltRelatedSubjectId,
),
rltSubjectId: unique('rlt_subject_id').on(
table.rltSubjectId,
table.rltRelatedSubjectId,
table.rltViceVersa,
table.relatedID,
table.order,
),
rltSubjectTypeId: index('rlt_subject_type_id').on(table.type),
rltRelationType: index('rlt_relation_type').on(table.relatedType, table.id, table.relatedID),
rltSubjectId: unique('rlt_subject_id').on(table.id, table.relatedID, table.viceVersa),
};
},
);
Expand Down
2 changes: 1 addition & 1 deletion lib/types/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function toUser(user: orm.IUser): res.IUser {
};
}

export function toFriend(user: orm.IUser, friend: orm.IFriends): res.IFriend {
export function toFriend(user: orm.IUser, friend: orm.IFriend): res.IFriend {
return {
user: toUser(user),
grade: friend.grade,
Expand Down
13 changes: 11 additions & 2 deletions lib/types/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,21 @@ import * as schema from '@app/drizzle/schema';
import * as convert from './convert.ts';
import type * as res from './res.ts';

export async function fetchSubjectByID(id: number): Promise<res.ISubject | null> {
export async function fetchSubjectByID(
id: number,
allowNsfw = false,
): Promise<res.ISubject | null> {
const data = await db
.select()
.from(schema.chiiSubjects)
.innerJoin(schema.chiiSubjectFields, op.eq(schema.chiiSubjects.id, schema.chiiSubjectFields.id))
.where(op.and(op.eq(schema.chiiSubjects.id, id), op.eq(schema.chiiSubjects.ban, 0)))
.where(
op.and(
op.eq(schema.chiiSubjects.id, id),
op.eq(schema.chiiSubjects.ban, 0),
allowNsfw ? undefined : op.eq(schema.chiiSubjects.nsfw, false),
),
)
.execute();
for (const d of data) {
return convert.toSubject(d.subject, d.subject_field);
Expand Down
2 changes: 2 additions & 0 deletions routes/private/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { App } from '@app/routes/type.ts';
import * as collection from './routes/collection.ts';
import * as login from './routes/login.ts';
import * as post from './routes/post.ts';
import * as subject from './routes/subject.ts';
import * as group from './routes/topic.ts';
import * as user from './routes/user.ts';
import * as wiki from './routes/wiki/index.ts';
Expand Down Expand Up @@ -46,6 +47,7 @@ async function API(app: App) {
await app.register(login.setup);
await app.register(group.setup);
await app.register(post.setup);
await app.register(subject.setup);
await app.register(user.setup);
await app.register(wiki.setup, { prefix: '/wiki' });
}
Loading

0 comments on commit 1761c13

Please sign in to comment.