Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
node_modules
.env
34 changes: 34 additions & 0 deletions controllers/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const axios = require('axios');
const jwt = require('jsonwebtoken');


function login (req, res){
const { username, password } = req.body

axios(`https://${process.env.AUTH0_DOMAIN}/oauth/token`, {
method: 'POST',
headers: {
'content-type': 'application/json'
},
data: {
grant_type: 'password',
username: username,
password: password,
audience: process.env.AUTH0_IDENTITY,
connection: 'Username-Password-Authentication',
client_id: process.env.AUTH0_CLIENT_ID,
client_secret: process.env.AUTH0_CLIENT_SECRET
}
})
.then(response => {
const { access_token } = response.data
res.json({
access_token
})
})
.catch(e => {
res.send(e)
})
}

module.exports = { login }
120 changes: 120 additions & 0 deletions controllers/customers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
const pool = require('../sql/connection.js');
const mysql = require('mysql');


function getDefaultRoute(req, res){
console.log('in the default route')
res.send('default route');
}

function getCustomers(req, res){
console.log('inside the customers route/path');

let sql = `SELECT * FROM customers`;

pool.query(sql, function(err, results){
if(err){
console.error({ 'message': 'Error occured: Cannot fetch list of customers ' + err });
res.status(404).send('Customer data not found');
}else{
console.log('success');
res.json(results);
}
})
}

function getCustomersByStoreId(req, res){
let storeId = req.params.storeId;
console.log('getting customers with store id', storeId);

let sql = `SELECT * from customers WHERE store_id = ?`;
let paramReplacements = [storeId];
sql = mysql.format(sql, paramReplacements);

pool.query(sql, function(err, results){
if(err){
console.error('Internal Service Error', err);
res.status(500).send('Error Occured');
}else if(results.length === 0){
res.status(404).send('There are no customers at this store or the store doesn\'t exist yet');
console.log('empty array returned', err);
}else{
res.json(results);
console.log('success!')
}
})
}

function getAllCustomerContactInfo(req, res){

let sql = `SELECT customers.customer_id, customers.first_name, customers.last_name, address.phone, customers.email, address.address, city.city, address.district, country.country
FROM address
JOIN customers
ON customers.address_id = address.address_id
JOIN city
ON address.city_id = city.city_id
JOIN country
ON city.country_id = country.country_id
GROUP BY customer_id`

pool.query(sql, function(err, results){
if(err){
console.error('Internal Service Error', err);
res.status(500).send('Internal Server Error')
}else if(results.length === 0){
console.log('empty array was returned');
res.status(404).send('No data found')
}else{
res.json(results);
console.log('Success in fetching all customer contact info');
}
})
}

function getCustomerContactInfoByCustomerId(req, res){
let customerId = req.params.customer_Id;
console.log('in the customer contact info path fetching customer Id', customerId);


let sql = `SELECT customers.customer_id, customers.first_name, customers.last_name, address.phone, customers.email, address.address, city.city, address.district, country.country
FROM address
JOIN customers
ON customers.address_id = address.address_id
JOIN city
ON address.city_id = city.city_id
JOIN country
ON city.country_id = country.country_id
WHERE customer_id = ?
GROUP BY customer_id`

let replacements = [customerId];
sql = mysql.format(sql, replacements);

pool.query(sql, function(err, results){
if(err){
console.error('Internal Server Error', err);
res.status(500).send('Internal Server Error Occured');
}else if(results.length === 0){
console.log('customer and corresponding contact info not found for customer id' + customerId);
res.status(404).send('Customer and corresponding contact info not found for customer id ' + customerId);
}else if(results.length > 1){
console.log('400 Bad Request');
res.status(400).send('More than 1 customer found with the same ID. Bad Request, refine search')
}else{
res.json(results)
}
})
}

function createCustomer(req, res){
res.send('in the create customer route');
console.log('success')
}

module.exports = { getCustomers,
getCustomersByStoreId,
getCustomerContactInfoByCustomerId,
getAllCustomerContactInfo,
getDefaultRoute,
createCustomer
}
131 changes: 131 additions & 0 deletions controllers/superHeroes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
//import connection and mysql
let pool = require('../sql/connection.js');
const mysql = require('mysql');

