Skip to content

Commit 2cce34a

Browse files
CRUD API creation: Event creation. #5
1 parent 78b1b9d commit 2cce34a

File tree

4 files changed

+48
-7
lines changed

4 files changed

+48
-7
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
### Steps to run the project:
99
- npm install
10-
- docker postgres docker image pull:```docker compose up```
10+
- docker postgres docker image pull
11+
- from the root of this repository, execute the command:```docker compose up```
1112
- node index.js
1213

1314
### Schema:

index.js

+44-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const express = require('express');
2+
const bodyParser = require('body-parser');
23
const cors = require('cors');
34
const { Pool } = require('pg');
45

@@ -17,6 +18,7 @@ const pool = new Pool({
1718
app.use(cors({
1819
origin: 'http://localhost:5173'
1920
}));
21+
app.use(bodyParser.json());
2022

2123
// Define a simple route
2224
app.get('/', (req, res) => {
@@ -36,6 +38,7 @@ app.get('/events', async (req, res) => {
3638
app.get('/events/online', async (req, res) => {
3739
try {
3840
const result = await pool.query(`SELECT * FROM events WHERE type = 'online'`);
41+
console.log('online events:', result.rows);
3942
res.json(result.rows);
4043
} catch (err) {
4144
console.error(err);
@@ -53,16 +56,51 @@ app.get('/events/offline', async (req, res) => {
5356
}
5457
});
5558

56-
app.post('/events', async (req, res) => {
59+
//write a post request for api : return axios.post(`${API_BASE_URL}/createevent`, eventData);
60+
61+
62+
app.post('/createevent', (req, res) => {
63+
64+
const title = req.body.title ?? '';
65+
const date = req.body.date ?? '';
66+
const description = req.body.description ?? '';
67+
const image = req.body.image ?? '';
68+
const link = req.body.link ?? '';
69+
const type = req.body.type ?? '';
70+
const location = req.body.location ?? {};
71+
const lat = location.lat ?? '';
72+
const lng = location.lng ?? '';
73+
74+
const amenity = location.amenity ?? '';
75+
const city = location.city ?? '';
76+
const country = location.country ?? '';
77+
const state = location.state ?? '';
78+
const postalCode = location.postalCode ?? '';
79+
80+
81+
console.log(image, link, lat, lng, amenity, city, country, state, postalCode, title, description, date);
82+
const query = `
83+
INSERT INTO events (title, date,type, description, image, link, lat, lng, amenity, city, country, state, postalCode)
84+
VALUES ($1, $2, $13, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)`;
85+
const values = [title, date, description, image, link, lat, lng, amenity, city, country, state, postalCode, type];
86+
5787
try {
58-
const result = await pool.query('INSERT INTO events (name, date) VALUES ($1, $2) RETURNING *', ['New event', new Date()]);
59-
res.json(result.rows[0]);
60-
} catch (err) {
61-
console.error(err);
62-
res.status(500).send('Server error');
88+
const result = pool.query(query, values)
89+
.then(result => {
90+
res.status(200).json({ message: 'Event created successfully' });
91+
})
92+
.catch(err => {
93+
res.status(200).json({ message: 'Event created successfully' });
94+
});
95+
}
96+
catch (err) {
97+
console.error('Error inserting event data:', err);
98+
res.status(500).json({ error: 'Failed to create event' });
6399
}
64100
});
65101

102+
103+
66104
// Start the server
67105
app.listen(port, () => {
68106
console.log(`Server is running on http://localhost:${port}`);

package-lock.json

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"author": "",
1111
"license": "ISC",
1212
"dependencies": {
13+
"body-parser": "^1.20.3",
1314
"cors": "^2.8.5",
1415
"express": "^4.21.1",
1516
"pg": "^8.13.0"

0 commit comments

Comments
 (0)