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
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ Your assignment page on Canvas should contain instructions for submitting this p

Visit [SQL Try Editor at W3Schools.com](https://www.w3schools.com/Sql/trysql.asp?filename=trysql_select_all) using Chrome and build the queries below. Once they work copy them to the `queries.sql` file at the root of the project.

- Find all customers with postal code 1010. Returns 3 records.
- Find the phone number for the supplier with the id 11. Should be (010) 9984510.
- List first 10 orders placed, sorted descending by the order date. The order with date 1997-02-12 should be at the top.
- Find all customers that live in London, Madrid, or Brazil. Returns 18 records.
- Add a customer record for _"The Shire"_, the contact name is _"Bilbo Baggins"_ the address is _"1 Hobbit-Hole"_ in _"Bag End"_, postal code _"111"_ and the country is _"Middle Earth"_.
- Update _Bilbo Baggins_ record so that the postal code changes to _"11122"_.
- [x] Find all customers with postal code 1010. Returns 3 records.
- [x] Find the phone number for the supplier with the id 11. Should be (010) 9984510.
- [x] List first 10 orders placed, sorted descending by the order date. The order with date 1997-02-12 should be at the top.
- [x] Find all customers that live in London, Madrid, or Brazil. Returns 18 records.
- [x] Add a customer record for _"The Shire"_, the contact name is _"Bilbo Baggins"_ the address is _"1 Hobbit-Hole"_ in _"Bag End"_, postal code _"111"_ and the country is _"Middle Earth"_.
- [x ]Update _Bilbo Baggins_ record so that the postal code changes to _"11122"_.

**Clicking the `Restore Database` button in the page will repopulate the database with the original data and discard all changes you have made**.

Expand Down
49 changes: 42 additions & 7 deletions api/accounts/accounts-middleware.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,48 @@
const Account = require('./accounts-model')
const db = require('../../data/db-config')
exports.checkAccountPayload = (req, res, next) => {
// DO YOUR MAGIC
// Note: you can either write "manual" validation logic
// or use the Yup library (not currently installed)
console.log('Check account payload middleware')
if (!req.body.name || !req.body.budget) {
next({status: 400, message: 'name and budget are required'})
} else if (typeof(req.body.name) !== 'string') {
next({status: 400, message: 'name of account must be a string'})
} else if (req.body.name.trim().length > 100 || req.body.name.trim().length < 3 ){
next({status: 400, message: 'name of account must be between 3 and 100'})
} else if (typeof(req.body.budget) !== 'number'){
next({status: 400, message: 'budget of account must be a number'})
} else if (req.body.budget > 1000000 || req.body.budget < 0 ){
next({status: 400, message: 'budget of account is too large or too small'})}
else {
next()
}
}

exports.checkAccountNameUnique = (req, res, next) => {
// DO YOUR MAGIC
exports.checkAccountNameUnique = async (req, res, next) => {
console.log('Check Account Name Unique middleware')
try {
const existing = await db('accounts').where('name', req.body.name.trim())
.first()
if (existing) {next({status: 400, message: 'that name is taken'})}
else {
next()
}
} catch (err) {
next(err)
}
}

exports.checkAccountId = (req, res, next) => {
// DO YOUR MAGIC
exports.checkAccountId = async (req, res, next) => {
console.log('Check Account ID middleware')
try {
const account = await Account.getById(req.params.id)
if (!account) {
next({status: 404, message: 'not found'})
} else {
console.log('account => ', account)
req.account = account
next()
}
} catch (err) {
next(err)
}
}
20 changes: 13 additions & 7 deletions api/accounts/accounts-model.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
const db = require('../../data/db-config')

const getAll = () => {
// DO YOUR MAGIC
// select * from accounts;
return db('accounts');
}

const getById = id => {
// DO YOUR MAGIC
// select * from accounts where id = id;
return db('accounts').where({id: id}).first()
}

const create = account => {
// DO YOUR MAGIC
const create = async account => {
const [id] = await db('accounts').insert(account)
return getById(id)
}

const updateById = (id, account) => {
// DO YOUR MAGIC
const updateById = async (id, account) => {
await db('accounts').where({id: id}).update(account)
return getById(id)
}

const deleteById = id => {
// DO YOUR MAGIC
return db('accounts').where({id: id}).del()
}

module.exports = {
Expand Down
49 changes: 38 additions & 11 deletions api/accounts/accounts-router.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,54 @@
const router = require('express').Router()
const md = require('./accounts-middleware')
const Accounts = require('./accounts-model')

router.get('/', (req, res, next) => {
// DO YOUR MAGIC

router.get('/', async (req, res, next) => {
try {
const accounts = await Accounts.getAll()
res.json(accounts)
} catch (err) {
next(err)
}
})

router.get('/:id', (req, res, next) => {
// DO YOUR MAGIC
router.get('/:id', md.checkAccountId, async (req, res, next) => {
res.json(req.account)
})

router.post('/', (req, res, next) => {
// DO YOUR MAGIC
router.post('/', md.checkAccountPayload, md.checkAccountNameUnique,
async (req, res, next) => {
try {
const newAccount = await Accounts.create(req.body)
res.status(201).json(newAccount)
} catch (err){
next(err)
}
})

router.put('/:id', (req, res, next) => {
// DO YOUR MAGIC
router.put('/:id', md.checkAccountId, md.checkAccountPayload,
md.checkAccountNameUnique, async (req, res, next) => {
try {
const updated = await Accounts.updateById(req.params.id, req.body)
res.status(201).json(updated)
} catch (err){
next(err)
}
});

router.delete('/:id', (req, res, next) => {
// DO YOUR MAGIC
router.delete('/:id', md.checkAccountId, async (req, res, next) => {
try {
await Accounts.deleteById(req.params.id)
res.json(req.account)
} catch {
next(err)
}
})

router.use((err, req, res, next) => { // eslint-disable-line
// DO YOUR MAGIC
res.status(err.status || 500).json({
message: err.message
})
})

module.exports = router;
10 changes: 8 additions & 2 deletions api/server.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
const express = require("express");

const accountsRouter = require('./accounts/accounts-router')
const server = express();

server.use(express.json());

server.use('/api/accounts', accountsRouter)

server.use('*', (req, res) => {
res.status(404).json({
message: 'not found'
})
})
module.exports = server;
Binary file modified data/budget.db3
Binary file not shown.