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/clubdetails.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
113 lines (83 sloc)
2.49 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 Club = require('../models/club'); | |
router.post('/',async(req,res)=>{ | |
const club=new Club({ | |
name:req.body.name, | |
email:req.body.email, | |
photo:req.body.photo, | |
location:req.body.location, | |
type:req.body.type, | |
president_mail:req.body. president_mail | |
}) | |
try{ | |
const newClub=await club.save() | |
res.status(201).json(newClub) | |
} | |
catch(err){ | |
res.status(201).json({message:err.message}) | |
} | |
}) | |
router.get('/:id', getClubdetails, (req,res) => { | |
// console.log(res.clubdetails.clubname) | |
res.send(res.clubdetails.clubname) | |
}) | |
router.get('/',async(req,res) => { | |
try { | |
const clubdetails = await Club.find() | |
res.json(clubdetails) | |
} catch(err) { | |
res.status(500).json({message: err.message}) | |
} | |
}) | |
router.patch('/:id',getClubdetails, async(req,res) => { | |
if(req.body.clubname != null) { | |
res.clubdetails.clubname = req.body.clubname | |
} | |
if(req.body.email != null) { | |
res.clubdetails.email = req.body.email | |
} | |
if(req.body.description != null) { | |
res.clubdetails.description = req.body.description | |
} | |
if(req.body.image != null) { | |
res.clubdetails.image = req.body.image | |
} | |
if(req.body.location != null) { | |
res.clubdetails.location= req.body.location | |
} | |
if(req.body.registrationtime != null) { | |
res.clubdetails.registrationtime = req.body.registrationtime | |
} | |
if(req.body.type != null) { | |
res.clubdetails.type = req.body.type | |
} | |
try { | |
const updatedClubdetails = await res.clubdetails.save() | |
res.json(updatedClubdetails) | |
} catch(err) { | |
res.status(400).json({message: err.message}) | |
} | |
}) | |
router.delete('/:id',getClubdetails, async(req,res) => { | |
try { | |
await res.clubdetails.remove() | |
res.json({message: "Deleted club"}) | |
}catch { | |
return res.status(500).json({message: err.message}) | |
} | |
}) | |
async function getClubdetails(req,res,next) { | |
let clubdetails | |
try { | |
clubdetails = await Club.findById(req.params.id) | |
if(clubdetails == null){ | |
return res.status(404).json({message: "cannot find club"}) | |
} | |
} catch (err){ | |
return res.status(500).json({message: err.message}) | |
} | |
res.clubdetails = clubdetails | |
next() | |
} | |
module.exports = router |