forked from sainanduk/onwe_backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
153 lines (126 loc) · 5.94 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
const express = require('express');
const bodyParser = require('body-parser');
const cors =require('cors')
const cron =require('node-cron')
const deleteOldPosts =require('./middlewares/deletepostsinterval.js')
const postsRoutes = require('./Routes/Post_route.js');
const commentsRoutes = require('./Routes/Comments_route.js');
const usernameRoutes = require('./Routes/username_route.js');
const { sequelize} = require('./Config/database');
const authRoutes = require('./Routes/authroutes.js')
const searchRoute = require('./Routes/search_route.js')
const UserFollowing = require('./models/userfollowing');
const UserFollowers = require('./models/userfollowers');
const mobileLogin = require("./mobile/routes/signin.js");
const Admins = require('./models/Admins');
const Clubs = require('./models/Clubs');
const Comments = require('./models/Comments');
const Event = require('./models/Event');
const Magazines = require('./models/Magazines');
const Posts = require('./models/Posts');
const Users = require('./models/Users');
const verifier = require('./middlewares/verifier.js')
const EventRoutes =require('./Routes/event_routes.js')
const UserUpdateRoute = require('./Routes/Users_route.js')
const PostLikes = require('./models/postLikes.js')
const FollowersFollowing =require('./Routes/followersandfollowing.js')
const magazineRoutes=require('./Routes/magazines_route.js')
const ClubStatus =require('./models/clubstatuses.js')
const ExploreRoutes=require('./Routes/explore_route.js')
const clubRoutes=require('./Routes/Clubs_route.js')
const updateusers =require('./Routes/Users_route.js');
const PollOptions = require('./models/PollOptions.js');
const Polls = require('./models/Polls.js');
const polls_route =require('./Routes/polls_route.js')
const mobileVerifier = require('./mobile/routes/middleware/mobileverifier.js')
const createclub =require('./Admin_Routes/club_routes.js')
const createevent =require('./Admin_Routes/event_routes.js')
const app = express();
app.use(bodyParser.json());
app.use(cors())
app.use(mobileLogin);
app.use(authRoutes);
app.use(createclub)
app.use(createevent)
app.use(magazineRoutes);
app.use(EventRoutes)
app.use(postsRoutes);
// app.use('/mobile',mobileVerifier,postsRoutes)
app.use(searchRoute);
app.use(verifier,ExploreRoutes)
// app.use('/mobile',verifier,ExploreRoutes)
app.use(commentsRoutes);
app.use(verifier,clubRoutes);
app.use(updateusers)
app.use(polls_route)
// app.use('/api',verifier,UserUpdateRoute)
// app.use('/api',verifier,FollowersFollowing)
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).json({ message: "Internal Server Error" });
});
// Define associations
// Comments.belongsTo(Posts, { foreignKey: "postId" });
// Comments.belongsTo(Users, { foreignKey: "userId" });
// Clubs.belongsTo(Users, { foreignKey: "admin" });
// Magazines.belongsTo(Admins, { foreignKey: "owner" });
// Posts.belongsTo(Users, { foreignKey: "userId" });
// Posts.hasMany(Comments, { foreignKey: "postId", as: "postComments" });
// Users.hasMany(Comments, { foreignKey: "userId", as: "userComments" });
// Users.hasMany(Clubs, { foreignKey: "admin", as: "adminClubs" });
// Admins.hasMany(Magazines, { foreignKey: "owner", as: "ownedMagazines" });
// Users.hasMany(Posts, { foreignKey: "userId", as: "userPosts" });
// Posts.hasMany(PostLikes, { foreignKey: 'postId', as: 'postLikes' });
// PostLikes.belongsTo(Posts, { foreignKey: 'postId' });
// // Users model associations
// Users.hasMany(UserFollowers, { foreignKey: 'userId', as: 'followers' });
// Users.hasMany(UserFollowing, { foreignKey: 'userId', as: 'following' });
// // userfollowers and userfollowing model associations (if needed)
// UserFollowers.belongsTo(Users, { foreignKey: 'followerId', as: 'follower' });
// UserFollowing.belongsTo(Users, { foreignKey: 'followingId', as: 'followed' });
// Comments associations
Posts.belongsTo(Users, { as: 'user', foreignKey: 'userid' });
Posts.hasMany(Comments, { foreignKey: 'postId', as: 'postComments' });
Posts.hasMany(PostLikes, { foreignKey: 'postId', as: 'postLikes' });
Users.hasMany(Comments, { foreignKey: 'userId', as: 'userComments' });
Users.hasMany(Clubs, { foreignKey: 'admin', as: 'adminClubs' });
Users.hasMany(Posts, { as: 'posts', foreignKey: 'userid' });
Users.hasMany(UserFollowers, { foreignKey: 'userId', as: 'followers' });
Users.hasMany(UserFollowing, { foreignKey: 'userId', as: 'following' });
Users.hasMany(ClubStatus, { foreignKey: 'userId' });
Admins.hasMany(Magazines, { foreignKey: 'owner', as: 'ownedMagazines' });
UserFollowers.belongsTo(Users, { foreignKey: 'followerId', as: 'follower' });
UserFollowing.belongsTo(Users, { foreignKey: 'followingId', as: 'followed' });
PostLikes.belongsTo(Posts, { foreignKey: 'postId' });
PostLikes.belongsTo(Users, { foreignKey: 'userId' });
Clubs.hasMany(ClubStatus, { foreignKey: 'clubId' });
ClubStatus.belongsTo(Clubs, { foreignKey: 'clubId' });
ClubStatus.belongsTo(Users, { foreignKey: 'userId' });
Polls.hasMany(PollOptions, { foreignKey: 'pollId', as: 'options' });
PollOptions.belongsTo(Polls, { foreignKey: 'pollId' });
Comments.belongsTo(Posts, { foreignKey: 'postId' });
Comments.belongsTo(Users, { foreignKey: 'userId' });
Magazines.belongsTo(Admins, { foreignKey: 'owner' });
const initializeDatabase = async () => {
try {
await sequelize.authenticate();
console.log('Connection to the database has been established successfully.');
// Sync all models
await sequelize.sync(); // force: true will drop tables if they exist, use it carefully
console.log('Database and tables have been synced successfully.');
} catch (error) {
console.error('Error initializing the database:', error);
}
};
// Initialize and sync database
initializeDatabase();
//delete posts after 48 hours
cron.schedule('0 * * * *', () => {
console.log('Running scheduled job to delete old posts');
deleteOldPosts();
});
// Start the server
const PORT = process.argv[2] || process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on http://127.0.0.1:${PORT}`);
});