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
59 changes: 57 additions & 2 deletions api/accounts/accounts-middleware.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,68 @@
// Bring in accounts-model
const Account = require('./accounts-model')
const db = require('../../data/db-config')

// This api is not working correctly?????
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('checkAccountPayload middleware')
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
//console.log('checkAccountPayload 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) => {
exports.checkAccountId = async (req, res, next) => {
// DO YOUR MAGIC
//console.log('checkAccountPayload middleware')
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)
}

}
14 changes: 12 additions & 2 deletions api/accounts/accounts-model.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
// pull in your db-config, its the 'door' to your database
const db = require('../../data/db-config')

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 => {
// DO YOUR MAGIC
return db('accounts').where('id', id).del()
}

module.exports = {
Expand Down
55 changes: 49 additions & 6 deletions api/accounts/accounts-router.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,70 @@
const router = require('express').Router()
// pulling in middleware
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{
//throw new Error('yikes!!!')
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
res.json(req.account)
})

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) => {
// DO YOUR MAGIC
router.put(
'/:id',
md.checkAccountId,
md.checkAccountPayload,
async (req, res, next) => {
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{
//res.json("delete account")
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;
10 changes: 10 additions & 0 deletions api/server.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
const express = require("express");
// consume the router
const accountsRouter = require('./accounts/accounts-router')

const server = express();

server.use(express.json());
// connect router exactly here
server.use('/api/accounts', accountsRouter);

// Test api
server.use('*', (req, res) => {
res.status(404).json({
message: 'not found',
})
})
module.exports = server;
Binary file modified data/budget.db3
Binary file not shown.
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ const PORT = process.env.PORT || 9000;
server.listen(PORT, () => {
console.log(`\n== API running on port ${PORT} ==\n`);
});

console.log(PORT)
7 changes: 5 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.