Categorygithub.com/Jerry2204/go-employee-rest-api
module
0.0.0-20240307024022-3f59df919cfb
Repository: https://github.com/jerry2204/go-employee-rest-api.git
Documentation: pkg.go.dev

# README

Employee Management API

This document provides instructions on how to set up and run this Golang application using Docker. It also includes information on setting up a PostgreSQL container for your application.

Prerequisites

  • Git
  • Docker

Cloning the Repository

  1. Run the following command to clone this project to your local machine:
    git clone --branch middle-developer-test https://github.com/Jerry2204/go-employee-rest-api.git
    
  2. Navigate to the project folder:
    cd go-employee-rest-api
    

Setting Up PostgreSQL Container

  1. Copy the .env.example file to .env in the root of your project directory:

    cp .env.example .env
    
  2. Open the .env file and set up the configuration for your PostgreSQL database. Replace the placeholders with your actual database credentials:

    DB_USER=your_postgres_user
    DB_PASSWORD=your_postgres_password
    DB_NAME=your_database_name
    

Running This Golang Application

  1. Run the following command to start the PostgreSQL container and this Golang application:

    docker-compose up --build
    
    
  2. If there is an error when you start the command above and application not running, you should follow this step by step:

    1. docker-compose down
    2. docker compose run --service-ports api bash
    3. go get -u -v -f all
    4. exit
    
    
  3. After that you should build and start the golang application again:

    docker-compose up --build
    
    
  4. This Golang application should now be running inside a Docker container. You can access it at http://localhost:8080 in your web browser or you can access the API using your API Management Tools.

Stopping This Golang Application

To stop and remove the PostgreSQL and Golang containers, use the following command:

docker compose down

API for Management Employee Specification

Create Employee

  • Endpoint : /api/employees
  • HTTP Method : POST
  • Request Header :
    • Accept : application/json
  • Request Body :
{
    "firstName"       : "Jerry Andrianto",  
    "lastName"        : "Pangaribuan",  
    "email"           : "[email protected]"
}
  • Response Body (Success) :
  • Response Code 201
{
    "status"   : "OK",
    "message"  : "Data Created Successfully",
    "data"     : [
        {
            "firstName": "Jerry Andrianto",
            "lastName" : "Pangaribuan",
            "email"    : "[email protected]"
        }
    ]
}
  • Response Body (Fail) :
  • Response Code 400
{
    "status"   : "error",
    "error"    : "Bad Request!"
}
  • Response Code 500
{
    "status"   : "error",
    "error"    : "Internal Server Error!"
}

Get All Employee

  • Endpoint : /api/employees

  • HTTP Method : GET

  • Request Header :

    • Accept : application/json
  • Response Body (Success) :

  • Status Code 200

{
    "status"   : "OK",
    "message"  : "Get Data Sucessfully!",
    "data"     : [
        {
            "ID"        : 28,
            "CreatedAt" : "2024-03-05T17:59:34.092438Z",
            "UpdatedAt" : "2024-03-06T14:21:06.891792Z",
            "DeletedAt" : null,
            "firstName" : "Irin Chandra",
            "lastName"  : "Pangaribuans",
            "email"     : "[email protected]",
            "hireDate"  : "0001-01-01T00:00:00Z"
        },
        {
            "ID"        : 37,
            "CreatedAt" : "2024-03-06T09:16:11.37196Z",
            "UpdatedAt" : "2024-03-06T09:16:11.37196Z",
            "DeletedAt" : null,
            "firstName" : "Alendra Yudistira",
            "lastName"  : "Hutahaean",
            "email"     : "[email protected]",
            "hireDate"  : "0001-01-01T00:00:00Z"
        },
        {
            "ID"        : 41,
            "CreatedAt" : "2024-03-06T10:04:44.27142Z",
            "UpdatedAt" : "2024-03-06T10:04:44.27142Z",
            "DeletedAt" : null,
            "firstName" : "Revalino Adhistana",
            "lastName"  : "Hutahaean",
            "email"     : "[email protected]",
            "hireDate"  : "2024-03-06T10:04:44.270006Z"
        }
    ]
}

Get Employee By Id

  • Endpoint : /api/employees/{employeeId}

  • HTTP Method : GET

  • Request Header :

    • Accept : application/json
  • Response Body (Success) :

  • Status Code 200

{
    "message" : "Get Data Sucessfully!",
    "status"  : "OK",
    "data"    : {
      "ID": 28,
      "CreatedAt": "2024-03-05T17:59:34.092438Z",
      "UpdatedAt": "2024-03-06T14:21:06.891792Z",
      "DeletedAt": null,
      "firstName": "Irin Chandra",
      "lastName": "Pangaribuans",
      "email": "[email protected]",
      "hireDate": "0001-01-01T00:00:00Z"
    }
}
  • Response Body (Fail) :
  • Status Code 404
{
    "status"   : "error",
    "message"  : "Data Not Found!"
}

Update Employee By Id

  • Endpoint : /api/employees/{employeeId}
  • HTTP Method : PUT
  • Request Header :
    • Accept : application/json
  • Request Body :
{
    "firstName"       : "Jerry Andrianto",  
    "lastName"        : "Pangaribuan",  
    "email"           : "[email protected]"
}
  • Response Body (Success) :
  • Status Code 200
{
  "status"  : "OK",
  "message" : "Data Updated Successfully!",
  "data"    : {
    "id": 28,
    "firstName": "Jerry Andrianto",
    "lastName": "Pangaribuan",
    "email": "[email protected]",
    "hireDate": "2024-03-06T10:04:44.270006Z"
  }
}
  • Response Body (Fail) :
  • Status Code 404
{
    "status"   : "error",
    "message"  : "Data Not Found!"
}
  • Response Code 400
{
    "status"   : "error",
    "error"    : "Bad Request!"
}
  • Response Code 500
{
    "status"   : "error",
    "error"    : "Internal Server Error!"
}

Delete Employee By Id

  • Endpoint : /api/employees/{employeeId}
  • HTTP Method : DELETE
  • Request Header :
    • Accept : application/json
  • Response Body (Success) :
  • Status Code 200
{
  "status"  : "OK",
  "message" : "Data Deleted Successfully!",
  "data"    : {
    "id": 28,
    "firstName": "Jerry Andrianto",
    "lastName": "Pangaribuan",
    "email": "[email protected]",
    "hireDate": "2024-03-06T10:04:44.270006Z"
  }
}
  • Response Body (Fail) :
  • Status Code 404
{
    "status"   : "error",
    "message"  : "Data Not Found!"
}
  • Response Code 500
{
    "status"   : "error",
    "error"    : "Internal Server Error!"
}

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author