Skip to content

Commit

Permalink
Merge pull request #85 from antoniomuso/refactor/shared-commons-types
Browse files Browse the repository at this point in the history
refactor: shared common types
  • Loading branch information
drodil authored Aug 1, 2023
2 parents 8dfefc5 + 0ba5d30 commit d6561ef
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 164 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
72 changes: 6 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,11 @@ 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 +47,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
8 changes: 7 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,12 @@ 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 +96,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;
}
36 changes: 15 additions & 21 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 @@ -127,10 +127,7 @@ export class QetaClient implements QetaApi {
return data;
}

async commentQuestion(
id: number,
content: string,
): Promise<QuestionResponse> {
async commentQuestion(id: number, content: string): Promise<Question> {
const response = await this.fetchApi.fetch(
`${await this.getBaseUrl()}/questions/${id}/comments`,
{
Expand All @@ -151,7 +148,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 +164,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 +188,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 +204,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 +220,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 +236,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 +274,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 +296,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 +312,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 @@ -331,10 +328,7 @@ export class QetaClient implements QetaApi {
return data;
}

async voteAnswerDown(
questionId: number,
id: number,
): Promise<AnswerResponse> {
async voteAnswerDown(questionId: number, id: number): Promise<Answer> {
if (!id || !questionId) {
throw new QetaError('Invalid id provided', undefined);
}
Expand Down Expand Up @@ -403,7 +397,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 d6561ef

Please sign in to comment.