Crear y mantener las API REST es EL trabajo diario para la mayoría de los desarrolladores backend, por lo que es una habilidad que necesitamos para madurar. En este proyecto practicaremos cada paso del proceso de desarrollo de API.
Vamos a construir la API que gestiona una base de datos de contactos y expondrá públicamente los endpoints para que las aplicaciones del cliente (incluida la nuestra) puedan usarla. Esta vez incluiremos Groups, una nueva entidad que permitirá al sistema agrupar Contacts. Ejemplo: Trabajo, Familia, Amigos. Sugerencia: podemos implementar esta lógica utilizando una relación many-to-many entre las dos tablas.
Crea una API con los siguientes 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}
Un contacto debe tener la siguiente estructura de datos en la base de datos:
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)
REQUEST (application/json)
type: GET
body: null
RESPONSE (application/json)
code: 200 | 404 | 500
body: [
{
"full_name": "Dave Bradley",
"email": "dave@gmail.com",
"address":"47568 NW 34ST, 33434 FL, USA",
"phone":"7864445566",
"groups": [2,3]
},
...
]
REQUEST (application/json)
type: POST
path: /contact
body: {
"full_name": "Dave Bradley",
"email": "dave@gmail.com",
"address":"47568 NW 34ST, 33434 FL, USA",
"phone":"7864445566",
"groups": [2,3]
}
RESPONSE (application/json)
code: 200 | 400 | 500
body: {
"id": 12
"full_name": "Dave Bradley",
"email": "dave@gmail.com",
"address":"47568 NW 34ST, 33434 FL, USA",
"phone":"7864445566",
"groups": [2,3]
}
REQUEST (application/json)
type: GET
path: /contact/{contact_id}
RESPONSE (application/json)
code: 200 | 404 | 400 | 500
body:{
"id": 12
"full_name": "Dave Bradley",
"email": "dave@gmail.com",
"address":"47568 NW 34ST, 33434 FL, USA",
"phone":"7864445566",
"groups": [
{
"id": 2,
"name": "Family"
},{
"id": 3,
"name": "Gamers"
}
]
}
REQUEST (application/json)
type: PUT
path: /contact/{contact_id}
body: {
"full_name": "Dave Bradley",
"email": "dave@gmail.com",
"address":"47568 NW 34ST, 33434 FL, USA",
"phone":"7864445566",
"groups": [2,3]
}
RESPONSE (application/json)
code: 200 | 404 | 400 | 500
body:{
"id": 12
"full_name": "Dave Bradley",
"email": "dave@gmail.com",
"address":"47568 NW 34ST, 33434 FL, USA",
"phone":"7864445566",
"groups": [2,3]
}
REQUEST (application/json)
type: DELETE
path: /contact/{contact_id}
body: null
RESPONSE (application/json)
code: 200 | 404 | 500
body: {
"deleted": {
"id": 12,
"full_name": "Dave Bradley",
}
}
REQUEST (application/json)
type: GET
path: /group/
body: null
RESPONSE (application/json)
code: 200 | 500
body: {
"data": [
{
"id": 1,
"name": "Work"
},{
"id": 2,
"name": "Gamers"
}
]
}
REQUEST (application/json)
type: GET
path: /group/{group_id}
RESPONSE (application/json)
code: 200 | 404 | 400 | 500
body:{
"id": 2
"name": "Work",
"contacts": [
{
"id": 12
"full_name": "Dave Bradley",
"email": "dave@gmail.com",
"address":"47568 NW 34ST, 33434 FL, USA",
"phone":"7864445566",
"groups": [2,3]
}
]
}
REQUEST (application/json)
type: PUT
path: /group/{group_id}
body: {
"name": "Beach Crew",
}
RESPONSE (application/json)
code: 200 | 404 | 400 | 500
body:{
"id": 2
"name": "Beach Crew",
}
REQUEST (application/json)
type: DELETE
path: /group/{group_id}
body: null
RESPONSE (application/json)
code: 200 | 404 | 500
body: {
"deleted": {
"id": 2,
"name": "Beach Crew",
}
}
fetch
.Sugerencia: use Postman como una herramienta de prueba antes de conectar su aplicación frontal (React Contact List).
Este ejercicio te hará practicar los siguientes fundamentos:
Para prepararte mejor para completar estos ejercicios, sugerimos los siguientes materiales