Answered step by step
Verified Expert Solution
Question
1 Approved Answer
The goal of this task is to provide an API with two endpoints: one for creating messages using the POST method and the other for
The goal of this task is to provide an API with two endpoints: one for creating messages using the POST method and the other for reading a list of them. The reading endpoint should be paginated with cursorbased pagination for more details take a look at "Hints" section
Requirements
The API must contain two endpoints:
POST messages
This endpoint will create a message stored in the local SQLite DB by calling provided saveMessage function. Each created message will receive an incremental id field from the database.
The payload body of this request will look like this:
json
"date":
"author": "John",
"channel": "general",
"text": Hi there"
The request will return a successful response with status code
and no payload.
If any of the properties is missing, the service should instead return a
status code.
GET messages
The endpoint should return the messages that are returned by findMessages function. This function does not accept any parameters and returns a Promise of all messages stored in the database. Order of those messages is not guaranteed.
If no query parameters are specified, this endpoint will return all messages sorted by internal DB index propert id D in descending order. The default count of records will be
The client may provide the following query parameters:
count number specifies the number of returned messages default:
next specifies a cursor pointing to the next record for the given request default: first records should be returned
Clients should take next cursor from the payload of one of the requests see payload below paging.next
Calling GET messages with a next
cursor will give the
issuer the next data for the given query. eg given messages A B C D first request with count returned messages D C and a next cursor. Sending second request with this next value and count should returns the next records: B and A
If there is nothing to show in the next cursor then the next
property must be set to the empty string.
Adding new message should not result in any shifting or duplication of records while paging.
For example, say the request GET messages has a total of messages. Initially, the first are returned with a cursor for messages If a new message is then added, the query with a cursor must still return the messages that were previously in places
even though they are actually now.
Example payload of a response looks like this:
json
"data":
"date":
"author" : "John"
"channel": "general",
"text": Hi hi
"date":
"author": "John",
"channel": "random",
"text": Hi there!"
"paging":
"next": MTMzNw
Sorting by id
should result in the newest messages being shown
first. So if client adds messages A B C D in that order then by requesting GET messagescount they should gif messages D and C
Hints
You can store data in a cursor in any way you want as long as the endpoint behaves as required. This might be just an id of the last record or encoded structure.
Common approach is to use id s from DB and filter the records that contain only higherlower id
This is possible when the id
is set by the database incrementally.
Relying on internal "indexing" id field of the data to ensures that adding new messages will not "break" the cursor.
The task uses following versions of dependencies:
"dependencies":
"bodyparser":
"express":
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started