-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
106 lines (82 loc) · 2.34 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
const express = require('express')
const app = express()
const mongoose = require('mongoose')
const PORT = 5000
const {MONGOURL}= require('./keys')
const cors = require('cors')
const bodyParser = require("body-parser");
mongoose.connect(MONGOURL,{
useNewUrlParser:true, useUnifiedTopology:true
})
mongoose.connection.on('connected',()=>{
console.log('Connected to mongo...')
})
mongoose.connection.on('error',(err)=>{
console.log('error connecting...',err)
})
require('./models/user')
require('./models/post')
app.use(cors())
app.use(bodyParser.json());
app.use(express.json())
app.get('/',(req,res)=>{
res.status(200).send('welcome back')
})
app.use(require('./routes/auth'))
app.use(require('./routes/posts'))
const colorClassMapping = {
"#B38BFA": "error",
"#FF79F2": "error-content",
"#43E6FC": "warning",
"#F19576": "warning-content",
"#0047FF": "success",
"#6691FF": "success-content",
};
// Define MongoDB schemas and models
const groupSchema = new mongoose.Schema({
name: String,
color: String,
});
const noteSchema = new mongoose.Schema({
groupId: mongoose.Schema.Types.ObjectId,
text: String,
date: String,
});
const Group = mongoose.model("Group", groupSchema);
const Note = mongoose.model("Note", noteSchema);
// REST API endpoints
app.get("/", (req, res) => {
res.send("Hello from the server!");
});
app.get("/groups", async (req, res) => {
const groups = await Group.find();
res.json(groups);
});
app.post("/groups", async (req, res) => {
try {
const { name, color } = req.body;
// Map color to its corresponding class name
const mappedColor = colorClassMapping[color] || color;
const newGroup = new Group({ name, color: mappedColor });
await newGroup.save();
res.json(newGroup);
} catch (error) {
console.error("Error creating group:", error);
res.status(500).json({ error: "Internal Server Error" });
}
});
app.get("/groups/:groupId", async (req, res) => {
const groupId = req.params.groupId;
const groupNotes = await Note.find({ groupId });
res.json(groupNotes);
});
app.post("/groups/:groupId", async (req, res) => {
const groupId = req.params.groupId;
const { text, date } = req.body;
const newNote = new Note({ groupId, text, date });
await newNote.save();
res.json(newNote);
});
app.listen(PORT,()=>{
console.log("Spacebook is launching on", PORT)
})