-
Notifications
You must be signed in to change notification settings - Fork 0
/
blog-server.js
192 lines (181 loc) · 5.29 KB
/
blog-server.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
const { append } = require("express/lib/response");
const fs = require("fs");
const { resolve } = require("path");
const Sequelize = require('sequelize');
const { gte } = Sequelize.Op;
var sequelize = new Sequelize('d56ljmssut0i60','fzuvhgoelhwhet','8e23c7ff7f2298655a3d35f8fe8fbea6f14302a36b6a3b5c44f07a7ff9ca8046', {
host: 'ec2-52-72-99-110.compute-1.amazonaws.com',
dialect: 'postgres',
port: 5432,
dialectOptions: {
ssl: { rejectUnauthorized: false }
},
query: { raw: true }
});
var Post = sequelize.define('Post', {
body: Sequelize.TEXT,
title: Sequelize.STRING,
postDate: Sequelize.DATE,
featureImage: Sequelize.STRING,
published: Sequelize.BOOLEAN
});
var Category= sequelize.define('Category', {
category : Sequelize.STRING
});
Post.belongsTo(Category, {foreignKey: 'category'});
function initialize(){
return new Promise((resolve,reject)=>{
setTimeout(()=>{
sequelize.sync({ force: true }).then(()=> {
resolve("Connected")
}).catch(()=> {
reject("Unable to Sync to the Database")
});
},3000)
})
}
function getAllPosts(){
return new Promise((resolve,reject)=>{
Post.findAll().then(function(data){
resolve(data) ;
}).catch(function(error){
reject("no results returned")
});
})
}
function getPublishedPosts(){
return new Promise((resolve,reject)=>{
Post.findAll({
where:{
published:true
}
}).then(function(data){
resolve(data) ;
}).catch(function(error){
reject(Error("no results returned"))
});
})
}
function getCategories(){
return new Promise((resolve,reject)=>{
Category.findAll().then(function(data){
resolve(data) ;
}).catch(function(error){
reject(Error("no results returned"))
});
})
}
function addPost(postData){
postData.published = (postData.published) ? true : false;
for(prop in postData){
if(postData[prop]==""){
postData[prop] = null
}
}
postData.postDate=new Date();
return new Promise((resolve,reject)=>{
setTimeout(()=>{
Post.create({
body: postData.body,
title: postData.title,
postDate: postData.postDate,
featureImage: postData.featureImage,
published: postData.published
}).then(function(){
resolve("Success")
}).catch(function(){
reject("unable to create post")
})
},3000)
})
}
function getPostsByCategory(categoryValue){
return new Promise((resolve,reject)=>{
Post.findAll({
where:{
category:categoryValue
}
}).then(function(data){
resolve(data) ;
}).catch(function(error){
reject(Error("no results returned"))
});
})
}
function getPostsByMinDate(minDate){
return new Promise((resolve,reject)=>{
Post.findAll({
where: {
postDate: {
[gte]: new Date(minDateStr)
}
}
}).then(function(data){
resolve(data) ;
}).catch(function(error){
reject(Error("no results returned"))
});
})
}
function getPostsById(p_id){
return new Promise((resolve,reject)=>{
Post.findAll({
where:{
id:p_id
}
}).then(function(data){
resolve(data) ;
}).catch(function(error){
reject("no results returned")
});
})
}
function getPublishedPostsByCategory(categoryValue){
return new Promise((resolve,reject)=>{
Post.findAll({
where:{
category: categoryValue,
published :true
}
}).then(function(data){
resolve(data);
}).catch(function(error){
reject("no results returned");
});
})
}
function addCategory(categoryData){
for(prop in categoryData){
if(categoryData[prop]==""){
categoryData[prop] = null
}
}
return new Promise((resolve,reject)=>{
Category.create({
category:categoryData.category
}).then(function(){
resolve("Success")
}).catch(function(){
reject("unable to create category")
})
})
}
function deleteCategoryById(c_id){
return new Promise((resolve,reject)=>{
Category.destroy({
where:{
id: c_id
}
}).then(function(){resolve("Deleted Successfully")}).catch(function(){reject("Not Deleted")})
})
}
function deletePostById(p_id){
return new Promise((resolve,reject)=>{
Post.destroy({
where:{
id: p_id
}
}).then(function(){resolve("Deleted Successfully")}).catch(function(){reject("Not Deleted")})
})
}
module.exports={initialize,getCategories,getPublishedPosts,getAllPosts,addPost,getPostsByCategory,getPostsByMinDate,getPostsById,getPublishedPostsByCategory,addCategory,deletePostById,deleteCategoryById}