|
|
@ -13,6 +13,9 @@ async function getStatus(ctx: Router.IRouterContext) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
let analyticUnit = await AnalyticUnit.findById(analyticUnitId); |
|
|
|
let analyticUnit = await AnalyticUnit.findById(analyticUnitId); |
|
|
|
|
|
|
|
if(analyticUnit === null) { |
|
|
|
|
|
|
|
throw new Error(`Cannot find analytic unit with id ${analyticUnitId}`); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
ctx.response.body = { |
|
|
|
ctx.response.body = { |
|
|
|
status: analyticUnit.status |
|
|
|
status: analyticUnit.status |
|
|
@ -24,19 +27,24 @@ async function getStatus(ctx: Router.IRouterContext) { |
|
|
|
} catch(e) { |
|
|
|
} catch(e) { |
|
|
|
console.error(e); |
|
|
|
console.error(e); |
|
|
|
ctx.response.status = 404; |
|
|
|
ctx.response.status = 404; |
|
|
|
ctx.response.body = 'Can`t find anything'; |
|
|
|
ctx.response.body = { |
|
|
|
|
|
|
|
code: 404, |
|
|
|
|
|
|
|
message: `GET /analyticUnits/status error: ${e.message}` |
|
|
|
|
|
|
|
}; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async function getUnit(ctx: Router.IRouterContext) { |
|
|
|
async function getUnit(ctx: Router.IRouterContext) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
let analyticUnitId = ctx.request.query.id; |
|
|
|
let analyticUnitId = ctx.request.query.id; |
|
|
|
|
|
|
|
|
|
|
|
if(analyticUnitId === undefined) { |
|
|
|
if(analyticUnitId === undefined) { |
|
|
|
throw new Error('No id param in query'); |
|
|
|
throw new Error('No id param in query'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
let analyticUnit = await AnalyticUnit.findById(analyticUnitId); |
|
|
|
let analyticUnit = await AnalyticUnit.findById(analyticUnitId); |
|
|
|
|
|
|
|
if(analyticUnit === null) { |
|
|
|
|
|
|
|
throw new Error(`Cannot find analytic unit with id ${analyticUnitId}`); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
ctx.response.body = { |
|
|
|
ctx.response.body = { |
|
|
|
name: analyticUnit.name, |
|
|
|
name: analyticUnit.name, |
|
|
@ -46,9 +54,11 @@ async function getUnit(ctx: Router.IRouterContext) { |
|
|
|
|
|
|
|
|
|
|
|
} catch(e) { |
|
|
|
} catch(e) { |
|
|
|
console.error(e); |
|
|
|
console.error(e); |
|
|
|
// TODO: better send 404 when we know than isn`t found
|
|
|
|
ctx.response.status = 404; |
|
|
|
ctx.response.status = 500; |
|
|
|
ctx.response.body = { |
|
|
|
ctx.response.body = 'Can`t find anything'; |
|
|
|
code: 404, |
|
|
|
|
|
|
|
message: `GET /analyticUnits error: ${e.message}` |
|
|
|
|
|
|
|
}; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -60,7 +70,7 @@ async function createUnit(ctx: Router.IRouterContext) { |
|
|
|
ctx.response.status = 500; |
|
|
|
ctx.response.status = 500; |
|
|
|
ctx.response.body = { |
|
|
|
ctx.response.body = { |
|
|
|
code: 500, |
|
|
|
code: 500, |
|
|
|
message: `Creation error: ${e.message}` |
|
|
|
message: `POST /analyticUnits error: ${e.message}` |
|
|
|
}; |
|
|
|
}; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -68,16 +78,21 @@ async function createUnit(ctx: Router.IRouterContext) { |
|
|
|
|
|
|
|
|
|
|
|
async function deleteUnit(ctx: Router.IRouterContext) { |
|
|
|
async function deleteUnit(ctx: Router.IRouterContext) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
await AnalyticUnit.remove(ctx.request.query.id); |
|
|
|
const analyticUnitId = ctx.request.query.id; |
|
|
|
|
|
|
|
if(analyticUnitId === undefined) { |
|
|
|
|
|
|
|
throw new Error('Cannot delete undefined id'); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
await AnalyticUnit.remove(analyticUnitId); |
|
|
|
ctx.response.body = { |
|
|
|
ctx.response.body = { |
|
|
|
code: 200, |
|
|
|
code: 200, |
|
|
|
message: 'Success' |
|
|
|
message: 'Success' |
|
|
|
}; |
|
|
|
}; |
|
|
|
} catch(e) { |
|
|
|
} catch(e) { |
|
|
|
|
|
|
|
console.error(e); |
|
|
|
ctx.response.status = 500; |
|
|
|
ctx.response.status = 500; |
|
|
|
ctx.response.body = { |
|
|
|
ctx.response.body = { |
|
|
|
code: 500, |
|
|
|
code: 500, |
|
|
|
message: `Deletion error: ${e.message}` |
|
|
|
message: `DELETE /analyticUnits error: ${e.message}` |
|
|
|
}; |
|
|
|
}; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|