Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
1 changed file
with
34 additions
and
97 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,113 +1,50 @@ | ||
const express = require('express'); | ||
const router = express.Router() | ||
const Club = require('../models/club'); | ||
import express from "express"; | ||
import { | ||
createClub, | ||
getAllClub, | ||
updateClub, | ||
getClub, | ||
deleteClubs, | ||
countByType, | ||
featuredClub, | ||
searchQ, | ||
registerClub, | ||
} from "../controllers/club.js"; | ||
import { verifyAdmin, verifyUser} from "../utils/verifyToken.js"; | ||
|
||
|
||
|
||
|
||
|
||
|
||
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}) | ||
} | ||
}) | ||
|
||
|
||
|
||
|
||
|
||
const router = express.Router(); | ||
|
||
router.patch('/:id',getClubdetails, async(req,res) => { | ||
if(req.body.clubname != null) { | ||
res.clubdetails.clubname = req.body.clubname | ||
} | ||
//CRUD | ||
|
||
if(req.body.email != null) { | ||
res.clubdetails.email = req.body.email | ||
} | ||
//Create | ||
router.post('/create-club',verifyAdmin, createClub) | ||
|
||
if(req.body.description != null) { | ||
res.clubdetails.description = req.body.description | ||
} | ||
//update | ||
router.put("/update/:id", verifyAdmin, updateClub); | ||
|
||
if(req.body.image != null) { | ||
res.clubdetails.image = req.body.image | ||
} | ||
//update user | ||
router.put("/update-user/:id",verifyUser,registerClub) | ||
//delete | ||
router.delete("/delete/:id", verifyAdmin, deleteClubs); | ||
//get | ||
router.get("/get/:id", getClub); | ||
//get list | ||
|
||
if(req.body.location != null) { | ||
res.clubdetails.location= req.body.location | ||
} | ||
router.get("/getAll", getAllClub); | ||
|
||
if(req.body.registrationtime != null) { | ||
res.clubdetails.registrationtime = req.body.registrationtime | ||
} | ||
|
||
if(req.body.type != null) { | ||
res.clubdetails.type = req.body.type | ||
} | ||
router.get("/countByClubs", getAllClub); | ||
router.get("/countByType", countByType); | ||
|
||
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}) | ||
} | ||
}) | ||
//Featured Club | ||
|
||
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() | ||
} | ||
router.get("/featured", featuredClub); | ||
|
||
|
||
//search | ||
|
||
router.get("/search", searchQ); | ||
|
||
module.exports = router | ||
export default router; |