Skip to content

Commit

Permalink
coderabbit changes
Browse files Browse the repository at this point in the history
  • Loading branch information
samar12-rad committed Oct 7, 2024
1 parent 0eceeac commit 827aae6
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 30 deletions.
41 changes: 30 additions & 11 deletions backend/controller/event.controller.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,51 @@
const logger = require("../config/logger");
const Event = require("../models/events.model");

// Create a new event
const createEvent = async (req, res) => {
try {
const event = req.body;
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: event.title,
description: event.description,
date: event.date,
time: event.time,
age: event.age,
image: event.image,
title,
description,
date,
time,
age,
image,
});

await newEvent.save();
res.status(201).json(newEvent);
const savedEvent = await newEvent.save();
res.status(201).json(savedEvent);
} catch (error) {
res.status(400).json({ message: error.message });
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) {
res.status(404).json({ message: error.message });
console.error("Error retrieving events:", error);
res.status(500).json({ message: "Internal server error" });
}
};

Expand Down
17 changes: 13 additions & 4 deletions backend/models/events.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,38 @@ const eventSchema = new mongoose.Schema({
title: {
type: String,
required: true,
trim: true,
},
description: {
type: String,
required: true,
},
date: {
type: String,
type: Date,
required: true,
},
time: {
type: String,
type: Date,
required: true,
},
age: {
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);

Expand Down
18 changes: 17 additions & 1 deletion backend/routes/eventRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,23 @@ 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("/", getEvents);
router.get("/all", getEvents);

module.exports = router;
2 changes: 1 addition & 1 deletion backend/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ try {
eventRouter = require("./eventRouter");
} catch (error) {
logger.error("Error loading eventRouter:", error); // Log the error with Winston
feedbackRouter = (req, res) => {
eventRouter = (req, res) => {
res
.status(500)
.json({ error: "Event functionality is currently unavailable" });
Expand Down
33 changes: 20 additions & 13 deletions frontend/src/components/Pages/Event.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,27 @@ const months = [
];
export default function Event() {
const [events, setEvents] = useState([]);

const [error, setError] = useState(null);
useEffect(() => {
const fetchData = async () => {
try {
const response = await fetch("http://localhost:3000/api/event", {
method: "GET",
headers: {
"Content-Type": "application/json",
},
});
const response = await fetch(
`${import.meta.env.VITE_BACKEND_URL}/api/event/all`,
{
method: "GET",
headers: {
"Content-Type": "application/json",
},
}
);

const data = await response.json();
console.log(data);
setEvents(data);
console.log(events);
// You can update the state with the fetched data here
} catch (error) {
setError(error);
console.error("Error fetching events:", error);
}
};
Expand Down Expand Up @@ -84,7 +89,11 @@ export default function Event() {
ease: "power1.inOut",
delay: 1,
});
});
return () => {
tl.kill();
splitText.revert();
};
}, []);

useEffect(() => {
new Splide(".splide", {
Expand Down Expand Up @@ -141,7 +150,7 @@ export default function Event() {
{Array(firstDayOfMonth)
.fill(null)
.map((_, i) => (
<div key={`empty-${i}`} className="p-2"></div>
<div key={{ i }} className="p-2"></div>
))}
{dates.map((day) => (
<div
Expand Down Expand Up @@ -195,12 +204,10 @@ export default function Event() {
</div>
</div>
</div>
<section
className="w-full py-12 md:py-24 lg:py-16 flex justify-center"
id="event"
>
<section className="w-full py-12 md:py-24 lg:py-16 flex justify-center">
<div className="container grid grid-cols-1 gap-8 px-4 md:grid-cols-2 lg:grid-cols-1 md:px-6">
<div className="event-list">
{error && <p className="text-red-500">{error}</p>}
{events.map((event) => (
<div
key={event._id}
Expand Down

0 comments on commit 827aae6

Please sign in to comment.