Skip to content

Commit 45d4130

Browse files
authored
Merge pull request #20 from v-ngu/refactor
Refactor code
2 parents 9d5f637 + c9a5c49 commit 45d4130

File tree

4 files changed

+72
-59
lines changed

4 files changed

+72
-59
lines changed

.changeset/gentle-poems-serve.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'backstage-plugin-bulletin-board-backend': patch
3+
---
4+
5+
Refactor router, DatabaseHandler and its unit test

plugins/bulletin-board-backend/src/service/DatabaseHandler.test.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { DatabaseHandler } from './DatabaseHandler';
22
import { TestDatabaseId, TestDatabases } from '@backstage/backend-test-utils';
33
import { Knex as KnexType } from 'knex';
4-
import { Request, Response } from 'express';
54

65
const body: any = {
76
id: "id1",
@@ -56,14 +55,6 @@ describe('DatabaseHandler', () => {
5655
updated_at: body.updated_at,
5756
});
5857

59-
let responseObject = {};
60-
const request = {};
61-
const response: Partial<Response> = {
62-
json: jest.fn().mockImplementation(result => {
63-
responseObject = result;
64-
})
65-
};
66-
6758
let data = {
6859
bulletin_id: "id1",
6960
bulletin_title: "T",
@@ -88,12 +79,9 @@ describe('DatabaseHandler', () => {
8879
}
8980
}
9081

91-
await dbHandler.getBulletins(request as Request, response as Response);
82+
const res = await dbHandler.getBulletins();
9283

93-
expect(responseObject).toEqual({
94-
status: 'ok',
95-
data: [expectedData()]
96-
});
84+
expect(res).toEqual([expectedData()]);
9785
},
9886
60000
9987
);
Lines changed: 27 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {PluginDatabaseManager, resolvePackagePath} from '@backstage/backend-common';
22
import { Knex } from 'knex';
3-
import { Request, Response } from 'express';
43

54
const migrationsDir = resolvePackagePath(
65
'backstage-plugin-bulletin-board-backend',
@@ -11,6 +10,15 @@ type Options = {
1110
database: PluginDatabaseManager;
1211
};
1312

13+
type Body = {
14+
id: string,
15+
title: string,
16+
description: string,
17+
url: string,
18+
tags: string,
19+
user: string
20+
}
21+
1422
export class DatabaseHandler {
1523
static async create(options: Options): Promise<DatabaseHandler> {
1624
const { database } = options;
@@ -31,20 +39,14 @@ export class DatabaseHandler {
3139
this.client = client;
3240
}
3341

34-
getBulletins = async (_request:Request, response:Response) => {
35-
const bulletins = await this.client
42+
getBulletins = async () => {
43+
return await this.client
3644
.select()
3745
.orderBy('updated_at','desc')
3846
.from('bulletins');
39-
if (bulletins?.length) {
40-
response.json({ status: 'ok', data: bulletins });
41-
} else {
42-
response.json({ status: 'ok', data: [] });
43-
}
4447
}
4548

46-
createBulletin = async (request:Request, response:Response) => {
47-
const body = request.body;
49+
createBulletin = async (body: Body) => {
4850
await this.client
4951
.insert({
5052
bulletin_id: body.id,
@@ -58,39 +60,25 @@ export class DatabaseHandler {
5860
updated_at: new Date().toISOString(),
5961
})
6062
.into('bulletins');
61-
response.json({ status: 'ok' });
6263
}
6364

64-
updateBulletin = async (request:Request, response:Response) => {
65-
const { id } = request.params;
66-
const body = request.body;
67-
const count = await this.client('bulletins')
68-
.where({bulletin_id: id})
69-
.update({
70-
bulletin_id: body.id,
71-
bulletin_title: body.title,
72-
bulletin_description: body.description,
73-
bulletin_url: body.url,
74-
bulletin_tags: body.tags.toString(),
75-
updated_by: body.user,
76-
updated_at: new Date().toISOString()
77-
})
78-
if (count) {
79-
response.json({ status: 'ok' });
80-
} else {
81-
response.status(404).json({ message: 'Record not found' });
82-
}
83-
}
65+
updateBulletin = async (id: string, body: Body) => {
66+
return await this.client('bulletins')
67+
.where({bulletin_id: id})
68+
.update({
69+
bulletin_id: body.id,
70+
bulletin_title: body.title,
71+
bulletin_description: body.description,
72+
bulletin_url: body.url,
73+
bulletin_tags: body.tags.toString(),
74+
updated_by: body.user,
75+
updated_at: new Date().toISOString()
76+
})
77+
};
8478

85-
deleteBulletin = async (request:Request, response:Response) => {
86-
const { id } = request.params;
87-
const count = await this.client('bulletins')
79+
deleteBulletin = async (id: string) => {
80+
return await this.client('bulletins')
8881
.where({bulletin_id: id})
8982
.del();
90-
if (count) {
91-
response.json({ status: 'ok' });
92-
} else {
93-
response.status(404).json({ message: 'Record not found' });
94-
}
9583
}
9684
}

plugins/bulletin-board-backend/src/service/router.ts

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Router from 'express-promise-router';
44
import { Logger } from 'winston';
55
import { Config } from '@backstage/config';
66
import { DatabaseHandler } from './DatabaseHandler';
7+
import { Request, Response } from 'express';
78

89
export interface RouterOptions {
910
logger: Logger;
@@ -22,13 +23,44 @@ export async function createRouter(
2223
const router = Router();
2324
router.use(express.json());
2425

25-
router.route('/bulletins')
26-
.get(dbHandler.getBulletins)
27-
.post(dbHandler.createBulletin);
26+
router.get('/bulletins', async (_request: Request, response: Response) => {
27+
const bulletins = await dbHandler.getBulletins();
2828

29-
router.route('/bulletins/:id')
30-
.patch(dbHandler.updateBulletin)
31-
.delete(dbHandler.deleteBulletin);
29+
if (bulletins?.length) {
30+
response.json({ status: 'ok', data: bulletins });
31+
} else {
32+
response.json({ status: 'ok', data: [] });
33+
}
34+
});
35+
36+
router.post('/bulletins', async (request: Request, response: Response) => {
37+
const body = request.body;
38+
await dbHandler.createBulletin(body);
39+
response.json({ status: 'ok' });
40+
});
41+
42+
router.patch('/bulletins/:id', async (request: Request, response: Response) => {
43+
const { id } = request.params;
44+
const body = request.body;
45+
const count = await dbHandler.updateBulletin(id, body);
46+
47+
if (count) {
48+
response.json({ status: 'ok' });
49+
} else {
50+
response.status(404).json({ message: 'Record not found' });
51+
}
52+
});
53+
54+
router.delete('/bulletins/:id', async (request: Request, response: Response) => {
55+
const { id } = request.params;
56+
const count = await dbHandler.deleteBulletin(id);
57+
58+
if (count) {
59+
response.json({ status: 'ok' });
60+
} else {
61+
response.status(404).json({ message: 'Record not found' });
62+
}
63+
});
3264

3365
router.use(errorHandler());
3466
return router;

0 commit comments

Comments
 (0)