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
31 changes: 26 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# Mac cruft
.DS_Store

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
Expand All @@ -19,6 +20,7 @@ lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output
Expand All @@ -42,12 +44,21 @@ jspm_packages/
# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

Expand All @@ -64,11 +75,18 @@ typings/
# parcel-bundler cache (https://parceljs.org/)
.cache

# next.js build output
# Next.js build output
.next

# nuxt.js build output
# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist
Expand All @@ -81,3 +99,6 @@ typings/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,20 @@ Demonstrate your understanding of this week's concepts by answering the followin

- [ ] Explain the difference between `Relational Databases` and `SQL`.

SQL or T-SQL is the language we use when interacting with relational databases in order to perform queries or modify the database schemas. A relational database, aka a relational database management system (RDBMS) is the application or software that can create relational databases and understands SQL/TSQL.

- [ ] Why do tables need a `primary key`?

Tables need a primary key in order to ensure that every record can be uniquely identified and that unique property will always refer to the same record no matter what else changes about it.

- [ ] What is the name given to a table column that references the primary key on another table.

Foreign key

- [ ] What do we need in order to have a _many to many_ relationship between two tables.

We need an intermediary (junction) table to connect two other tables in order to create a * to * relationship. This intermediary teable will have foreign keys to the tables it is linking.

## Minimum Viable Product

Take the steps necessary to complete the project from scratch. Start by initializing your project with a `package.json` and go from there.
Expand Down
19 changes: 19 additions & 0 deletions app/api/apiRouter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const api = require('express').Router();
module.exports = api;

// ROUTER METHODS

// basic hello world response to root path showing server is running
api.get('/', (req, res) => {
res.send('API is running');
})

// sub-routes
const projectsRouter = require('./projects/projectsRouter')
api.use('/projects', projectsRouter);

const resourcesRouter = require('./resources/resourcesRouter')
api.use('/resources', resourcesRouter);

const tasksRouter = require('./tasks/tasksRouter')
api.use('/tasks', tasksRouter);
59 changes: 59 additions & 0 deletions app/api/projects/projectsRouter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const router = require('express').Router()
module.exports = router;

// data access
const dbx = require('../../../data/dbContext');
// shorthands
const projects = dbx.projects;
const log = console.log;

router.get('/', (req, res) => {
projects.all()
.then(result => {
res.status(200).json({data: result})
})
.catch(error => {
log('proj router get /:', error)
res.status(500).json({message: "Unable to retrieve data"})
})
})

router.get('/:id', (req, res) => {
projects.getOne(req.params.id)
.then(result => {
if (result) {
res.status(200).json({data: result})
} else {
res.status(404).json({data: null, message: "Item not found"})
}
})
.catch(error => {
log('proj router get /id:', error)
res.status(500).json({message: "Unable to retrieve data"})
})
})

router.post('/', (req, res) => {
projects.create(req.body)
.then(result => {
if (result) {
res.status(201).json({data: result});
} else {
res.status(404).json({data: result, message: "Error retrieving new item"})
}
})
.catch(error => {
log('proj router post:', error)
res.status(500).json({message: "Unable to create item"})
})
})

router.delete('/:id', (req, res) => {
projects.remove(req.params.id)
.then(result => {
res.status(200).json({ message: 'Successfully deleted item'})
})
.catch(error => {
res.status(500).json({ message: 'Unable to delete item'})
})
})
Empty file added app/api/projects/tasksRouter.js
Empty file.
50 changes: 50 additions & 0 deletions app/api/resources/resourcesRouter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const router = require('express').Router()
module.exports = router;

// data access
const dbx = require('../../../data/dbContext');
// shorthands
const resources = dbx.resources;
const log = console.log;

router.get('/', (req, res) => {
resources.all()
.then(result => {
log('get all:', result)
res.status(200).json({data: result})
})
.catch(error => {
log('res router get /:', error)
res.status(500).json({message: "Unable to retrieve data"})
})
})

