Preview 1 | Preview 2 | Preview 3 |
---|---|---|
This project is a robust RESTful API built with Elysia.js, following the enterprise MVC (Model-View-Controller) architecture. It leverages Prisma ORM for efficient database management and uses TypeScript to enhance type safety and developer experience.
- Create and manage cow data (code, color, age) through CowModel and CowService
- Simulate milking and calculate BSOD chances in CowController
- Generate milk production reports via MilkProductionService
- Display results through GUI using HTML generated from CowView
- Manage BSOD status and lemon feeding in CowController
This project follows a modified Model-View-Controller (MVC) architecture pattern, adapted for a RESTful API using Elysia.js. Here's an overview of each component and its corresponding folder in the project structure:
CowModel.ts
andMilkProductionModel.ts
- Define data structures, validation, and business logic
- Handle data management, storage, and retrieval
- Use Elysia.model for schema validation
- In a RESTful API, the "View" is the data sent back to the client
- Managed by Elysia.js through route handlers and data serialization
- CowView.ts creates HTML templates for displaying cow data and milk production results
- Swagger UI is used for API documentation
CowController.ts
- Defines API endpoints and manages the request/response cycle
- Uses models for request validation and response formatting
- Calls appropriate service methods to process requests
CowService.ts
andMilkProductionService.ts
- Contain business logic that doesn't belong in models or routes
- Handle complex operations, often involving database interactions
- Use Prisma client for database operations
- Controller receives HTTP requests, calls Service to process data
- Service uses Model for data validation and database operations
- Controller uses View (in this case, sending data back) to send response
index.ts
is the application entry point, configured with Elysia.js, and invokesCowController
- π¦ Elysia.js: A lightweight, flexible web framework for Node.js
- π οΈ Prisma: Modern database toolkit for TypeScript and Node.js
- π Bun: JavaScript runtime for building web applications
- π¦ TypeScript: Typed superset of JavaScript
- π¦ MariaDB: Open-source relational database management system
- π¦ Swagger UI: Open-source tool for API documentation
-
Clone the repository:
git clone https://github.com/PunGrumpy/kmitl-mvc.git cd kmitl-mvc
-
Install the dependencies:
bun install
-
Configure the environment variables:
- Create a
.env
file in the root of the project. - Add the following environment variables:
DATABASE_URL="mysql://username:password@localhost:3306/your_database_name"
- Create a
-
Create Prisma client:
bun db:generate
-
Run the migrations:
bun db:migrate
-
Start the development server:
bun dev
-
Access the API:
- Open your browser and go to
http://localhost:3000
for the Swagger documentation. - The API endpoints will be available at
http://localhost:3000/users
.
- Open your browser and go to
.
βββ src/
β βββ models/
β β βββ userModel.ts
β βββ controllers/
β β βββ userControllers.ts
β βββ services/
β βββ userService.ts
βββ prisma/
β βββ schema.prisma
βββ index.ts
βββ .env
βββ package.json
βββ README.md
GET /api/cows
: Retrieve all cows or find a cow by codePOST /api/cows
: Create a new cowPOST /api/cows/reset-bsod
: Reset BSOD status for all cowsPOST /api/cows/:code/add-lemon
: Add lemon to a cowPOST /api/milk/:code
: Milk a cowGET /api/milk/report
: Get milk production report
For detailed API documentation, visit the Swagger UI at http://localhost:3000
when the server is running.
Warning
This API endpoint may change in the future. Please refer to the Swagger documentation for the latest information.
Note
In MVC architecture, API endpoints are considered as the "View" layer.
To test the database connection:
bun test:connection
This project uses Bun as the JavaScript runtime. Here are some useful commands:
bun dev
: Start the development serverbun db:generate
: Generate Prisma clientbun db:migrate
: Run database migrationsbun test:connection
: Test database connection
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
This project is licensed under the MIT License. See the LICENSE file for more information.
If you have any questions or need help, please open an issue in the GitHub repository or contact the maintainer.