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: 54 additions & 5 deletions api/accounts/accounts-middleware.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,62 @@
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)
}
const err = { satus: 400 };
const { name, budget } = req.body;
if (name === undefined || budget === undefined) {
res.status(400).json({
message: "name and budget are required",
});
} else if (name.trim().length < 3 || name.trim().length > 100) {
res.status(400).json({
message: "name of account must be between 3 and 100",
});
} else if (typeof budget !== "number" || isNaN(budget)) {
res.status(400).json({
message: "budget of account must be a number",
});
} else if (budget < 0 || budget > 10 ** 6) {
res.status(400).json({
message: "budget of account is too large or too small",
});
} else {
next();
}
};

exports.checkAccountNameUnique = (req, res, next) => {
exports.checkAccountNameUnique = async (req, res, next) => {
// DO YOUR MAGIC
}
try {
const name = await db("accounts")
.where("name", req.body.name.trim())
.first();
if (name) {
res.status(400).json({
message: "this name is taken",
});
} else {
next();
}
} catch {
next();
}
};

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) {
res.status(404).json({
message: "account not found",
});
} else {
next();
}
} catch (err) {
next(err);
}
};
30 changes: 20 additions & 10 deletions api/accounts/accounts-model.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,37 @@
const db = require("../../data/db-config.js");
const getAll = () => {
// DO YOUR MAGIC
}
return db("accounts");
};

const getById = id => {
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 = async (id) => {
// DO YOUR MAGIC
}
const removedAcocunt = getById(id);
await db("accounts").where("id", id).del();
return removedAcocunt;
};

module.exports = {
getAll,
getById,
create,
updateById,
deleteById,
}
};
73 changes: 60 additions & 13 deletions api/accounts/accounts-router.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,74 @@
const router = require('express').Router()
const router = require("express").Router();
const Accounts = require("./accounts-model");
const {
checkAccountId,
checkAccountPayload,
checkAccountNameUnique,
} = require("./accounts-middleware");

router.get('/', (req, res, next) => {
router.get("/", (req, res, next) => {
// DO YOUR MAGIC
})
Accounts.getAll()
.then((account) => {
res.json(account);
})
.catch((err) => {
next(err);
});
});

router.get('/:id', (req, res, next) => {
router.get("/:id", checkAccountId, (req, res, next) => {
// DO YOUR MAGIC
})
Accounts.getById(req.params.id).then((account) => {
res.json(account);
});
});

router.post('/', (req, res, next) => {
// DO YOUR MAGIC
})
router.post(
"/",
checkAccountPayload,
checkAccountNameUnique,
(req, res, next) => {
// DO YOUR MAGIC
const { name, budget } = req.body;
Accounts.create({
name: name.trim(),
budget: budget,
})
.then((newAccount) => {
res.status(201).json(newAccount);
})
.catch((err) => {
next();
});
}
);

router.put('/:id', (req, res, next) => {
router.put("/:id", checkAccountId, checkAccountPayload, (req, res, next) => {
// DO YOUR MAGIC
Accounts.updateById(req.params.id, req.body)
.then((updatedAccount) => {
res.status(200).json(updatedAccount);
})
.catch((err) => {
next(err);
});
});

router.delete('/:id', (req, res, next) => {
router.delete("/:id", checkAccountId, (req, res, next) => {
// DO YOUR MAGIC
})
Accounts.deleteById(req.params.id)
.then((removedAccount) => {
res.json(removedAccount);
})
.catch((err) => {
next(err);
});
});

router.use((err, req, res, next) => { // eslint-disable-line
router.use((err, req, res, next) => {
// eslint-disable-line
// DO YOUR MAGIC
})
});

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

const server = express();

server.use(express.json());

server.use("/api/accounts", accountsRouter);

module.exports = server;
Binary file modified data/budget.db3
Binary file not shown.
2 changes: 2 additions & 0 deletions queries.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
-- 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

Expand Down