Permalink
Cannot retrieve contributors at this time
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?
Sports_Club/eventdetails.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
109 lines (82 sloc)
2.67 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |