Added backend server
This commit is contained in:
1 parent
91c2ab054d
commit
085e7ffd73
612 files changed
+67073
-3
No files matched your search
Whitespace-only changes.
@@ -0,0 +1,129 @@
|
||||
import express from "express";
|
||||
import cors from "cors";
|
||||
import dotenv from "dotenv";
|
||||
dotenv.config();
|
||||
|
||||
const app = express();
|
||||
|
||||
// Middleware
|
||||
app.use(cors());
|
||||
app.use(express.json());
|
||||
|
||||
// =========================
|
||||
// ROOT (optional)
|
||||
// =========================
|
||||
app.get("/", (req, res) => {
|
||||
res.send("Encore API is running");
|
||||
});
|
||||
|
||||
// =========================
|
||||
// TICKETMASTER EVENTS (PROXY)
|
||||
// =========================
|
||||
const TM_API_KEY = process.env.TM_API_KEY;
|
||||
|
||||
app.get("/api/events", async (req, res) => {
|
||||
|
||||
const city = req.query.city;
|
||||
|
||||
if (!city) {
|
||||
return res.status(400).json({ message: "City is required" });
|
||||
}
|
||||
|
||||
try {
|
||||
const url = `https://app.ticketmaster.com/discovery/v2/events.json?apikey=${TM_API_KEY}&city=${city}`;
|
||||
|
||||
const response = await fetch(url);
|
||||
const data = await response.json();
|
||||
|
||||
res.json(data);
|
||||
|
||||
} catch (error) {
|
||||
console.error("Event fetch error:", error);
|
||||
res.status(500).json({ message: "Error fetching events" });
|
||||
}
|
||||
});
|
||||
|
||||
// =========================
|
||||
// INVITATIONS SYSTEM
|
||||
// =========================
|
||||
let invitations = [];
|
||||
let idCounter = 1;
|
||||
|
||||
// SEND INVITATION
|
||||
app.post("/api/invitation", (req, res) => {
|
||||
|
||||
if (!req.body) {
|
||||
return res.status(400).json({ message: "Missing request body" });
|
||||
}
|
||||
|
||||
const fromUser = req.header("X-Username");
|
||||
const { toUser, eventId, eventName } = req.body;
|
||||
|
||||
if (!fromUser || !toUser) {
|
||||
return res.status(400).json({ message: "Missing users" });
|
||||
}
|
||||
|
||||
const invitation = {
|
||||
id: idCounter++,
|
||||
fromUser,
|
||||
toUser,
|
||||
eventId,
|
||||
eventName,
|
||||
status: "pending"
|
||||
};
|
||||
|
||||
invitations.push(invitation);
|
||||
|
||||
res.json(invitation);
|
||||
});
|
||||
|
||||
// GET INVITATIONS FOR USER
|
||||
app.get("/api/invitation", (req, res) => {
|
||||
|
||||
const user = req.header("X-Username");
|
||||
|
||||
if (!user) {
|
||||
return res.status(400).json({ message: "Missing username" });
|
||||
}
|
||||
|
||||
const userInvitations = invitations.filter(
|
||||
inv => inv.toUser === user
|
||||
);
|
||||
|
||||
res.json(userInvitations);
|
||||
});
|
||||
|
||||
// ACCEPT INVITATION
|
||||
app.post("/api/invitation/:id/accept", (req, res) => {
|
||||
|
||||
const inv = invitations.find(i => i.id == req.params.id);
|
||||
|
||||
if (!inv) {
|
||||
return res.status(404).json({ message: "Invitation not found" });
|
||||
}
|
||||
|
||||
inv.status = "accepted";
|
||||
|
||||
res.json(inv);
|
||||
});
|
||||
|
||||
// DECLINE INVITATION
|
||||
app.post("/api/invitation/:id/decline", (req, res) => {
|
||||
|
||||
const inv = invitations.find(i => i.id == req.params.id);
|
||||
|
||||
if (!inv) {
|
||||
return res.status(404).json({ message: "Invitation not found" });
|
||||
}
|
||||
|
||||
inv.status = "declined";
|
||||
|
||||
res.json(inv);
|
||||
});
|
||||
|
||||
// =========================
|
||||
// START SERVER
|
||||
// =========================
|
||||
app.listen(3000, () => {
|
||||
console.log("Server running on http://localhost:3000");
|
||||
});
|
||||
Whitespace-only changes.
Reference in new issue
Block a user