Creating and maintaining REST APIs is THE everyday job for most of back-end developers, so it is a skill that we need to mature. In this project we'll practice every step of the API development process.
We are going to build the API that manages a contact-list database and publicly expose the endpoints so client applications (including ours) can use it. This time we'll include Groups, a new entity that will allow the system to group Contacts. Example: Work, Family, Friends. Hint: we can implement this logic by using a many-to-many relationship between the two tables.
Create an API with the following endpoints:
GET /contact/all
POST /contact
GET /contact/{contact_id}
DELETE /contact/{contact_id}
UPDATE /contact/{contact_id}
GET /group/all
POST /group
GET /group/{group_id}
UPDATE /group/{group_id}
DELETE /group/{group_id}
A contact must have the following data-structure on the database:
1# Contact 2 id: (int, primary_key) 3 full_name: (string, mandatory) 4 email: (string, mandatory) 5 address: (string, optional) 6 phone: (string, optional) 7 groups: (list of foreign_key) 8 9# Group 10 id: (int, primary_key) 11 name: (string, mandatory) 12 contacts: (list of foreign_key)
1REQUEST (application/json) 2 type: GET 3 body: null 4RESPONSE (application/json) 5 code: 200 | 404 | 500 6 body: [ 7 { 8 "full_name": "Dave Bradley", 9 "email": "dave@gmail.com", 10 "address":"47568 NW 34ST, 33434 FL, USA", 11 "phone":"7864445566", 12 "groups": [2,3] 13 }, 14 ... 15 ]
1REQUEST (application/json) 2 type: POST 3 path: /contact 4 body: { 5 "full_name": "Dave Bradley", 6 "email": "dave@gmail.com", 7 "address":"47568 NW 34ST, 33434 FL, USA", 8 "phone":"7864445566", 9 "groups": [2,3] 10 } 11RESPONSE (application/json) 12 code: 200 | 400 | 500 13 body: { 14 "id": 12 15 "full_name": "Dave Bradley", 16 "email": "dave@gmail.com", 17 "address":"47568 NW 34ST, 33434 FL, USA", 18 "phone":"7864445566", 19 "groups": [2,3] 20 }
1REQUEST (application/json) 2 type: GET 3 path: /contact/{contact_id} 4RESPONSE (application/json) 5 code: 200 | 404 | 400 | 500 6 body:{ 7 "id": 12 8 "full_name": "Dave Bradley", 9 "email": "dave@gmail.com", 10 "address":"47568 NW 34ST, 33434 FL, USA", 11 "phone":"7864445566", 12 "groups": [ 13 { 14 "id": 2, 15 "name": "Family" 16 },{ 17 "id": 3, 18 "name": "Gamers" 19 } 20 ] 21 }
1REQUEST (application/json) 2 type: PUT 3 path: /contact/{contact_id} 4 body: { 5 "full_name": "Dave Bradley", 6 "email": "dave@gmail.com", 7 "address":"47568 NW 34ST, 33434 FL, USA", 8 "phone":"7864445566", 9 "groups": [2,3] 10 } 11RESPONSE (application/json) 12 code: 200 | 404 | 400 | 500 13 body:{ 14 "id": 12 15 "full_name": "Dave Bradley", 16 "email": "dave@gmail.com", 17 "address":"47568 NW 34ST, 33434 FL, USA", 18 "phone":"7864445566", 19 "groups": [2,3] 20 }
1REQUEST (application/json) 2 type: DELETE 3 path: /contact/{contact_id} 4 body: null 5RESPONSE (application/json) 6 code: 200 | 404 | 500 7 body: { 8 "deleted": { 9 "id": 12, 10 "full_name": "Dave Bradley", 11 } 12 }
1REQUEST (application/json) 2 type: GET 3 path: /group/ 4 body: null 5RESPONSE (application/json) 6 code: 200 | 500 7 body: { 8 "data": [ 9 { 10 "id": 1, 11 "name": "Work" 12 },{ 13 "id": 2, 14 "name": "Gamers" 15 } 16 ] 17 }
1REQUEST (application/json) 2 type: GET 3 path: /group/{group_id} 4RESPONSE (application/json) 5 code: 200 | 404 | 400 | 500 6 body:{ 7 "id": 2 8 "name": "Work", 9 "contacts": [ 10 { 11 "id": 12 12 "full_name": "Dave Bradley", 13 "email": "dave@gmail.com", 14 "address":"47568 NW 34ST, 33434 FL, USA", 15 "phone":"7864445566", 16 "groups": [2,3] 17 } 18 ] 19 }
1 REQUEST (application/json) 2 type: PUT 3 path: /group/{group_id} 4 body: { 5 "name": "Beach Crew", 6 } 7 RESPONSE (application/json) 8 code: 200 | 404 | 400 | 500 9 body:{ 10 "id": 2 11 "name": "Beach Crew", 12 }
1 REQUEST (application/json) 2 type: DELETE 3 path: /group/{group_id} 4 body: null 5 RESPONSE (application/json) 6 code: 200 | 404 | 500 7 body: { 8 "deleted": { 9 "id": 2, 10 "name": "Beach Crew", 11 } 12 }
fetch
.Hint: Use Postman as a testing tool before you connect your front end application (React Contact List).
This exercise will make you practice the following fundamentals: