-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabase.js
More file actions
148 lines (120 loc) · 3.25 KB
/
Copy pathDatabase.js
File metadata and controls
148 lines (120 loc) · 3.25 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
const Sequelize = require('sequelize');
const db = new Sequelize('sql2238626', 'sql2238626', 'mS1*uV6*', {
host : 'sql2.freemysqlhosting.net',
dialect : 'mysql'
});
//entretien@aditus.info
//YSaCxq5pY^c82rsc
const User = db.define('user', {
username : { type: Sequelize.STRING },
password : { type: Sequelize.STRING },
icon : { type: Sequelize.STRING, defaultValue : "https://d2ln1xbi067hum.cloudfront.net/assets/default_user-abdf6434cda029ecd32423baac4ec238.png"}
});
const Saloon = db.define('saloon', {
title : { type: Sequelize.STRING },
author : { type: Sequelize.STRING },
firstContent : { type : Sequelize.STRING},
numberOfResponse : { type : Sequelize.INTEGER, defaultValue : 0 }
});
const Response = db.define('response', {
author : { type: Sequelize.STRING },
content : { type: Sequelize.STRING }
});
User.hasMany(Response, {as : 'responses'});
Response.belongsTo(Saloon);
Response.belongsTo(User);
Saloon.hasOne(User);
//db.sync();
//db.drop();
const getUserById = (id, next) => {
return User
.findOne({where : {id : id}})
.then(res => {
next(res);
});
};
const getUserByName = (name, next = ()=>{}) => {
return User
.findOne({where : {username : name}})
.then(res => {
next(res);
});
};
const getAllSaloons = (next)=>{
return Saloon
.findAll()
.then(res => {
next(res);
});
};
const getSaloonById = (id, next) => {
return Saloon
.findOne({where : { id : id}})
.then(res => {
next(res);
});
};
const getReponsesByUserId = (id, next) => {
return Response
.findAll({where : { userId : id}})
.then(res => {
next(res);
});
};
const getReponsesBySaloonId = (id, next) => {
return Response
.findAll({where : { saloonId : id}})
.then(res => {
next(res);
});
};
const createUser = (user) => {
return User
.sync()
.then(()=>{
User.create(user);
});
};
const createSaloon = (saloon) => {
return Saloon
.sync()
.then(()=>{
Saloon.create(saloon);
});
};
const createResponse = (response) => {
return Response
.sync()
.then(()=>{
Response.create(response);
});
};
const updateSaloonById = ((id, update)=>{
return Saloon
.findOne({where : {id : id}})
.then(res=>{
Saloon.update(update,
{where : {id : id}})
});
});
const updateUserById = (id, update)=>{
return User
.findOne({whrere : {id : id}})
.then((res)=>{
User.update(update,
{where : {id : id}})
});
};
module.exports = {
getUserById : getUserById,
getUserByName : getUserByName,
getAllSaloons : getAllSaloons,
getSaloonById : getSaloonById,
getReponsesByUserId : getReponsesByUserId,
getReponsesBySaloonId : getReponsesBySaloonId,
createUser : createUser,
createSaloon : createSaloon,
createResponse : createResponse,
updateUserById : updateUserById,
updateSaloonById : updateSaloonById
};