Routes#
API routes for managing contactos in the "Contactos" application.
Routes
GET /contactos/agenda
: Retrieve the list of all contactos.POST /contactos/create
: Create a new contacto in the agenda.PUT /contactos/edit/{contacto_id}
: Edit an existing contacto by ID.DELETE /contactos/delete/{contacto_id}
: Delete a contacto by ID.GET /contactos/search/{nombre}
: Search for contactos by name.
Functions:
Name | Description |
---|---|
- `agenda_list` |
Retrieve the list of all contactos in the agenda. |
- `crear_contacto` |
Create a new contacto in the agenda. |
- `editar_contacto` |
Edit an existing contacto by ID. |
- `eliminar_contacto` |
Delete a contacto by ID. |
- `buscar_contacto` |
Search for contactos by name. |
Dependencies
- fastapi: FastAPI framework for building APIs.
- pydantic: Data validation and settings management using Python type annotations.
- sqlalchemy: SQL toolkit and Object-Relational Mapping (ORM) library.
- sqlmodel: SQL databases in Python, designed to be compatible with FastAPI.
app.db
: Module containing the database session..models
: Module containing theAgenda
model..schemas
: Module containing the response schemas (ContactoDB
,ContactoResponse
).
agenda_list(db)
#
Retrieve the list of all contactos in Agenda.
This endpoint returns all contactos recorded in the Contactos application.
- Returns:
list[Agenda]
: A list of all contactos in the database.
buscar_contacto(nombre, db)
async
#
Search for a contacto in the Agenda by name.
This endpoint allows you to search for a contacto by providing the name.
- Path Parameter:
nombre
- The name of the contacto to search for. - Returns:
ContactoResponse
: The result of the operation and matching results.
crear_contacto(contacto, db)
async
#
Create a new contacto in the Agenda.
This endpoint allows you to create a new contacto by providing the required details.
- Request Body:
ContactoDB
schema withnombre
,telefono
, and optionalcorreo
. - Returns:
ContactoResponse
: The result of the operation and the updated agenda.
editar_contacto(contacto_id, contacto, db)
async
#
Edit an existing contacto in the Agenda.
This endpoint allows you to edit an existing contacto by providing the contacto ID and updated details.
- Path Parameter:
contacto_id
- The ID of the contacto to be edited. - Request Body:
ContactoDB
schema with updatednombre
,telefono
, and optionalcorreo
. - Returns:
ContactoResponse
: The result of the operation and the updated agenda.
eliminar_contacto(contacto_id, db)
async
#
Delete a contacto from the Agenda.
This endpoint allows you to delete a contacto by providing the contacto ID.
- Path Parameter:
contacto_id
- The ID of the contacto to be deleted. - Returns:
ContactoResponse
: The result of the operation and the updated agenda.
Schemas#
Esquemas de validación de datos.
ContactoDB
#
Bases: BaseModel
Contacto(BaseModel) is a Pydantic model that represents the schema for creating a Contacto.
Attributes:
Name | Type | Description |
---|---|---|
nombre |
str
|
The name of the individual or entity associated with the Contacto. |
telefono |
str
|
The phone number of the individual or entity. This field is validated to ensure it is in a valid phone number format. |
correo |
EmailStr | None
|
The email address of the individual or entity. The field type EmailStr validates that the data has proper email format. This field is optional and can be None. |
Methods:
Name | Description |
---|---|
validate_telefono |
Validates the phone number format. Ensures the phone number: - Contains only digits or starts with a '+' followed by digits. - Has at least 10 digits (excluding the '+', if present). Raises a ValueError if the phone number contains invalid characters or is too short. |
validate_telefono(value)
#
Validate the phone number format.
Ensures the phone number contains only digits and has at least 10 digits.
Raises:
Type | Description |
---|---|
ValueError
|
If the phone number contains non-digit characters or is too short. |
ContactoResponse
#
Bases: BaseModel
Represents the result of an operation on Contacto data.
Models#
All the database models here.
Agenda
#
Bases: SQLModel
Modelo para registrar contactos en la base de datos.
Esta clase representa un contacto en la agenda, con atributos como nombre, teléfono y correo electrónico.
Se utiliza para interactuar con la tabla contactos_agenda
en la base de datos.
Atributos
- id (int | None): Identificador único del contacto. Es la clave primaria de la tabla.
- nombre (str): Nombre del contacto. Este campo es obligatorio.
- telefono (str): Número de teléfono del contacto. Este campo es obligatorio.
- correo (str | None): Dirección de correo electrónico del contacto. Este campo es opcional.
Configuración adicional
- La tabla asociada a este modelo se llama
contactos_agenda
. - Ejemplos de datos JSON para este modelo: [ {"id": 1, "nombre": "Juan Perez", "telefono": "+1234567890", "correo": "juan@example.com"}, {"id": 2, "nombre": "Maria Lopez", "telefono": "+9876543210", "correo": "maria@example.com"} ]