The Barbershop API provides the backend services for managing appointments, reviews, barbers, and user profiles for a hairdressing salon. Built with Node.js, Express, and MongoDB, this API powers the Barbershop appointment management system.
- CRUD Operations: Manage users, barbers, appointments, and reviews.
- Authentication and Authorization: Secure routes with JWT tokens.
- Validation: Robust request validation with Joi.
- Error Handling: Centralized error management.
- API Documentation: Interactive Swagger UI.
- Role-based Access Control: Admin and user roles with permissions.
- Pagination: Efficient data retrieval with Mongoose pagination.
- Health Check Endpoint: Monitor API status.
- Node.js >= 12.0.0
- MongoDB
-
Clone the repository:
git clone https://github.com/stekatag/barbershop-api.git cd barbershop-api
-
Install dependencies:
yarn install
-
Set up environment variables:
bash cp .env.example .env
Modify.env
with your settings. -
Start the development server:
yarn dev
- Running locally:
yarn dev
- Running in production:
yarn start
- Testing:
yarn test
- Docker:
yarn docker:dev
(development),yarn docker:prod
(production) - Linting:
yarn lint
oryarn lint:fix
- Coverage:
yarn coverage
Configure your environment variables in the .env
file:
# Server configuration
PORT=3000
# Database
MONGODB_URL=mongodb://127.0.0.1:27017/barbershop-api
# JWT
JWT_SECRET=your_jwt_secret
JWT_ACCESS_EXPIRATION_MINUTES=30
JWT_REFRESH_EXPIRATION_DAYS=30
# Email service
SMTP_HOST=email-server
SMTP_PORT=587
SMTP_USERNAME=email-server-username
SMTP_PASSWORD=email-server-password
[email protected]
# For more .env variables, check the .env.example file
src\
|--config\ # Environment variables and configurations
|--controllers\ # Route controllers
|--docs\ # Swagger files
|--middlewares\ # Custom express middlewares
|--models\ # Mongoose models
|--routes\ # Routes
|--services\ # Business logic
|--utils\ # Utility classes and functions
|--validations\ # Request data validation schemas
|--app.js # Express app
|--index.js # App entry point
To view the list of available APIs and their specifications, run the server and go to http://localhost:3000/v1/docs
in your browser. This documentation page is automatically generated using the swagger definitions written as comments in the route files.
Auth routes:
POST /v1/auth/register
- RegisterPOST /v1/auth/login
- LoginPOST /v1/auth/refresh-tokens
- Refresh auth tokensPOST /v1/auth/forgot-password
- Send reset password emailPOST /v1/auth/reset-password
- Reset passwordPOST /v1/auth/send-verification-email
- Send verification emailPOST /v1/auth/verify-email
- Verify email
User routes:
POST /v1/users
- Create a userGET /v1/users
- Get all usersGET /v1/users/:userId
- Get a userPATCH /v1/users/:userId
- Update a userPATCH /v1/users/:userId/password
- Change user passwordDELETE /v1/users/:userId
- Delete a user
Barber routes:
GET /v1/barbers
- Get all barbersGET /v1/barbers/:barberId
- Get a barber by IDPATCH /v1/barbers/:barberId/assign
- Assign a barberPATCH /v1/barbers/:barberId/unassign
- Unassign a barber
Appointment routes:
POST /v1/appointments
- Create an appointmentGET /v1/appointments
- Get all appointmentsGET /v1/appointments/:appointmentId
- Get an appointmentPATCH /v1/appointments/:appointmentId
- Update an appointmentDELETE /v1/appointments/:appointmentId
- Delete an appointment
Review routes:
POST /v1/reviews
- Create a reviewGET /v1/reviews
- Get all reviewsGET /v1/reviews/:reviewId
- Get a reviewPATCH /v1/reviews/:reviewId
- Update a reviewDELETE /v1/reviews/:reviewId
- Delete a review
Service routes:
POST /v1/services
- Create a serviceGET /v1/services
- Get all servicesGET /v1/services/:serviceId
- Get a service by IDPATCH /v1/services/:serviceId
- Update a serviceDELETE /v1/services/:serviceId
- Delete a service
Service Category routes:
POST /v1/service-categories
- Create a service categoryGET /v1/service-categories
- Get all service categoriesGET /v1/service-categories/:categoryId
- Get a service category by IDPATCH /v1/service-categories/:categoryId
- Update a service categoryDELETE /v1/service-categories/:categoryId
- Delete a service category
Health check route:
GET /v1/health
- Check API health status
The API uses centralized error handling. Errors are captured and returned in a consistent format. In development, stack traces are included.
{
"code": 404,
"message": "Not found"
}
The API uses Joi for request validation. Validation schemas are defined in the src/validations
directory and are used in conjunction with the validate
middleware.
Authentication is handled using JWT tokens. Secure routes by adding the auth
middleware. For role-based access control, the middleware can accept permissions.
router.patch('/users/:userId', auth('manageUsers'), userController.updateUser);
The API uses Winston for logging, with different levels (error, warn, info) logged depending on the environment. HTTP request logs are managed by Morgan.
The project includes custom Mongoose plugins for transforming JSON responses and implementing pagination. These are added to the schemas in the src/models
directory.
Contributions are welcome! Please refer to the contributing guide.