Appointy_Task
This api has been built according to the task assigned by Apponity. All the tasks Provided have been completed
Link to the Document of the task
Please excuse my mistakes in formatting or in the way program is structured if there are any, As this was the first time I have worked with the language GO
List of Task:
- Api call to add new user
- Api call to retrieve user based on the user_id
- Api call to add posts
- Api call to retrieve post based on post_id
- Api call to retrieve all posts made by a particular user
- The Api should be made using GO
- MongoDB should be used for the database
- Only particular libraries and packages were allowed
- Password should be stored safely, so that it cannot be reengineered
- The API should be thread safe
- There must be pagination at list end points
- There should be unit tests for all functions
Methods
Security of Password
For the security and safety of the password I have added salting and Hashing before storing it in the database.
hashedPassword, _ := bcrypt.GenerateFromPassword([]byte(u.Password), 8)
The function bcrypt has been used for this functionality
Thread safety of the Api
For thread safety so that only one user can update the database at a time. The lock.Lock()
function has been applied on all the functions that have the ability to alter the database. In case of functions that don't alter the database the lock.Lock()
function has not been applied because that would unnecessarlily make the api slower.
Pagination
All the API calls return only one json object except the last API which returns a list of objects containing posts generated by a particular user. In this particualr API call pagination has been applied by limiting the number of posts returned to 10.
findOptions := options.Find()
findOptions.SetLimit(5)
findOptions.SetSkip(0)
This is the code used for it
output of API calls :
Output of Adding a user to the MongoDB database :
As you can see the Password is not in plain text it is in a hashed form
Output of Retrieving the User based on the user_id :
Output of Adding a Post to the MongoDB database :
Output of Retrieving a post based on the post_id :
Output of Retrieving all the posts made by a particular user based on the user_id :
Because of Pagination a maximum 10 posts can be retrieved this will improve the time for API call