Skip to content

Commit

Permalink
refactor(types): shared common types
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniomuso committed Aug 1, 2023
1 parent 8dfefc5 commit 60fd263
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 157 deletions.
8 changes: 4 additions & 4 deletions plugins/qeta-backend/src/database/DatabaseQetaStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
68 changes: 2 additions & 66 deletions plugins/qeta-backend/src/database/QetaStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion plugins/qeta-backend/src/service/router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -91,6 +92,7 @@ const answer: Answer = {
};

const comment: Comment = {
id: 23,
author: 'user',
content: 'content',
created: new Date('2022-01-01T00:00:00Z'),
Expand Down
2 changes: 1 addition & 1 deletion plugins/qeta-backend/src/service/routes/attachments.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
3 changes: 2 additions & 1 deletion plugins/qeta-backend/src/service/upload/filesystem.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down
68 changes: 68 additions & 0 deletions plugins/qeta-common/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
30 changes: 15 additions & 15 deletions plugins/qeta/src/api/QetaClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -109,7 +109,7 @@ export class QetaClient implements QetaApi {
return data;
}

async postQuestion(question: QuestionRequest): Promise<QuestionResponse> {
async postQuestion(question: QuestionRequest): Promise<Question> {
const response = await this.fetchApi.fetch(
`${await this.getBaseUrl()}/questions`,
{
Expand All @@ -130,7 +130,7 @@ export class QetaClient implements QetaApi {
async commentQuestion(
id: number,
content: string,
): Promise<QuestionResponse> {
): Promise<Question> {
const response = await this.fetchApi.fetch(
`${await this.getBaseUrl()}/questions/${id}/comments`,
{
Expand All @@ -151,7 +151,7 @@ export class QetaClient implements QetaApi {
async deleteQuestionComment(
questionId: number,
id: number,
): Promise<QuestionResponse> {
): Promise<Question> {
const response = await this.fetchApi.fetch(
`${await this.getBaseUrl()}/questions/${questionId}/comments/${id}`,
{
Expand All @@ -167,7 +167,7 @@ export class QetaClient implements QetaApi {
return data;
}

async getQuestion(id?: string): Promise<QuestionResponse> {
async getQuestion(id?: string): Promise<Question> {
if (!id) {
throw new QetaError('Invalid id provided', undefined);
}
Expand All @@ -191,7 +191,7 @@ export class QetaClient implements QetaApi {
return (await response.json()) as TagResponse[];
}

async voteQuestionUp(id: number): Promise<QuestionResponse> {
async voteQuestionUp(id: number): Promise<Question> {
if (!id) {
throw new QetaError('Invalid id provided', undefined);
}
Expand All @@ -207,7 +207,7 @@ export class QetaClient implements QetaApi {
return data;
}

async voteQuestionDown(id: number): Promise<QuestionResponse> {
async voteQuestionDown(id: number): Promise<Question> {
if (!id) {
throw new QetaError('Invalid id provided', undefined);
}
Expand All @@ -223,7 +223,7 @@ export class QetaClient implements QetaApi {
return data;
}

async favoriteQuestion(id: number): Promise<QuestionResponse> {
async favoriteQuestion(id: number): Promise<Question> {
if (!id) {
throw new QetaError('Invalid id provided', undefined);
}
Expand All @@ -239,7 +239,7 @@ export class QetaClient implements QetaApi {
return data;
}

async unfavoriteQuestion(id: number): Promise<QuestionResponse> {
async unfavoriteQuestion(id: number): Promise<Question> {
if (!id) {
throw new QetaError('Invalid id provided', undefined);
}
Expand Down Expand Up @@ -277,7 +277,7 @@ export class QetaClient implements QetaApi {
questionId: number,
id: number,
content: string,
): Promise<AnswerResponse> {
): Promise<Answer> {
const response = await this.fetchApi.fetch(
`${await this.getBaseUrl()}/questions/${questionId}/answers/${id}/comments`,
{
Expand All @@ -299,7 +299,7 @@ export class QetaClient implements QetaApi {
questionId: number,
answerId: number,
id: number,
): Promise<AnswerResponse> {
): Promise<Answer> {
const response = await this.fetchApi.fetch(
`${await this.getBaseUrl()}/questions/${questionId}/answers/${answerId}/comments/${id}`,
{
Expand All @@ -315,7 +315,7 @@ export class QetaClient implements QetaApi {
return data;
}

async voteAnswerUp(questionId: number, id: number): Promise<AnswerResponse> {
async voteAnswerUp(questionId: number, id: number): Promise<Answer> {
if (!id || !questionId) {
throw new QetaError('Invalid id provided', undefined);
}
Expand All @@ -334,7 +334,7 @@ export class QetaClient implements QetaApi {
async voteAnswerDown(
questionId: number,
id: number,
): Promise<AnswerResponse> {
): Promise<Answer> {
if (!id || !questionId) {
throw new QetaError('Invalid id provided', undefined);
}
Expand Down Expand Up @@ -403,7 +403,7 @@ export class QetaClient implements QetaApi {
async updateQuestion(
id: string,
question: QuestionRequest,
): Promise<QuestionResponse> {
): Promise<Question> {
const response = await this.fetchApi.fetch(
`${await this.getBaseUrl()}/questions/${id}`,
{
Expand Down
Loading

0 comments on commit 60fd263

Please sign in to comment.