Skip to content

Commit

Permalink
feat: add server (#329)
Browse files Browse the repository at this point in the history
* fix: solve repetition in events

* fix: fix error for react import

* feat: add server

* Delete index.tsx

* package installation

* add event file

* Update cfp.router.js

* fix: security issues

---------

Co-authored-by: Deependra Kumar <[email protected]>
  • Loading branch information
suzy-g38 and Deepu178 authored Jun 27, 2023
1 parent 396cd59 commit b37b38d
Show file tree
Hide file tree
Showing 18 changed files with 885 additions and 2 deletions.
3 changes: 3 additions & 0 deletions server/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
.env
yarn.lock
36 changes: 36 additions & 0 deletions server/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { mongoConnect } from './schema/mongo.connect.js';
import { cfpRouter } from './src/cpfs/cfp.router.js';
import { eventRouter } from './src/events/events.router.js';
import { hackathonRouter } from './src/hackathons/hackathon.router.js';
import { userRouter } from './src/users/user.router.js';
import cors from 'cors';
import dotenv from 'dotenv';
import express from 'express';

dotenv.config();
const app = express();
const port = process.env.PORT;

const allowedOrigins = [`${process.env.UI_ENDPOINT}`];

const options = {
origin: allowedOrigins,
credentials: true,
};

app.use(cors(options));
await mongoConnect();

app.get('/', () => {
throw new Error('');
});

app.use(express.json());

app.use('/api/user', userRouter);
app.use('/api/events', eventRouter);
app.use('/api/hackathons', hackathonRouter);
app.use('/api/cfps', cfpRouter);

// eslint-disable-next-line no-console
app.listen(port, () => console.log(`server is running on ${port}`));
25 changes: 25 additions & 0 deletions server/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "opensource",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"dev": "nodemon index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.20.2",
"cors": "^2.8.5",
"dotenv": "^16.3.1",
"eslint": "^8.43.0",
"express": "^4.18.2",
"http": "^0.0.1-security",
"husky": "^8.0.3",
"mongodb": "^5.6.0",
"mongoose": "^7.3.1",
"prettier": "^2.8.8"
}
}
47 changes: 47 additions & 0 deletions server/schema/cfps/CfpsSchema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { model, Schema } from 'mongoose';

const CfpsSchema = new Schema(
{
name: {
type: String,
require: true,
},
slug: {
type: String,
require: true,
},
organizer: {
type: String,
require: true,
},
description: {
type: String,
require: true,
},
address: {
type: { isOnline: Boolean, location: String },
required: true,
},
image: {
type: Buffer,
require: false,
},
date: {
type: Date,
require: true,
},
duration: {
type: Number,
require: true,
},
tags: {
type: [String],
require: true,
},
},
{
timestamps: true,
}
);

export const CfpModel = model('cfps', CfpsSchema);
47 changes: 47 additions & 0 deletions server/schema/events/EventsSchema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { model, Schema } from 'mongoose';

const EventsSchema = new Schema(
{
name: {
type: String,
require: true,
},
slug: {
type: String,
require: true,
},
organizer: {
type: String,
require: true,
},
description: {
type: String,
require: true,
},
address: {
type: { isOnline: Boolean, location: String },
required: true,
},
image: {
type: Buffer,
require: false,
},
date: {
type: Date,
require: true,
},
duration: {
type: Number,
require: true,
},
tags: {
type: [String],
require: true,
},
},
{
timestamps: true,
}
);

export const EventModel = model('events', EventsSchema);
47 changes: 47 additions & 0 deletions server/schema/hackathon/hackathonSchema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { model, Schema } from 'mongoose';

const HackathonSchema = new Schema(
{
name: {
type: String,
require: true,
},
slug: {
type: String,
require: true,
},
organizer: {
type: String,
require: true,
},
description: {
type: String,
require: true,
},
address: {
type: { isOnline: Boolean, location: String },
required: true,
},
image: {
type: Buffer,
require: false,
},
date: {
type: Date,
require: true,
},
duration: {
type: Number,
require: true,
},
tags: {
type: [String],
require: true,
},
},
{
timestamps: true,
}
);