// logic (code) for get, get by id, post, put, and delete requests

function getDefaultRoute(req, res){
console.log('in the default route')
res.send('default route');
}


function getComicBookCharacters(req, res){
console.log('currently in superheroes path/route fetching all superheros');

pool.query('SELECT * FROM comicbookcharacters', function(err, rows){
if(err){
console.log({ 'message': 'Error occured: Cannot fetch list of comic book characters' + err });
return res.status(404).send('List of comic book heroes not found');
}else{
console.log('SUCCESS!');
res.json(rows);
}
})
}

function getSuperheroById(req, res){
let id = req.params.id
console.log('currently in superheroes path/route fetching superhero with requested id', id);

let sql = `SELECT * FROM comicbookcharacters WHERE id = ?`
let replacements = [id];
sql = mysql.format(sql, replacements);

pool.query(sql, function(err, rows){
if(err){
console.log( 'Internal Service Error', err )
res.status(500).send('Error');
} else if(rows.length === 0) {
console.log({ 'message': 'Error occured: Cannot fetch superhero with id ' + id + ' ' + err })
res.status(404).send('Superhero not found')
} else if(rows.length > 1){
console.log('Found too many superheroes with id ' + id);
res.status(500).send('Too many superheroes found with this id')
} else{
res.json(rows[0]);
console.log('SUCCESS!');
}
})
}

function createNewSuperHero(req, res){
console.log('currently in the superheroes path/route creating a new superhero');

let newSuperHero = {
realName: req.body.realName,
intelligence: req.body.intelligence,
strength: req.body.strength,
alterEgo: req.body.alterEgo,
occupation: req.body.occupation,
birthPlace: req.body.birthPlace
}

let sql = 'INSERT INTO comicbookcharacters (realName, intelligence, strength, alterEgo, occupation, birthPlace) VALUES (?, ?, ?, ?, ?, ?)'
let replacements = [newSuperHero.realName, newSuperHero.intelligence, newSuperHero.strength, newSuperHero.alterEgo, newSuperHero.occupation, newSuperHero.birthPlace];
sql = mysql.format(sql, replacements);

pool.query(sql, function(err, rows){
if(err){
console.log('Internal Service Error ', err);
res.status(500).send('Error occured');
}else{
res.json(newSuperHero);
console.log('SUCCESS!');
}
})
}

function updateSuperHero(req, res){
let id = req.params.id;
console.log('currently in the superHeroes route/path updating superHero with id', id);

let updatedSuperHero = {
id: req.params.id,
realName: req.body.realName,
intelligence: req.body.intelligence,
strength: req.body.strength,
alterEgo: req.body.alterEgo,
occupation: req.body.occupation,
birthPlace: req.body.birthPlace
}

let sql = 'UPDATE comicbookcharacters SET realName = ?, intelligence = ?, strength = ?, alterEgo = ?, occupation = ?, birthPlace = ? WHERE id = ?'
let replacements = [updatedSuperHero.realName, updatedSuperHero.intelligence, updatedSuperHero.strength, updatedSuperHero.alterEgo, updatedSuperHero.occupation, updatedSuperHero.birthPlace, id];
sql = mysql.format(sql, replacements);

pool.query(sql, function(err, rows){
if(err){
console.log('Internal Service Error ', err);
res.status(500).send('Error occured');
}else{
res.json(updatedSuperHero);
console.log('SUCCESS!');
}
})
}

function deleteSuperHero(req, res){
let id = req.params.id
console.log('currently in the superheroes route/path deleting superHero with id', id);

let sql = 'DELETE FROM comicbookcharacters WHERE id = ?'
let replacements = [id];
sql = mysql.format(sql, replacements);

pool.query(sql, function(err, rows){
if(err){
console.log('Internal Service Error ', err);
res.status(500).send('Error occured');
}else{
res.send('Superhero ' + id + ' deleted');
console.log('SUCCESS!');
}
})
}

module.exports = { getComicBookCharacters, getSuperheroById, createNewSuperHero, updateSuperHero, deleteSuperHero, getDefaultRoute};




Loading