Skip to content

A RESTful API built with Go that performs standard CRUD operations

Notifications You must be signed in to change notification settings

kyratzakos/simple-api-app

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GoLang MongoDB CRUD API

A RESTful API built with Go that performs standard CRUD operations on a products resource, backed by a MongoDB database. This API includes the ability to create, read, update, and delete products, with built-in validation for required fields and timestamp management.

Features

  • Create, read, update, and delete operations on products.
  • Uses MongoDB for persistent storage.
  • Dynamic field handling: Only non-empty fields are inserted or updated in MongoDB.
  • Meta timestamps: Automatically handles createdAt and updatedAt fields.
  • Field validation: Only allowed fields are accepted, with checks for required fields like title and price.

Table of Contents

Installation

To run this project locally, ensure that you have the following prerequisites installed:

Prerequisites

Steps

  1. Clone the Repository:
git clone https://github.com/kyratzakos/simple-api-app.git
cd simple-api-app
  1. Install Dependencies:

    Run the following command in the project root directory to install the required Go dependencies:

go mod tidy
  1. Set Up MongoDB:

    Ensure MongoDB is running on your machine. You can start MongoDB using:

  • Linux/Mac:
sudo service mongod start
  • Windows:
net start MongoDB
  1. Set Environment Variables:

Create a copy of .env.example file in the root directory with name .env:

API Endpoints

  1. Create Product
    • Endpoint: POST /products
    • Description: Creates a new product.
    • Body Parameters:
{
  "title": "Product A",
  "description": "A high-quality product",
  "price": 199.99,
  "category": "Optional",
  "meta": {
    "createdAt": "optional_timestamp",
    "updatedAt": "optional_timestamp"
  }
}
  • Required Fields: title, price
  • Response: 201 Created, with the inserted product id.
  1. Get Products (Paginated)
    • Endpoint: GET /products?page=1&limit=10
    • Description: Retrieves all products with pagination.
    • Query Parameters: page, limit
    • Response: 200 OK with paginated product list.
  2. Get Single Product
    • Endpoint: GET /products/{id}
    • Description: Retrieves a single product by its MongoDB ObjectId.
    • Response: 200 OK with the product data.
  3. Update Product
    • Endpoint: PUT /products/{id}
    • Description: Updates an existing product's fields dynamically (only non-empty fields are updated).
    • Body Parameters:
{
  "titel": "Updated Product title",
  "price": 299.99,
  "meta": {
    "updatedAt": "2024-09-17T12:34:56Z"
  }
}
  • Response: 200 OK with a success message.
  1. Delete Product
    • Endpoint: DELETE /products/{id}
    • Description: Deletes a product by its MongoDB ObjectId.
    • Response: 200 OK with a success message.

Running the Project

Once you have set up the environment variables and MongoDB is running, you can start the server with:

go run main.go

The API will be available at http://localhost:3000.

Future Improvements

  • Add unit tests for the CRUD functionality.
  • Implement authentication and authorization.
  • Add more comprehensive validation for request payloads.

About

A RESTful API built with Go that performs standard CRUD operations

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages