Skip to content

Commit

Permalink
Admin Panel
Browse files Browse the repository at this point in the history
  • Loading branch information
samar12-rad committed Oct 10, 2024
1 parent 0d9bc24 commit 3e5e3d3
Show file tree
Hide file tree
Showing 6 changed files with 323 additions and 18 deletions.
13 changes: 3 additions & 10 deletions backend/controller/admin.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async function createAdmin(req, res) {
if (!validation.success) {
return res.status(400).json({ error: validation.error.errors });
}
const existingAdmin = await Admin.findOne({ email: req.body.email });

const existingAdmin = await Admin.findOne({ email: req.body.email });
if (existingAdmin) {
return res.status(409).json({ error: "Email is already registered" });
Expand All @@ -43,15 +43,8 @@ async function createAdmin(req, res) {
}

async function loginAdmin(req, res) {
const adminLoginSchema = 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 = adminLoginSchema.safeParse(req.body);
if (!validation.success) {
return res.status(400).json({ error: validation.error.errors });
}


const adminLoginSchema = z.object({
email: z.string().email("Invalid email address"),
password: z.string().min(6, "Password must be at least 6 characters long"),
Expand Down
26 changes: 23 additions & 3 deletions backend/controller/event.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ 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;
const { title, description, date, time, ageRange, image } = req.body;

// Input validation
if (!title || !description || !date || !time) {
Expand All @@ -16,7 +16,7 @@ const createEvent = async (req, res) => {
description,
date,
time,
age,
ageRange,
image,
});

Expand All @@ -34,6 +34,26 @@ const createEvent = async (req, res) => {
}
};

const deleteEvent = async (req, res) => {
try {
const eventId = req.query.id; // Change to req.query.id
console.log(eventId);
const event = await Event.findById(eventId); // Remove the object wrapper

if (!event) {
return res.status(404).json({ message: "Event not found" });
}

await Event.findByIdAndDelete(eventId);
res.status(200).json({message: "Event deleted "});
} catch (error) {
logger.error("Error deleting event:", error);
res.status(500).json({ message: "Internal server error" });
}
};



const getEvents = async (req, res) => {
try {
const events = await Event.find();
Expand All @@ -49,4 +69,4 @@ const getEvents = async (req, res) => {
}
};

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

const mongoose = require("mongoose");
const { string } = require("zod");

// Define the Event schema
const eventSchema = new mongoose.Schema({
Expand All @@ -14,21 +15,21 @@ const eventSchema = new mongoose.Schema({
required: true,
},
date: {
type: Date,
type: String,
required: true,
},
time: {
type: Date,
type: String,
required: true,
},
ageRange: {
type: String,
type: String,
required: true,
enum: ["0-12", "13-17", "18+", "All Ages"],
},
image: {
type: String,
required: true,
default: "https://imgur.com/4fSKUI4.jpg",
validate: {
validator: function (v) {
return /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/.test(
Expand Down
3 changes: 2 additions & 1 deletion backend/routes/eventRouter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const express = require("express");
const logger = require("../config/logger");
const { createEvent, getEvents } = require("../controller/event.controller");
const { createEvent, getEvents, deleteEvent } = require("../controller/event.controller");

const router = express.Router();

Expand All @@ -22,5 +22,6 @@ router.get("/", async (req, res) => {
});
router.post("/create", createEvent);
router.get("/all", getEvents);
router.get("/delete", deleteEvent);

module.exports = router;
Loading

0 comments on commit 3e5e3d3

Please sign in to comment.