Skip to content

Commit

Permalink
feat: add question api
Browse files Browse the repository at this point in the history
  • Loading branch information
timepresent95 committed Aug 20, 2024
1 parent a5cf6fd commit aaa92b7
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 8 deletions.
94 changes: 86 additions & 8 deletions src/api/quetion.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,98 @@
import {kyInstance} from './ky';
import {questionApis} from './routes';

import {AlertToken} from '@/types';
import {Answer, Question} from '@/types/question';
import {createUrl} from '@/utils/url';

type ParamGetQuestion = {
familyId: string;
};

type ResponseGetQuestion = {
questionHistoryId: string;
question: string;
isAnswered: boolean;
};
} & Question;

async function getQuestion({familyId}: ParamGetQuestion) {
return (await kyInstance
.get(`question/${familyId}`)
.json()) as ResponseGetQuestion;
const apiUrl = createUrl(questionApis.getQuestion, {param: {familyId}});
return await kyInstance.get(apiUrl).json<ResponseGetQuestion>();
}

type BodyPostAnswer = {
questionHistoryId: number;
emoji: string | null;
answer: string;
};

async function postAnswer(body: BodyPostAnswer) {
return await kyInstance.post(questionApis.postAnswer, {json: body}).json();
}

type ParamGetAnswer = {
questionHistoryId: number;
};

type ResponseGetAnswer = {
answer: Answer[];
};

async function getAnswer({questionHistoryId}: ParamGetAnswer) {
const apiUrl = createUrl(questionApis.getAnswer, {
param: {questionHistoryId},
});
return await kyInstance.get(apiUrl).json<ResponseGetAnswer>();
}

export {getQuestion};
export type {ParamGetQuestion, ResponseGetQuestion};
type ParamPatchAnswer = {
answerId: string;
};

type BodyPatchAnswer = {
userId: string;
answer: string;
};

async function patchAnswer(
{answerId}: ParamPatchAnswer,
body: BodyPatchAnswer,
) {
const apiUrl = createUrl(questionApis.patchAnser, {param: {answerId}});
return await kyInstance.patch(apiUrl, {json: body}).json();
}

type ParamGetList = {
familyId: string;
};

type ResponseGetList = {
question: Question[];
};

async function getList({familyId}: ParamGetList) {
const apiUrl = createUrl(questionApis.getList, {param: {familyId}});
return await kyInstance.get(apiUrl).json<ResponseGetList>();
}

type BodyCheerUp = {
userId: string;
familyId: string;
alertToken: AlertToken;
};

async function cheerUp(body: BodyCheerUp) {
return await kyInstance.post(questionApis.cheerUp, {json: body}).json();
}

export {getQuestion, postAnswer, getAnswer, patchAnswer, getList, cheerUp};
export type {
ParamGetQuestion,
ResponseGetQuestion,
BodyPostAnswer,
ParamGetAnswer,
ResponseGetAnswer,
ParamPatchAnswer,
BodyPatchAnswer,
ParamGetList,
ResponseGetList,
BodyCheerUp,
};
14 changes: 14 additions & 0 deletions src/types/question.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {GoupRole} from './member';

export type Question = {
questionHistoryId: number;
question: string;
};

export type Answer = {
nickname: string;
groupRole: GoupRole;
emoji: string;
content: string;
createAt: Date;
};

0 comments on commit aaa92b7

Please sign in to comment.