Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/17arindam/PlayCafe into log…
Browse files Browse the repository at this point in the history
…out_modal
  • Loading branch information
17arindam committed Oct 9, 2024
2 parents edc3c6a + 180babd commit 96da1cf
Show file tree
Hide file tree
Showing 14 changed files with 669 additions and 194 deletions.
38 changes: 30 additions & 8 deletions backend/controller/customer.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ const bcrypt = require("bcrypt");
const { z } = require("zod");
const Customer = require("../models/customer.model");



// Define the schema
const customerSchema = z.object({
name: z.string().min(1, "Name is required"),
Expand All @@ -19,10 +17,10 @@ async function createCustomer(req, res) {
return res.status(400).json({ error: validation.error.errors });
}

const existingCustomer = await Customer.findOne({ email: req.body.email });
if (existingCustomer) {
return res.status(400).json({ error: "Email is already registered" });
}
const existingCustomer = await Customer.findOne({ email: req.body.email });
if (existingCustomer) {
return res.status(400).json({ error: "Email is already registered" });
}

try {
const hashedPassword = await bcrypt.hash(req.body.password, 10);
Expand Down Expand Up @@ -67,9 +65,33 @@ async function loginCustomer(req, res) {
}
}

async function resetPassword(req, res) {
const customerResetPasswordSchema = z.object({
email: z.string().email("Invalid email address"),
password: z.string().min(6, "Password must be at least 6 characters long"),
});
// Validate the request body
const validation = customerResetPasswordSchema.safeParse(req.body);
if (!validation.success) {
return res.status(400).json({ error: validation.error.errors });
}

try {
const customer = await Customer.findOne({ email: req.body.email });
if (!customer) {
return res.status(401).json({ error: "Invalid email" });
}
const hashedPassword = await bcrypt.hash(req.body.password, 10);
customer.password = hashedPassword;
await customer.save();
res.json({ message: "Password reset successful" });
} catch (error) {
res.status(500).json({ error: "Internal server error" });
}
}

module.exports = {
createCustomer,
loginCustomer
}
loginCustomer,
resetPassword,
};
52 changes: 52 additions & 0 deletions backend/controller/event.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const logger = require("../config/logger");
const Event = require("../models/events.model");

// Create a new event
const createEvent = async (req, res) => {
try {
const { title, description, date, time, age, image } = req.body;

// Input validation
if (!title || !description || !date || !time) {
return res.status(400).json({ message: "Missing required fields" });
}

const newEvent = new Event({
title,
description,
date,
time,
age,
image,
});

const savedEvent = await newEvent.save();
res.status(201).json(savedEvent);
} catch (error) {
logger.error("Error creating event:", error);
if (error.name === "ValidationError") {
res
.status(400)
.json({ message: "Invalid input data", details: error.errors });
} else {
res.status(500).json({ message: "Internal server error" });
}
}
};

const getEvents = async (req, res) => {
try {
const events = await Event.find();

if (events.length === 0) {
return res.status(204).send(); // No Content
}

res.status(200).json(events);
} catch (error) {
logger.error("Error retrieving events:", error);
res.status(500).json({ message: "Internal server error" });
}
};

module.exports = { createEvent, getEvents };
45 changes: 45 additions & 0 deletions backend/models/events.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// models/Event.js

const mongoose = require("mongoose");

// Define the Event schema
const eventSchema = new mongoose.Schema({
title: {
type: String,
required: true,
trim: true,
},
description: {
type: String,
required: true,
},
date: {
type: Date,
required: true,
},
time: {
type: Date,
required: true,
},
ageRange: {
type: String,
required: true,
enum: ["0-12", "13-17", "18+", "All Ages"],
},
image: {
type: String,
required: true,
validate: {
validator: function (v) {
return /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/.test(
v
);
},
message: (props) => `${props.value} is not a valid URL!`,
},
},
});
// Create the Event model
const Event = mongoose.model("Event", eventSchema);

module.exports = Event;
1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.21.0",
"jsonwebtoken": "^9.0.2",
"mongoose": "^8.7.0",
"nodemailer": "^6.9.15",
"validator": "^13.12.0",
Expand Down
16 changes: 10 additions & 6 deletions backend/routes/customerRouter.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
const express = require("express");
const { loginCustomer, createCustomer } = require("../controller/customer.controller");
const {
loginCustomer,
createCustomer,
resetPassword,
} = require("../controller/customer.controller");
const router = express.Router();
require("dotenv").config();



router.get("/", (req, res) => {
res.json({
message: "Welcome to the User API!",
version: "1.0.0",
endpoints: {
login: "/login",
register: "/register",
login: "/login",
register: "/register",
},
documentation: "https://api-docs-url.com",});
documentation: "https://api-docs-url.com",
});
});

router.post("/register", createCustomer);
router.post("/login", loginCustomer);
router.post("/reset-password", resetPassword);

module.exports = router;
26 changes: 26 additions & 0 deletions backend/routes/eventRouter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const express = require("express");
const logger = require("../config/logger");
const { createEvent, getEvents } = require("../controller/event.controller");

const router = express.Router();

router.get("/", async (req, res) => {
try {
res.json({
message: "Welcome to the event API!",
version: "1.0.0",
endpoints: {
CreateEvent: "/event/create",
GetEvents: "/event/all",
},
documentation: "https://api-docs-url.com",
});
} catch (error) {
logger.error("Error in /event route:", error); // Log the error
res.status(500).json({ error: "Internal server error" });
}
});
router.post("/create", createEvent);
router.get("/all", getEvents);

module.exports = router;
16 changes: 15 additions & 1 deletion backend/routes/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const express = require("express");
const Reservation = require("../models/reservation.model");
const logger = require("../config/logger"); // Import your Winston logger

const router = express.Router();
Expand All @@ -19,6 +18,20 @@ try {
};
}

let eventRouter;
try {
eventRouter = require("./eventRouter");
} catch (error) {
logger.error("Error loading eventRouter:", error); // Log the error with Winston
eventRouter = (req, res) => {
res
.status(500)
.json({ error: "Event functionality is currently unavailable" });
};
}




router.get("/", (req, res) => {
res.json({
Expand All @@ -33,6 +46,7 @@ router.get("/", (req, res) => {
});


router.use("/event", eventRouter);
router.use("/admin", require("./adminRouter"));
router.use("/feedback", feedbackRouter);
router.use("/user", require("./customerRouter"));
Expand Down
Binary file added frontend/src/assets/login.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 96da1cf

Please sign in to comment.