Skip to content

Comments

Nicolina happy thoughts api#41

Open
Nicolinabl wants to merge 23 commits intoTechnigo:masterfrom
Nicolinabl:master
Open

Nicolina happy thoughts api#41
Nicolinabl wants to merge 23 commits intoTechnigo:masterfrom
Nicolinabl:master

Conversation

@Nicolinabl
Copy link

})

// Message model
const Message = mongoose.model('Message', messageSchema)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks really clean and easy to read 👍

I like that you added minlength validation for the message — that’s a nice touch to prevent empty or too short content.

Also good call setting default values for hearts and createdAt, it makes the model more robust and we don’t have to handle that manually every time.

The userId reference is set up properly with ref: 'User', so population should work smoothly.

Overall, solid schema structure, nothing overcomplicated — great job! 🚀

})

// User model
const User = mongoose.model('User', userSchema)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything looks simple and readable, no unnecessary stuff. Solid implementation, good job! 🚀

} catch (error) {
res.status(500).json({ message: "Internal server error", error: error.message })
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, good job handling both cases: when the user exists and when the token is missing/invalid. Returning 401 with a clear message is very clear for the frontend.

In the future, you could add a small check to ensure req.header('Authorization') exists before calling .replace(), to avoid possible runtime errors.

response: error,
})
}
})

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This signup flow looks really solid 👍

Great job hashing the password with bcrypt before saving — that’s super important for security. Using salt makes it even better 🔐

I like that you’re returning only safe user data in the response (no password), that’s a very good practice.

The response structure is also clean and consistent with success, message, and response, which makes it easy to handle on the frontend.

Maybe in the future you could consider using the async versions of bcrypt methods (genSalt / hash) to avoid blocking, but overall this is a really nice implementation.

Good job, this looks production-ready to me 🚀

} catch (error) {
res.status(500).json({ error: "Failed to get liked messages" })
}
})

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice endpoint, very clear and focused 👍

} catch (error) {
res.status(500).json({ error: "Failed to get messages including the word happy" })
}
})

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also like that you’re handling the case when no messages are found and returning a clear 404 response.

} catch (error) {
res.status(400).json({ error: "could not update likes" })
}
})

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using $inc to increase hearts is a really good choice — it’s efficient and avoids race conditions compared to updating the value manually.

I like that you’re returning the updated document with { new: true }, that’s super practical for the frontend.

} catch (error) {
res.status(400).json({ error: "Invalid message-id or request" })
}
})

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using || to fallback to the old message if no new message is provided is a neat little touch.

Overall, really clean and safe implementation — looks solid! 👏

}

})

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error handling is clean too, returning validation errors if something goes wrong.

} catch (error) {
res.status(400).json({ error: "Invalid message id" })
}
})

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice DELETE route 👍

I like that you’re using authenticateUser to make sure only logged-in users can delete messages — that’s super important 🔒

Checking both if the message exists (404) and if the user is the owner (403) is great for security and makes the API predictable.

Returning { success: true } on success is clean and easy for the frontend to handle.

Small tip: since you already used findByIdAndDelete, the extra deleteOne() might not be necessary, but overall the route is solid, readable, and safe 🚀

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

Successfully merging this pull request may close these issues.

2 participants