2
\$\begingroup\$

My question is have i gotten the three-layered architecture right with error handling etc. I have a webpage containing different resources. The code works but I am not sure if have structured it correct.

This is a function located in the BLL

createPost: function (accountId, title, body, callback) {
    postValidator.validatePost(title, body, function (error) {
        if (error) {
            callback(error, null)
        } else {
            postRepository.createPost(title, body, accountId, function (error, postId) {
                if (error) {
                    callback(new BusinessError(BusinessErrorType.DATA_LAYER_ERROR, "Error creating post"), null)
                } else {
                    callback(null, postId)
                }
            })
        }
    })
}

Basically it sends a request to create desired post in the DL if the DL returns the error value the BLL will return it to the PL. My function in the PL that communicates with the BLL:

router.post("/new", function (request, response) {
   const title = request.body.title
   const body = request.body.body
   const account = request.session.account
   loginManager.isLoggedIn(request.session.login, function (error, IsLoggedIn) {
       if (IsLoggedIn) {
            postManager.createPost(account.id, title, body, function (error, createdPostId) {
                if (error) {
                    response.render("new-post.hbs", { title: title, body: body, notification: error.description, notificationType: "danger" })
                } else {
                    response.redirect("/posts/" + createdPostId)
                }
            })
        } else {
            response.render("accounts-sign-in.hbs", { notification: error.description, notificationType: "danger" })
        }
    })
})

Is this the correct way to go about?

\$\endgroup\$
1
  • \$\begingroup\$ Welcome to Code Review. I edited your javascript snippets, please include the javascript framework you are using if there is one with the corresponding tag. \$\endgroup\$ – dariosicily May 4 at 12:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.