From 60fd263a54c5f8cad9171dc6624d1e4d8021794a Mon Sep 17 00:00:00 2001 From: Antonio Musolino Date: Tue, 1 Aug 2023 11:40:07 +0200 Subject: [PATCH] refactor(types): shared common types --- .../src/database/DatabaseQetaStore.ts | 8 +- .../qeta-backend/src/database/QetaStore.ts | 68 +---------------- .../qeta-backend/src/service/router.test.ts | 4 +- .../src/service/routes/attachments.ts | 2 +- .../src/service/upload/filesystem.ts | 3 +- plugins/qeta-common/src/types.ts | 68 +++++++++++++++++ plugins/qeta/src/api/QetaClient.ts | 30 ++++---- plugins/qeta/src/api/types.ts | 76 ++----------------- 8 files changed, 102 insertions(+), 157 deletions(-) diff --git a/plugins/qeta-backend/src/database/DatabaseQetaStore.ts b/plugins/qeta-backend/src/database/DatabaseQetaStore.ts index 1bd4c1f5..dacf9c21 100644 --- a/plugins/qeta-backend/src/database/DatabaseQetaStore.ts +++ b/plugins/qeta-backend/src/database/DatabaseQetaStore.ts @@ -4,21 +4,21 @@ import { } from '@backstage/backend-common'; import { Knex } from 'knex'; import { - Answer, - Attachment, AttachmentParameters, MaybeAnswer, MaybeQuestion, QetaStore, - Question, Questions, QuestionsOptions, TagResponse, - Vote, } from './QetaStore'; import { Statistic, StatisticsRequestParameters, + Vote, + Question, + Answer, + Attachment } from '@drodil/backstage-plugin-qeta-common'; const migrationsDir = resolvePackagePath( diff --git a/plugins/qeta-backend/src/database/QetaStore.ts b/plugins/qeta-backend/src/database/QetaStore.ts index 2165af56..7d00d381 100644 --- a/plugins/qeta-backend/src/database/QetaStore.ts +++ b/plugins/qeta-backend/src/database/QetaStore.ts @@ -2,60 +2,7 @@ import { Statistic, StatisticsRequestParameters, } from '@drodil/backstage-plugin-qeta-common'; - -export interface Question { - id: number; - author: string; - title: string; - content: string; - created: Date; - updated?: Date; - updatedBy?: string; - score: number; - views: number; - answersCount: number; - correctAnswer: boolean; - favorite: boolean; - ownVote?: number; - tags?: string[]; - entities?: string[]; - answers?: Answer[]; - own?: boolean; - votes?: Vote[]; - trend?: number; - comments?: Comment[]; -} - -export interface Answer { - id: number; - questionId: number; - author: string; - content: string; - correct: boolean; - created: Date; - updated?: Date; - updatedBy?: string; - score: number; - ownVote?: number; - own?: boolean; - votes?: Vote[]; - comments?: Comment[]; -} - -export interface Vote { - author: string; - score: number; - timestamp: Date; -} - -export interface Comment { - author: string; - content: string; - created: Date; - own?: boolean; - updated?: Date; - updatedBy?: string; -} +import type { Question, Answer, Attachment } from '@drodil/backstage-plugin-qeta-common'; export type MaybeAnswer = Answer | null; export type MaybeQuestion = Question | null; @@ -96,18 +43,7 @@ export interface TagResponse { tag: string; questionsCount: number; } -export interface Attachment { - id: number; - uuid: string; - locationType: string; - locationUri: string; - path: string; - binaryImage: Buffer; - mimeType: string; - extension: string; - creator: string; - created: Date; -} + export interface AttachmentParameters { uuid: string; locationType: string; diff --git a/plugins/qeta-backend/src/service/router.test.ts b/plugins/qeta-backend/src/service/router.test.ts index 8f702b35..e35bbab9 100644 --- a/plugins/qeta-backend/src/service/router.test.ts +++ b/plugins/qeta-backend/src/service/router.test.ts @@ -19,7 +19,8 @@ import express from 'express'; import request from 'supertest'; import { createRouter } from './router'; -import { Answer, QetaStore, Question, Comment } from '../database/QetaStore'; +import { QetaStore} from '../database/QetaStore'; +import { Answer, Question, Comment } from '@drodil/backstage-plugin-qeta-common'; import { BackstageIdentityResponse, IdentityApi, @@ -91,6 +92,7 @@ const answer: Answer = { }; const comment: Comment = { + id: 23, author: 'user', content: 'content', created: new Date('2022-01-01T00:00:00Z'), diff --git a/plugins/qeta-backend/src/service/routes/attachments.ts b/plugins/qeta-backend/src/service/routes/attachments.ts index df457601..30ff2063 100644 --- a/plugins/qeta-backend/src/service/routes/attachments.ts +++ b/plugins/qeta-backend/src/service/routes/attachments.ts @@ -1,6 +1,6 @@ import { Router } from 'express'; import { RouterOptions } from '../router'; -import { Attachment } from '../../database/QetaStore'; +import { Attachment } from '@drodil/backstage-plugin-qeta-common'; import multiparty from 'multiparty'; import FilesystemStoreEngine from '../upload/filesystem'; import DatabaseStoreEngine from '../upload/database'; diff --git a/plugins/qeta-backend/src/service/upload/filesystem.ts b/plugins/qeta-backend/src/service/upload/filesystem.ts index 3a9a0abf..5b39864f 100644 --- a/plugins/qeta-backend/src/service/upload/filesystem.ts +++ b/plugins/qeta-backend/src/service/upload/filesystem.ts @@ -1,7 +1,8 @@ import * as fs from 'fs'; import { v4 as uuidv4 } from 'uuid'; import { Config } from '@backstage/config'; -import { Attachment, QetaStore } from '../../database/QetaStore'; +import { QetaStore } from '../../database/QetaStore'; +import { Attachment } from '@drodil/backstage-plugin-qeta-common'; import { File } from '../types'; type Options = { diff --git a/plugins/qeta-common/src/types.ts b/plugins/qeta-common/src/types.ts index b2394211..119575d3 100644 --- a/plugins/qeta-common/src/types.ts +++ b/plugins/qeta-common/src/types.ts @@ -19,3 +19,71 @@ export interface StatisticsRequestParameters { author?: string; options?: StatisticsOptions; } + +export interface Question { + id: number; + author: string; + title: string; + content: string; + created: Date; + updated?: Date; + updatedBy?: string; + score: number; + views: number; + answersCount: number; + correctAnswer: boolean; + favorite: boolean; + ownVote?: number; + tags?: string[]; + entities?: string[]; + answers?: Answer[]; + own?: boolean; + votes?: Vote[]; + trend?: number; + comments?: Comment[]; +} + +export interface Answer { + id: number; + questionId: number; + author: string; + content: string; + correct: boolean; + created: Date; + updated?: Date; + updatedBy?: string; + score: number; + ownVote?: number; + own?: boolean; + votes?: Vote[]; + comments?: Comment[]; +} + +export interface Vote { + author: string; + score: number; + timestamp: Date; +} + +export interface Comment { + id: number; + author: string; + content: string; + created: Date; + own?: boolean; + updated?: Date; + updatedBy?: string; +} + +export interface Attachment { + id: number; + uuid: string; + locationType: string; + locationUri: string; + path: string; + binaryImage: Buffer; + mimeType: string; + extension: string; + creator: string; + created: Date; +} diff --git a/plugins/qeta/src/api/QetaClient.ts b/plugins/qeta/src/api/QetaClient.ts index bc26470f..750c960c 100644 --- a/plugins/qeta/src/api/QetaClient.ts +++ b/plugins/qeta/src/api/QetaClient.ts @@ -8,16 +8,16 @@ import { import { CustomErrorBase } from '@backstage/errors'; import { AnswerRequest, - AnswerResponse, AnswerResponseBody, AttachmentResponseBody, QuestionRequest, - QuestionResponse, QuestionResponseBody, QuestionsResponse, QuestionsResponseBody, TagResponse, } from './types'; + +import { Answer, Question } from '@drodil/backstage-plugin-qeta-common'; import omitBy from 'lodash/omitBy'; import isEmpty from 'lodash/isEmpty'; import { @@ -109,7 +109,7 @@ export class QetaClient implements QetaApi { return data; } - async postQuestion(question: QuestionRequest): Promise { + async postQuestion(question: QuestionRequest): Promise { const response = await this.fetchApi.fetch( `${await this.getBaseUrl()}/questions`, { @@ -130,7 +130,7 @@ export class QetaClient implements QetaApi { async commentQuestion( id: number, content: string, - ): Promise { + ): Promise { const response = await this.fetchApi.fetch( `${await this.getBaseUrl()}/questions/${id}/comments`, { @@ -151,7 +151,7 @@ export class QetaClient implements QetaApi { async deleteQuestionComment( questionId: number, id: number, - ): Promise { + ): Promise { const response = await this.fetchApi.fetch( `${await this.getBaseUrl()}/questions/${questionId}/comments/${id}`, { @@ -167,7 +167,7 @@ export class QetaClient implements QetaApi { return data; } - async getQuestion(id?: string): Promise { + async getQuestion(id?: string): Promise { if (!id) { throw new QetaError('Invalid id provided', undefined); } @@ -191,7 +191,7 @@ export class QetaClient implements QetaApi { return (await response.json()) as TagResponse[]; } - async voteQuestionUp(id: number): Promise { + async voteQuestionUp(id: number): Promise { if (!id) { throw new QetaError('Invalid id provided', undefined); } @@ -207,7 +207,7 @@ export class QetaClient implements QetaApi { return data; } - async voteQuestionDown(id: number): Promise { + async voteQuestionDown(id: number): Promise { if (!id) { throw new QetaError('Invalid id provided', undefined); } @@ -223,7 +223,7 @@ export class QetaClient implements QetaApi { return data; } - async favoriteQuestion(id: number): Promise { + async favoriteQuestion(id: number): Promise { if (!id) { throw new QetaError('Invalid id provided', undefined); } @@ -239,7 +239,7 @@ export class QetaClient implements QetaApi { return data; } - async unfavoriteQuestion(id: number): Promise { + async unfavoriteQuestion(id: number): Promise { if (!id) { throw new QetaError('Invalid id provided', undefined); } @@ -277,7 +277,7 @@ export class QetaClient implements QetaApi { questionId: number, id: number, content: string, - ): Promise { + ): Promise { const response = await this.fetchApi.fetch( `${await this.getBaseUrl()}/questions/${questionId}/answers/${id}/comments`, { @@ -299,7 +299,7 @@ export class QetaClient implements QetaApi { questionId: number, answerId: number, id: number, - ): Promise { + ): Promise { const response = await this.fetchApi.fetch( `${await this.getBaseUrl()}/questions/${questionId}/answers/${answerId}/comments/${id}`, { @@ -315,7 +315,7 @@ export class QetaClient implements QetaApi { return data; } - async voteAnswerUp(questionId: number, id: number): Promise { + async voteAnswerUp(questionId: number, id: number): Promise { if (!id || !questionId) { throw new QetaError('Invalid id provided', undefined); } @@ -334,7 +334,7 @@ export class QetaClient implements QetaApi { async voteAnswerDown( questionId: number, id: number, - ): Promise { + ): Promise { if (!id || !questionId) { throw new QetaError('Invalid id provided', undefined); } @@ -403,7 +403,7 @@ export class QetaClient implements QetaApi { async updateQuestion( id: string, question: QuestionRequest, - ): Promise { + ): Promise { const response = await this.fetchApi.fetch( `${await this.getBaseUrl()}/questions/${id}`, { diff --git a/plugins/qeta/src/api/types.ts b/plugins/qeta/src/api/types.ts index aa64d5ee..0a8daf73 100644 --- a/plugins/qeta/src/api/types.ts +++ b/plugins/qeta/src/api/types.ts @@ -1,4 +1,5 @@ import { ErrorObject } from 'ajv'; +import { Question, Answer, Attachment } from '@drodil/backstage-plugin-qeta-common' interface CustomError { message: string; @@ -10,45 +11,13 @@ interface ErrorResponse { } export interface QuestionsResponse { - questions: QuestionResponse[]; + questions: Question[]; total: number; } -export interface CommentResponse { - id: number; - content: string; - author: string; - created: Date; - own: boolean; - updated?: Date; - updatedBy?: string; -} - export type QuestionsResponseBody = QuestionsResponse | ErrorResponse; -export interface QuestionResponse { - id: number; - author: string; - title: string; - content: string; - tags?: string[]; - entities?: string[]; - created: Date; - views: number; - score: number; - answersCount: number; - correctAnswer: boolean; - favorite: boolean; - ownVote?: number; - updated?: string; - updatedBy?: string; - own?: boolean; - answers?: AnswerResponse[]; - votes?: VoteResponse[]; - comments: CommentResponse[]; -} - -export type QuestionResponseBody = QuestionResponse | ErrorResponse; +export type QuestionResponseBody = Question | ErrorResponse; export interface QuestionRequest { title: string; @@ -64,46 +33,15 @@ export interface AnswerRequest { images?: number[]; } -export interface AnswerResponse { - id: number; - author: string; - content: string; - questionId: number; - created: Date; - score: number; - updated?: Date; - correct: boolean; - updatedBy: string; - ownVote?: number; - own?: boolean; - votes?: VoteResponse[]; - comments?: CommentResponse[]; -} -export type AnswerResponseBody = AnswerResponse | ErrorResponse; - -export interface VoteResponse { - author: string; - score: number; - timestamp: Date; -} +export type AnswerResponseBody = Answer | ErrorResponse; export interface TagResponse { tag: string; questionsCount: number; } -export interface AttachmentResponse { - id: number; - uuid: string; - locationType: string; - locationUri: string; - path: string; - binaryImage: Buffer; - mimeType: string; - extension: string; - creator: string; - created: Date; -} +export type AttachmentResponseBody = Attachment | ErrorResponse; -export type AttachmentResponseBody = AttachmentResponse | ErrorResponse; +export type QuestionResponse = Question; +export type AnswerResponse = Answer;