Skip to content

Commit a931d8c

Browse files
committed
⚡ users events
1 parent 0e0ad79 commit a931d8c

File tree

5 files changed

+153
-80
lines changed

5 files changed

+153
-80
lines changed

FrontEnd/event.html

+39-17
Original file line numberDiff line numberDiff line change
@@ -65,27 +65,49 @@
6565
}
6666
};
6767

68-
const register = async (id) => {
69-
try {
70-
await axios.put(baseUrl + `/event/${id}`, {}, config);
68+
// const axios = require('axios');
7169

72-
73-
const userEmailInput = document.getElementById('userEmailInput');
74-
const userEmail = userEmailInput.value;
70+
const register = async (id) => {
71+
try {
72+
// Fetch the event details using the Eventbrite API
73+
const eventResponse = await axios.get(`https://www.eventbriteapi.com/v3/events/${id}/`, {
74+
headers: {
75+
'Authorization': 'Bearer KCIV4OKXGX3FO6FDGVYC'
76+
}
77+
});
7578

76-
if (!userEmail || !validateEmail(userEmail)) {
77-
alert("Please enter a valid email address.");
78-
return;
79-
}
79+
// Verify the structure of the API response and access the event name accordingly
80+
const eventName = eventResponse.data?.name?.text;
81+
if (!eventName) {
82+
console.error('Event name not found in the API response.');
83+
return;
84+
}
8085

81-
82-
await axios.post(baseUrl + '/send-registration-email', { userEmail, eventId: id }, config);
86+
// Perform the PUT request with the eventName
87+
await axios.put(baseUrl + `/event/${id}`, { eventName }, config);
8388

84-
alert("Registration successful");
85-
} catch (error) {
86-
console.log(error);
87-
}
88-
};
89+
// Get user email from the input field and validate it
90+
const userEmailInput = document.getElementById('userEmailInput');
91+
const userEmail = userEmailInput.value;
92+
if (!userEmail || !validateEmail(userEmail)) {
93+
alert("Please enter a valid email address.");
94+
return;
95+
}
96+
97+
// Perform the POST request to send the registration email
98+
await axios.post(baseUrl + '/send-registration-email', { userEmail, eventName, eventId: id }, config);
99+
100+
alert("Registration successful");
101+
} catch (error) {
102+
console.log(error);
103+
}
104+
};
105+
106+
// Validate email function (you should have this function defined)
107+
function validateEmail(email) {
108+
// Implement your email validation logic here
109+
// Return true if the email is valid, false otherwise
110+
}
89111

