-
Notifications
You must be signed in to change notification settings - Fork 1
/
task.controller.ts
35 lines (29 loc) 路 1.09 KB
/
task.controller.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { Uuid } from '@dandi/common'
import { Inject } from '@dandi/core'
import { PathParam, RequestModel } from '@dandi/http-model'
import { Controller, HttpGet, HttpPatch } from '@dandi/mvc'
import { AccessorResourceId, ResourceAccessor } from '@dandi/mvc-hal'
import { Task, TaskResource } from './task'
import { TaskManager } from './task.manager'
@Controller('/task')
export class TaskController {
constructor(@Inject(TaskManager) private taskManager: TaskManager) {}
@HttpGet(':taskId')
@ResourceAccessor(TaskResource)
public async getTask(
@PathParam(Uuid)
@AccessorResourceId()
taskId: Uuid,
): Promise<TaskResource> {
return new TaskResource(await this.taskManager.getTask(taskId))
}
@HttpPatch(':taskId')
public async updateTask(@PathParam(Uuid) taskId, @RequestModel(Task) task): Promise<TaskResource> {
if (taskId !== task.taskId) {
throw new Error('taskId on path did not match taskId on model')
}
const existingTask = await this.taskManager.getTask(taskId)
Object.assign(existingTask, task)
return new TaskResource(existingTask)
}
}