1
1
const express = require ( 'express' ) ;
2
+ const bodyParser = require ( 'body-parser' ) ;
2
3
const cors = require ( 'cors' ) ;
3
4
const { Pool } = require ( 'pg' ) ;
4
5
@@ -17,6 +18,7 @@ const pool = new Pool({
17
18
app . use ( cors ( {
18
19
origin : 'http://localhost:5173'
19
20
} ) ) ;
21
+ app . use ( bodyParser . json ( ) ) ;
20
22
21
23
// Define a simple route
22
24
app . get ( '/' , ( req , res ) => {
@@ -36,6 +38,7 @@ app.get('/events', async (req, res) => {
36
38
app . get ( '/events/online' , async ( req , res ) => {
37
39
try {
38
40
const result = await pool . query ( `SELECT * FROM events WHERE type = 'online'` ) ;
41
+ console . log ( 'online events:' , result . rows ) ;
39
42
res . json ( result . rows ) ;
40
43
} catch ( err ) {
41
44
console . error ( err ) ;
@@ -53,16 +56,51 @@ app.get('/events/offline', async (req, res) => {
53
56
}
54
57
} ) ;
55
58
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
+
57
87
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' } ) ;
63
99
}
64
100
} ) ;
65
101
102
+
103
+
66
104
// Start the server
67
105
app . listen ( port , ( ) => {
68
106
console . log ( `Server is running on http://localhost:${ port } ` ) ;
0 commit comments