- fork and clone this repository
- run
bundle
to install gems and dependencies - run
rails db:create db:migrate
to create a local database - seed database with your comapny's customer base and tea offerings.
- run
rails s
You are now running the API! Pleas note these instructions are intended for local deployment on port 3000. Depending on your particular deployment solution, your base URL may be different from that noted in the instructions below.
To create a new subsciption; send a POST
request to /api/v1/subscriptions
with the queary parameters customer_id
and tea_id
representing valid database id's for both; and a frequency
paramater of:
- weekly
- semi_weekly
- monthly
- bi_monthly
- semi_monthly
- semi_anually
post "http://localhost:300/api/v1/subscriptions?customer_id=1&tea_id=3&frequency=monthly"
{
"data": {
"id": "2",
"type": "subscription",
"attributes": {
"title": "Cedric's Gen Mai Cha subscription",
"description": "Tea delivered to 130 Green Acres Dr, Burlington, VT, 05401",
"frequency": "monthly",
"price": 1250,
"status": "active",
"created_at": "2022-08-05T06:01:15.083Z",
"updated_at": "2022-08-05T06:01:15.083Z"
}
}
}
To cancel an existing subscription; send a PATCH
request to http://localhost:300/api/v1/subscriptions
with a valid subscription_id
queary parameter. If found, the subscription's status will be updated to cancelled
patch "http://localhost:300/api/v1/subscriptions?subscription_id=2"
{
"data": {
"id": "2",
"type": "subscription",
"attributes": {
"title": "Cedric's Gen Mai Cha subscription",
"description": "Tea delivered to 130 Green Acres Dr, Burlington, VT, 05401",
"frequency": "monthly",
"price": 1250,
"status": "cancelled",
"created_at": "2022-08-05T06:01:15.083Z",
"updated_at": "2022-08-05T06:29:15.725Z"
}
}
}
To reques a complete list of a user's subscriptions, active and cancelled; send a GET
request to "http://localhost:300/api/v1/subscriptions"
with a valid user ID. It will return a list list of all subscriptions ordered by created_at
by default.
get "http://localhost:300/api/v1/subscriptions?customer_id=1"
{
"data": [
{
"id": "1",
"type": "subscription",
"attributes": {
"title": "Gen Mai Cha Monthly",
"description": "whoops",
"frequency": "bi_monthly",
"price": 2100,
"status": "active",
"created_at": "2022-08-05T03:22:08.244Z",
"updated_at": "2022-08-05T03:22:08.244Z"
}
},
{
"id": "2",
"type": "subscription",
"attributes": {
"title": "Cedric's Gen Mai Cha subscription",
"description": "Tea delivered to 130 Green Acres Dr, Burlington, VT, 05401",
"frequency": "monthly",
"price": 1250,
"status": "active",
"created_at": "2022-08-05T06:01:15.083Z",
"updated_at": "2022-08-05T06:01:15.083Z"
}
}
]
}
The Schema for this PostgreSQL database is a simple many-to-many setup with subscriptions connecting one single customer to one tea. Given the constraints of the prompt, there is no way to tailor a quantity, and so assuming that each tea is sold in a fixed quantity and therefore an equal price (for simplicity's sake), price was therefore based entirely upon the user's indicated frequency
parameter, incorporating a slight logorithmic discount scale with quantity purchased and calculating price for an assumed monthly billing cycle. These are all set in the method ::price_setter(freq)
in app/models/subscription.rb