Skip to content
Permalink
4828ba6c13
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
109 lines (82 sloc) 2.67 KB
const express = require('express');
const router = express.Router()
const Event = require('../models/event');
router.post('/', async(req,res)=>{
// console.log("hii");
console.log("called")
const events=new Event({
eventname:req.body.eventname,
clubname:req.body.clubname,
description:req.body.description,
image:req.body.image,
location:req.body.location,
time:req.body. time,
})
try{
const newEvents=await events.save()
console.log(newEvents)
res.status(201).json(newEvents)
}
catch(err){
console.log("error")
res.status(201).json({message:err.message})
}
})
router.get('/',async(req,res) => {
try {
const eventdetails = await Event.find()
res.json(eventdetails)
console.log(eventdetails)
} catch(err) {
res.status(500).json({message: err.message})
}
})
router.patch('/:id',getEventdetails, async(req,res) => {
if(req.body.eventname != null) {
res.eventdetails.eventname = req.body.eventname
}
if(req.body.description != null) {
res.eventdetails.description = req.body.description
}
if(req.body.image != null) {
res.eventdetails.image = req.body.image
}
if(req.body.location != null) {
res.eventdetails.location= req.body.location
}
if(req.body.time != null) {
res.eventdetails.time = req.body.time
}
try {
const updatedEventdetails = await res.eventdetails.save()
res.json(updatedEventdetails)
} catch(err) {
res.status(400).json({message: err.message})
}
})
router.delete('/:id',getEventdetails, async(req,res) => {
try {
await res.eventdetails.remove()
res.json({message: "Deleted event"})
}catch {
return res.status(500).json({message: err.message})
}
})
router.get('/:id', getEventdetails, (req,res) => {
console.log(res.eventdetails.eventname)
res.send(res.eventdetails)
})
async function getEventdetails(req,res,next) {
let eventdetails
try {
eventdetails = await Event.findById(req.params.id)
if(eventdetails == null){
return res.status(404).json({message: "cannot find event"})
}
} catch (err){
return res.status(500).json({message: err.message})
}
res.eventdetails = eventdetails
next()
}
module.exports = router