From 55efce819598157ad26dd628e3c4e8ce9df12de3 Mon Sep 17 00:00:00 2001 From: Alexey Velikiy Date: Sat, 19 Jan 2019 22:56:46 +0300 Subject: [PATCH] basic unit update (#346) --- server/src/models/analytic_unit_model.ts | 4 +++- server/src/routes/analytic_units_router.ts | 25 ++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/server/src/models/analytic_unit_model.ts b/server/src/models/analytic_unit_model.ts index 600a956..d8577df 100644 --- a/server/src/models/analytic_unit_model.ts +++ b/server/src/models/analytic_unit_model.ts @@ -64,6 +64,7 @@ export type FindManyQuery = { error?: string }; + export class AnalyticUnit { constructor( public name: string, @@ -74,8 +75,9 @@ export class AnalyticUnit { public id?: AnalyticUnitId, public lastDetectionTime?: number, public status?: AnalyticUnitStatus, - public error?: string, + public error?: string ) { + if(name === undefined) { throw new Error(`Missing field "name"`); } diff --git a/server/src/routes/analytic_units_router.ts b/server/src/routes/analytic_units_router.ts index b2f28fb..93500a5 100644 --- a/server/src/routes/analytic_units_router.ts +++ b/server/src/routes/analytic_units_router.ts @@ -105,6 +105,29 @@ async function createUnit(ctx: Router.IRouterContext) { } } +async function updateUnit(ctx: Router.IRouterContext) { + try { + const unit = ctx.request.body as AnalyticUnit.AnalyticUnit; + if(unit.id === undefined) { + throw new Error('Cannot update undefined id'); + } + + // TODO: we can't allow to update everything + AnalyticUnit.update(unit.id, unit); + + ctx.response.body = { + code: 200, + message: 'Success' + }; + } catch (e) { + ctx.response.status = 500; + ctx.response.body = { + code: 500, + message: `PATCH /analyticUnits error: ${e.message}` + }; + } +} + async function updateMetric(ctx: Router.IRouterContext) { try { const { analyticUnitId, metric, datasource } = ctx.request.body as { @@ -192,5 +215,7 @@ router.get('/status', getStatus); router.get('/types', getTypes); router.patch('/metric', updateMetric); router.patch('/alert', updateAlert); + router.post('/', createUnit); router.delete('/', deleteUnit); +router.patch('/', updateUnit);