Browse Source

basic unit update (#346)

pull/1/head
Alexey Velikiy 6 years ago committed by GitHub
parent
commit
55efce8195
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      server/src/models/analytic_unit_model.ts
  2. 25
      server/src/routes/analytic_units_router.ts

4
server/src/models/analytic_unit_model.ts

@ -64,6 +64,7 @@ export type FindManyQuery = {
error?: string error?: string
}; };
export class AnalyticUnit { export class AnalyticUnit {
constructor( constructor(
public name: string, public name: string,
@ -74,8 +75,9 @@ export class AnalyticUnit {
public id?: AnalyticUnitId, public id?: AnalyticUnitId,
public lastDetectionTime?: number, public lastDetectionTime?: number,
public status?: AnalyticUnitStatus, public status?: AnalyticUnitStatus,
public error?: string, public error?: string
) { ) {
if(name === undefined) { if(name === undefined) {
throw new Error(`Missing field "name"`); throw new Error(`Missing field "name"`);
} }

25
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) { async function updateMetric(ctx: Router.IRouterContext) {
try { try {
const { analyticUnitId, metric, datasource } = ctx.request.body as { const { analyticUnitId, metric, datasource } = ctx.request.body as {
@ -192,5 +215,7 @@ router.get('/status', getStatus);
router.get('/types', getTypes); router.get('/types', getTypes);
router.patch('/metric', updateMetric); router.patch('/metric', updateMetric);
router.patch('/alert', updateAlert); router.patch('/alert', updateAlert);
router.post('/', createUnit); router.post('/', createUnit);
router.delete('/', deleteUnit); router.delete('/', deleteUnit);
router.patch('/', updateUnit);

Loading…
Cancel
Save