Skip to content

batuncer/Ecommercial-Backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Products and Users Backend Service

This backend service manages products and users in a microservices architecture, utilizing MongoDB for storage. The service is built with Node.js and Express, and it provides RESTful APIs for creating, reading, updating, and deleting products and users.

Table of Contents

Related Projects

Installation

  1. Clone the repository:

    git clone https://github.com/yourusername/products-users-backend.git
    cd products-users-backend
  2. Install dependencies:

    npm install
  3. Set up environment variables:

    Create a .env file in the root directory and add the following variables:

    MONGODB_URI=mongodb://localhost:27017/yourdbname
    PORT=3000
    JWT_SECRET=your_jwt_secret
    CLOUDINARY_CLOUD_NAME=your_cloud_name
    CLOUDINARY_API_KEY=your_api_key
    CLOUDINARY_API_SECRET=your_api_secret
    
  4. Start the service:

    npm start

Usage

After starting the service, it will be available at http://localhost:3000. Use a tool like Postman to interact with the API endpoints.

API Endpoints

Products

Create Product

  • URL: /product/new

  • Method: POST

  • Request Body:

    {
      "name": "Product1",
      "description": "A sample product",
      "price": 29.99,
      "stock": 10,
      "category": "Category1",
      "images": [
        {
          "url": "images.com"
        }
      ]
    }
  • Response:

    {
      "id": "60c72b2f9b1d4b3d5c4a627a",
      "name": "Product1",
      "description": "A sample product",
      "price": 29.99,
      "category": "Category1",
      "createdAt": "2024-05-21T14:09:55.418Z",
      "updatedAt": "2024-05-21T14:09:55.418Z"
    }

Get All Products

  • URL: /products

  • Method: GET

  • Response:

    [
      {
        "id": "60c72b2f9b1d4b3d5c4a627a",
        "name": "Product1",
        "description": "A sample product",
        "price": 29.99,
        "category": "Category1",
        "createdAt": "2024-05-21T14:09:55.418Z",
        "updatedAt": "2024-05-21T14:09:55.418Z"
      }
    ]

Get Product by ID

  • URL: /products/:productId

  • Method: GET

  • Response:

    {
      "id": "60c72b2f9b1d4b3d5c4a627a",
      "name": "Product1",
      "description": "A sample product",
      "price": 29.99,
      "category": "Category1",
      "createdAt": "2024-05-21T14:09:55.418Z",
      "updatedAt": "2024-05-21T14:09:55.418Z"
    }

Update Product

  • URL: /products/:productId

  • Method: PUT

  • Request Body:

    {
      "name": "Updated Product",
      "description": "Updated description",
      "price": 39.99,
      "category": "Updated Category"
    }
  • Response:

    {
      "id": "60c72b2f9b1d4b3d5c4a627a",
      "name": "Updated Product",
      "description": "Updated description",
      "price": 39.99,
      "category": "Updated Category",
      "createdAt": "2024-05-21T14:09:55.418Z",
      "updatedAt": "2024-05-21T14:09:55.418Z"
    }

Delete Product

  • URL: /products/:productId

  • Method: DELETE

  • Response:

    {
      "message": "Product deleted successfully"
    }

Users

Create User

  • URL: /users

  • Method: POST

  • Request Body:

    {
      "username": "John Doe",
      "email": "john.doe@example.com",
      "password": "password123"
    }
  • Response:

    {
      "id": "60c72b2f9b1d4b3d5c4a627b",
      "username": "John Doe",
      "email": "john.doe@example.com",
      "createdAt": "2024-05-21T14:09:55.418Z",
      "updatedAt": "2024-05-21T14:09:55.418Z"
    }

Get All Users

  • URL: /users

  • Method: GET

  • Response:

    [
      {
        "id": "60c72b2f9b1d4b3d5c4a627b",
        "username": "John Doe",
        "email": "john.doe@example.com",
        "createdAt": "2024-05-21T14:09:55.418Z",
        "updatedAt": "2024-05-21T14:09:55.418Z"
      }
    ]

Get User by ID

  • URL: /users/:userId

  • Method: GET

  • Response:

    {
      "id": "60c72b2f9b1d4b3d5c4a627b",
      "name": "John Doe",
      "email": "john.doe@example.com",
      "createdAt": "2024-05-21T14:09:55.418Z",
      "updatedAt": "2024-05-21T14:09:55.418Z"
    }

Update User

  • URL: /users/:userId

  • Method: PUT

  • Request Body:

    {
      "name": "Updated User",
      "email": "updated.email@example.com",
      "password": "newpassword123"
    }
  • Response:

    {
      "id": "60c72b2f9b1d4b3d5c4a627b",
      "name": "Updated User",
      "email": "updated.email@example.com",
      "createdAt": "2024-05-21T14:09:55.418Z",
      "updatedAt": "2024-05-21T14:09:55.418Z"
    }

Delete User

  • URL: /users/:userId

  • Method: DELETE

  • Response:

    {
      "message": "User deleted successfully"
    }

Technologies

  • Node.js: JavaScript runtime built on Chrome's V8 JavaScript engine.
  • Express: Fast, unopinionated, minimalist web framework for Node.js.
  • MongoDB: NoSQL database for storing product and user data.
  • Mongoose: Elegant MongoDB object modeling for Node.js.
  • JWT (JSON Web Tokens): For authentication and authorization.
  • Cloudinary: Cloud-based image and video management services.

Security

This service uses JWT (JSON Web Tokens) for authentication and authorization. Ensure you have a valid token to access the protected endpoints.

Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository.

  2. Create a new branch:

    git checkout -b feature-branch
  3. Make your changes.

  4. Commit your changes:

    git commit -m 'Add some feature'
  5. Push to the branch:

    git push origin feature-branch
  6. Open a pull request.

Please ensure your code adheres to the project's coding standards and includes appropriate tests.

License

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

Future Improvements

Testing

  • Add unit tests to increase the reliability of the API.
  • Create integration tests to validate API behavior in different scenarios.

General Improvements

  • Utilize linter and formatter tools for code cleanliness.
  • Regularly update dependencies and stay informed about security vulnerabilities.