mirror of
https://github.com/yangshun/tech-interview-handbook.git
synced 2025-07-15 02:33:50 +08:00
[questions][feat] add encounters sorting (#458)
Co-authored-by: Bryann Yeap Kok Keong <bryannyeapkk@gmail.com> Co-authored-by: Ai Ling <hong-ailing@hotmail.com> Co-authored-by: Zhang Ziqing <zhangziqing9926@gmail.com> Co-authored-by: Bryann Yeap Kok Keong <77266823+BryannYeap@users.noreply.github.com>
This commit is contained in:
@ -0,0 +1,5 @@
|
||||
-- AlterTable
|
||||
ALTER TABLE "QuestionsQuestion" ADD COLUMN "numEncounters" INTEGER NOT NULL DEFAULT 0;
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "QuestionsQuestion_numEncounters_id_idx" ON "QuestionsQuestion"("numEncounters", "id");
|
@ -438,14 +438,15 @@ enum QuestionsQuestionType {
|
||||
}
|
||||
|
||||
model QuestionsQuestion {
|
||||
id String @id @default(cuid())
|
||||
userId String?
|
||||
content String @db.Text
|
||||
questionType QuestionsQuestionType
|
||||
lastSeenAt DateTime?
|
||||
upvotes Int @default(0)
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
id String @id @default(cuid())
|
||||
userId String?
|
||||
content String @db.Text
|
||||
questionType QuestionsQuestionType
|
||||
lastSeenAt DateTime?
|
||||
upvotes Int @default(0)
|
||||
numEncounters Int @default(0)
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
user User? @relation(fields: [userId], references: [id], onDelete: SetNull)
|
||||
encounters QuestionsQuestionEncounter[]
|
||||
@ -455,6 +456,7 @@ model QuestionsQuestion {
|
||||
questionsListQuestionEntries QuestionsListQuestionEntry[]
|
||||
|
||||
@@index([lastSeenAt, id])
|
||||
@@index([numEncounters, id])
|
||||
@@index([upvotes, id])
|
||||
}
|
||||
|
||||
|
@ -41,19 +41,21 @@ export const questionsQuestionEncounterUserRouter = createProtectedRouter()
|
||||
});
|
||||
}
|
||||
|
||||
if (
|
||||
questionToUpdate.lastSeenAt === null ||
|
||||
questionToUpdate.lastSeenAt < input.seenAt
|
||||
) {
|
||||
await tx.questionsQuestion.update({
|
||||
data: {
|
||||
lastSeenAt: input.seenAt,
|
||||
|
||||
await tx.questionsQuestion.update({
|
||||
data: {
|
||||
lastSeenAt: (questionToUpdate.lastSeenAt === null ||
|
||||
questionToUpdate.lastSeenAt < input.seenAt)
|
||||
? input.seenAt : undefined,
|
||||
numEncounters: {
|
||||
increment: 1,
|
||||
},
|
||||
where: {
|
||||
id: input.questionId,
|
||||
},
|
||||
});
|
||||
}
|
||||
},
|
||||
where: {
|
||||
id: input.questionId,
|
||||
},
|
||||
});
|
||||
|
||||
return questionEncounterCreated;
|
||||
});
|
||||
},
|
||||
@ -160,6 +162,8 @@ export const questionsQuestionEncounterUserRouter = createProtectedRouter()
|
||||
}),
|
||||
]);
|
||||
|
||||
let lastSeenVal = undefined;
|
||||
|
||||
if (questionToUpdate!.lastSeenAt === questionEncounterToDelete.seenAt) {
|
||||
const latestEncounter =
|
||||
await ctx.prisma.questionsQuestionEncounter.findFirst({
|
||||
@ -171,17 +175,20 @@ export const questionsQuestionEncounterUserRouter = createProtectedRouter()
|
||||
},
|
||||
});
|
||||
|
||||
const lastSeenVal = latestEncounter ? latestEncounter!.seenAt : null;
|
||||
lastSeenVal = latestEncounter ? latestEncounter!.seenAt : null;
|
||||
}
|
||||
|
||||
await tx.questionsQuestion.update({
|
||||
await tx.questionsQuestion.update({
|
||||
data: {
|
||||
lastSeenAt: lastSeenVal,
|
||||
numEncounters: {
|
||||
increment: -1,
|
||||
},
|
||||
},
|
||||
where: {
|
||||
id: questionToUpdate!.id,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return questionEncounterDeleted;
|
||||
});
|
||||
|
@ -27,24 +27,40 @@ export const questionsQuestionRouter = createRouter()
|
||||
async resolve({ ctx, input }) {
|
||||
const { cursor } = input;
|
||||
|
||||
const sortCondition =
|
||||
input.sortType === SortType.TOP
|
||||
? [
|
||||
{
|
||||
upvotes: input.sortOrder,
|
||||
},
|
||||
{
|
||||
id: input.sortOrder,
|
||||
},
|
||||
]
|
||||
: [
|
||||
{
|
||||
lastSeenAt: input.sortOrder,
|
||||
},
|
||||
{
|
||||
id: input.sortOrder,
|
||||
},
|
||||
];
|
||||
let sortCondition = undefined;
|
||||
|
||||
switch (input.sortType) {
|
||||
case SortType.TOP:
|
||||
sortCondition = [
|
||||
{
|
||||
upvotes: input.sortOrder,
|
||||
},
|
||||
{
|
||||
id: input.sortOrder,
|
||||
},
|
||||
]
|
||||
break;
|
||||
case SortType.NEW:
|
||||
sortCondition = [
|
||||
{
|
||||
lastSeenAt: input.sortOrder,
|
||||
},
|
||||
{
|
||||
id: input.sortOrder,
|
||||
},
|
||||
];
|
||||
break;
|
||||
case SortType.ENCOUNTERS:
|
||||
sortCondition = [
|
||||
{
|
||||
numEncounters: input.sortOrder,
|
||||
},
|
||||
{
|
||||
id: input.sortOrder,
|
||||
},
|
||||
];
|
||||
break;
|
||||
}
|
||||
|
||||
const questionsData = await ctx.prisma.questionsQuestion.findMany({
|
||||
cursor: cursor ? { id: cursor } : undefined,
|
||||
|
1
apps/portal/src/types/questions.d.ts
vendored
1
apps/portal/src/types/questions.d.ts
vendored
@ -88,4 +88,5 @@ export enum SortOrder {
|
||||
export enum SortType {
|
||||
TOP,
|
||||
NEW,
|
||||
ENCOUNTERS,
|
||||
}
|
||||
|
Reference in New Issue
Block a user