Giter Site home page Giter Site logo

shreyank031 / go-postgres Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 9 KB

Go application for managing stocks in a PostgreSQL database. It provides functionalities to Create, Read, Update, and Delete (CRUD) stock entries through a RESTful API.

Go 100.00%
golang gorilla-mux postgresql-database

go-postgres's Introduction

Stock Management API

This is a Go application for managing stocks in a PostgreSQL database. It provides functionalities to Create, Read, Update, and Delete (CRUD) stock entries through a RESTful API. This time I have not used Gorm so that I can get in-depth knowledge of whats happening under the hood.

Features:

  • Create new stock entries.
  • Retrieve a stock by its ID.
  • Fetch all stocks from the database.
  • Update existing stock entries.
  • Delete stock entries by ID.

Technology Stack:

  • Go Programming Language
  • PostgreSQL Database
  • Gorilla Mux
  • go-postgres
  • godotenv

Installation:

Prerequisites:

  • Go installed on your system
  • PostgreSQL database server running

Clone the repository:

    https://github.com/Shreyank031/go-postgres.git

Install dependencies:

    cd go-postgres
    go mod download

Configuration:

  • Create a .env file in the project root directory.

  • Add the following environment variable to the .env file, replacing <username> with your postgres username, <password> with your postgres password of the mentioned user and <db_name> with name of database with your actual PostgreSQL connection string:

    POSTGRES_URL="postgres://<username>:<password>@localhost:5432/<db_name>?sslmode=disable"

Running the API:

    go run main.go

This will start the API server on port 8080 by default

API Endpoints:

  • /api/stock = GET method, Fetches all stocks from the database.
  • /api/stock/{id} = GET method, Retrieves a stock entry by its ID.
  • /api/stock{id} = PUT method, Updates an existing stock entry based on the request body and ID.
  • /api/newstock{id} = POST method, Creates a new stock entry based on the request body (JSON).
  • /api/deletestock{id} = DELETE method, Deletes a stock entry by its ID.

Request and Response Format:

Requests should be sent in JSON format. Responses are also in JSON format

Example Usage (using CURL):

Create a new stock:

    curl localhost:8080/api/newstock/1 --include --header "Content-Type: application/json" -d '{
  "name": "Apple Inc.",
  "price": 182,
  "company": "AAPL"
}' --request "POST"

Response

{
  "ID": 1,
  "Message": "Stock created successfully"
}

Postman

Pasted image

Postgres running in my local machine

Pasted image (2)

go-postgres's People

Contributors

shreyank031 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.