# API Specification

Values wrapped in `< >` are placeholders for what the field values should be. Also be sure to read the request route carefully when you implement it.

## Expected Functionality

### Get all courses

**`GET`**`/api/courses/`

{% code title="Response" %}

```javascript
<HTTP STATUS CODE 200>
{
    "courses": [
        {
            "id": 1,
            "code": "CS 1998",
            "name": "Intro to Backend Development",
            "assignments": [ <SERIALIZED ASSIGNMENT WITHOUT COURSE FIELD>, ... ],
            "instructors": [ <SERIALIZED USER WITHOUT COURSES FIELD>, ... ],
            "students": [ <SERIALIZED USER WITHOUT COURSES FIELD>, ... ]
        },
        {
            "id": 2,
            "code": "CS 1110",
            "name": "Intro to Computer Science: Python",
            "assignments": [ <SERIALIZED ASSIGNMENT WITHOUT COURSE FIELD>, ... ],
            "instructors": [ <SERIALIZED USER WITHOUT COURSES FIELD>, ... ],
            "students": [ <SERIALIZED USER WITHOUT COURSES FIELD>, ... ]
        }
        ...
    ]
}
```

{% endcode %}

{% hint style="warning" %}
It is important that you return the serialized relationships WITHOUT the course field. If you attempt to return them with the course field still there, your code will end up in an infinite loop. This is because your course, when returning its relationships (say, instructors), asks the instructors which courses they teach, which will then cause those courses to return their instructors, etc.
{% endhint %}

### Create a course

**`POST`**`/api/courses/`

{% code title="Request" %}

```javascript
{
    "code": "CS1998",
    "name": "Intro to Backend Development"
}
```

{% endcode %}

{% code title="Response" %}

```javascript
<HTTP STATUS CODE 201>
{
    "id": <ID>,
    "code": "CS1998",
    "name": "Intro to Backend Development",
    "assignments": [],
    "instructors": [],
    "students": []
}
```

{% endcode %}

{% hint style="info" %}
If the client does not provide a code or name or both, respond with an error message with status code 400.
{% endhint %}

### Get a specific course

**`GET`**`/api/courses/{id}/`

{% code title="Response" %}

```javascript
<HTTP STATUS CODE 200>
{
    "id": <ID>,
    "code": <STORED CODE FOR COURSE WITH ID {id}>,
    "name": <STORED NAME FOR COURSE WITH ID {id}>,
    "assignments": [ <SERIALIZED ASSIGNMENT WITHOUT COURSE FIELD>, ... ],
    "instructors": [ <SERIALIZED USER WITHOUT COURSES FIELD>, ... ],
    "students": [ <SERIALIZED USER WITHOUT COURSES FIELD>, ... ]
}
```

{% endcode %}

### Delete a specific course

**`DELETE`**`/api/courses/{id}/`

{% code title="Response" %}

```javascript
<HTTP STATUS CODE 200>
{
    "id": <ID>,
    "code": <STORED CODE FOR COURSE WITH ID {id}>,
    "name": <STORED NAME FOR COURSE WITH ID {id}>,
    "assignments": [ <SERIALIZED ASSIGNMENT WITHOUT COURSE FIELD>, ... ],
    "instructors": [ <SERIALIZED USER WITHOUT COURSES FIELD>, ... ],
    "students": [ <SERIALIZED USER WITHOUT COURSES FIELD>, ... ]
}
```

{% endcode %}

### Create a user

**`POST`**`/api/users/`

{% code title="Request" %}

```javascript
{
    "name": "Raahi Menon",
    "netid": "rm834"
}
```

{% endcode %}

{% code title="Response" %}

```javascript
<HTTP STATUS CODE 201>
{
    "id": <ID>,
    "name": "Raahi Menon",
    "netid": "rm834",
    "courses": []
}
```

{% endcode %}

{% hint style="info" %}
If the client does not provide a name or netid or both, respond with an error message.
{% endhint %}

### Get a specific user

**`GET`**`/api/users/{id}/`

{% code title="Response" %}

```javascript
<HTTP STATUS CODE 200>
{
    "id": <ID>,
    "name": <USER INPUT FOR NAME>,
    "netid": <USER INPUT FOR NETID>,
    "courses": [ <SERIALIZED COURSE WITHOUT ASSIGNMENTS, STUDENT, OR INSTRUCTOR FIELDS>, ... ]
}
```

{% endcode %}

{% hint style="info" %}
The `courses` field should contain ALL courses that user is in, both as a student and as an instructor (remember users can be both! Just like your backend instructors!).
{% endhint %}

### Add a user to a course

**`POST`**`/api/courses/{id}/add/`

{% code title="Request" %}

```javascript
{
    "user_id": <USER INPUT>,
    "type": "student" or "instructor"
}
```

{% endcode %}

{% code title="Response" %}

```javascript
<HTTP STATUS CODE 200>
{
    "id": <ID>,
    "code": <STORED CODE FOR COURSE WITH ID {id}>,
    "name": <STORED NAME FOR COURSE WITH ID {id}>,
    "assignments": [ <SERIALIZED ASSIGNMENT WITHOUT COURSE FIELD>, ... ],
    "instructors": [ <SERIALIZED USER WITHOUT COURSES FIELD>, ... ],
    "students": [ <SERIALIZED USER WITHOUT COURSES FIELD>, ... ]
}
```

{% endcode %}

{% hint style="info" %}
The field `instructors` or `students` in the response should include the newly added user in the respective array.&#x20;
{% endhint %}

### Create an assignment for a course

**`POST`**`/api/courses/{id}/assignment/`

{% code title="Request" %}

```javascript
{
    "title": "PA4",
    "due_date": 1553354209 // in Unix time (seconds since the epoch)
}
```

{% endcode %}

{% code title="Response" %}

```javascript
<HTTP STATUS CODE 201>
{
    "id": <ASSIGNMENT ID>,
    "title": "PA4",
    "due_date": 1553354209,  // in Unix time
    "course": {
        "id": {id},
        "code": <STORED CODE FOR COURSE WITH ID {id}>,
        "name": <STORED NAME FOR COURSE WITH ID {id}>
    }
}
```

{% endcode %}

{% hint style="info" %}
If the client does not provide a title or due date or both, respond with an error message (status code 400).
{% endhint %}

###