export const HackathonModel = model('hackathon', HackathonSchema);
23 changes: 23 additions & 0 deletions server/schema/mongo.connect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import mongoose, { connect } from 'mongoose';

//surprass deprication warning
mongoose.set('strictQuery', false);

export const mongoConnect = async () => {
const connectionString =
process.env.MONGODB_PROTO +
process.env.MONGODB_USER +
':' +
process.env.MONGODB_PASSWORD +
'@' +
process.env.MONGODB_URL +
'/' +
process.env.MONGODB_DATABASE;
try {
// eslint-disable-next-line no-console
console.log('connection');
await connect(connectionString);
} catch (error) {
throw new Error(error);
}
};
44 changes: 44 additions & 0 deletions server/schema/user/UserSchema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { model, Schema } from 'mongoose';

const UserSchema = new Schema(
{
name: {
type: String,
require: true,
},
email: {
type: String,
require: true,
},
password: {
type: String,
require: true,
},
image: {
type: Buffer,
require: false,
},
bio: {
type: String,
require: false,
},
role: {
type: String,
require: true,
},
socials: {
type: [
{
name: String,
link: String,
},
],
require: false,
},
},
{
timestamps: true,
}
);

export const UserModel = model('user', UserSchema);
93 changes: 93 additions & 0 deletions server/src/cpfs/cfp.router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { CfpService } from './cfp.service.js';
import express from 'express';

export const cfpRouter = express.Router();

// get all cfps
cfpRouter.get('/', async (_, res) => {
try {
const cfps = await CfpService.getAllCfps();
res.status(200).send({ success: true, cfps });
} catch (error) {
res.status(500).json({ success: false, message: 'Internal server error' });
}
});

// get cfp by id
cfpRouter.get('/id/:id', async (req, res) => {
try {
const { id } = req.params;
if (!id) {
res.status(422).send({ success: false, message: 'Invalid id parameter' });
}

const cfps = await CfpService.getCfpsById(id);
res.status(200).send({ success: true, cfps });
} catch (error) {
res.status(500).json({ success: false, message: 'Internal server error' });
}
});

// get cfp by slug
cfpRouter.get('/slug/:slug', async (req, res) => {
try {
const { slug } = req.params;
if (!slug) {
res
.status(422)
.send({ success: false, message: 'Invalid slug parameter' });
}

const cfps = await CfpService.getCfpsBySlug(slug);
res.status(200).send({ success: true, cfps });
} catch (error) {
res.status(500).json({ success: false, message: 'Internal server error' });
}
});

// add cfp
cfpRouter.post('/create', async (req, res) => {
try {
const { data } = req.body;

if (!data) {
res.status(422).send({ success: false, message: 'Invalid data' });
}
const cfps = await CfpService.createCfp(data);
res.status(200).send({ success: true, cfps });
} catch (error) {
res.status(500).json({ success: false, message: 'Internal server error' });
}
});

// update cfp
cfpRouter.post('/update/:id', async (req, res) => {
try {
const { id } = req.params;
const { data } = req.body;
if (!id) {
res.status(422).send({ success: false, message: 'Invalid id parameter' });
}
if (!data) {
res.status(422).send({ success: false, message: 'Invalid data' });
}
const cfps = await CfpService.updateCfp(id, data);
res.status(200).send({ success: true, cfps });
} catch (error) {
res.status(500).json({ success: false, message: 'Internal server error' });
}
});

// delete cfp by id
cfpRouter.post('/delete', async (req, res) => {
try {
const { id } = req.body;
if (!id) {
res.status(422).send({ success: false, message: 'Invalid id' });
}
const cfps = await CfpService.deleteCfp(id);
res.status(200).send({ success: true, cfps });
} catch (error) {
res.status(500).json({ success: false, message: 'Internal server error' });
}
});
Loading

1 comment on commit b37b38d

@vercel
Copy link

@vercel vercel bot commented on b37b38d Jun 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.