Added: event and coach routes and controllers
This commit is contained in:
parent
9cee08033c
commit
daaec59b7c
|
@ -0,0 +1,55 @@
|
|||
import { PrismaClient } from "@prisma/client";
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
export const getCoaches = async (req, res) => {
|
||||
try {
|
||||
const coaches = await prisma.coach.findMany();
|
||||
res.status(200).json({ status: 200, data: coaches });
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
export const createCoach = async (req, res) => {
|
||||
try {
|
||||
const newEvent = await prisma.coach.create({
|
||||
data: {
|
||||
...req?.body,
|
||||
},
|
||||
});
|
||||
res.status(201).json({ status: 201, data: newEvent });
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
export const updateCoach = async (req, res) => {
|
||||
const { id } = req.params;
|
||||
try {
|
||||
const updatedEvent = await prisma.coach.update({
|
||||
where: { id: id },
|
||||
data: {
|
||||
...req?.body,
|
||||
},
|
||||
});
|
||||
res.status(200).json({ status: 200, data: updatedEvent });
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
export const deleteCoach = async (req, res) => {
|
||||
const { id } = req.params;
|
||||
try {
|
||||
await prisma.coach.delete({
|
||||
where: { id: id },
|
||||
});
|
||||
res.status(200).json({
|
||||
status: 200,
|
||||
message: "Event deleted successfully",
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: error.message });
|
||||
}
|
||||
};
|
|
@ -0,0 +1,58 @@
|
|||
import { PrismaClient } from "@prisma/client";
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
export const getEvents = async (req, res) => {
|
||||
try {
|
||||
const events = await prisma.events.findMany();
|
||||
res.status(200).json({ status: 200, data: events });
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
export const createEvent = async (req, res) => {
|
||||
const { title, description, date } = req.body;
|
||||
try {
|
||||
const newEvent = await prisma.events.create({
|
||||
data: {
|
||||
title,
|
||||
description,
|
||||
date,
|
||||
},
|
||||
});
|
||||
res.status(201).json({ status: 201, data: newEvent });
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
export const updateEvent = async (req, res) => {
|
||||
const { id } = req.params;
|
||||
try {
|
||||
const updatedEvent = await prisma.events.update({
|
||||
where: { id: id },
|
||||
data: {
|
||||
...req?.body,
|
||||
},
|
||||
});
|
||||
res.status(200).json({ status: 200, data: updatedEvent });
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
export const deleteEvent = async (req, res) => {
|
||||
const { id } = req.params;
|
||||
try {
|
||||
await prisma.events.delete({
|
||||
where: { id: id },
|
||||
});
|
||||
res.status(200).json({
|
||||
status: 200,
|
||||
message: "Event deleted successfully",
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: error.message });
|
||||
}
|
||||
};
|
|
@ -1,14 +1,27 @@
|
|||
config();
|
||||
import { config } from "dotenv";
|
||||
import express from "express";
|
||||
import cors from "cors";
|
||||
|
||||
import EventRouter from "./routes/Event.route.js";
|
||||
import CoachRouter from "./routes/Coach.route.js";
|
||||
import HeroImageRouter from "./routes/HeroImage.route.js";
|
||||
|
||||
const PORT = process.env.PORT;
|
||||
|
||||
const app = express();
|
||||
|
||||
app.use(express.json());
|
||||
app.use(express.urlencoded({ extended: true }));
|
||||
app.use(cors());
|
||||
|
||||
// Test endpoint
|
||||
app.get("/", (req, res) =>
|
||||
app.get("/api", (req, res) =>
|
||||
res.status(200).json({ message: "API is working!" })
|
||||
);
|
||||
|
||||
app.use("/api/coach", CoachRouter);
|
||||
app.use("/api/event", EventRouter);
|
||||
app.use("/api/hero-image", HeroImageRouter);
|
||||
|
||||
app.listen(PORT, () => console.log(`Server started on port = ${PORT}`));
|
||||
|
|
1
backend/package-lock.json
generated
1
backend/package-lock.json
generated
|
@ -9,7 +9,6 @@
|
|||
"version": "1.0.0",
|
||||
"dependencies": {
|
||||
"@prisma/client": "^6.2.1",
|
||||
"body-parser": "^1.20.3",
|
||||
"cors": "^2.8.5",
|
||||
"dotenv": "^16.4.7",
|
||||
"express": "^4.21.2",
|
||||
|
|
|
@ -7,7 +7,6 @@
|
|||
},
|
||||
"dependencies": {
|
||||
"@prisma/client": "^6.2.1",
|
||||
"body-parser": "^1.20.3",
|
||||
"cors": "^2.8.5",
|
||||
"dotenv": "^16.4.7",
|
||||
"express": "^4.21.2",
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
import express from "express";
|
||||
|
||||
import {
|
||||
getCoaches,
|
||||
createCoach,
|
||||
updateCoach,
|
||||
deleteCoach,
|
||||
} from "../controllers/Coach.controller.js";
|
||||
|
||||
const CoachRouter = express.Router();
|
||||
|
||||
CoachRouter.get("/", getCoaches)
|
||||
.post("/", createCoach)
|
||||
.put("/:id", updateCoach)
|
||||
.delete("/:id", deleteCoach);
|
||||
|
||||
export default CoachRouter;
|
|
@ -0,0 +1,17 @@
|
|||
import express from "express";
|
||||
|
||||
import {
|
||||
getEvents,
|
||||
createEvent,
|
||||
updateEvent,
|
||||
deleteEvent,
|
||||
} from "../controllers/Event.controller.js";
|
||||
|
||||
const EventRouter = express.Router();
|
||||
|
||||
EventRouter.get("/", getEvents)
|
||||
.post("/", createEvent)
|
||||
.put("/:id", updateEvent)
|
||||
.delete("/:id", deleteEvent);
|
||||
|
||||
export default EventRouter;
|
|
@ -0,0 +1,7 @@
|
|||
import express from "express";
|
||||
|
||||
const HeroImageRouter = express.Router();
|
||||
|
||||
HeroImageRouter.get("/");
|
||||
|
||||
export default HeroImageRouter;
|
Loading…
Reference in a new issue