Untitled

 avatar
unknown
plain_text
3 years ago
2.2 kB
4
Indexable
Your task is to implement a simple REST API to manage a collection of stock trades. Each trade is a JSON entry with the following keys:


id: The unique trade ID. (Integer)
type: The trade type, either 'buy' or 'sell'. (String)
user_id: The unique user ID. (Integer):
symbol: The stock symbol. (String)
shares: The total number of shares traded. The traded shares value is between 10 and 30 shares, inclusive. (Integer)
price: The price of one share of stock at the time of the trade. (Integer)
timestamp: The epoch time of the stock trade in milliseconds. (Integer)

The task is to implement the REST service that exposes the  endpoint, which allows for managing the collection of trade records in the following way:

POST request to /trades:

creates a new trade
expects a JSON trade object without an id property as a body payload. The shares value might be out of accepted [1, 100] range, and type might be invalid (i.e., not 'buy' or 'sell'), and in such cases, the API must return error code 400. You can assume that besides that, the given payload is always valid.
if parameters are valid, adds the given trade object to the collection of trades and assigns a unique integer id to it. The first created trade must have id 1, the second one 2, and so on.
the response code is 201, and the response body is the created trade object

GET request to /trades:

returns a collection of all trades
the response code is 200, and the response body is an array of all trade objects ordered by their ids in increasing order
accepts the optional query parameter user_id. When user_id is provided, it returns trades of a specified user only.
accepts the optional query parameter type. When type is provided, it returns trades of a specified type only.

GET request to /trades/<id>:

returns a trade with the given id
if the matching trade exists, the response code is 200 and the response body is the matching trade object
if there is no trade with the given id in the collection, the response code is 404

DELETE, PUT, PATCH request to /trades/<id>:

the response code is 405 because the API does not allow deleting or modifying trades for any id value
Editor is loading...