Overview
The Users API provides endpoints for managing user accounts in the Adoptme system. Users can be listed, retrieved, updated, and deleted. Each user has a profile with personal information and a list of adopted pets.
Base Path: /api/users
Source Files:
Routes: src/routes/users.router.js
Controller: src/controllers/users.controller.js
Model: src/dao/models/User.js
User Model
The User schema includes the following fields:
MongoDB-generated unique identifier
User’s email address (must be unique)
User role (default: “user”)
Array of pet ObjectIds that the user has adopted [
{ "_id" : "60acc54545c8e82e0475100a" },
{ "_id" : "60acc54545c8e82e0475100b" }
]
Get All Users
curl -X GET http://localhost:8080/api/users
Endpoint
Retrieves a list of all users in the system.
Response
Example Response
{
"status" : "success" ,
"payload" : [
{
"_id" : "6893eaba2ac0b16fa177be7a" ,
"first_name" : "Jerónimo" ,
"last_name" : "Arroyo Alonzo" ,
"email" : "[email protected] " ,
"password" : "$2b$10$xjkkakjsiz..." ,
"role" : "user" ,
"pets" : []
},
{
"_id" : "6893eaba2ac0b16fa177be7b" ,
"first_name" : "María" ,
"last_name" : "González" ,
"email" : "[email protected] " ,
"password" : "$2b$10$abc123..." ,
"role" : "user" ,
"pets" : [
{ "_id" : "60acc54545c8e82e0475100a" }
]
}
]
}
Error Responses
Show 500 Internal Server Error
Ha ocurrido un error en la petición, por favor ver detalle: [error]
Get User by ID
curl -X GET http://localhost:8080/api/users/6893eaba2ac0b16fa177be7a
Endpoint
Retrieves a single user by their unique ID.
Path Parameters
The unique identifier (MongoDB ObjectId) of the user
Response
User object with all fields
Example Response
{
"status" : "success" ,
"payload" : {
"_id" : "6893eaba2ac0b16fa177be7a" ,
"first_name" : "Jerónimo" ,
"last_name" : "Arroyo Alonzo" ,
"email" : "[email protected] " ,
"password" : "$2b$10$xjkkakjsiz..." ,
"role" : "user" ,
"pets" : []
}
}
Error Responses
{
"status" : "error" ,
"error" : "User not found"
}
Returned when the user with the specified ID doesn’t exist.
Show 500 Internal Server Error
Ha ocurrido un error en la petición, por favor ver detalle: [error]
Update User
curl -X PUT http://localhost:8080/api/users/6893eaba2ac0b16fa177be7a \
-H "Content-Type: application/json" \
-d '{
"first_name": "Jerónimo",
"last_name": "Arroyo Pérez",
"email": "[email protected] "
}'
Endpoint
Updates an existing user’s information.
Path Parameters
The unique identifier (MongoDB ObjectId) of the user to update
Request Body
Send any user fields you want to update. All fields are optional:
Updated pets array (array of pet ObjectIds)
Do not update the password field directly through this endpoint. Password updates should go through proper authentication flows with hashing.
Example Request Body
{
"first_name" : "Jerónimo" ,
"last_name" : "Arroyo Pérez" ,
"email" : "[email protected] "
}
Response
Example Response
{
"status" : "success" ,
"message" : "User updated"
}
Error Responses
{
"status" : "error" ,
"error" : "User not found"
}
Returned when the user with the specified ID doesn’t exist.
Show 500 Internal Server Error
Ha ocurrido un error en la petición, por favor ver detalle: [error]
Delete User
curl -X DELETE http://localhost:8080/api/users/6893eaba2ac0b16fa177be7a
Endpoint
Deletes a user from the system.
Path Parameters
The unique identifier (MongoDB ObjectId) of the user to delete
Response
Example Response
{
"status" : "success" ,
"message" : "User deleted"
}
Error Responses
{
"status" : "error" ,
"error" : "User not found"
}
Returned when the user with the specified ID doesn’t exist.
Show 500 Internal Server Error
Ha ocurrido un error en la petición, por favor ver detalle: [error]
Deleting a user does not automatically delete their adoptions or remove them from adopted pets. You may need to handle these relationships separately.
Implementation Details
Controller Source
The Users controller (src/controllers/users.controller.js) implements the following logic:
Show getAllUsers - Lines 3-11
const getAllUsers = async ( req , res ) => {
try {
const users = await usersService . getAll ();
res . send ({ status: "success" , payload:users })
} catch ( error ) {
res . status ( 500 ). send ( "Ha ocurrido un error en la petición, por favor ver detalle: " , error )
}
}
Show getUser - Lines 13-23
const getUser = async ( req , res ) => {
try {
const userId = req . params . uid ;
const user = await usersService . getUserById ( userId );
if ( ! user ) return res . status ( 404 ). send ({ status: "error" , error: "User not found" })
res . send ({ status: "success" , payload:user })
} catch ( error ) {
res . status ( 500 ). send ( "Ha ocurrido un error en la petición, por favor ver detalle: " , error )
}
}
Show updateUser - Lines 25-37
const updateUser = async ( req , res ) => {
try {
const updateBody = req . body ;
const userId = req . params . uid ;
const user = await usersService . getUserById ( userId );
if ( ! user ) return res . status ( 404 ). send ({ status: "error" , error: "User not found" })
const result = await usersService . update ( userId , updateBody );
res . send ({ status: "success" , message: "User updated" })
} catch ( error ) {
res . status ( 500 ). send ( "Ha ocurrido un error en la petición, por favor ver detalle: " , error )
}
}
Show deleteUser - Lines 39-50
const deleteUser = async ( req , res ) => {
try {
const userId = req . params . uid ;
const user = await usersService . getUserById ( userId );
if ( ! user ) return res . status ( 404 ). send ({ status: "error" , error: "User not found" })
const result = await usersService . delete ( user );
res . send ({ status: "success" , message: "User deleted" })
} catch ( error ) {
res . status ( 500 ). send ( "Ha ocurrido un error en la petición, por favor ver detalle: " , error )
}
}
Router Configuration
Routes are defined in src/routes/users.router.js:
router . get ( '/' , usersController . getAllUsers );
router . get ( '/:uid' , usersController . getUser );
router . put ( '/:uid' , usersController . updateUser );
router . delete ( '/:uid' , usersController . deleteUser );
Sessions API Register and authenticate users
Adoptions API Link users to adopted pets