Create an API that can be used to:
- Find out the number of days between two datetime parameters.
- Find out the number of weekdays between two datetime parameters.
- Find out the number of complete weeks between two datetime parameters.
- Accept a third parameter to convert the result of (1, 2 or 3) into one of seconds, minutes, hours, years.
- Allow the specification of a timezone for comparison of input parameters from different timezones.
AdonisJS is a backend framework for Node.js. The framework is written in TypeScript and the application created using AdonisJS is also in TypeScript. It includes everything that is required to create a fully functional web app or an API server. AdonisJS offers a stable ecosystem to write server-side web application. Further explanation can be found here
The luxon library is used for dealing with date time in this project.
Japa is a test runner to create test runners. It is a tiny Node.js test runner that can be used to test apps or even create test runner. It is simple, fast and has minimal core. It doesn't ship with any CLI. Read More
In this project, supertest is used to make HTTP requests. More about the supertest can be found here.
Command for running the test
npm run test
OR
node -r @adonisjs/assembler/build/register japaFile.ts test
In this project middleware is used to check the validity of the input. Middleware is a series of function that are executed during an HTTP request before it reaches the route handler. Every middleware class must implement the handle method to handle the http request and call the next method to forward the request to the next middleware or the route handler. More about the middleware in adonis can be found here.
Link for the Further explanation
- AdonisJS is a Node.js framework, and hence it requires Node.js to be installed on computer. We need at least the latest release of Node.js v14.
- Use command below to install all the required packages
npm install
- API development environment : Postman
Command for running the app
node ace serve --watch
OR
npm run dev
The port number is defined in .env file which is 80. So, after running the app, the user needs to enter http://localhost/ in the browser for GET request and http://localhost/ROUTENAME in the API development Environment for POST request.
WHERE ROUTENAME can be listed using the command below:
Command for listing all the routes
node ace list:routes
Method | Route | Handler | Middleware | Name |
---|---|---|---|---|
HEAD, GET | /uploads/* | Closure | drive.local.serve | |
HEAD, GET | / | Closure | ||
POST | /days | DaysController.handleRequest | ||
POST | /weeks | WeeksController.handleRequest | ||
POST | /weekdays | WeekDays.controller.handleRequest |
These api accepts "first_date" and "second_date" as two parameters and optional parameters which are "format(Output type)" and "timezone" to give the required results.
This API is for calculation of numbers of days between two dates
Input with first_date and second_date parameter
{
"first_date" : "2019-03-20T00:01:00 Asia/Damascus" ,
"second_date" : "2019-03-17T00:01:00 Asia/kuwait"
}
Result in default format
{
"result" : 3
}
Input with format
{
"first_date" : "2022-09-20T00:01:00 Asia/Damascus" ,
"second_date" : "2021-03-21T00:01:00 Asia/kuwait",
"format" : "seconds"
}
Output in Seconds
{
"result" : 47347200
}
Input with format and Timezone
{
"first_date" : "2022-03-20T00:01:00 Asia/Damascus" ,
"second_date" : "2021-03-21T00:01:00 Asia/kuwait",
"format" : "seconds" ,
"timezone" : "Asia/Kuala_lumpur"
}
Output in seconds
{
"result" : 31449600
}
Input with wrong first_date parameter
{
"first_date": "sdfdsfsdf",
"second_date": "2021-03-21T00:01:00 Asia/Kuwait",
"format": "days",
"timezone": "Asia/Kuala_Lumpur"
}
Result
{
"error": "Date is in incorrect form",
"parameter": "first_date"
}
This API is for calculation of numbers of complete weeks between two dates
Input with all the possible parameters
{
"first_date" : "2017-03-20T00:01:00 Asia/Damascus" ,
"second_date" : "2014-03-17T12:01:00 Asia/kuwait",
"format" : "minutes" ,
"timezone" : "Asia/Kuwait"
}
Output in Minutes
{ "result" : 1572480 }
Input all the parameters except timezone.
{
"first_date": "2021-05-01T00:00:00",
"second_date": "2021-05-15T00:00:00",
"format": "seconds"
}
Result in seconds
{
"result": 1209600
}
Input with all the parameters and expected result in minutes
{
"first_date": "2021-03-20T00:01:00",
"second_date": "2021-05-23T00:01:00",
"format": "minutes",
"timezone" : "Asia/kuwait"
}
Result in minutes
{
"result": 90720
}
Input with incorret format parameter
{
"first_date": "2021-03-20T00:01:00 Asia/Damascus",
"second_date": "2021-03-21T00:01:00 Asia/Kuwait",
"format": "secon",
"timezone": "Asia/Kuala_Lumpur"
}
Expected Output
{
"error": "Parameter is not acceptable",
"parameter": "format"
}
This API is for calculation of numbers of weekdays between two dates
Input with first_date and second_date parameter
{
"first_date" : "2022-03-20T00:01:00 Asia/Damascus" ,
"second_date" : "2021-03-17T00:01:00 Asia/kuwait"
}
Output in default format
{
"result" : 263
}
Input with all the parameters except timezone
{
"first_date": "2021-05-01T00:00:00",
"second_date": "2021-05-15T00:00:00",
"format": "seconds"
}
Output in seconds
{
"result": 864000
}
Input with all the parameters and expected number of weekdays in years.
{
"first_date": "2021-03-20T00:01:00 Australia/sydney",
"second_date": "2023-05-17T00:01:00",
"format": "years",
"timezone" : "Asia/kuwait"
}
Output in years
{
"result": 1
}
Input with wrong timezone
{
"first_date": "2021-03-20T00:01:00 Asia/kuwait",
"second_date": "2021-01-17T12:02:01 Asia/Kuala_Lumpur",
"format": "seconds",
"timezone": "Asiala_Lur"
}
Error shown as a result
{
"error": "The timezone is invalid",
"parameter": "timezone"
}