/api-catatan

Ini adalah rest api catatan(notebook)

Primary LanguageJavaScriptMIT LicenseMIT

API Catatan

Badge Github Badge Github Badge Twitter

Table of Content

Overview

ini adalah source code rest api untuk Web App catatan.

untuk mencobanya tinggal download, atau clone

git clone https://github.com/wiklapandu/api-catatan.git

Requirements

  • Node js
  • Mongoo DB
  • Hapi js
  • NPM
    • dotenv
    • htmlspecialchars
    • mongoose (library untuk databases Mongoo DB)

Change ENV

sebelum menggunakan ada baiknya untuk mengubah port atau host dari API ini.

untuk mengubah hapus # di depan key ENV (example: #APP_HOST).

APPLICATION

Key ENV Description Example
APP_HOST untuk mengubah host server APP_HOST=localhost
APP_PORT untuk mengubah port server APP_PORT=5000

DATABASES

Key ENV Description Example
DB_HOST mengatur tempat/host databases DB_HOST=mongodb://127.0.0.1:27017/catatan

How to Use

Getting Data

  • URL: / dan /catatan
    • Method GET
    • Success Response:
      • Code: 200
        Content:

        {
            "status": "success",
            "notes": [
                {
                    "tags":[
                        "tags",
                        "tags",
                        "tags",
                    ],
                    "_id":"60fbe86c953fd12168a63731",
                    "title":"title",
                    "body":"note catatan",
                    "createdAt":"2021-07-24T10:16:12.427Z",
                    "updateAt":"2021-07-24T10:16:12.427Z",
                }
            ],
        }
    • Sample call
        $.ajax({
            url: "http://localhost:5000/catatan",
            method: 'GET',
            success: ({ notes }) => {
                $('ul#notes')
                    .append($.map(notes, (note) => {
                        return `
                            <li class="list-group-item list-group-item-action">
                                <a href="/catatan/${note._id}" class="text-dark">
                                    <span class="d-flex">
                                        <h5>${note.title}</h5>
                                        <small class="ms-auto">${note.createdAt.split('T')[0].split('-').reverse().join('-')}</small>
                                    </span>
                                    <p>${note.tags.join(', ')}</p>
                                </a>
                            </li>`;
                    }))
            }
        });

Getting Data by Id

  • URL: /catatan/{id}
    • Method GET
    • URL Params
      • Required:
        • id example: 60fbe86c953fd12168a63731
    • Success Response:
      • Code: 200
        content:
        {
            "status": "success",
            "note":{
                "tags":["satu","dua","tiga"],
                "_id":"60fbe86c953fd12168a63731",
                "title":"ada isinya",
                "body":"catatan title kosong",
                "createdAt":"2021-07-24T10:16:12.427Z",
                "updatedAt":"2021-07-24T10:16:12.427Z",
                "__v":0
                }
        }
    • Error Response:
      • Code: 400
        content:
        {
            "status": "fail",
            "message": "catatan tidak ditemukan",
        }
    • Sample call
        $.ajax({
            url:
                `/catatan/60fbe86c953fd12168a63731`,
            method: 'GET',
            success: ({ note, message }) => {
                $('h3.title').text(note.title)
                $('#catatan').append(
                    /*html*/`
                    <div class="fs-2 textTitle">
                        ${note.title}
                    </div>
                    <hr>
                    <div class="fs-4 my-3">
                        ${note.tags.join(', ')}
                    </div>
                    <p>
                        ${note.body}
                    </p>
                    `
                );
            },
            error: ({ responseJSON: result }) => {
                $('#catatan').append(
                    /*html*/`
                    <div class= "fs-2 text-center">
                        ${result.message}
                    </div>
                    `
                );
            }
        })

Post Data

  • URL: /catatan
    • Method POST
    • Data Params
      • required:
        • title type string
        • tags type string (example: "tags1,tags2,tags3") / dipisahkan ,
        • body type string
    • Success Response:
      • Code: 200
        Content:

        {
            "status": "success",
            "message": "catatan berhasil ditambahkan",
            "note": {
                    "tags":["satu","dua","tiga"],
                    "_id":"60fbe86c953fd12168a63731",
                    "title":"ada isinya",
                    "body":"catatan title kosong",
                    "createdAt":"2021-07-24T10:16:12.427Z",
                    "updatedAt":"2021-07-24T10:16:12.427Z",
                    "__v":0
                }
        }
    • Error Response:
      • Code: 200
        Content: jika body kosong

        {
            "status": "fail",
            "message": "Body tidak boleh kosong",
        }
    • Sample call
        $.ajax({
            url: "/catatan",
            method: 'POST',
            data: {
                title: $('form#formAdd div input#title').val(),
                tags: $('form#formAdd div input#tags').val(),
                body: $('form#formAdd textarea#body').val(),
            },
            success: ({ message }) => {
                $('form#formAdd').trigger('reset');
                setTimeout(() => {
                    $(location).attr('href', '/');
                }, 500);
            },
            error: ({ message }) => {
                console.log(message);
            }
        });
    • Notes
      untuk upload data menngunakan ini

Updated Data by Id

  • URL: /catatan/{id}
    • Method PUT
    • URL Params
      • Required:
        • id example: 60fbe86c953fd12168a63731
    • Data Params
      • Optional:
        • title type string
        • tags type string (example: "tags1,tags2,tags3") / dipisahkan ,
        • body type string
    • Success Response:
      • Code: 200
        content:
        {
            "status": "success",
            "message": "data telah diubah",
        }
    • Error Response:
      • Code: 400
        content:
        {
            "status": "fail",
            "message": "Gagal diubah, catatan tidak ditemukan",
        }
    • Sample call
        $.ajax({
            url:
                `/catatan/60fbe86c953fd12168a63731`,
            method: 'PUT',
            data: {
                title: $('form#formEdit div input#title').val(),
                tags: $('form#formEdit div input#tags').val(),
                body: $('form#formEdit textarea#body').val(),
            },
            success: ({ message }) => {
                setTimeout(() => {
                    $(location).attr('href', '/');
                }, 500);
            },
            error: ({ message }) => {
                console.log(message);
            }
        })

Delete Data by Id

  • URL: /catatan/{id}
    • Method DELETE
    • URL Params
      • Required:
        • id example: 60fbe86c953fd12168a63731
    • Success Response:
      • Code: 200
        content:
        {
            "status": "success",
            "message": "catatan telah dihapus",
        }
    • Error Response:
      • Code: 400
        content:
        {
            "status": "fail",
            "message": "Gagal menghapus, catatan tidak ditemukan",
        }
    • Sample call
        $.ajax({
            url:
                `/catatan/60fbe86c953fd12168a63731`,
            method: 'DELETE',
            success: (result) => {
                $(location).attr('href', '/')
            }
            error:({message})=>{
                console.log(message)
            }
        })