From 4828ba6c13eb4886539144a4e7c0e48facef0b33 Mon Sep 17 00:00:00 2001 From: "Joysy (joysyj)" Date: Tue, 1 Nov 2022 20:27:41 +0530 Subject: [PATCH] route testing commit --- authentication.js | 77 +++++++++++++++++++++++++++++++ clubdetails.js | 113 ++++++++++++++++++++++++++++++++++++++++++++++ eventdetails.js | 109 ++++++++++++++++++++++++++++++++++++++++++++ login.js | 53 ++++++++++++++++++++++ route.rest | 111 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 463 insertions(+) create mode 100644 authentication.js create mode 100644 clubdetails.js create mode 100644 eventdetails.js create mode 100644 login.js create mode 100644 route.rest diff --git a/authentication.js b/authentication.js new file mode 100644 index 0000000..2ec7895 --- /dev/null +++ b/authentication.js @@ -0,0 +1,77 @@ +const express = require('express'); + + +const router = express.Router() +const User = require('../models/user') + + +router.get('/:id',getUsers,(req,res) => { + + + console.log(res.data) + res.send(res.data); + }) + + +router.post('/',async(req,res)=>{ + console.log(req.body.name); + const user=new User({ + + email:req.body.email, + student_id:req.body.student_id, + name:req.body.name, + password:req.body.password, + type:req.body.type + }) + try{ + const newUser=await user.save() + res.status(201).json(newUser) + } + catch(err){ + res.status(201).json({message:err.message}) + } +}) + +async function getUsers(req,res,next){ + let data; + try{ + + data=await User.findById(req.params.id); + + if(data==null) + { + + return res.status(404); + } + } + catch(err){ + + return res.status(500); + } + res.data=data; + next(); +} + +router.get('/:id', getStudent, (req,res) => { + + console.log(res.studentdetails.email) + res.send(res.studentdetails.name) + }) + +async function getStudent(req,res,next) { + let studentdetails + try { + studentdetails = await Club.findById(req.params.id) + if(studentdetails == null){ + return res.status(404).json({message: "cannot find club"}) + } + } catch (err){ + return res.status(500).json({message: err.message}) + } + res.studentdetails = studentdetails + next() +} + + + +module.exports = router \ No newline at end of file diff --git a/clubdetails.js b/clubdetails.js new file mode 100644 index 0000000..4d547bf --- /dev/null +++ b/clubdetails.js @@ -0,0 +1,113 @@ +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 \ No newline at end of file diff --git a/eventdetails.js b/eventdetails.js new file mode 100644 index 0000000..d9e7003 --- /dev/null +++ b/eventdetails.js @@ -0,0 +1,109 @@ +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 diff --git a/login.js b/login.js new file mode 100644 index 0000000..0a499a0 --- /dev/null +++ b/login.js @@ -0,0 +1,53 @@ +const express = require('express'); + + +const router = express.Router() +const User = require('../models/user') + + + +router.post('/',async(req,res)=>{ + + + + var email = req.body.email; + var password = req.body.password; + const username=await User.findOne({email:email + ,password:password}); + console.log(username); + if(username==null) + { + return res.status(400).json("False") + } + try{ + + res.status(200).json(username) + } + catch(err){ + res.status(201).json({message:err.message}) + } + + +}) + +// router.post('/',async(req,res)=>{ +// console.log(req.body.name); +// const user=new User({ + +// name:req.body.name, +// email:req.body.email, +// password:req.body.password, +// type:req.body.type +// }) +// try{ +// const newUser=await user.save() +// res.status(201).json(newUser) +// } +// catch(err){ +// res.status(201).json({message:err.message}) +// } +// }) + + + +module.exports = router \ No newline at end of file diff --git a/route.rest b/route.rest new file mode 100644 index 0000000..d4dc851 --- /dev/null +++ b/route.rest @@ -0,0 +1,111 @@ +//signup + +POST http://localhost:3002/signup +Content-Type: application/json + +{ + + "email" :"manii@gmail.com", + "student_id":"225685433", + "name" : "Mani", + "password":"12345" + +} + +### +GET http://localhost:3002/signup/6354410be87c3ba994c4b951 + +### + +//login +POST http://localhost:3002/login +Content-Type: application/json + +{ + "email" :"rijin@gmail.com", + "password":"12345" +} + +### +//adding club +POST http://localhost:3002/addclub +Content-Type: application/json + +{ + "name": "batminton champions", + "email":"batminton@gmail.com", + "description":"club for football", + "image":"https://www.google.com/url?sa=i&url=https%3A%2F%2Funsplash.com%2Fs%2Fphotos%2Fcricket&psig=AOvVaw28zMIxEJ4s7EYUq58O2c3g&ust=1665577888469000&source=images&cd=vfe&ved=0CAsQjRxqFwoTCLjk0qSX2PoCFQAAAAAdAAAAABAE", + "location":"coventry", + "registrationtime":"11:00pm", + "type":"cricket", + "president_mail": "batmintonpresident@gmail.com" + + + +} +### + +//getting single club +GET http://localhost:3002/addclub/6359145d47a216fbcf7f8cb4 + +### +//getting single club +GET http://localhost:3002/signup/6353906dd5dcfb977afa9bd7 + +### + +//deleting club +DELETE http://localhost:3002/addclub/6351b26c6a3c4de7769528fd + + +### + +//updating club +PATCH http://localhost:3002/addclub/63518079ff2f4e0b74eda4bf +Content-Type: application/json + +{ +"clubname":"football masters" +} +### +//getting entire club +GET http://localhost:3002/addclub +### +//adding events +POST http://localhost:3002/addevent +Content-Type: application/json + +{ + "eventname": "football match", + "description":"football event", + "image":"https://www.google.com/url?sa=i&url=https%3A%2F%2Funsplash.com%2Fs%2Fphotos%2Fcricket&psig=AOvVaw28zMIxEJ4s7EYUq58O2c3g&ust=1665577888469000&source=images&cd=vfe&ved=0CAsQjRxqFwoTCLjk0qSX2PoCFQAAAAAdAAAAABAE", + "location":"coventry", + "time":"11:00pm" + + + +} + +### +GET http://localhost:3002/addevent/63591523594a3f60ecfe1d99 + + +### +//getting all events +GET http://localhost:3002/addevent + +### +//updating event +PATCH http://localhost:3002/addevent/6351969f4fd72fe9e2807171 +Content-Type: application/json + +{ +"description":"footballl event of football" +} +### +//getting single event details +GET http://localhost:3002/addevent/6351b20d6a3c4de7769528f7 +### +//deleting event +DELETE http://localhost:3002/addevent/6351969f4fd72fe9e2807171