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
50 changes: 47 additions & 3 deletions api/accounts/accounts-middleware.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,55 @@
const Account = require('./accounts-model.js');
const db = require('../../data/db-config');

exports.checkAccountPayload = (req, res, next) => {
// DO YOUR MAGIC
const error = { status: 400 }
const { name, budget } = req.body
if (name === undefined || budget === undefined) {
error.message = 'name and budget are required'
} else if (typeof name !== 'string') {
error.message = 'name of account must be a string'
} else if (name.trim().length < 3 || name.trim().length > 100) {
error.message = 'name of account must be between 3 and 100'
} else if (typeof budget !== 'number' || !isNaN(budget)) {
error.message = 'budget of account must be a number'
} else if (budget < 0 || budget > 1000000) {
error.message = 'budget of account is too large or too small'
}
if (error.message) {
next(error)
} else {
next()
}
}

exports.checkAccountNameUnique = (req, res, next) => {
exports.checkAccountNameUnique = async (req, res, next) => {
// DO YOUR MAGIC
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) => {
exports.checkAccountId = async (req, res, next) => {
// DO YOUR MAGIC
try {
const account = await Account.getById(req.params.id)
if (!account) {
next({ status: 404, message: 'account not found'})
} else {
req.account = account
next()
}
} catch(err) {
next(err)
}

}
15 changes: 12 additions & 3 deletions api/accounts/accounts-model.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
const db = require('../../data/db-config.js')

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

const getById = id => {
// DO YOUR MAGIC
return db('accounts').where('id', id).first()
}

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

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

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

module.exports = {
Expand Down
55 changes: 50 additions & 5 deletions api/accounts/accounts-router.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,72 @@
const router = require('express').Router()
const md = require('./accounts-middleware');
const Account = require('./accounts-model')

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

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

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

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

router.delete('/:id', (req, res, next) => {
router.delete('/:id', md.checkAccountId, async (req, res, next) => {
// DO YOUR MAGIC
try {
await Account.deleteById(req.params.id)
res.json(req.account)
} catch (err) {
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;
9 changes: 9 additions & 0 deletions api/server.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
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;
6 changes: 6 additions & 0 deletions queries.sql
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
-- Database Queries

-- Find all customers with postal code 1010
SELECT * FROM Customers where postalcode = 1010;

-- Find the phone number for the supplier with the id 11
SELECT phone FROM Suppliers where supplierid = 11;

-- List first 10 orders placed, sorted descending by the order date
SELECT * FROM orders where orderid > 10433 order by orderdate desc

-- Find all customers that live in London, Madrid, or Brazil
SELECT * FROM Customers where country in ('London', 'Brazil', 'Madrid')

-- 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"
insert into Customers (customername, contactname, address, city, postalcode, country) values ('The Shire', 'Bilbo Baggins', '1 Hobbit-Hole', 'Bags End', '111', 'Middle Earth')

-- Update Bilbo Baggins record so that the postal code changes to "11122"
update Customers set postalcode = '11122' where customername = 'The Shire'

-- (Stretch) Find a query to discover how many different cities are stored in the Customers table. Repeats should not be double counted

Expand Down