From ac2b0d31f381bf25f64ecb154a67b3658b0a0023 Mon Sep 17 00:00:00 2001 From: uptown Date: Sun, 20 Aug 2023 15:03:55 -0700 Subject: [PATCH] feat: added questions to frontend --- .gitignore | 2 + apps/backend/src/app/app.controller.ts | 61 ++++++++++++++----- apps/backend/src/app/app.model.ts | 35 ++++++----- apps/website/src/modules/qna/NewTopic.svelte | 26 ++++++++ apps/website/src/modules/qna/Topic.svelte | 54 ++++++++++++++++ apps/website/src/modules/qna/Topics.svelte | 19 ++++++ apps/website/src/modules/qna/api.ts | 5 ++ apps/website/src/pages/tools.astro | 1 + .../pages/tools/question-answer/index.astro | 24 ++++++++ .../src/pages/tools/question-answer/new.astro | 23 +++++++ .../pages/tools/question-answer/topic.astro | 24 ++++++++ package.json | 1 + 12 files changed, 244 insertions(+), 31 deletions(-) create mode 100644 apps/website/src/modules/qna/NewTopic.svelte create mode 100644 apps/website/src/modules/qna/Topic.svelte create mode 100644 apps/website/src/modules/qna/Topics.svelte create mode 100644 apps/website/src/modules/qna/api.ts create mode 100644 apps/website/src/pages/tools/question-answer/index.astro create mode 100644 apps/website/src/pages/tools/question-answer/new.astro create mode 100644 apps/website/src/pages/tools/question-answer/topic.astro diff --git a/.gitignore b/.gitignore index b4781921..973dba00 100644 --- a/.gitignore +++ b/.gitignore @@ -29,3 +29,5 @@ pnpm-debug.log* !.yarn/releases !.yarn/sdks !.yarn/versions + +*.key* diff --git a/apps/backend/src/app/app.controller.ts b/apps/backend/src/app/app.controller.ts index 56ca389a..541413e7 100644 --- a/apps/backend/src/app/app.controller.ts +++ b/apps/backend/src/app/app.controller.ts @@ -2,6 +2,7 @@ import { Body, Controller, Get, + HttpException, Param, ParseIntPipe, Post, @@ -17,6 +18,7 @@ import { Answer, CreatePageInput, Page, + PageWithQuestions, Question, } from './app.model'; import { ApiResponse } from '@nestjs/swagger'; @@ -81,37 +83,64 @@ export class AppController { }; } - @ApiResponse({ type: Question, status: 200, isArray: true }) + @ApiResponse({ type: Page, status: 200 }) + @Get('page/:id') + async getPage(id: number): Promise { + const page = await this.lovDb.page.findUnique({ + where: { + id, + }, + }); + + if (!page) { + throw new HttpException('Page not found', 404); + } + + return page; + } + + @ApiResponse({ type: PageWithQuestions, status: 200 }) @Get('page/:id/questions') async getPageQuestions( @Param('id', ParseIntPipe) pageId: number - ): Promise { - const questions = await this.lovDb.question.findMany({ + ): Promise { + const page = await this.lovDb.page.findUnique({ where: { - pageId, + id: pageId, }, - select: { - id: true, - title: true, - votes: { - select: { - id: true, - }, - }, - answers: { - select: { - id: true, + include: { + questions: { + include: { + votes: { + select: { + id: true, + }, + }, + answers: { + select: { + id: true, + }, + }, }, }, }, }); - return questions.map((q) => ({ + if (!page) { + throw new HttpException('Page not found', 404); + } + + const mappedQuestions = page.questions.map((q) => ({ ...q, voteCount: q.votes.length, answerCount: q.answers.length, })); + + return { + ...page, + questions: mappedQuestions, + }; } @ApiResponse({ type: Answer, status: 201 }) diff --git a/apps/backend/src/app/app.model.ts b/apps/backend/src/app/app.model.ts index 34b70f50..44904e93 100644 --- a/apps/backend/src/app/app.model.ts +++ b/apps/backend/src/app/app.model.ts @@ -5,31 +5,28 @@ export class CreatePageInput { title: string; } -export class Page { - @ApiProperty() - id: number; - +export class AddQuestionInput { @ApiProperty() title: string; @ApiProperty() - slug: string; - + pageId: number; +} +export class Question { @ApiProperty() - createdAt: Date; + id: number; @ApiProperty() - updatedAt: Date; -} + title: string; -export class AddQuestionInput { @ApiProperty() - title: string; + answerCount: number; @ApiProperty() - pageId: number; + voteCount: number; } -export class Question { + +export class Page { @ApiProperty() id: number; @@ -37,10 +34,18 @@ export class Question { title: string; @ApiProperty() - answerCount: number; + slug: string; @ApiProperty() - voteCount: number; + createdAt: Date; + + @ApiProperty() + updatedAt: Date; +} + +export class PageWithQuestions extends Page { + @ApiProperty({ type: Question, isArray: true }) + questions: Question[]; } export class AddAnswerInput { diff --git a/apps/website/src/modules/qna/NewTopic.svelte b/apps/website/src/modules/qna/NewTopic.svelte new file mode 100644 index 00000000..f602d131 --- /dev/null +++ b/apps/website/src/modules/qna/NewTopic.svelte @@ -0,0 +1,26 @@ + + +
+ + +
diff --git a/apps/website/src/modules/qna/Topic.svelte b/apps/website/src/modules/qna/Topic.svelte new file mode 100644 index 00000000..67b31b3b --- /dev/null +++ b/apps/website/src/modules/qna/Topic.svelte @@ -0,0 +1,54 @@ + + +
+ {#if topic} +

{topic.title}

+ {/if} +
addQuestion('test')}> + + +
+ + {#if topic} +
    + {#each topic.questions as question} +
  • + {question.title}
    + Votes: {question.voteCount} | Answers: {question.answerCount} +
  • + {/each} +
+ {/if} +
diff --git a/apps/website/src/modules/qna/Topics.svelte b/apps/website/src/modules/qna/Topics.svelte new file mode 100644 index 00000000..54a92d38 --- /dev/null +++ b/apps/website/src/modules/qna/Topics.svelte @@ -0,0 +1,19 @@ + + +
+

Topics + New Topic

+
    + {#each topics as topic (topic.id)} +
  • {topic.title}
  • + {/each} +
+
diff --git a/apps/website/src/modules/qna/api.ts b/apps/website/src/modules/qna/api.ts new file mode 100644 index 00000000..cb3f8df3 --- /dev/null +++ b/apps/website/src/modules/qna/api.ts @@ -0,0 +1,5 @@ +const baseUrl = import.meta.env.PUBLIC_API_URL; + +export function questionApi(endPoint: string, options: any) { + return fetch(`${baseUrl}${endPoint}`, options).then((res) => res.json()); +} diff --git a/apps/website/src/pages/tools.astro b/apps/website/src/pages/tools.astro index bd2de6e4..50736236 100644 --- a/apps/website/src/pages/tools.astro +++ b/apps/website/src/pages/tools.astro @@ -21,6 +21,7 @@ import { SITE_TITLE, SITE_DESCRIPTION } from '../config';
diff --git a/apps/website/src/pages/tools/question-answer/index.astro b/apps/website/src/pages/tools/question-answer/index.astro new file mode 100644 index 00000000..19be5b9c --- /dev/null +++ b/apps/website/src/pages/tools/question-answer/index.astro @@ -0,0 +1,24 @@ +--- +import BaseHead from '../../../components/BaseHead.astro'; +import Header from '../../../components/Header.astro'; +import Footer from '../../../components/Footer.astro'; +import { SITE_TITLE, SITE_DESCRIPTION } from '../../../config'; +import Topics from "../../../modules/qna/Topics.svelte"; +const pageTitle = 'Question and Answers'; + +--- + + + + + + + +
+
+

🏁 {pageTitle}

+ +
+