90112
function validateEmail(email) {
91113
const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;

FrontEnd/js/profile.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const deleteUserBtn = document.getElementById('btn2');
88
const eventsList = document.getElementById('events-list');
99

1010
const baseUrl = 'http://localhost:8000/api/user';
11-
const eventbriteUrl='https://www.eventbriteapi.com/v3';
11+
const eventbriteUrl = 'https://www.eventbriteapi.com/v3';
1212

1313
if (!localStorage.getItem('userToken')) {
1414
window.location.href = 'http://127.0.0.1:5500/Frontend/index.html';
@@ -73,9 +73,9 @@ const fetchUser = async () => {
7373
// Update events list
7474
eventsList.innerHTML = '';
7575
if (user.events && user.events.length > 0) {
76-
const eventsHTML = await Promise.all(user.events.map(async (eventId) => {
77-
const eventName = await fetchEventName(eventId);
78-
return `<li>${eventId} - ${eventName}</li>`;
76+
const eventsHTML = await Promise.all(user.events.map(async (event) => {
77+
const eventName = await fetchEventName(event.eventId);
78+
return `<li>${event.eventId} - ${eventName}</li>`;
7979
}));
8080
eventsList.innerHTML = eventsHTML.join('');
8181
} else {
@@ -99,4 +99,5 @@ const fetchUser = async () => {
9999
}
100100
};
101101

102+
// Call the fetchUser function to populate the profile page
102103
fetchUser();

FrontEnd/users.html

+65-19
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,28 @@
11
<!DOCTYPE html>
22
<html lang="en">
3-
43
<head>
54
<meta charset="UTF-8">
65
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7-
<title>All Users</title>
6+
<title>User List</title>
87
<link rel="stylesheet" href="./css/users.css">
98
<link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
109
<script src="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
1110
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
1211
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/1.2.3/axios.min.js" integrity="sha512-L4lHq2JI/GoKsERT8KYa72iCwfSrKYWEyaBxzJeeITM9Lub5vlTj8tufqYk056exhjo2QDEipJrg6zen/DDtoQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
1312
</head>
14-
1513
<body>
16-
<div style="width: 100%; height: 60px;">
17-
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
18-
<a class="navbar-brand" href="#">EventM - Enjoy the Events in your City</a>
19-
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
20-
<span class="navbar-toggler-icon"></span>
21-
</button>
22-
23-
<div class="collapse navbar-collapse" id="navbarSupportedContent">
24-
<ul class="navbar-nav ml-auto mr-3" id="navbar-items" >
25-
26-
</ul>
27-
</div>
28-
</nav>
14+
<div style="width: 100%; height: 60px;">
15+
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
16+
<a class="navbar-brand" href="#">EventM - Enjoy the Events in your City</a>
17+
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
18+
<span class="navbar-toggler-icon"></span>
19+
</button>
20+
<div class="collapse navbar-collapse" id="navbarSupportedContent">
21+
<ul class="navbar-nav ml-auto mr-3" id="navbar-items" >
22+
</ul>
2923
</div>
24+
</nav>
25+
</div>
3026
<div class="container">
3127
<h1>All Users</h1>
3228
<table>
@@ -40,13 +36,63 @@ <h1>All Users</h1>
4036
</tr>
4137
</thead>
4238
<tbody id="userTableBody">
43-
4439
</tbody>
4540
</table>
4641
</div>
42+
<script>
43+
document.addEventListener("DOMContentLoaded", function () {
44+
const userTableBody = document.getElementById("userTableBody");
45+
46+
const token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI2NGM2NjU4Nzg5YzVlNDU4NTg3NzdmMWUiLCJpYXQiOjE2OTA3MzgxMTksImV4cCI6MTY5MDgyNDUxOX0.IfBnFvEq9u0g1c5ko8b5hKGA22j4XDCxsTZiqvJG410";
4747

48-
<script src="./js/users.js"></script>
48+
axios.get("http://localhost:8000/api/user/all", {
49+
headers: {
50+
Authorization: `Bearer ${token}`,
51+
},
52+
})
53+
.then((response) => {
54+
const data = response.data;
55+
if (data.users && data.users.length > 0) {
56+
data.users.forEach((user) => {
57+
const row = document.createElement("tr");
58+
const nameCell = document.createElement("td");
59+
nameCell.textContent = user.firstName + " " + user.lastName;
60+
row.appendChild(nameCell);
61+
const emailCell = document.createElement("td");
62+
emailCell.textContent = user.email;
63+
row.appendChild(emailCell);
64+
const cityCell = document.createElement("td");
65+
cityCell.textContent = user.city;
66+
row.appendChild(cityCell);
67+
const interestsCell = document.createElement("td");
68+
interestsCell.textContent = user.interests.join(", ");
69+
row.appendChild(interestsCell);
70+
const eventsCell = document.createElement("td");
71+
const eventNames = user.events.map((event) => event.eventName);
72+
eventsCell.textContent = eventNames.join(", ");
73+
row.appendChild(eventsCell);
74+
userTableBody.appendChild(row);
75+
});
76+
} else {
77+
const row = document.createElement("tr");
78+
const messageCell = document.createElement("td");
79+
messageCell.colSpan = 5;
80+
messageCell.textContent = "No users found";
81+
row.appendChild(messageCell);
82+
userTableBody.appendChild(row);
83+
}
84+
})
85+
.catch((error) => {
86+
console.error("Error fetching users:", error);
87+
const row = document.createElement("tr");
88+
const messageCell = document.createElement("td");
89+
messageCell.colSpan = 5;
90+
messageCell.textContent = "Error fetching users";
91+
row.appendChild(messageCell);
92+
userTableBody.appendChild(row);
93+
});
94+
});
95+
</script>
4996
<script src="./js/nav.js"></script>
5097
</body>
51-
5298
</html>

backend/controllers/user.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,15 @@ let getAllEvents = async (req, res) => {
9292

9393
let registerForEvents = async (req, res) => {
9494
try {
95-
let params = req.params;
95+
let eventId = req.params.id;
96+
let { eventName } = req.body;
97+
let newEvent = {eventId, eventName};
9698
await User.findOneAndUpdate(
9799
{ _id: req.user._id },
98100
{
99-
$addToSet: {
100-
events: params.id,
101-
},
101+
$push: {
102+
events: newEvent
103+
}
102104
}
103105
);
104106
res.status(200).json({ msg: "Event registered successfully" });

backend/models/userModel.js

+38-36
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,44 @@
1-
const mongoose = require('mongoose')
2-
1+
const mongoose = require('mongoose');
32
const userSchema = mongoose.Schema(
43
{
5-
firstName: {
6-
type: String,
7-
required: true
4+
firstName: {
5+
type: String,
6+
required: true,
7+
},
8+
lastName: {
9+
type: String,
10+
required: true,
11+
},
12+
email: {
13+
type: String,
14+
required: true,
15+
unique: true,
16+
},
17+
city: {
18+
type: String,
19+
required: true,
20+
},
21+
password: {
22+
type: String,
23+
required: true,
24+
},
25+
interests: [
26+
{
27+
type: String,
828
},
9-
lastName: {
10-
type: String,
11-
required: true
12-
},
13-
email: {
14-
type: String,
15-
required: true,
16-
unique: true
17-
},
18-
city: {
19-
type: String,
20-
required: true
21-
},
22-
password: {
23-
type: String,
24-
required: true
25-
},
26-
interests: [{
27-
type: String,
28-
}],
29-
events: [
30-
{
31-
type: String
32-
}
33-
]
29+
],
30+
events: [
31+
{
32+
eventId: {type: String, },
33+
eventName: {type: String, },
34+
}
35+
],
3436
},
3537
{
36-
timestamps: true
38+
timestamps: true,
3739
}
38-
)
39-
40-
const User = mongoose.model('User', userSchema);
41-
42-
module.exports = User;
40+
);
41+
42+
const User = mongoose.model('User', userSchema);
43+
44+
module.exports = User;

0 commit comments

Comments
 (0)