EcommerceAPI is a RESTful API for E-commerce Management developed using ASP.NET Core, Entity Framework Core, MySQL, and JWT for security. This project serves as an example of implementing a complete API with JWT-based authentication and authorization, and CRUD operations for product management.
- ASP.NET Core: Robust and scalable framework for developing web applications.
- Entity Framework Core: ORM to facilitate interaction with the MySQL database.
- MySQL: Relational database for data storage.
- JWT (JSON Web Tokens): Secure authentication and authorization implementation.
- CRUD Operations: Complete product management (create, read, update, delete).
- Swagger: Interactive API documentation and testing, which opens automatically in the browser when running
dotnet run.
- .NET Core SDK
- MySQL Server
- Visual Studio Code
- Postman (optional, for testing)
git clone https://github.com/yourusername/EcommerceAPI.git
cd EcommerceAPIModify the appsettings.json file with your MySQL database connection string:
{
"ConnectionStrings": {
"DefaultConnection": "server=localhost;database=ecommercedb;user=root;password=yourpassword;"
},
"Jwt": {
"Key": "yourSecretKey",
"Issuer": "yourIssuer",
"Audience": "yourAudience"
}
}Run the following commands to apply the migrations and create the database:
dotnet ef migrations add InitialCreate
dotnet ef database updateStart the application with the command:
dotnet runThe API will be available at https://localhost:5070, and Swagger UI will open automatically at https://localhost:5070/index.html.
-
User Registration
POST /api/auth/registerBody:
{ "Username": "testuser", "Password": "password123" } -
User Login
POST /api/auth/loginBody:
{ "Username": "testuser", "Password": "password123" }Response:
{ "token": "yourJWTtoken" }
Endpoints protected with JWT. Add the token in the Authorization header with the format Bearer <yourJWTtoken>.
-
Get All Products
GET /api/products -
Get Product by ID
GET /api/products/{id} -
Create a New Product
POST /api/productsBody:
{ "Name": "Product1", "Description": "Description1", "Price": 10.99 } -
Update a Product
PUT /api/products/{id}Body:
{ "Id": 1, "Name": "Updated Product", "Description": "Updated Description", "Price": 15.99 } -
Delete a Product
DELETE /api/products/{id}
The API includes Swagger integration for easy documentation and testing of endpoints. Swagger UI will automatically open in the browser when running the application with dotnet run, accessible at https://localhost:5070/index.html.
Contributions are welcome. Please open an issue or create a pull request for improvements and bug fixes.
This project is licensed under the MIT License. See the LICENSE file for details.
EcommerceAPI es una API RESTful para la Gestión de Comercio Electrónico desarrollada utilizando ASP.NET Core, Entity Framework Core, MySQL, y JWT para la seguridad. Este proyecto sirve como un ejemplo de implementación de una API completa con autenticación y autorización basadas en JWT, y operaciones CRUD para la gestión de productos.
- ASP.NET Core: Framework robusto y escalable para desarrollar aplicaciones web.
- Entity Framework Core: ORM para facilitar la interacción con la base de datos MySQL.
- MySQL: Base de datos relacional para almacenamiento de datos.
- JWT (JSON Web Tokens): Implementación de autenticación y autorización segura.
- Operaciones CRUD: Gestión completa de productos (crear, leer, actualizar, eliminar).
- Swagger: Documentación interactiva y pruebas de la API, se abre automáticamente en el navegador al ejecutar
dotnet run.
- .NET Core SDK
- MySQL Server
- Visual Studio Code
- Postman (opcional, para pruebas)
git clone https://github.com/tuusuario/EcommerceAPI.git
cd EcommerceAPIModificar el archivo appsettings.json con la cadena de conexión a tu base de datos MySQL:
{
"ConnectionStrings": {
"DefaultConnection": "server=localhost;database=ecommercedb;user=root;password=yourpassword;"
},
"Jwt": {
"Key": "yourSecretKey",
"Issuer": "yourIssuer",
"Audience": "yourAudience"
}
}Ejecutar los siguientes comandos para aplicar las migraciones y crear la base de datos:
dotnet ef migrations add InitialCreate
dotnet ef database updateIniciar la aplicación con el comando:
dotnet runLa API estará disponible en https://localhost:5070 y Swagger UI se abrirá automáticamente en https://localhost:5070/index.html.
-
Registro de Usuario
POST /api/auth/registerCuerpo:
{ "Username": "testuser", "Password": "password123" } -
Login de Usuario
POST /api/auth/loginCuerpo:
{ "Username": "testuser", "Password": "password123" }Respuesta:
{ "token": "yourJWTtoken" }
Endpoints protegidos con JWT. Añadir el token en el encabezado Authorization con el formato Bearer <yourJWTtoken>.
-
Obtener todos los productos
GET /api/products -
Obtener un producto por ID
GET /api/products/{id} -
Crear un nuevo producto
POST /api/productsCuerpo:
{ "Name": "Product1", "Description": "Description1", "Price": 10.99 } -
Actualizar un producto
PUT /api/products/{id}Cuerpo:
{ "Id": 1, "Name": "Updated Product", "Description": "Updated Description", "Price": 15.99 } -
Eliminar un producto
DELETE /api/products/{id}
La API incluye integración con Swagger para facilitar la documentación y prueba de los endpoints. Swagger UI se abrirá automáticamente en el navegador al ejecutar la aplicación con dotnet run, accesible en http://localhost:5070.
Las contribuciones son bienvenidas. Por favor, abre un issue o crea un pull request para mejoras y correcciones de errores.
Este proyecto está licenciado bajo la Licencia MIT. Consulta el archivo LICENSE para más detalles.