Newer
Older
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import winston from 'winston'
import CrudService from '../../service/abstract/CrudService.mjs';
import EntityResource from './EntityResource.mjs'
export default class CrudEntityResource extends EntityResource {
/**
* Request handling for basic CRUD operations (create, retrieve, update, delete)
*
* @param {class} EntityClass
* @param {CrudService} [service]
*/
constructor(EntityClass, service) {
super();
this.EntityClass = EntityClass;
this.pathSegment = `/:${EntityClass.name}Id`;
this.service = service || new CrudService(EntityClass);
}
/**
*
* @param object
* @param id
* @returns {Promise<void>}
*/
async createEntity(object, id) {
winston.info(`Submitting entity ${id}: ${JSON.stringify(object)}`);
await this.service.create(new this.EntityClass(object), id);
}
/**
*
* @param id
* @returns {Promise<EntityClass>}
*/
async retrieveEntity(id) {
winston.info(`Retrieving entity ${id}`);
return this.service.retrieve(id);
}
/**
*
* @param object
* @param id
* @returns {Promise<void>}
*/
async updateEntity(object, id) {
winston.info(`Updating entity ${id}: ${JSON.stringify(object)}`);
await this.service.update(object, id);
}
/**
*
* @param id
* @returns {Promise<void>}
*/
async deleteEntity(id) {
winston.info(`Deleting entity ${id}`);
await this.service.delete(id);
}
}