vivaldy22 / simpleRestApiLA

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Simple REST API

I made this project for Link Aja Assignment
This project was made using:

  1. Go (main language of this project)
  2. MySQL (main database)
  3. sqlite3 (temporary database for unit testing)

Requirements

  1. Go (This was made in go1.15.5)
  2. MySQL / MariaDB

Preparation

  1. Import the database provided in database/db.sql file
  2. Important If you're executing with docker, you don't need to change anything in .env file. You can just skip step 3 and 4
  3. Inside .env file, there are configurations needed for the api to run, change them according to your system. The most important variable that you have to change is:
    • DB_USER - DB_PASSWORD - API_HOST (optional) - API_PORT (optional)
  4. (Optional) Install make program for your system

Execution with Docker

  1. Launch your terminal
  2. Go to the directory of this project
  3. Type docker-compose up --build
  4. If you got error listen tcp 0.0.0.0:3306: bind: address already in use, you have to stop mysql service first..
    • For linux run this in your terminal
      systemctl stop mysql
    
    • For Windows run this in your command prompt
      net stop MySQL
    
    • If the commands doesn't work, you need to search how to stop mysql service for your OS.
  5. If you any error after executin docker-compose up --build try to run it with sudo so the command should be something like this sudo docker-compose up --build

Execution without Docker

  1. Launch your terminal
  2. Go to the directory of this project
  3. Type make dev in the terminal. If your terminal says make command not found, make sure to install it first on your system
  4. If everything is right, the output should be something like this
       $ make dev
       go run main.go
       2020/12/05 05:36:36 [MySQL] Database connected, simple_rest_la
       2020/12/05 05:36:36 Starting Web Server at localhost port: 8080
       Available endpoints:
       /account/{account_number} [GET]
       /account/{from_account_number}/transfer [POST]
    ``` <br>
    

Endpoints

Show Balance

Returns json data about a single account.

  • URL

    /account/{account_number}

  • Method:

    GET

  • URL Params

    Required:

    account_number=[string]

  • Data Params

    None

  • Success Response:

    • Code: 200
    • Content:
    {
      "account_number": "555001",
      "customer_number": "1001",
      "balance": "10000"
    }
  • Error Response:

    • Code: 404
    • Content:
    {
      "message": "Data not found",
      "error_code": 404
    }

Transfer Money

Transfer money from an account to another account.

  • URL

    /account/{from_account_number}/transfer

  • Method:

    POST

  • URL Params

    Required:

    from_account_number=[string]

  • Data Params

    {
      "to_account_number": "555002",
      "amount": "100"
    }
  • Success Response:

    • Code: 201
    • Content: None.
  • Error Response:

    • Code: 400
    • Content:
    {
      "message": "Decoding json failed",
      "error_code": 400
    }

    OR

    • There are multiple error message with code 404, this is one of them
    • Code: 404
    • Content:
    {
      "message": "Your Balance is not enough",
      "error_code": 404
    }

About


Languages

Language:Go 96.6%Language:Dockerfile 2.5%Language:Shell 0.8%Language:Makefile 0.1%