router.get('/:id', (req, res) => {
resources.getOne(req.params.id)
.then(result => {
if (result) {
res.status(200).json({data: result})
} else {
res.status(404).json({data: null, message: "Item not found"})
}
})
.catch(error => {
log('res router get /id:', error)
res.status(500).json({message: "Unable to retrieve data"})
})
})

router.post('/', (req, res) => {
resources.create(req.body)
.then(result => {
if (result) {
res.status(201).json({data: result});
} else {
res.status(404).json({data: result, message: "Error retrieving new item"})
}
})
.catch(error => {
log('res router post:', error)
res.status(500).json({message: "Unable to create item"})
})
})
62 changes: 62 additions & 0 deletions app/api/tasks/tasksRouter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const router = require('express').Router()
module.exports = router;

// data access
const dbx = require('../../../data/dbContext');
// shorthands
const tasks = dbx.tasks;
const log = console.log;

router.get('/', (req, res) => {
tasks.all()
.then(result => {
log('get all:', result)
res.status(200).json({data: result})
})
.catch(error => {
log('task router get /:', error)
res.status(500).json({message: "Unable to retrieve data"})
})
})

router.get('/:id', (req, res) => {
tasks.getOne(req.params.id)
.then(result => {
if (result) {
res.status(200).json({data: result})
} else {
res.status(404).json({data: null, message: "Item not found"})
}
})
.catch(error => {
log('task router get /id:', error)
res.status(500).json({message: "Unable to retrieve data"})
})
})

// create a task for given project ID
router.post('/:id', (req, res) => {
const projID = req.params.id;
tasks.create({...req.body, project_id: projID})
.then(result => {
if (result) {
res.status(201).json({data: result});
} else {
res.status(404).json({data: result, message: "Error retrieving new item"})
}
})
.catch(error => {
log('task router post:', error)
res.status(500).json({message: "Unable to create item"})
})
})

router.delete('/:id', (req, res) => {
tasks.remove(req.params.id)
.then(result => {
res.status(200).json({ message: 'Successfully deleted item'})
})
.catch(error => {
res.status(500).json({ message: 'Unable to delete item'})
})
})
21 changes: 21 additions & 0 deletions app/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const express = require("express");

// create a new express app instance
const app = express();
// config express with json middleware
app.use(express.json());
// config express with cors
const cors = require('cors');
app.use(cors());

// basic hello world response to root path showing server is running
app.get('/', (req, res) => {
res.send('server is running');
})

// additional route handling
const apiRouter = require('./api/apiRouter')
app.use('/api', apiRouter);


module.exports = app;
Binary file added data/AdvancedToDo.db3
Binary file not shown.
41 changes: 41 additions & 0 deletions data/data-access/projects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const db = require('../dbConnection');
module.exports = {
all,
create,
getOne,
remove
}

function all(id) {
return id ?
db('projects').where('id', id) :
db('projects');
}

function getOne(id) {
if (id) {
return all(id).first();
}
else {
throw 'must pass an id argument';
}
}

function create(data) {
return db('projects')
.insert(data, 'id')
.then(result => {
console.log('proj insert res:', result)
if (result)
return getOne(result[0]);
else {
throw 'database failed to return new ID';
}
})
}

function remove(id) {
return db('projects')
.where("id", id)
.del()
}
36 changes: 36 additions & 0 deletions data/data-access/resources.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const db = require('../dbConnection');
module.exports = {
all,
create,
getOne
}

const tableName = 'resources';

function all(id) {
return id ?
db(tableName).where('id', id) :
db(tableName);
}

function getOne(id) {
if (id) {
return all(id).first();
}
else {
throw 'must pass an id argument';
}
}

function create(data) {
return db(tableName)
.insert(data, 'id')
.then(result => {
console.log('proj insert res:', result)
if (result)
return getOne(result[0]);
else {
throw 'database failed to return new ID';
}
})
}
Loading