5 changed files with 149 additions and 8 deletions
@ -0,0 +1,67 @@
|
||||
import { AnalyticUnitId } from './analytic_unit_model'; |
||||
|
||||
import { Collection, makeDBQ } from '../services/data_service'; |
||||
|
||||
let db = makeDBQ(Collection.PANELS); |
||||
|
||||
|
||||
export type PanelId = string; |
||||
|
||||
export class Panel { |
||||
constructor( |
||||
public panelUrl: string, |
||||
public analyticUnits: AnalyticUnitId[], |
||||
public id?: PanelId |
||||
) { |
||||
if(this.panelUrl === undefined) { |
||||
throw new Error('panelUrl is undefined'); |
||||
} |
||||
} |
||||
|
||||
public toObject() { |
||||
return { |
||||
_id: this.id, |
||||
panelUrl: this.panelUrl, |
||||
analyticUnits: this.analyticUnits |
||||
}; |
||||
} |
||||
|
||||
static fromObject(obj: any): Panel { |
||||
if(obj === undefined) { |
||||
throw new Error('obj is undefined'); |
||||
} |
||||
return new Panel( |
||||
obj.panelUrl, |
||||
obj.analyticUnits, |
||||
obj._id |
||||
); |
||||
} |
||||
} |
||||
|
||||
export type FindOneQuery = { |
||||
panelUrl: string |
||||
} |
||||
|
||||
export async function findOne(query: FindOneQuery): Promise<Panel> { |
||||
let panel = await db.findOne(query); |
||||
if(panel === null) { |
||||
return null; |
||||
} |
||||
return Panel.fromObject(panel); |
||||
} |
||||
|
||||
export async function insertAnalyticUnit(panelUrl: string, analyticUnitId: AnalyticUnitId) { |
||||
const panel = await db.findOne({ panelUrl }); |
||||
|
||||
return db.updateOne({ panelUrl }, { |
||||
analyticUnits: panel.analyticUnits.concat(analyticUnitId) |
||||
}); |
||||
} |
||||
|
||||
export async function removeAnalyticUnit(panelUrl: string, analyticUnitId: AnalyticUnitId) { |
||||
const panel = await db.findOne({ panelUrl }); |
||||
|
||||
return db.updateOne({ panelUrl }, { |
||||
analyticUnits: panel.analyticUnits.filter(analyticUnit => analyticUnit !== analyticUnitId) |
||||
}); |
||||
} |
@ -0,0 +1,73 @@
|
||||
import { AnalyticUnitId } from '../models/analytic_unit_model'; |
||||
import * as Panel from '../models/panel_model'; |
||||
|
||||
import * as Router from 'koa-router'; |
||||
|
||||
|
||||
async function getAnalyticUnits(ctx: Router.IRouterContext) { |
||||
try { |
||||
let panelUrl: string = ctx.request.query.panelUrl; |
||||
if(panelUrl === undefined || panelUrl === '') { |
||||
throw new Error('panelUrl is missing'); |
||||
} |
||||
|
||||
const analyticUnits = await Panel.findOne({ panelUrl }); |
||||
|
||||
ctx.response.body = { analyticUnits }; |
||||
} catch(e) { |
||||
ctx.response.status = 500; |
||||
ctx.response.body = { |
||||
code: 500, |
||||
message: `GET /panel error: ${e.message}` |
||||
}; |
||||
} |
||||
} |
||||
|
||||
async function addAnalyticUnit(ctx: Router.IRouterContext) { |
||||
try { |
||||
let { panelUrl, analyticUnitId } = ctx.request.body as { |
||||
panelUrl: string, analyticUnitId: AnalyticUnitId |
||||
}; |
||||
|
||||
await Panel.insertAnalyticUnit(panelUrl, analyticUnitId); |
||||
|
||||
ctx.response.body = { |
||||
code: 200, |
||||
message: 'Success' |
||||
}; |
||||
} catch(e) { |
||||
ctx.response.status = 500; |
||||
ctx.response.body = { |
||||
code: 500, |
||||
message: `POST /panel error: ${e.message}` |
||||
}; |
||||
} |
||||
} |
||||
|
||||
async function deleteAnalyticUnit(ctx: Router.IRouterContext) { |
||||
try { |
||||
let { panelUrl, analyticUnitId } = ctx.request.body as { |
||||
panelUrl: string, analyticUnitId: AnalyticUnitId |
||||
}; |
||||
|
||||
// TODO: stop task when analytic unit is removed
|
||||
await Panel.removeAnalyticUnit(panelUrl, analyticUnitId); |
||||
|
||||
ctx.response.body = { |
||||
code: 200, |
||||
message: 'Success' |
||||
}; |
||||
} catch(e) { |
||||
ctx.response.status = 500; |
||||
ctx.response.body = { |
||||
code: 500, |
||||
message: `DELETE /panel error: ${e.message}` |
||||
}; |
||||
} |
||||
} |
||||
|
||||
export const router = new Router(); |
||||
|
||||
router.get('/', getAnalyticUnits); |
||||
router.post('/', addAnalyticUnit); |
||||
router.delete('/', deleteAnalyticUnit); |
Loading…
Reference in new issue