forked from HICHAM-EZZYTY/minprojet1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
170 lines (118 loc) · 3.1 KB
/
app.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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
const express = require('express');
const app = express();
const cors = require('cors');
//import routes
const users = require('./routes/users');
const categories = require('./routes/categories')
const comments = require('./routes/comments')
const posts = require('./routes/posts')
const types = require('./routes/types')
const bodyParser = require('body-parser');
app.use(cors());
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({
extended: true
}))
app.use(bodyParser.json({
type: 'application/*+json'
}))
app.use(bodyParser.raw({
type: 'application/vnd.custom-type'
}))
app.use(bodyParser.text({
type: 'text/html'
}))
//Connection with MySQL
const connection = require('./config/database');
//Models
const Category = require('./models/category');
const Comment = require('./models/comment');
const Post = require('./models/post');
const Tag = require('./models/tag');
const Type = require('./models/type')
const User = require('./models/user');
//Usage of routes
app.use('/categories', categories)
app.use('/comments', comments)
app.use('/users', users)
app.use('/posts', posts)
app.use('/types', types)
User.belongsTo(Type)
Type.hasMany(User)
Post.belongsTo(Category)
Post.belongsTo(User)
Category.hasMany(Post)
User.hasMany(Post)
Comment.belongsTo(User)
Comment.belongsTo(Post)
Post.hasMany(Comment)
User.hasMany(Comment)
// Users.belongsToMany(models.Groups, {
// through: 'GroupUsers',
// as: 'groups',
// foreignKey: 'userId'
// });
Post.belongsToMany(Tag, {
through: 'Post_Tag',
as: 'items'
})
Tag.belongsToMany(Post, {
through: 'Post_Tag',
as: 'items'
})
// Relation between User and Type table
User.belongsTo(Type)
Type.hasMany(User)
// Relation between Post and User table
Post.belongsTo(Category)
Post.belongsTo(User)
// Relation between Category and User table
Category.hasMany(Post)
User.hasMany(Post)
// Relation between Post and USER table
Comment.belongsTo(User)
Comment.belongsTo(Post)
// Relation between post and tag table
Post.hasMany(Comment)
User.hasMany(Comment)
// Relation between user and type table
Post.belongsToMany(Tag, {
through: 'Post_Tag'
})
Tag.belongsToMany(Post, {
through: 'Post_Tag'
})
connection.sync()
.then(result => {
app.listen(5000, () => console.log('Server ON'))
// User.create({
// name: "anas",
// email: "anas@mai.com",
// password: "AHAHAH"
// })
// Category.create({
// title: "ahmed",
// active: true,
// })
// Comment.create({
// commentaire: "Commenatie",
// active: true,
// })
// Post.create({
// title: "hahhaha",
// urlImage: "zzzzzzzzzzzzzzzzzzzzzz",
// description: "frrrrrrrrrr",
// active: true,
// })
// Tag.create({
// name: "kacch",
// active: true,
// })
// Type.create({
// name: "kakakaka",
// active: true,
// })
})
.catch((err) => {
console.log('error: ', err)
})