167 lines
3.4 KiB
JavaScript

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
// =========================
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);
});
let users = [];
// CREATE USER
app.post("/api/user", (req, res) => {
const { username } = req.body;
if (!username) {
return res.status(400).json({ message: "Username required" });
}
if (users.find(u => u.username === username)) {
return res.status(400).json({ message: "User exists" });
}
const password = "1234";
const user = { username, password };
users.push(user);
res.json({ name: username, password });
});
// LOGIN
app.get("/api/user", (req, res) => {
const username = req.header("X-Username");
const password = req.header("X-Password");
const user = users.find(
u => u.username === username && u.password === password
);
if (!user) return res.sendStatus(401);
res.sendStatus(201);
});
// =========================
// START SERVER
// =========================
app.listen(3000, () => {
console.log("Server running on http://localhost:3000");
});