This repo has a basic API implementation which allows the creation and storing of data about studies, and submissions to those studies.
A study has a set number of available places, once this number of submissions has been received the study should be completed and no further submissions allowed.
The support team have recently received the following complaint from a researcher:
I set my study to have 100 available places, but I've received 103 submissions. I can't afford to pay for all of these submissions.
- Get the API set up and running.
- Identify the cause of the above bug, and write code to fix it.
- Write unit tests to cover this case, and ensure all tests are passing.
For the next part of the assignment, we would like you to:
- Extend the API to allow the submission list to be filtered for a specific user
- Perform a code review for the open pull request and offer comments for how it could be improved. Additionally, if you have any comments on the rest of the codebase and how you would refactor/improve it if you had the chance, please offer them too!
Install the latest version of python3
Clone and navigate to the repository
Install dependencies
python -m venv env
source env/bin/activate
pip install -r requirements.txt
Run tests
python manage.py test
Initialise database
python manage.py makemigrations prolific
python manage.py migrate
Run server (on localhost:8000)
python manage.py runserver
Returns a list of all existing studies
List all studies in database
> curl -XGET "http://localhost:8000/studies/"
HTTP 200 OK
[
{
"id": 1,
"name": "study1",
"total_places": 1,
"user_id": 1,
"status": "COMPLETED"
},
{
"id": 2,
"name": "study2",
"available_places": 1,
"user_id": 2,
"status": "COMPLETED"
},
{
"id": 3,
"name": "study3",
"available_places": 1,
"user_id": 2,
"status": "ACTIVE"
}
]
Creates a new study
name: str Name of the study total_places: int Maximum number of submissions allowed user_id: int Owning user of the study
> curl -XPOST "http://localhost:8000/studies/" -d "name=study1&total_places=1&user_id=1"
HTTP 201 Created
{
"id": 3,
"name": "study3",
"available_places": 1,
"user_id": 2,
"status": "ACTIVE"
}
Returns a list of all submissions for a given study
List all submissions for a given study
> curl -XGET "http://localhost:8000/studies/1/submissions/"
HTTP 200 OK
[
{
"id": 1,
"study_id": 1,
"user_id": 2,
"created_at": "2019-11-27T17:41:27.205398Z"
"completed_at": "2019-11-27T17:44:32.115467Z",
"status": "COMPLETED"
}
]
Returns a list of all existing submissions
List all submissions in database
> curl -XGET "http://localhost:8000/submissions/"
HTTP 200 OK
[
{
"id": 1,
"study_id": 1,
"user_id": 2,
"created_at": "2019-11-27T17:41:27.205398Z"
"completed_at": "2019-11-27T17:44:32.115467Z",
"status": "COMPLETED"
},
{
"id": 2,
"study_id": 2,
"user_id": 1,
"created_at": "2019-11-27T17:41:46.787398Z"
"completed_at": "2019-11-27T17:43:17.215294Z",
"status": "COMPLETED"
},
{
"id": 3,
"study_id": 3,
"user_id": 1,
"created_at": "2019-11-27T17:42:14.518398Z"
"completed_at": null,
"status": "ACTIVE"
}
]
Creates a new submission
study_id: int Study to respond to
user_id: int Owning user of the submission
> curl -XPOST 'http://localhost:8000/submissions/' -d "study_id=1&user_id=2"
HTTP 201 Created
{
"id": 3,
"study_id": 3,
"user_id": 1,
"created_at": "2019-11-27T17:42:14.518398Z"
"completed_at": null,
"status": "ACTIVE"
}
Updates an existing submission
action: str The update action to perform
> curl -XPOST 'http://localhost:8000/submissions/3' -d "action=complete"
HTTP 200 OK
{
"id": 3,
"study_id": 3,
"user_id": 1,
"created_at": "2019-11-27T17:42:14.518398Z"
"completed_at": "2019-11-28T11:21:18.183351Z",
"status": "COMPLETED_AT"
}