Skip to content

Commit

Permalink
fix(api): fix Internal Server Error when question already exists in d…
Browse files Browse the repository at this point in the history
…atabase (#443)

* feat(api): add filtering questions by content

* fix(app): fix Internal Server Error when question already exists in database

* refactor(api): remove filtering questions by content
  • Loading branch information
AdiPol1359 authored Dec 20, 2022
1 parent 6dfe163 commit a73ed8e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 31 deletions.
4 changes: 2 additions & 2 deletions apps/api/.env-example
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
ENV=dev
NODE_ENV=dev
ENV=development
NODE_ENV=development
PORT=3002

COOKIE_DOMAIN=devfaq.localhost
Expand Down
44 changes: 26 additions & 18 deletions apps/api/modules/questions/questions.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,26 +81,34 @@ const questionsPlugin: FastifyPluginAsync = async (fastify) => {
async handler(request, reply) {
const { question, level, category } = request.body;

const newQuestion = await fastify.db.question.create({
data: {
question,
levelId: level,
categoryId: category,
statusId: "pending",
},
});
try {
const newQuestion = await fastify.db.question.create({
data: {
question,
levelId: level,
categoryId: category,
statusId: "pending",
},
});

const data = {
id: newQuestion.id,
question: newQuestion.question,
_categoryId: newQuestion.categoryId,
_levelId: newQuestion.levelId,
_statusId: newQuestion.statusId,
acceptedAt: newQuestion.acceptedAt?.toISOString(),
votesCount: 0,
};
const data = {
id: newQuestion.id,
question: newQuestion.question,
_categoryId: newQuestion.categoryId,
_levelId: newQuestion.levelId,
_statusId: newQuestion.statusId,
acceptedAt: newQuestion.acceptedAt?.toISOString(),
votesCount: 0,
};

return { data };
return { data };
} catch (err) {
if (isPrismaError(err) && err.code === "P2002") {
throw fastify.httpErrors.conflict(`Question with content: ${question} already exists!`);
}

throw err;
}
},
});

Expand Down
26 changes: 15 additions & 11 deletions apps/api/modules/questions/questions.schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,21 @@ const generateGetQuestionsQuerySchema = <
levels: Levels;
statuses: Statuses;
}) =>
Type.Object({
category: Type.Optional(Type.Union(args.categories.map((val) => Type.Literal(val)))),
status: Type.Optional(Type.Union(args.statuses.map((val) => Type.Literal(val)))),
level: Type.Optional(Type.String({ pattern: `^([${args.levels.join("|")}],?)+$` })),
limit: Type.Optional(Type.Integer()),
offset: Type.Optional(Type.Integer()),
orderBy: Type.Optional(
Type.Union([Type.Literal("acceptedAt"), Type.Literal("level"), Type.Literal("votesCount")]),
),
order: Type.Optional(Type.Union([Type.Literal("asc"), Type.Literal("desc")])),
});
Type.Partial(
Type.Object({
category: Type.Union(args.categories.map((val) => Type.Literal(val))),
status: Type.Union(args.statuses.map((val) => Type.Literal(val))),
level: Type.String({ pattern: `^([${args.levels.join("|")}],?)+$` }),
limit: Type.Integer(),
offset: Type.Integer(),
orderBy: Type.Union([
Type.Literal("acceptedAt"),
Type.Literal("level"),
Type.Literal("votesCount"),
]),
order: Type.Union([Type.Literal("asc"), Type.Literal("desc")]),
}),
);
export type GetQuestionsQuery = Static<ReturnType<typeof generateGetQuestionsQuerySchema>>;
export type GetQuestionsOrderBy = GetQuestionsQuery["orderBy"];
export type GetQuestionsOrder = GetQuestionsQuery["order"];
Expand Down

0 comments on commit a73ed8e

Please # to comment.