-
Notifications
You must be signed in to change notification settings - Fork 0
/
blog-service.js
283 lines (258 loc) · 6.2 KB
/
blog-service.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*********************************************************************************
//no significant changes made since version:
* WEB322 – Assignment 05
* I declare that this assignment is my own work in accordance with Seneca Academic Policy. No part
* of this assignment has been copied manually or electronically from any other source
* (including 3rd party web sites) or distributed to other students.
*
* Name: Lorenz Alvin Tubo Student ID: 1090934224 Date: 07/15/2023
*
* Cyclic Web App URL: https://easy-teal-elk-gear.cyclic.app/about
*
* GitHub Repository URL: https://github.com/YuhanPizza/web322-app
*
********************************************************************************/
//sequelize
const {Sequelize, DataTypes} = require('sequelize');
//set up sequelize to point to our postgres database
var sequelize = new Sequelize('chcyjspl','chcyjspl','q1iCbG_DUaJHCYpssuZEKpPYazdvxoaf',{
host: 'stampy.db.elephantsql.com',
dialect: 'postgres',
port: 5432,
dialectOptions:{
ssl:{rejectUnauthorized: false}
},
query: { raw: true},
pool:{
max:5,
min:0,
acquire: 30000,
idle:10000
}
})
sequelize.authenticate().then(()=>{
console.log('connection has been established successfully.');
}).catch((err)=>{
console.log('unable to connect to the data base:', err);
})
//Post model
const Post = sequelize.define('Post', {
body: {
type: DataTypes.TEXT,
allowNull: false,
},
title: {
type: DataTypes.STRING,
allowNull: false,
},
postDate: {
type: DataTypes.DATE,
allowNull: false,
},
featureImage: {
type: DataTypes.STRING,
allowNull: true,
},
published: {
type: DataTypes.BOOLEAN,
allowNull: false,
},
});
//Category model
const Category = sequelize.define('Category', {
category: {
type: DataTypes.STRING,
allowNull: false,
},
});
//relationship
Post.belongsTo(Category, { foreignKey: 'category' });
//initialize
const initialize = () => {
return new Promise((resolve, reject) => {
sequelize.sync()
.then(() => {
resolve();
})
.catch(() => {
reject('Unable to sync the database');
});
});
};
//get all posts.
const getAllPosts = () => {
return new Promise((resolve, reject) => {
Post.findAll()
.then((posts) => {
resolve(posts);
})
.catch(() => {
reject('No results returned');
});
});
};
// filter through published post = true
const getPublishedPosts = () => {
return new Promise((resolve, reject) => {
Post.findAll({ where: { published: true } })
.then((posts) => {
resolve(posts);
})
.catch(() => {
reject('No results returned');
});
});
};
//categories
const getCategories = () => {
return new Promise((resolve, reject) => {
Category.findAll()
.then((categories) => {
resolve(categories);
})
.catch(() => {
reject('No results returned');
});
});
};
//add posts
const addPost = (postData) => {
return new Promise((resolve, reject) => {
postData.published = (postData.published) ? true : false;
for (let key in postData) {
if (postData[key] === "") {
postData[key] = null;
}
}
postData.postDate = new Date();
Post.create(postData)
.then(() => {
resolve();
})
.catch(() => {
reject('Unable to create post');
});
});
};
//get post by cat
const getPostsByCategory = (category) => {
return new Promise((resolve, reject) => {
Post.findAll({ where: { category } })
.then((posts) => {
resolve(posts);
})
.catch(() => {
reject('No results returned');
});
});
};
//getpost by date
const getPostsByMinDate = (minDateStr) => {
return new Promise((resolve, reject) => {
Post.findAll({
where: {
postDate: { [Sequelize.Op.gte]: new Date(minDateStr) }
}
})
.then((posts) => {
resolve(posts);
})
.catch(() => {
reject('No results returned');
});
});
};
//getpost by id
const getPostById = (id) => {
return new Promise((resolve, reject) => {
Post.findAll({ where: { id } })
.then((posts) => {
if (posts.length > 0) {
resolve(posts[0]);
} else {
reject('No results returned');
}
})
.catch(() => {
reject('No results returned');
});
});
};
const getPublishedPostsByCategory = (category) => {
return new Promise((resolve, reject) => {
Post.findAll({ where: { published: true, category } })
.then((posts) => {
resolve(posts);
})
.catch(() => {
reject('No results returned');
});
});
};
// addCategory
const addCategory = (categoryData) => {
return new Promise((resolve, reject) => {
for (let key in categoryData) {
if (categoryData[key] === "") {
categoryData[key] = null;
}
}
Category.create(categoryData)
.then(() => {
resolve();
})
.catch(() => {
reject('Unable to create category');
});
});
};
// deleteCategoryById
const deleteCategoryById = (id) => {
return new Promise((resolve, reject) => {
Category.destroy({ where: { id } })
.then((rowsDeleted) => {
if (rowsDeleted > 0) {
resolve();
} else {
reject('No category found with the specified ID');
}
})
.catch(() => {
reject('Failed to delete category');
});
});
};
// deletePostById
const deletePostById = (id) => {
return new Promise((resolve, reject) => {
Post.destroy({ where: { id } })
.then((rowsDeleted) => {
if (rowsDeleted > 0) {
resolve();
} else {
reject('No post found with the specified ID');
}
})
.catch(() => {
reject('Failed to delete post');
});
});
};
//when this module is imported into another file using require the imported object will have access to
//these properties. server.js can call these functions.
module.exports = {
Post,
Category,
initialize,
getAllPosts,
getPublishedPosts,
getCategories,
addPost,
getPostsByCategory,
getPostsByMinDate,
getPostById,
getPublishedPostsByCategory,
addCategory,
deleteCategoryById,
deletePostById,
};