Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model shouldn't handle request logic #33

Open
oliverjam opened this issue Apr 10, 2020 · 0 comments
Open

Model shouldn't handle request logic #33

oliverjam opened this issue Apr 10, 2020 · 0 comments

Comments

@oliverjam
Copy link

week6-BGJK/src/model.js

Lines 89 to 99 in c967c93

function deletePost(postId, res) {
db.query("DELETE FROM blog_posts WHERE ($1)=id", [postId])
.then(() => {
res.writeHead(302, {
location: "/"
});
// res.writeHead(200
res.end();
})
.catch(console.log);
}

This model function is sending a response, which isn't a good idea. The reason we separate our data access into separate functions is to ensure each bit of our app has a single responsibility. This model function is now much harder to test (you'd need to use Supertest or something, instead of just calling the function and seeing what it gives you back).

I'd really recommend making all your model functions just resolve with data, and handle the actual logic/response stuff in your handlers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant