/
Order.js
107 lines (99 loc) · 2.71 KB
/
Order.js
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
const DatabaseAccessObject = require('../lib/DatabaseAccessObject')
const RequiredValidator = require('../lib/validators/RequiredValidator')
const NumericValidator = require('../lib/validators/NumericValidator')
const MySqlDatabaseConnection = require('../lib/databaseConnections/MySqlDatabaseConnection')
const RelationBelongsTo = require('../lib/relation/RelationBelongsTo')
const RelationHasOne = require('../lib/relation/RelationHasOne')
const RelationHasMany = require('../lib/relation/RelationHasMany')
const RelationManyMany = require('../lib/relation/RelationManyMany')
const Customer = require('./Customer')
const Item = require('./Item')
const Remark = require('./Remark')
const Shop = require('./Shop')
const databaseConfig = require('./db.config')
class Order extends DatabaseAccessObject {
constructor () {
super()
/**
* @member {integer}
*/
this.id = undefined
/**
* @member {integer}
*/
this.shopId = undefined
/**
* @member {integer}
*/
this.customerId = undefined
/**
* @member {double}
*/
this.amount = undefined
/**
* @member {Shop}
*/
this.shop = undefined
/**
* @member {Customer}
*/
this.customer = undefined
/**
* @member {Remark[]}
*/
this.remarks = undefined
/**
* @member {Item[]}
*/
this.items = undefined
}
/**
* set up validators
*/
initValidators () {
this.addValidator('id', new NumericValidator(0))
this.addValidator('shopId', new RequiredValidator())
this.addValidator('shopId', new NumericValidator(0))
this.addValidator('customerId', new RequiredValidator())
this.addValidator('customerId', new NumericValidator(0))
this.addValidator('amount', new RequiredValidator())
this.addValidator('amount', new NumericValidator(Number.NEGATIVE_INFINITY))
}
/**
* Attributes with should be validated (or stored in db)
* @returns {string[]}
*/
static getAttributeNames () {
return ['id', 'shopId', 'customerId', 'amount']
}
/**
* @returns {Relation[]}
*/
static getRelations () {
return [
new RelationBelongsTo('shop', 'shopId', Shop, 'id'),
new RelationHasOne('customer', 'customerId', Customer, 'id'),
new RelationHasMany('remarks', 'id', Remark, 'orderId'),
new RelationManyMany('items', 'id', Item, 'id', 'item_order', 'orderId', 'itemId')
]
}
/**
* @returns {MySqlDatabaseConnection}
*/
static getDatabaseConnection () {
return new MySqlDatabaseConnection(databaseConfig)
}
/**
* @returns {string}
*/
static getTableName () {
return 'order'
}
/**
* @returns {string}
*/
static getPrimaryKey () {
return 'id'
}
}
module.exports = Order