{"id":19273097,"url":"https://github.com/remonhasan/repository-pattern-microservice-api","last_synced_at":"2025-07-23T04:35:57.637Z","repository":{"id":258147959,"uuid":"872265275","full_name":"Remonhasan/repository-pattern-microservice-api","owner":"Remonhasan","description":"Repository pattern practice rest api for microservices. ","archived":false,"fork":false,"pushed_at":"2024-11-10T11:56:54.000Z","size":54,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-23T20:41:42.998Z","etag":null,"topics":["microservice","repository-pattern","resources","rest-api"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Remonhasan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-10-14T06:20:57.000Z","updated_at":"2024-11-10T11:56:58.000Z","dependencies_parsed_at":"2024-10-17T20:59:21.349Z","dependency_job_id":"4aa62492-714e-4347-93c6-923880294429","html_url":"https://github.com/Remonhasan/repository-pattern-microservice-api","commit_stats":null,"previous_names":["remonhasan/repository-pattern-microservice-api"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Remonhasan/repository-pattern-microservice-api","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Remonhasan%2Frepository-pattern-microservice-api","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Remonhasan%2Frepository-pattern-microservice-api/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Remonhasan%2Frepository-pattern-microservice-api/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Remonhasan%2Frepository-pattern-microservice-api/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Remonhasan","download_url":"https://codeload.github.com/Remonhasan/repository-pattern-microservice-api/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Remonhasan%2Frepository-pattern-microservice-api/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266618971,"owners_count":23957273,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-23T02:00:09.312Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["microservice","repository-pattern","resources","rest-api"],"created_at":"2024-11-09T20:40:58.544Z","updated_at":"2025-07-23T04:35:57.602Z","avatar_url":"https://github.com/Remonhasan.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Repository Pattern REST API for Microservices\n\nThis repository demonstrates how to implement a **REST API** using the **Repository Pattern** in a Laravel-based microservice. The main objective of this project is to showcase how to manage categories efficiently, abstracting the data handling through the repository pattern and utilizing **JsonResource** for API response formatting.\n\nBy following the principles of **clean code**, **separation of concerns**, and **scalable design**, this repository presents a maintainable solution for managing categories in a microservice architecture.\n\n## Features\n\n- **Category Management**: Allows CRUD operations on categories.\n- **Repository Pattern**: Implemented to decouple the application’s business logic from data persistence.\n- **JsonResource**: Uses `JsonResource` to ensure consistent and structured API responses.\n- **Microservice-Ready**: Designed with the flexibility to integrate into a microservice environment, with clear separation between layers.\n\n---\n\n## Table of Contents\n\n1. [Installation](#installation)\n2. [Technologies](#technologies)\n3. [Project Structure](#project-structure)\n4. [API Endpoints](#api-endpoints)\n5. [Usage](#usage)\n6. [Repository Pattern](#repository-pattern)\n7. [JsonResource](#jsonresource)\n8. [Contributing](#contributing)\n9. [License](#license)\n\n---\n\n## Installation\n\nTo get started with this repository, follow the instructions below:\n\n### 1. Clone the Repository\n\n```bash\ngit clone https://github.com/Remonhasan/repository-pattern-microservice-api.git\ncd repository-pattern-microservice-api\n```\n\n### 2. Install Dependencies\n\nEnsure you have Composer installed. Then, run the following command to install the required dependencies:\n\n```bash\ncomposer install\n```\n\n### 3. Configure Environment\n\nCopy the `.env.example` to `.env` and set up your database and application configurations.\n\n```bash\ncp .env.example .env\nphp artisan key:generate\n```\n\nUpdate the `.env` file with your database credentials, for example:\n\n```env\nDB_CONNECTION=mysql\nDB_HOST=127.0.0.1\nDB_PORT=3306\nDB_DATABASE=your_database_name\nDB_USERNAME=your_database_username\nDB_PASSWORD=your_database_password\n```\n\n### 4. Migrate the Database\n\nRun the database migrations to create the necessary tables:\n\n```bash\nphp artisan migrate\n```\n\nOptionally, you can seed the database with sample data:\n\n```bash\nphp artisan db:seed\n```\n\n### 5. Start the Development Server\n\nOnce everything is set up, start the server:\n\n```bash\nphp artisan serve\n```\n\nYour application should now be running at `http://localhost:8000`.\n\n---\n\n## Technologies\n\nThis project uses the following technologies:\n\n- **Laravel** (PHP framework)\n- **MySQL** (Database)\n- **Repository Pattern**\n- **JsonResource** (for transforming data into a consistent JSON format)\n- **RESTful API** (with standard HTTP methods)\n\n---\n\n## Project Structure\n\nHere's an overview of the project structure:\n\n```\napp/\n├── Http/\n│   ├── Controllers/\n│   │   └── CategoryController.php    # Handles requests related to categories\n│   └── Resources/\n│       └── CategoryResource.php      # API resource for category data transformation\n├── Interfaces/\n│   └── CategoryInterface.php         # Defines the contract for category repository\n├── Repositories/\n│   └── CategoryRepository.php        # Implements the repository interface\n└── Models/\n    └── Category.php                  # The Eloquent model for the category\n```\n\n### Breakdown:\n\n- **CategoryController**: This controller handles all HTTP requests related to categories (e.g., create, update, delete, list).\n- **CategoryResource**: A `JsonResource` used to transform the category data before sending it as a response, ensuring a consistent output format.\n- **CategoryInterface**: An interface that defines the methods for category-related data operations, ensuring loose coupling and better testability.\n- **CategoryRepository**: Implements `CategoryInterface` and interacts directly with the database to perform CRUD operations.\n- **Category Model**: The Eloquent model that represents the `Category` table in the database.\n\n---\n\n## API Endpoints\n\n### 1. **Get All Categories**\n\n- **URL**: `/api/categories`\n- **Method**: `GET`\n- **Description**: Retrieves a list of all categories.\n- **Response**: A JSON array of categories.\n\nExample:\n\n```json\n[\n  {\n    \"id\": 1,\n    \"name\": \"Category One\",\n    \"description\": \"Description for Category One\"\n  },\n  {\n    \"id\": 2,\n    \"name\": \"Category Two\",\n    \"description\": \"Description for Category Two\"\n  }\n]\n```\n\n### 2. **Create Category**\n\n- **URL**: `/api/categories`\n- **Method**: `POST`\n- **Description**: Creates a new category.\n- **Request Body**:\n\n```json\n{\n  \"name\": \"New Category\",\n  \"description\": \"A description for the new category\"\n}\n```\n\n- **Response**: The newly created category data with a 201 status code.\n\n### 3. **Update Category**\n\n- **URL**: `/api/categories/{id}`\n- **Method**: `PUT`\n- **Description**: Updates an existing category.\n- **Request Body**:\n\n```json\n{\n  \"name\": \"Updated Category Name\",\n  \"description\": \"Updated description\"\n}\n```\n\n- **Response**: The updated category data.\n\n### 4. **Delete Category**\n\n- **URL**: `/api/categories/{id}`\n- **Method**: `DELETE`\n- **Description**: Deletes the category by ID.\n- **Response**: A success message confirming the deletion.\n\n---\n\n## Usage\n\nThe following describes the code implementation and how the repository pattern and `JsonResource` are used in the project:\n\n### **Repository Pattern** Overview\n\nThe **Repository Pattern** helps abstract data access logic from the rest of the application. It allows you to work with different data sources (databases, external APIs, etc.) without changing the core application logic.\n\nIn this project:\n\n- The **CategoryRepository** is responsible for interacting with the database and performing CRUD operations.\n- The **CategoryInterface** defines the contract that the repository must follow, ensuring that the business logic interacts with the data layer in a consistent and decoupled manner.\n\n#### **CategoryInterface**\n\n```php\nnamespace App\\Interfaces;\n\ninterface CategoryInterface\n{\n    public function getAllCategories();\n    public function createCategory(array $data);\n    public function updateCategory($id, array $data);\n    public function deleteCategory($id);\n}\n```\n\n#### **CategoryRepository**\n\n```php\nnamespace App\\Repositories;\n\nuse App\\Models\\Category;\nuse App\\Interfaces\\CategoryInterface;\n\nclass CategoryRepository implements CategoryInterface\n{\n    protected $model;\n\n    public function __construct(Category $category)\n    {\n        $this-\u003emodel = $category;\n    }\n\n    public function getAllCategories()\n    {\n        return $this-\u003emodel-\u003eall();\n    }\n\n    public function createCategory(array $data)\n    {\n        return $this-\u003emodel-\u003ecreate($data);\n    }\n\n    public function updateCategory($id, array $data)\n    {\n        $category = $this-\u003emodel-\u003efindOrFail($id);\n        $category-\u003eupdate($data);\n        return $category;\n    }\n\n    public function deleteCategory($id)\n    {\n        $category = $this-\u003emodel-\u003efindOrFail($id);\n        $category-\u003edelete();\n        return $category;\n    }\n}\n```\n\n---\n\n### **JsonResource** for API Responses\n\nThe **JsonResource** class is used to transform model data into a consistent and well-structured JSON format. It ensures that the API response remains uniform and easy to consume.\n\n#### **CategoryResource**\n\n```php\nnamespace App\\Http\\Resources;\n\nuse Illuminate\\Http\\Resources\\Json\\JsonResource;\n\nclass CategoryResource extends JsonResource\n{\n    public function toArray($request)\n    {\n        return [\n            'id' =\u003e $this-\u003eid,\n            'name' =\u003e $this-\u003ename,\n            'description' =\u003e $this-\u003edescription,\n        ];\n    }\n}\n```\n\nUsing `JsonResource` helps separate the logic for formatting the response from the rest of the application, ensuring that API responses are consistent and easier to modify.\n\n---\n\n## Contributing\n\nWe welcome contributions! If you’d like to contribute to this project, please follow these steps:\n\n1. Fork the repository.\n2. Create a feature branch (`git checkout -b feature-name`).\n3. Make your changes.\n4. Commit your changes (`git commit -am 'Add new feature'`).\n5. Push to the branch (`git push origin feature-name`).\n6. Open a pull request.\n\nPlease ensure that your code follows the existing coding conventions and includes appropriate tests.\n\n---\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\n\n---\n\nThis README provides comprehensive documentation for your project, including installation instructions, details about the repository pattern, usage of `JsonResource`, and API endpoints. Feel free to adjust the content to suit your specific project details! Let me know if you need any further changes.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fremonhasan%2Frepository-pattern-microservice-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fremonhasan%2Frepository-pattern-microservice-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fremonhasan%2Frepository-pattern-microservice-api/lists"}