Skip to content

Commit

Permalink
feat: added ability to answer
Browse files Browse the repository at this point in the history
  • Loading branch information
uptownhr committed Aug 26, 2023
1 parent a10947a commit 2394473
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 20 deletions.
13 changes: 6 additions & 7 deletions apps/backend/src/app/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,13 @@ export class AppController {
},
},
},
include: {
answers: true,
},
});

return {
...question,
answerCount: 0,
voteCount: 0,
};
}
Expand Down Expand Up @@ -120,6 +122,7 @@ export class AppController {
answers: {
select: {
id: true,
value: true,
},
},
},
Expand Down Expand Up @@ -157,10 +160,7 @@ export class AppController {
},
});

return {
...answer,
voteCount: 0,
};
return answer;
}

@ApiResponse({ type: Answer, status: 200, isArray: true })
Expand Down Expand Up @@ -206,7 +206,6 @@ export class AppController {
return {
...vote.question,
voteCount: vote.question.votes.length,
answerCount: vote.question.answers.length,
};
}

Expand All @@ -232,7 +231,7 @@ export class AppController {

return {
...vote.answer,
voteCount: vote.answer.votes.length,
// voteCount: vote.answer.votes.length,
};
}
}
7 changes: 2 additions & 5 deletions apps/backend/src/app/app.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ export class Question {
title: string;

@ApiProperty()
answerCount: number;
voteCount: number;

@ApiProperty()
voteCount: number;
answers: Answer[];
}

export class Page {
Expand Down Expand Up @@ -62,7 +62,4 @@ export class Answer {

@ApiProperty()
value: string;

@ApiProperty()
voteCount: number;
}
41 changes: 41 additions & 0 deletions apps/website/src/modules/qna/Question.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<script>
import { questionApi } from '@modules/qna/api';
export let question;
let form = {
answer: null,
};
async function addAnswer(questionId, a) {
const answer = await questionApi(`/answer`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
questionId,
value: a,
}),
});
question.answers = [...question.answers, answer];
form.answer = null;
}
</script>

<div>
{question.title} <br />
Votes: {question.voteCount} | Answers: {question.answers.length}
<ul>
{#each question.answers as answer}
<li>
{answer.value}
</li>
{/each}
</ul>
<form on:submit|preventDefault={() => addAnswer(question.id, form.answer)}>
<textarea bind:value={form.answer} />
<input type="submit" value="Add Answer" />
</form>
</div>
10 changes: 5 additions & 5 deletions apps/website/src/modules/qna/Topic.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script>
import { onMount } from 'svelte';
import { questionApi } from '@modules/qna/api';
import Question from '@modules/qna/Question.svelte';
const url = new URL(window.location.href);
const id = parseInt(url.searchParams.get('id'));
Expand All @@ -15,14 +16,14 @@
question: null,
};
async function addQuestion(x) {
async function addQuestion(q) {
const question = await questionApi(`/question`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
title: form.question,
title: q,
pageId: id,
}),
});
Expand All @@ -36,7 +37,7 @@
{#if topic}
<h1>{topic.title}</h1>
{/if}
<form on:submit|preventDefault={() => addQuestion('test')}>
<form on:submit|preventDefault={() => addQuestion(form.question)}>
<input
type="text"
bind:value={form.question}
Expand All @@ -49,8 +50,7 @@
<ul>
{#each topic.questions as question}
<li>
{question.title} <br />
Votes: {question.voteCount} | Answers: {question.answerCount}
<Question {question} />
</li>
{/each}
</ul>
Expand Down
18 changes: 15 additions & 3 deletions apps/website/src/modules/qna/Topics.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,30 @@
import { onMount } from 'svelte';
import { questionApi } from '@modules/qna/api';
let topics = []
let topics = [];
let form = {
answer: null,
};
onMount(async () => {
topics = await questionApi('/pages');
});
async function addAnswer() {}
</script>

<div>
<h2>Topics <a href="/tools/question-answer/new" style="float: right; font-size: 1rem">+ New Topic</a></h2>
<h2>
Topics <a
href="/tools/question-answer/new"
style="float: right; font-size: 1rem">+ New Topic</a
>
</h2>
<ul>
{#each topics as topic (topic.id)}
<li><a href="/tools/question-answer/topic?id={topic.id}">{topic.title}</a></li>
<li>
<a href="/tools/question-answer/topic?id={topic.id}">{topic.title}</a>
</li>
{/each}
</ul>
</div>

0 comments on commit 2394473

Please sign in to comment.