-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbObjects.js
95 lines (79 loc) · 2.87 KB
/
dbObjects.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
const Sequelize = require('sequelize');
const sequelize = new Sequelize('database', 'username', 'password', {
host: 'localhost',
dialect: 'sqlite',
logging: false,
storage: 'database.sqlite',
});
const Users = require('./models/Users')(sequelize, Sequelize.DataTypes);
const CurrencyShop = require('./models/CurrencyShop')(sequelize, Sequelize.DataTypes);
const UserItems = require('./models/UserItems')(sequelize, Sequelize.DataTypes);
const PlantedCurrency = require('./models/PlantedCurrency')(sequelize, Sequelize.DataTypes);
const Waifus = require('./models/Waifus')(sequelize, Sequelize.DataTypes);
const WaifuItems = require('./models/WaifuItems')(sequelize, Sequelize.DataTypes);
const Transactions = require('./models/Transactions')(sequelize, Sequelize.DataTypes);
UserItems.belongsTo(CurrencyShop, { foreignKey: 'item_id', as: 'item' });
Users.hasOne(Waifus, { foreignKey: 'waifu_id', as: 'waifuinfo' });
Users.hasMany(Waifus, { foreignKey: 'owner_id', as: 'waifus' });
Users.hasMany(WaifuItems, { foreignKey: 'waifu_id', as: 'gifts' });
Waifus.belongsTo(Users, { foreignKey: 'owner_id', as: 'owner' });
Waifus.belongsTo(Users, { foreignKey: 'waifu_id', as: 'waifu' });
Reflect.defineProperty(Users.prototype, 'getWaifus', {
value: async function() {
return Waifus.findAll({
where: { owner_id: this.user_id },
include: ['waifu']
});
}
});
Reflect.defineProperty(Users.prototype, 'getGifts', {
value: async function() {
return WaifuItems.findAll({
where: { waifu_id: this.user_id },
attributes: [
'name',
[sequelize.fn('sum', sequelize.col('cost')), 'total'],
[sequelize.fn('COUNT', 'name'), 'count']
],
group: ['name'],
order: [[sequelize.fn('COUNT', 'name'), 'DESC']],
raw: true
});
}
});
Reflect.defineProperty(Users.prototype, 'transaction', {
value: async function(amount, reason) {
return Transactions.create({
user_id: this.user_id, amount: amount, reason: reason
});
}
});
Reflect.defineProperty(Users.prototype, 'plant', {
value: async function(amount, password) {
return PlantedCurrency.create({ amount: amount, password: password });
}
});
Reflect.defineProperty(Users.prototype, 'pick', {
value: async function(password) {
return PlantedCurrency.destroy({ where: { password: password } });
}
});
Users.prototype.addItem = async function(item) {
const userItem = await UserItems.findOne({
where: { user_id: this.user_id, item_id: item.id },
});
if (userItem) {
userItem.amount += 1;
return userItem.save();
}
return UserItems.create({ user_id: this.user_id, item_id: item.id, amount: 1 });
};
Users.prototype.getItems = function() {
return UserItems.findAll({
where: { user_id: this.user_id },
include: ['item'],
});
};
module.exports = {
Users, CurrencyShop, UserItems, PlantedCurrency, Waifus, WaifuItems, Transactions
};