First commit
This commit is contained in:
commit
72aa8893f3
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
node_modules
|
||||||
|
.env
|
||||||
|
# Keep environment variables out of version contro
|
55
controllers/Coach.controller.js
Normal file
55
controllers/Coach.controller.js
Normal file
|
@ -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({ status: 500, 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({ status: 500, 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({ status: 500, 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({ status: 500, message: error.message });
|
||||||
|
}
|
||||||
|
};
|
58
controllers/Event.controller.js
Normal file
58
controllers/Event.controller.js
Normal file
|
@ -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({ status: 500, 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({ status: 500, 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({ status: 500, 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({ status: 500, message: error.message });
|
||||||
|
}
|
||||||
|
};
|
80
controllers/HeroImage.controller.js
Normal file
80
controllers/HeroImage.controller.js
Normal file
|
@ -0,0 +1,80 @@
|
||||||
|
import { PrismaClient } from "@prisma/client";
|
||||||
|
import CloudinaryUploadHandler from "../utils/CloudinaryClient.js";
|
||||||
|
|
||||||
|
const prisma = new PrismaClient();
|
||||||
|
|
||||||
|
export const getHeroImages = async (req, res) => {
|
||||||
|
try {
|
||||||
|
const coaches = await prisma.heroImages.findMany();
|
||||||
|
res.status(200).json({ status: 200, data: coaches });
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({ status: 500, message: error.message });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const uploadHeroImages = async (req, res) => {
|
||||||
|
try {
|
||||||
|
const files = req.files;
|
||||||
|
const heroImages = files.map((file) => file.filename);
|
||||||
|
const CloudinaryURLs = await heroImages.map((item) =>
|
||||||
|
CloudinaryUploadHandler(`./uploads/${item}`)
|
||||||
|
);
|
||||||
|
Promise.all(CloudinaryURLs)
|
||||||
|
.then(async (results) => {
|
||||||
|
console.log("RTesults: ", results);
|
||||||
|
const newHeroImages = await prisma.heroImages.create({
|
||||||
|
data: {
|
||||||
|
urls: results,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return res
|
||||||
|
.status(201)
|
||||||
|
.json({ status: 201, data: newHeroImages });
|
||||||
|
})
|
||||||
|
.catch((error) =>
|
||||||
|
res.status(500).json({ status: 500, message: error.message })
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({ status: 500, message: error.message });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const updateHeroImage = async (req, res) => {
|
||||||
|
console.log(`req--->`, req);
|
||||||
|
const { id, index } = req.params;
|
||||||
|
try {
|
||||||
|
const data = await prisma.heroImages.findUnique({ where: { id: id } });
|
||||||
|
console.log(`data---->`, data);
|
||||||
|
let dataArray = data.urls;
|
||||||
|
const fileName = req?.file?.filename;
|
||||||
|
const CloudinaryURL = await CloudinaryUploadHandler(
|
||||||
|
`./uploads/${fileName}`
|
||||||
|
);
|
||||||
|
console.log("Cloud", CloudinaryURL);
|
||||||
|
dataArray[index] = CloudinaryURL;
|
||||||
|
const newHeroImages = await prisma.heroImages.update({
|
||||||
|
where: { id },
|
||||||
|
data: {
|
||||||
|
urls: dataArray,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return res.status(200).json({ status: 200, data: newHeroImages });
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({ status: 500, message: error.message });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const deleteHeroImage = async (req, res) => {
|
||||||
|
const { id } = req.params;
|
||||||
|
try {
|
||||||
|
await prisma.heroImages.delete({
|
||||||
|
where: { id: id },
|
||||||
|
});
|
||||||
|
res.status(200).json({
|
||||||
|
status: 200,
|
||||||
|
message: "Hero Image deleted successfully",
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({ status: 500, message: error.message });
|
||||||
|
}
|
||||||
|
};
|
27
index.js
Normal file
27
index.js
Normal file
|
@ -0,0 +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("/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}`));
|
1158
package-lock.json
generated
Normal file
1158
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load diff
17
package.json
Normal file
17
package.json
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
{
|
||||||
|
"name": "poc-backend",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"type": "module",
|
||||||
|
"scripts": {
|
||||||
|
"dev": "nodemon index.js"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"@prisma/client": "^6.2.1",
|
||||||
|
"cloudinary": "^2.5.1",
|
||||||
|
"cors": "^2.8.5",
|
||||||
|
"dotenv": "^16.4.7",
|
||||||
|
"express": "^4.21.2",
|
||||||
|
"multer": "^1.4.5-lts.1",
|
||||||
|
"prisma": "^6.2.1"
|
||||||
|
}
|
||||||
|
}
|
38
prisma/schema.prisma
Normal file
38
prisma/schema.prisma
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
// This is your Prisma schema file,
|
||||||
|
// learn more about it in the docs: https://pris.ly/d/prisma-schema
|
||||||
|
|
||||||
|
// Looking for ways to speed up your queries, or scale easily with your serverless or edge functions?
|
||||||
|
// Try Prisma Accelerate: https://pris.ly/cli/accelerate-init
|
||||||
|
|
||||||
|
generator client {
|
||||||
|
provider = "prisma-client-js"
|
||||||
|
}
|
||||||
|
|
||||||
|
datasource db {
|
||||||
|
provider = "mongodb"
|
||||||
|
url = env("DATABASE_URL")
|
||||||
|
}
|
||||||
|
|
||||||
|
model HeroImages {
|
||||||
|
id String @id @default(auto()) @map("_id") @db.ObjectId
|
||||||
|
urls String[]
|
||||||
|
createdAt DateTime @default(now())
|
||||||
|
updatedAt DateTime @updatedAt
|
||||||
|
}
|
||||||
|
|
||||||
|
model Coach {
|
||||||
|
id String @id @default(auto()) @map("_id") @db.ObjectId
|
||||||
|
name String
|
||||||
|
title String
|
||||||
|
createdAt DateTime @default(now())
|
||||||
|
updatedAt DateTime @updatedAt
|
||||||
|
}
|
||||||
|
|
||||||
|
model Events {
|
||||||
|
id String @id @default(auto()) @map("_id") @db.ObjectId
|
||||||
|
title String
|
||||||
|
description String
|
||||||
|
date DateTime
|
||||||
|
createdAt DateTime @default(now())
|
||||||
|
updatedAt DateTime @updatedAt
|
||||||
|
}
|
17
routes/Coach.route.js
Normal file
17
routes/Coach.route.js
Normal file
|
@ -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;
|
17
routes/Event.route.js
Normal file
17
routes/Event.route.js
Normal file
|
@ -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;
|
18
routes/HeroImage.route.js
Normal file
18
routes/HeroImage.route.js
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
import express from "express";
|
||||||
|
import MulterClient from "../utils/multer.js";
|
||||||
|
|
||||||
|
import {
|
||||||
|
getHeroImages,
|
||||||
|
uploadHeroImages,
|
||||||
|
updateHeroImage,
|
||||||
|
deleteHeroImage,
|
||||||
|
} from "../controllers/HeroImage.controller.js";
|
||||||
|
|
||||||
|
const HeroImageRouter = express.Router();
|
||||||
|
|
||||||
|
HeroImageRouter.get("/", getHeroImages)
|
||||||
|
.post("/upload", MulterClient.array("heroImages", 10), uploadHeroImages)
|
||||||
|
.put("/:id/:index", MulterClient.single("heroImages"), updateHeroImage)
|
||||||
|
.delete("/:id", deleteHeroImage);
|
||||||
|
|
||||||
|
export default HeroImageRouter;
|
38
utils/CloudinaryClient.js
Normal file
38
utils/CloudinaryClient.js
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
import { v2 as cloudinary } from "cloudinary";
|
||||||
|
|
||||||
|
cloudinary.config({
|
||||||
|
cloud_name: "ds6sqynn2",
|
||||||
|
api_key: "814617551846369",
|
||||||
|
api_secret: "Gx3rvuiGhNDRIu9qBnBvyzM6eew", // Click 'View API Keys' above to copy your API secret
|
||||||
|
});
|
||||||
|
|
||||||
|
// // Upload an image
|
||||||
|
// const uploadResult = await cloudinary.uploader
|
||||||
|
// .upload(
|
||||||
|
// "https://res.cloudinary.com/demo/image/upload/getting-started/shoes.jpg",
|
||||||
|
// {
|
||||||
|
// public_id: "shoesByRIshav",
|
||||||
|
// }
|
||||||
|
// )
|
||||||
|
// .catch((error) => {
|
||||||
|
// console.log(error);
|
||||||
|
// });
|
||||||
|
|
||||||
|
// console.log(uploadResult);
|
||||||
|
// console.log(optimizeUrl);
|
||||||
|
|
||||||
|
export default async function CloudinaryUploadHandler(imagePath) {
|
||||||
|
const options = {
|
||||||
|
use_filename: true,
|
||||||
|
unique_filename: false,
|
||||||
|
overwrite: true,
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
const result = await cloudinary.uploader.upload(imagePath, options);
|
||||||
|
console.log(result.secure_url)
|
||||||
|
return result.secure_url;
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
}
|
||||||
|
}
|
15
utils/multer.js
Normal file
15
utils/multer.js
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
import multer from "multer";
|
||||||
|
|
||||||
|
|
||||||
|
const storage = multer.diskStorage({
|
||||||
|
destination: function (req, file, cb) {
|
||||||
|
cb(null, "./uploads");
|
||||||
|
},
|
||||||
|
filename: function (req, file, cb) {
|
||||||
|
cb(null, (Date.now() + "-" + file.originalname).replaceAll(' ','-'));
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const MulterClient = multer({ storage: storage });
|
||||||
|
|
||||||
|
export default MulterClient;
|
Loading…
Reference in a new issue