From 365307221b406bc22c376e136171f820bc611dbe Mon Sep 17 00:00:00 2001 From: aritroCoder Date: Sun, 18 Jun 2023 09:59:33 +0530 Subject: [PATCH] added project routes --- src/index.ts | 1 + src/routes/addProject.ts | 66 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 src/routes/addProject.ts diff --git a/src/index.ts b/src/index.ts index 22b35a3..b225447 100644 --- a/src/index.ts +++ b/src/index.ts @@ -20,6 +20,7 @@ if (dbURI) { // express routes /* eslint-disable */ app.use('/api/register/', require('./routes/register')) + app.use('/api/add-project/', require('./routes/addProject')) app.listen(port, () => { console.log(`Server is listening on port ${port}`) diff --git a/src/routes/addProject.ts b/src/routes/addProject.ts new file mode 100644 index 0000000..4ae65d2 --- /dev/null +++ b/src/routes/addProject.ts @@ -0,0 +1,66 @@ +/* eslint-disable */ +const express = require('express') +const Project = require('../../models/Project') +const router = express.Router() +import { body, validationResult } from 'express-validator' + +// @route POST /api/add-project +// @desc Add project +// @access Public +// @body title, description, tags, mentor, mentorGithub, languages, githubLink, image, sponsored, year +router.post( + '/', + body('title').isString(), + body('description').isString(), + body('tags').isArray(), + body('mentor').isString(), + body('mentorGithub').isString(), + body('languages').isArray(), + body('githubLink').isString(), + body('image').isString(), + body('sponsored').isBoolean(), + body('year').isNumeric(), + async ( + req: { + body: { + title: string + description: string + tags: string[] + mentor: string + mentorGithub: string + languages: string[] + githubLink: string + image: string | undefined + sponsored: boolean + year: number + } + }, + res: { + status: (arg0: number) => { + (): any + new (): any + json: { (arg0: { error?: any; message?: any }): void; new (): any } + } + } + ) => { + const errors = validationResult(req) + if (!errors.isEmpty()) { + return res.status(400).json({ error: errors.array() }) + } + try { + // check if project already exists + let project = await Project.findOne({ githubLink: req.body.githubLink }) + if (project) { + return res.status(400).json({ error: { msg: 'Project already exists' } }) + } + + const newProject = new Project(req.body) + await newProject.save() + res.status(200).json({ message: 'Project added successfully' }) + } catch (err: any) { + console.error(err.message) + res.status(500).json({error: 'Server Error'}) + } + } +) +