-
Notifications
You must be signed in to change notification settings - Fork 2
/
product.controller.ts
62 lines (49 loc) · 1.71 KB
/
product.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
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
import * as yup from 'yup';
import { ChaincodeTx } from '@worldsibu/convector-platform-fabric';
import {
Controller,
ConvectorController,
Invokable,
Param
} from '@worldsibu/convector-core';
import { Marble, MarblePrivateDetails, MarbleTransientInput } from './product.model';
@Controller('product')
export class ProductController extends ConvectorController<ChaincodeTx> {
@Invokable()
public async initMarble(): Promise<any> {
const req = await this.tx.getTransientValue<MarbleTransientInput>('marble', MarbleTransientInput);
// Let's split the input data into 2 models in two different collections
const col1Data = new Marble({
id: req.name,
color: req.color,
name: req.name,
owner: req.owner,
size: req.size
});
await col1Data.save({ privateCollection: 'collectionMarbles' });
const col2Data = new MarblePrivateDetails({
id: req.name,
name: req.name,
price: req.price
});
await col2Data.save({ privateCollection: 'collectionMarblePrivateDetails' });
}
@Invokable()
public async readMarble(@Param(yup.string()) id: string): Promise<any> {
const marble = await Marble.getOne(id, Marble, { privateCollection: 'collectionMarbles' });
if (!marble.id) {
throw new Error(`No marble with id ${id}`);
}
return marble.toJSON();
}
@Invokable()
public async readMarblePrivateDetails(@Param(yup.string()) id: string): Promise<any> {
const marblePrivate = await MarblePrivateDetails.getOne(id, MarblePrivateDetails, {
privateCollection: 'collectionMarblePrivateDetails'
});
if (!marblePrivate.id) {
throw new Error(`No marble private data with id ${id}`);
}
return marblePrivate.toJSON();
}
}