Skip to content

πŸŽ“ Exit Exam for MVC (Model View and Controller)

License

Notifications You must be signed in to change notification settings

PunGrumpy/kmitl-mvc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

27 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🦊 KMITL Exit Exam (MVC)

Preview 1 Preview 2 Preview 3
Preview Preview Preview

πŸ“„ Description

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.

  1. Create and manage cow data (code, color, age) through CowModel and CowService
  2. Simulate milking and calculate BSOD chances in CowController
  3. Generate milk production reports via MilkProductionService
  4. Display results through GUI using HTML generated from CowView
  5. Manage BSOD status and lemon feeding in CowController

🧱 MVC Architecture

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:

πŸ“ Models (src/models/)

  • CowModel.ts and MilkProductionModel.ts
  • Define data structures, validation, and business logic
  • Handle data management, storage, and retrieval
  • Use Elysia.model for schema validation

πŸ–₯️ Views (Swagger UI)

  • 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

πŸ›£οΈ Routes and Controllers (src/controllers/)

  • 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

πŸ› οΈ Services (src/services/)

  • CowService.ts and MilkProductionService.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

πŸ‘― How They Work Together

  1. Controller receives HTTP requests, calls Service to process data
  2. Service uses Model for data validation and database operations
  3. Controller uses View (in this case, sending data back) to send response
  4. index.ts is the application entry point, configured with Elysia.js, and invokes CowController

πŸ“¦ Technologies

  • 🦊 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

πŸš€ Getting Started

πŸ“‹ Prerequisites

πŸ› οΈ Installation Steps

  1. Clone the repository:

    git clone https://github.com/PunGrumpy/kmitl-mvc.git
    cd kmitl-mvc
  2. Install the dependencies:

    bun install
  3. 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"
  4. Create Prisma client:

    bun db:generate
  5. Run the migrations:

    bun db:migrate

πŸƒβ€β™‚οΈ Running the Project

  1. Start the development server:

    bun dev
  2. 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.

πŸ—οΈ Project Structure

.
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ models/
β”‚   β”‚   └── userModel.ts
β”‚   β”œβ”€β”€ controllers/
β”‚   β”‚   └── userControllers.ts
β”‚   └── services/
β”‚       └── userService.ts
β”œβ”€β”€ prisma/
β”‚   └── schema.prisma
β”œβ”€β”€ index.ts
β”œβ”€β”€ .env
β”œβ”€β”€ package.json
└── README.md

πŸ”‘ API Endpoints

  • GET /api/cows: Retrieve all cows or find a cow by code
  • POST /api/cows: Create a new cow
  • POST /api/cows/reset-bsod: Reset BSOD status for all cows
  • POST /api/cows/:code/add-lemon: Add lemon to a cow
  • POST /api/milk/:code: Milk a cow
  • GET /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.

πŸ§ͺ Testing

To test the database connection:

bun test:connection

πŸ› οΈ Development

This project uses Bun as the JavaScript runtime. Here are some useful commands:

  • bun dev: Start the development server
  • bun db:generate: Generate Prisma client
  • bun db:migrate: Run database migrations
  • bun test:connection: Test database connection

🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

πŸ“ License

This project is licensed under the MIT License. See the LICENSE file for more information.

πŸ“š References

πŸ™‹β€β™‚οΈ Support

If you have any questions or need help, please open an issue in the GitHub repository or contact the maintainer.

About

πŸŽ“ Exit Exam for MVC (Model View and Controller)

Topics

Resources

License

Stars

Watchers

Forks