The shopcarts resource allows customers to make a collection of products that they want to purchase
IBM Cloud Foundry App for this project can be accessed at here.
To run the service in development mode, please use:
FLASK_APP=service FLASK_ENV=development flask run -h 0.0.0.0 -p 5000
The project uses honcho which gets it's commands from the Procfile
. To start the service in production mode, simply use:
honcho start
Name | Type | Purpose |
---|---|---|
id | int | Primary key for the table |
user_id | int | User id whose shopcart this is |
create_time | datetime | Time when shopcart was created |
update_time | datetime | Time when shopcart was last modified |
Name | Type | Purpose |
---|---|---|
id | int | Primary key for the table |
sid | int | Shopcart id in which the item is present |
sku | int | Product/Item id |
name | string | Name of product/item |
price | float | Price of the product/item |
amount | int | Count of the item in the shopcart |
create_time | datetime | Time when item was added to the shopcart |
update_time | datetime | Time when the item in the shopcart was last modified |
Run the tests using nose
$ nosetests
Nose is configured via the included setup.cfg
file to automatically include the flags --with-spec --spec-color
so that red-green-refactor is meaningful. If you are in a command shell that supports colors, passing tests will be green while failing tests will be red.
Nose is also configured to automatically run the coverage
tool and you should see a percentage of coverage report at the end of your tests. If you want to see what lines of code were not tested use:
$ coverage report -m
This is particularly useful because it reports the line numbers for the code that is not covered so that you can write more test cases to get higher code coverage.
You can also manually run nosetests
with coverage
(but setup.cfg
does this already)
$ nosetests --with-coverage --cover-package=service
Logging is set up to track events.
URL | HTTP method | Description |
---|---|---|
/shopcarts | GET | Get a shopcart list |
/shopcarts | POST | Create a shopcart |
/shopcarts/<:id> | DELETE | Delete a shopcart |
/shopcarts/<:id>/items | GET | Get item list from a shopcart |
/shopcarts/<:id>/items | POST | Create a shopcart item |
/shopcarts/<:id>/items/<item_id> | PUT | Update a shopcart item |
/shopcarts/<:id>/items/<item_id> | DELETE | Delete a shopcart item |
/shopcarts/<:id>/items/<item_id> | GET | Gets a shopcart item |
/shopcarts/items | GET | Get all shopcart items |
/shopcarts/<:id>/place-order | PUT | Place an order |
A shopcart can be created with a POST
request on '/shopcarts'
with, for example, the required conent including the following parameters:
{
"id": null,
"user_id": 101,
"create_time": null,
"update_time": null
}
A shopcart item can be created with a POST
request on /shopcarts/:id/items'
with required parameters:
{
"id": null,
"sid": 100,
"sku": 5000,
"name": "soap",
"price": 2.23,
"amount": 3,
"create_time": null,
"update_time": null
}
On success the response code is 201 and the newly created shopcart or shopcart item will be contained in the response in JSON format. The location header of the response will contain the id of the newly created shopcart, for example /shopcarts/1
, or for a shopcart item, /shopcartitems/1
.
To get the list of items in a shopcart, you can hit a GET
request with the id of the shopcart you are looking for.
curl --location --request GET 'http://0.0.0.0:5000/shopcartitems/10'
The response will be something like this if
- There are items in the shopcart - status code 200
{
"amount": 1,
"create_time": "2020-10-18T17:21:44.626229",
"id": 1,
"name": "hello",
"price": 100.0,
"sid": 10,
"sku": 10,
"update_time": "2020-10-18T17:21:44.626229"
},
{
"amount": 1,
"create_time": "2020-10-18T17:22:06.844597",
"id": 2,
"name": "hello2",
"price": 100.0,
"sid": 10,
"sku": 20,
"update_time": "2020-10-18T17:22:06.844597"
},
{
"amount": 1,
"create_time": "2020-10-18T17:22:14.123710",
"id": 3,
"name": "hello3",
"price": 100.0,
"sid": 10,
"sku": 30,
"update_time": "2020-10-18T17:22:14.123710"
}
]
- If the shopcart is empty or shopcart is not found status code 404
{}
A shopcart list can be got with a GET
request on '/shopcarts'
. The response will be something like
[
{
"create_time": "2020-10-17T04:17:28.593445",
"id": 1,
"update_time": "2020-10-17T04:17:28.593445",
"user_id": 435345
},
{
"create_time": "2020-10-17T04:17:29.696271",
"id": 2,
"update_time": "2020-10-17T04:17:29.696271",
"user_id": 435345
},
{
"create_time": "2020-10-17T04:17:30.522651",
"id": 3,
"update_time": "2020-10-17T04:17:30.522651",
"user_id": 435345
}
]
A shopcart item can be updated with a PUT
request on '/shopcarts/:id/items/:item_id'
.
When an authorized user hits a PUT request on '/shopcartitems', the API will return the updated shopcart item with a status code 200. The content for the PUT request would something like this:
{
"amount": 4,
}
The response will contain the full shopcart item.
A shopcart can be queried by user with a GET
request on /shopcarts
with the user_id set in the query string of the request, for example, /shopcarts?user_id=100
The response will be the shopcart for that user, or 404 if a shopcart does not exist for that user.
Shopcart items can be queried by sku, name, price, or amount with a GET
request on /shopcarts/items
with the appropriate field indicated in the query of the request, for example, /shopcarts/items?sku=1000
The response will be a list of shopcart items where the indicated field has the desired value, or 404 if no shopcart items contain a field with that value.
To delete a shopcart and everything in it, make a DELETE
request to /shopcarts/:id
.
To delete a shopcart item, make a DELETE
request to /shopcarts/:id/items/:item_id
.
To place an order, make a PUT
request to /shopcarts/:id/place-order
. The shopcart will be deleted after the order is placed.