-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
247 lines (221 loc) · 6.58 KB
/
app.js
File metadata and controls
247 lines (221 loc) · 6.58 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
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
const express = require('express');
const cors = require('cors');
const ArangoDBManager = require('./database');
require('dotenv').config();
const app = express();
const port = process.env.PORT || 3000;
// Middleware
app.use(cors());
app.use(express.json());
app.use(express.static('public'));
// Initialize database
const dbManager = new ArangoDBManager();
// Routes
// User Management (Document operations)
app.post('/api/users', async (req, res) => {
try {
const { username, displayName, email, bio } = req.body;
const result = await dbManager.createUser({
username,
displayName,
email,
bio
});
res.status(201).json(result);
} catch (error) {
res.status(400).json({ error: error.message });
}
});
app.get('/api/users', async (req, res) => {
try {
const users = await dbManager.getAllUsers();
res.json(users);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// Popular users endpoint - must be before the generic :id route
app.get('/api/users/popular', async (req, res) => {
try {
const limit = parseInt(req.query.limit) || 5;
console.log('Getting popular users with limit:', limit);
const popularUsers = await dbManager.getPopularUsers(limit);
console.log('Popular users result:', popularUsers);
res.json(popularUsers);
} catch (error) {
console.error('Error in /api/users/popular:', error);
res.status(500).json({ error: error.message });
}
});
app.get('/api/users/:id', async (req, res) => {
try {
const user = await dbManager.getUser(req.params.id);
res.json(user);
} catch (error) {
res.status(404).json({ error: 'User not found' });
}
});
// Post Management (Document operations)
app.post('/api/posts', async (req, res) => {
try {
const { authorId, content, title, tags } = req.body;
const result = await dbManager.createPost({
authorId,
content,
title,
tags: tags || []
});
res.status(201).json(result);
} catch (error) {
res.status(400).json({ error: error.message });
}
});
app.get('/api/posts', async (req, res) => {
try {
const posts = await dbManager.getAllPosts();
res.json(posts);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// Graph Operations
app.post('/api/users/:followerId/follow/:followeeId', async (req, res) => {
try {
const { followerId, followeeId } = req.params;
// Validation
if (followerId === followeeId) {
return res.status(400).json({ error: 'Cannot follow yourself' });
}
const result = await dbManager.followUser(followerId, followeeId);
res.status(201).json(result);
} catch (error) {
console.error('Follow user error:', error);
res.status(400).json({ error: error.message });
}
});
app.post('/api/users/:userId/like/:postId', async (req, res) => {
try {
const { userId, postId } = req.params;
console.log('Like post request:', { userId, postId });
const result = await dbManager.likePost(userId, postId);
console.log('Like post result:', result);
res.status(201).json(result);
} catch (error) {
console.error('Like post error:', error);
res.status(400).json({ error: error.message });
}
});
// Debug endpoints for graph operations
app.get('/api/debug/follows/:userId?', async (req, res) => {
try {
const { userId } = req.params;
const follows = await dbManager.getFollowRelationships(userId);
res.json(follows);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
app.get('/api/debug/likes/:userId?', async (req, res) => {
try {
const { userId } = req.params;
const likes = await dbManager.getLikeRelationships(userId);
res.json(likes);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// Simplified debug endpoints for frontend
app.get('/api/debug/graph', async (req, res) => {
try {
const follows = await dbManager.getFollowRelationships();
const likes = await dbManager.getLikeRelationships();
res.json({
relationships: {
follows: follows.length,
likes: likes.length
},
details: {
follows: follows,
likes: likes
}
});
} catch (error) {
res.status(500).json({ error: error.message });
}
});
app.get('/api/users/:userId/stats', async (req, res) => {
try {
const { userId } = req.params;
const stats = await dbManager.getUserStats(userId);
res.json(stats);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// Key-Value Operations
app.post('/api/users/:userId/settings/:key', async (req, res) => {
try {
const { userId, key } = req.params;
const { value } = req.body;
const result = await dbManager.setUserSetting(userId, key, value);
res.status(201).json(result);
} catch (error) {
res.status(400).json({ error: error.message });
}
});
app.get('/api/users/:userId/settings/:key', async (req, res) => {
try {
const { userId, key } = req.params;
const setting = await dbManager.getUserSetting(userId, key);
if (setting) {
res.json(setting);
} else {
res.status(404).json({ error: 'Setting not found' });
}
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// Complex Multimodal Queries
app.get('/api/users/:userId/feed', async (req, res) => {
try {
const { userId } = req.params;
const limit = parseInt(req.query.limit) || 10;
const feed = await dbManager.getUserFeed(userId, limit);
res.json(feed);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
app.get('/api/users/:userId1/mutual/:userId2', async (req, res) => {
try {
const { userId1, userId2 } = req.params;
const mutualFollows = await dbManager.getMutualFollows(userId1, userId2);
res.json(mutualFollows);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// Health check
app.get('/health', (req, res) => {
res.json({ status: 'OK', timestamp: new Date().toISOString() });
});
// Serve the demo page
app.get('/', (req, res) => {
res.sendFile(__dirname + '/public/index.html');
});
// Start server
async function startServer() {
try {
await dbManager.initializeDatabase();
app.listen(port, () => {
console.log(`🚀 ArangoDB Multimodal App running on http://localhost:${port}`);
console.log(`📊 Database: ${process.env.ARANGO_DATABASE || 'multimodal_demo'}`);
console.log(`🔗 ArangoDB: ${process.env.ARANGO_URL || 'http://localhost:8529'}`);
});
} catch (error) {
console.error('Failed to start server:', error);
process.exit(1);
}
}
startServer();