Skip to content

Commit

Permalink
Added: Update Employee/Venue, Create Venue, Delete Venue
Browse files Browse the repository at this point in the history
  • Loading branch information
jakebonk committed Feb 23, 2019
1 parent 00921e9 commit 842d614
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 16,092 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ yarn-error.log*
server/properties\.js

server/config\.js

client/build/

client/package-lock\.json
16,088 changes: 0 additions & 16,088 deletions client/package-lock.json

This file was deleted.

2 changes: 2 additions & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
"proxy": "http://localhost:5000/",
"private": true,
"dependencies": {
"material-ui": "^0.20.2",
"react": "^16.8.3",
"react-dom": "^16.8.3",
"react-router-dom": "^4.3.1",
"react-scripts": "2.1.5"
},
"scripts": {
Expand Down
4 changes: 4 additions & 0 deletions client/src/Components/Login/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class Login extends Component {
e.preventDefault();
const response = await fetch('/api/login', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({ username: this.state.username, password: this.state.password}),
});
const body = await response.text();
Expand Down
8 changes: 6 additions & 2 deletions client/src/Components/SignUp/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { Component } from 'react';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
import AppBar from 'material-ui/AppBar';

import RaisedButton from 'material-ui/RaisedButton';
import TextField from 'material-ui/TextField';
import './index.css';
Expand All @@ -21,7 +21,11 @@ class SignUp extends Component {
e.preventDefault();
const response = await fetch('/api/signup', {
method: 'POST',
body: JSON.stringify({ company_name: this.company_name, username: this.state.username, password: this.state.password}),
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({ company_name: this.state.company_name, username: this.state.username, password: this.state.password}),
});
const body = await response.text();
this.setState({ responseToPost: body });
Expand Down
119 changes: 117 additions & 2 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,46 @@ app.get('/api/logout',
res.redirect('/');
});

app.put('/api/employee/:id',
async function(req,res){
try{
var name = req.body.name;
var airline_class = req.body.airline_class;
var longitude = req.body.longitude;
var latitude = req.body.latitude;
var data = {};
if(utils.validVenueName(name)){
data['employee.$.name'] = name;
}
if(!isNaN(parseFloat(longitude))){
data['employee.$.longitude'] = longitude;
}
if(!isNaN(parseFloat(latitude))) {
data['employee.$.latitude'] = latitude;
}
if(utils.validAirlineClass(airline_class)){
data['employee.$.airline_class'] = airline_class;
}
if(data != {}) {
const db = req.app.locals.db;
console.log(data);
var company = await db.collection("companies").findOneAndUpdate({_id: ObjectID(req.user._id),'employee._id':ObjectID(req.params.id)}, {
$set: data,
}, {returnOriginal: false});
if (company) {
res.send(company.value);
} else {
res.send({"message": "Could not update employee"});
}
}else{
res.send({"message":"Invalid data"});
}
}catch (e) {
console.log(e);
res.end();
}
});

app.delete('/api/employee/:id',
async function(req,res){
try{
Expand All @@ -83,7 +123,7 @@ app.delete('/api/employee/:id',
if(company){
res.send(company.value);
}else{
res.send({"message":"Could not create employee"});
res.send({"message":"Could not delete employee"});
}
}catch (e) {
console.log(e);
Expand All @@ -98,7 +138,7 @@ app.post('/api/employee',
var airline_class = req.body.airline_class;
var longitude = req.body.longitude;
var latitude = req.body.latitude;
if(utils.validName(name) && parseFloat(longitude) && parseFloat(latitude) && utils.validAirlineClass(airline_class)){
if(utils.validName(name) && !isNaN(parseFloat(longitude)) && !isNaN(parseFloat(latitude)) && utils.validAirlineClass(airline_class)){
const db = req.app.locals.db;
var company = await db.collection("companies").findOneAndUpdate({_id:ObjectID(req.user._id)},{$push:{employees:{name:name,airline_class:airline_class,longitude:longitude,latitude:latitude,_id:ObjectID()}}},{returnOriginal:false});
if(company){
Expand All @@ -115,6 +155,81 @@ app.post('/api/employee',
}
});

app.delete('/api/venue/:id',
async function(req,res){
try{
var employee_id = req.params.id;
const db = req.app.locals.db;
var company = await db.collection("companies").findOneAndUpdate({_id:ObjectID(req.user._id)},{$pull:{venue:{_id:ObjectID(employee_id)}}},{returnOriginal:false});
if(company){
res.send(company.value);
}else{
res.send({"message":"Could not delete venue"});
}
}catch (e) {
console.log(e);
res.end();
}
});

app.put('/api/venue/:id',
async function(req,res){
try{
var name = req.body.name;
var longitude = req.body.longitude;
var latitude = req.body.latitude;
var data = {};
if(utils.validVenueName(name)){
data['venue.$.name'] = name;
}
if(!isNaN(parseFloat(longitude))){
data['venue.$.longitude'] = longitude;
}
if(!isNaN(parseFloat(latitude))) {
data['venue.$.latitude'] = latitude;
}
if(data != {}) {
const db = req.app.locals.db;
var company = await db.collection("companies").findOneAndUpdate({_id: ObjectID(req.user._id),'venue._id':ObjectID(req.params.id)}, {
$set: data,
}, {returnOriginal: false});
if (company) {
res.send(company.value);
} else {
res.send({"message": "Could not update venue"});
}
}else{
res.send({"message":"Invalid data"});
}
}catch (e) {
console.log(e);
res.end();
}
});

app.post('/api/venue',
async function(req,res){
try{
var name = req.body.name;
var longitude = req.body.longitude;
var latitude = req.body.latitude;
if(utils.validVenueName(name) && !isNaN(parseFloat(longitude)) && !isNaN(parseFloat(latitude))){
const db = req.app.locals.db;
var company = await db.collection("companies").findOneAndUpdate({_id:ObjectID(req.user._id)},{$push:{venue:{name:name,longitude:longitude,latitude:latitude,_id:ObjectID()}}},{returnOriginal:false});
if(company){
res.send(company.value);
}else{
res.send({"message":"Could not create venue"});
}
}else{
res.send("error")//TODO throw better error
}
}catch (e) {
console.log(e);
res.end();
}
});

app.post('/api/signup', async (req, res) => {
try {
var username = req.body.username;
Expand Down
5 changes: 5 additions & 0 deletions server/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ module.exports.validAirlineClass = function(airline_class){
return true;//TODO
};

module.exports.validVenueName = function(venue){
return true;//TODO
};


module.exports.validName = function(name){
return true;//TODO
};
Expand Down

0 comments on commit 842d614

Please sign in to comment.