Assuming that we need tests to have sure about if each piece of code is working we need test each piece of code. So to do that, we need write each class/method separately and that's why i changed a little of the project structure. Also i cleaned the code because functions like fetchTopParticipants was also updating the topParticipants array and incrementParticipantPoints was creating a participant when he doesn't exist. By this way each function fit on the single responsibility principle
About testing database operations, i used the MongoDB Memory Server that uses the last mongod binary so it's unnecessary create two databases to test everything.
Also i resolved that TODO comment about returning just the name and the points of a viewer
Changes
Implements fetchTopParticipants and incrementParticipantPoints separately as utils functions.
Implements a DatabaseClient class that extends the MongoClient. To have access to participants collection, we had to access the database then access the collection but we wasn't using the database instance to do anything. Thinking like the Repository Pattern, DatabaseClient do the same as creating the MongoClient and then connecting to the database but now we have the getCollection method that receives the collection name, the collection type and returns the collection without having to instantiate the database.
Remove the topParticipants array from src/app.ts. I think that it's unnecessary when we already have a function fetchTopParticipants that return what topParticipants have to store and without it we can handle the data more precisely without side effects. Also on this way when we implement this project using the MVC pattern, we will able to create controllers separately without depending of a global variable
Motivation
Assuming that we need tests to have sure about if each piece of code is working we need test each piece of code. So to do that, we need write each class/method separately and that's why i changed a little of the project structure. Also i cleaned the code because functions like
fetchTopParticipants
was also updating thetopParticipants
array andincrementParticipantPoints
was creating a participant when he doesn't exist. By this way each function fit on the single responsibility principleAbout testing database operations, i used the MongoDB Memory Server that uses the last
mongod
binary so it's unnecessary create two databases to test everything.Also i resolved that
TODO
comment about returning just thename
and thepoints
of a viewerChanges
fetchTopParticipants
andincrementParticipantPoints
separately as utils functions.DatabaseClient
class that extends theMongoClient
. To have access toparticipants
collection, we had to access the database then access the collection but we wasn't using the database instance to do anything. Thinking like theRepository Pattern
,DatabaseClient
do the same as creating the MongoClient and then connecting to the database but now we have thegetCollection
method that receives the collection name, the collection type and returns the collection without having to instantiate the database.topParticipants
array fromsrc/app.ts
. I think that it's unnecessary when we already have a functionfetchTopParticipants
that return whattopParticipants
have to store and without it we can handle the data more precisely without side effects. Also on this way when we implement this project using the MVC pattern, we will able to create controllers separately without depending of a global variable