mirror of
https://github.com/yangshun/tech-interview-handbook.git
synced 2025-07-30 05:34:33 +08:00
[questions][feat] sort answers, comments (#457)
Co-authored-by: Jeff Sieu <jeffsy00@gmail.com>
This commit is contained in:
@ -11,22 +11,18 @@ import { SortOrder, SortType } from '~/types/questions.d';
|
||||
export const questionsQuestionRouter = createRouter()
|
||||
.query('getQuestionsByFilter', {
|
||||
input: z.object({
|
||||
companyNames: z.string().array(),
|
||||
cursor: z
|
||||
.object({
|
||||
idCursor: z.string().optional(),
|
||||
lastSeenCursor: z.date().nullish().optional(),
|
||||
upvoteCursor: z.number().optional(),
|
||||
})
|
||||
.nullish(),
|
||||
cityIds: z.string().array(),
|
||||
companyIds: z.string().array(),
|
||||
countryIds: z.string().array(),
|
||||
cursor: z.string().nullish(),
|
||||
endDate: z.date().default(new Date()),
|
||||
limit: z.number().min(1).default(50),
|
||||
locations: z.string().array(),
|
||||
questionTypes: z.nativeEnum(QuestionsQuestionType).array(),
|
||||
roles: z.string().array(),
|
||||
sortOrder: z.nativeEnum(SortOrder),
|
||||
sortType: z.nativeEnum(SortType),
|
||||
startDate: z.date().optional(),
|
||||
stateIds: z.string().array(),
|
||||
}),
|
||||
async resolve({ ctx, input }) {
|
||||
const { cursor } = input;
|
||||
@ -51,12 +47,7 @@ export const questionsQuestionRouter = createRouter()
|
||||
];
|
||||
|
||||
const questionsData = await ctx.prisma.questionsQuestion.findMany({
|
||||
cursor:
|
||||
cursor !== undefined
|
||||
? {
|
||||
id: cursor ? cursor!.idCursor : undefined,
|
||||
}
|
||||
: undefined,
|
||||
cursor: cursor ? { id: cursor } : undefined,
|
||||
include: {
|
||||
_count: {
|
||||
select: {
|
||||
@ -66,10 +57,12 @@ export const questionsQuestionRouter = createRouter()
|
||||
},
|
||||
encounters: {
|
||||
select: {
|
||||
city: true,
|
||||
company: true,
|
||||
location: true,
|
||||
country: true,
|
||||
role: true,
|
||||
seenAt: true,
|
||||
state: true,
|
||||
},
|
||||
},
|
||||
user: {
|
||||
@ -95,19 +88,39 @@ export const questionsQuestionRouter = createRouter()
|
||||
gte: input.startDate,
|
||||
lte: input.endDate,
|
||||
},
|
||||
...(input.companyNames.length > 0
|
||||
...(input.companyIds.length > 0
|
||||
? {
|
||||
company: {
|
||||
name: {
|
||||
in: input.companyNames,
|
||||
id: {
|
||||
in: input.companyIds,
|
||||
},
|
||||
},
|
||||
}
|
||||
: {}),
|
||||
...(input.locations.length > 0
|
||||
...(input.cityIds.length > 0
|
||||
? {
|
||||
location: {
|
||||
in: input.locations,
|
||||
city: {
|
||||
id: {
|
||||
in: input.cityIds,
|
||||
},
|
||||
},
|
||||
}
|
||||
: {}),
|
||||
...(input.countryIds.length > 0
|
||||
? {
|
||||
country: {
|
||||
id: {
|
||||
in: input.countryIds,
|
||||
},
|
||||
},
|
||||
}
|
||||
: {}),
|
||||
...(input.stateIds.length > 0
|
||||
? {
|
||||
state: {
|
||||
id: {
|
||||
in: input.stateIds,
|
||||
},
|
||||
},
|
||||
}
|
||||
: {}),
|
||||
@ -134,16 +147,8 @@ export const questionsQuestionRouter = createRouter()
|
||||
processedQuestionsData.pop();
|
||||
|
||||
const nextIdCursor: string | undefined = nextItem.id;
|
||||
const nextLastSeenCursor =
|
||||
input.sortType === SortType.NEW ? nextItem.lastSeenAt : undefined;
|
||||
const nextUpvoteCursor =
|
||||
input.sortType === SortType.TOP ? nextItem.upvotes : undefined;
|
||||
|
||||
nextCursor = {
|
||||
idCursor: nextIdCursor,
|
||||
lastSeenCursor: nextLastSeenCursor,
|
||||
upvoteCursor: nextUpvoteCursor,
|
||||
};
|
||||
nextCursor = nextIdCursor;
|
||||
}
|
||||
|
||||
return {
|
||||
@ -167,10 +172,12 @@ export const questionsQuestionRouter = createRouter()
|
||||
},
|
||||
encounters: {
|
||||
select: {
|
||||
city: true,
|
||||
company: true,
|
||||
location: true,
|
||||
country: true,
|
||||
role: true,
|
||||
seenAt: true,
|
||||
state: true,
|
||||
},
|
||||
},
|
||||
user: {
|
||||
@ -201,21 +208,23 @@ export const questionsQuestionRouter = createRouter()
|
||||
async resolve({ ctx, input }) {
|
||||
const escapeChars = /[()|&:*!]/g;
|
||||
|
||||
const query =
|
||||
input.content
|
||||
.replace(escapeChars, " ")
|
||||
.trim()
|
||||
.split(/\s+/)
|
||||
.join(" | ");
|
||||
const query = input.content
|
||||
.replace(escapeChars, ' ')
|
||||
.trim()
|
||||
.split(/\s+/)
|
||||
.join(' | ');
|
||||
|
||||
const relatedQuestionsId : Array<{id:string}> = await ctx.prisma.$queryRaw`
|
||||
const relatedQuestionsId: Array<{ id: string }> = await ctx.prisma
|
||||
.$queryRaw`
|
||||
SELECT id FROM "QuestionsQuestion"
|
||||
WHERE
|
||||
to_tsvector("content") @@ to_tsquery('english', ${query})
|
||||
ORDER BY ts_rank_cd(to_tsvector("content"), to_tsquery('english', ${query}), 4) DESC;
|
||||
`;
|
||||
|
||||
const relatedQuestionsIdArray = relatedQuestionsId.map(current => current.id);
|
||||
const relatedQuestionsIdArray = relatedQuestionsId.map(
|
||||
(current) => current.id,
|
||||
);
|
||||
|
||||
const relatedQuestionsData = await ctx.prisma.questionsQuestion.findMany({
|
||||
include: {
|
||||
@ -227,10 +236,12 @@ export const questionsQuestionRouter = createRouter()
|
||||
},
|
||||
encounters: {
|
||||
select: {
|
||||
city: true,
|
||||
company: true,
|
||||
location: true,
|
||||
country: true,
|
||||
role: true,
|
||||
seenAt: true,
|
||||
state: true,
|
||||
},
|
||||
},
|
||||
user: {
|
||||
@ -241,9 +252,9 @@ export const questionsQuestionRouter = createRouter()
|
||||
votes: true,
|
||||
},
|
||||
where: {
|
||||
id : {
|
||||
in : relatedQuestionsIdArray,
|
||||
}
|
||||
id: {
|
||||
in: relatedQuestionsIdArray,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
@ -252,5 +263,5 @@ export const questionsQuestionRouter = createRouter()
|
||||
);
|
||||
|
||||
return processedQuestionsData;
|
||||
}
|
||||
},
|
||||
});
|
||||
|
Reference in New Issue
Block a user