Skip to content

Commit

Permalink
feat: add stock-shipment interfaces (#287)
Browse files Browse the repository at this point in the history
* feat: add stock-shipment interfaces

* fix: parse error without ; at the end
  • Loading branch information
orzyyyy authored Oct 29, 2019
1 parent 5fd3f02 commit 09ead4e
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 50 deletions.
24 changes: 10 additions & 14 deletions server/controller/ToyController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,23 @@ import { Controller, Request } from '../utils/decorator';
import ToyService from '../service/ToyService';
import { Context } from 'koa';

const getData = async (key: string, query: any) => {
const service = new ToyService();
const { result } = await service.getDataBySqlKey(key, query);
return result;
};

const common = async (ctx: Context) => {
const { key } = ctx.params;
const result = await getData(key, ctx.query);
return result;
};

@Controller('/toy')
export default class ToyController {
constructor(private service: ToyService) {
this.service = new ToyService();
}

@Request({ url: '/get/:key', method: 'get' })
async getDataByGet(ctx: Context) {
return common(ctx);
const { key } = ctx.params;
const { result } = await this.service.getDataBySqlKey(key, ctx.query);
return result;
}

@Request({ url: '/post/:key', method: 'post' })
async getDataByPost(ctx: Context) {
return common(ctx);
const { key } = ctx.params;
const { result } = await this.service.getDataBySqlKey(key, ctx.request.body);
return result;
}
}
6 changes: 6 additions & 0 deletions server/resource/sql/stock-shipment.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[goods-in]
sql = """
insert into `stock-shipment` (type, length, width, height, weight, patch, material_cost, freight, material_type)
values (@type, @length, @width, @height, @weight, @patch, @materialCost, @freight, @materialType)
"""
method = 'post'
80 changes: 45 additions & 35 deletions server/utils/__tests__/sql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ import { replacePlaceholderWithParams } from '../sql';

describe('sql', () => {
it('replacePlaceholderWithParams', () => {
expect(
replacePlaceholderWithParams(
'SELECT * FROM dictionary where value = 1;',
{},
),
).toBe('SELECT * FROM dictionary where value = 1;');
expect(replacePlaceholderWithParams('SELECT * FROM dictionary where value = 1;', {})).toBe(
'SELECT * FROM dictionary where value = 1;',
);

expect(
replacePlaceholderWithParams(
Expand All @@ -18,46 +15,39 @@ describe('sql', () => {
).toBe(`SELECT * FROM dictionary where
value = 1;`);

expect(
replacePlaceholderWithParams(
'SELECT * FROM dictionary where value = @value;',
{ value: 33 },
),
).toBe('SELECT * FROM dictionary where value = 33;');
expect(replacePlaceholderWithParams('SELECT * FROM dictionary where value = @value;', { value: 33 })).toBe(
'SELECT * FROM dictionary where value = 33;',
);

expect(
replacePlaceholderWithParams(
'SELECT * FROM dictionary where value = @value;',
{ value: 'test' },
),
).toBe('SELECT * FROM dictionary where value = \'test\';');
expect(replacePlaceholderWithParams('SELECT * FROM dictionary where value = @value;', { value: 'test' })).toBe(
// eslint-disable-next-line quotes
"SELECT * FROM dictionary where value = 'test';",
);

expect(
replacePlaceholderWithParams(
'SELECT * FROM dictionary where value = @value and id = @id;',
{ value: 33 },
),
replacePlaceholderWithParams('SELECT * FROM dictionary where value = @value and id = @id;', { value: 33 }),
).toBe('SELECT * FROM dictionary where value = 33 and id = @id;');

expect(
replacePlaceholderWithParams(
'SELECT * FROM dictionary where value = @value and id = @id;',
{ value: 33, id: '' },
),
).toBe('SELECT * FROM dictionary where value = 33 and id = \'\';');
replacePlaceholderWithParams('SELECT * FROM dictionary where value = @value and id = @id;', {
value: 33,
id: '',
}),
// eslint-disable-next-line quotes
).toBe("SELECT * FROM dictionary where value = 33 and id = '';");

expect(
replacePlaceholderWithParams(
'SELECT * FROM dictionary where value = @value and id = @id;',
{ value: 33, id: null },
),
replacePlaceholderWithParams('SELECT * FROM dictionary where value = @value and id = @id;', {
value: 33,
id: null,
}),
).toBe('SELECT * FROM dictionary where value = 33 and id = null;');

expect(
replacePlaceholderWithParams(
'SELECT * FROM dictionary where value = @value and id = @id;',
{ value: 33, id: undefined },
),
replacePlaceholderWithParams('SELECT * FROM dictionary where value = @value and id = @id;', {
value: 33,
id: undefined,
}),
).toBe('SELECT * FROM dictionary where value = 33 and id = @id;');

expect(
Expand All @@ -68,5 +58,25 @@ describe('sql', () => {
),
).toBe(`SELECT * FROM dictionary where value = 33
and id = '';`);

expect(
replacePlaceholderWithParams(
`insert into stock-shipment (type, length, width, height, weight, patch, material_cost, freight, material_type)
values (@type, @length, @width, @height, @weight, @patch, @materialCost, @freight, @materialType)`,
{
type: 0,
length: 100,
width: 101,
height: 102,
weight: 103,
patch: '201910291159',
materialCost: 104.1,
freight: 105.1,
materialType: 1,
},
),
)
.toBe(`insert into stock-shipment (type, length, width, height, weight, patch, material_cost, freight, material_type)
values (0, 100, 101, 102, 103, '201910291159', 104.1, 105.1, 1)`);
});
});
6 changes: 5 additions & 1 deletion server/utils/sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ export const replacePlaceholderWithParams = (sql: string, params: any) => {
for (const item of originVariables) {
const key = item
// remove all line break, \r and \n
.replace(/[\r\n]/g, '')
.replace(/[\r]/g, '')
.replace(/[\n]/g, '')
// remove all ()
/* eslint-disable-next-line no-useless-escape */
.replace(/[\()]/g, '')
// remove all space
/* eslint-disable-next-line no-useless-escape */
.replace(/\ +/g, '')
Expand Down

0 comments on commit 09ead4e

Please sign in to comment.