Skip to content

jakec-dev/travel-planner-server

Repository files navigation

Contributors Forks Stargazers Issues MIT License


Travel Planner Server

Backend server for Travel Planner app. Performs CRUD operations on the Travel Planner database via REST APIs.
Explore the docs »

View Demo · Report Bug · Request Feature

Table of Contents
  1. About The Project
  2. Getting Started
  3. Usage
  4. Roadmap
  5. Contributing
  6. License
  7. Contact
  8. Acknowledgments

About The Project

This project is the Travel Planner backend server that's responsible for integrating the Travel Planner frontend app with the MySQL database that persists data. It does this by providing REST API endpoints to allow the frontend app to perform CRUD operations on the database.

(back to top)

Built With

  • Node.js
  • Express.js
  • MySQL
  • ESLint
  • Prettier
  • Mocha
  • Chai
  • Swagger

(back to top)

Getting Started

To get a local copy up and running follow these simple steps.

Prerequisites

You will need the following software installed in your environment to run this app.

Installation

  1. Clone the repo
    git clone https://github.com/jakec-dev/travel-planner-server.git
  2. Install packages
    yarn install
  3. Create an .env file in the project root directory and enter your database configuration settings
    DB_HOST=database.example.com
    DB_DATABASE=database-name
    DB_USER=username
    DB_PASSWORD=password
  4. Start the server
    yarn start

(back to top)

Usage

This API is designed to be used in conjunction with the Travel Planner App. It provides the following functionality:

Create an Item

Send a POST request to /items. The request body should contain the new item, for example:

{
  "name": "New item name",
  "brand": "New item brand (optional)"
}

JavaScript/React example:

const SERVER_URL = "https://api.example.com";

const createItem = async (newItem) => {
  try {
    const resp = await fetch(`${SERVER_URL}/items`, {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify(newItem),
    }).then((res) => res.json());
    if (resp.status === "error") {
      throw Error(resp.message);
    }
    return resp.data;
  } catch (err) {
    throw Error(err);
  }
};

const result = createItem({
  name: "New item name",
  brand: "New item brand",
})
  .then((resp) => resp)
  .catch((err) => console.log(err));

(back to top)

Update an Item

Send a PUT request to /items. The request body should contain the modified item. The modified item will entirely replace the original item, rather than merge fields, so be sure to merge any existing fields before sending the request. For example:

{
  "id": 3,
  "name": "Original item name",
  "brand": "Updated item brand"
}

JavaScript/React example:

const SERVER_URL = "https://api.example.com";

const updateItem = async (modifiedItem) => {
  try {
    const resp = await fetch(`${SERVER_URL}/items`, {
      method: "PUT",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify(modifiedItem),
    }).then((res) => res.json());
    if (resp.status === "error") {
      throw Error(resp.message);
    }
    return resp.data;
  } catch (err) {
    throw Error(err);
  }
};

const result = updateItem({
  id: 3,
  name: "Original item name",
  brand: "Updated item brand",
})
  .then((resp) => resp)
  .catch((err) => console.log(err));

(back to top)

Delete an Item

Send a DELETE request to /items/:id, where :id is the ID of the item to be deleted. No request body is required.

JavaScript/React example:

const SERVER_URL = "https://api.example.com";

const deleteItem = async (id) => {
  try {
    const resp = await fetch(`${SERVER_URL}/items/${id}`, {
      method: "DELETE",
    }).then((res) => res.json());
    if (resp.status === "error") {
      throw Error(resp.message);
    }
    return resp.data;
  } catch (err) {
    throw Error(err);
  }
};

const result = deleteItem(4)
  .then((resp) => resp)
  .catch((err) => console.log(err));

(back to top)

Fetch a Specific Item

Send a GET request to /items/:id, where :id is the ID of the item to be fetched. No request body is required.

JavaScript/React example:

const SERVER_URL = "https://api.example.com";

const fetchItem = async (itemId) => {
  try {
    const resp = await fetch(`${SERVER_URL}/items/${itemId}`).then((res) =>
      res.json()
    );
    if (resp.status === "error") {
      throw Error(resp.message);
    }
    return resp.data;
  } catch (err) {
    throw Error(err);
  }
};

const result = fetchItem(2)
  .then((resp) => resp)
  .catch((err) => console.log(err));

(back to top)

Fetch All Items

Send a GET request to /items. No request body is required.

JavaScript/React example:

const SERVER_URL = "https://api.example.com";

const fetchAllItems = async (itemId) => {
  try {
    const resp = await fetch(`${SERVER_URL}/items`).then((res) => res.json());
    if (resp.status === "error") {
      throw Error(resp.message);
    }
    return resp.data;
  } catch (err) {
    throw Error(err);
  }
};

const result = fetchAllItems()
  .then((resp) => resp)
  .catch((err) => console.log(err));

(back to top)

Access API Documentation

Navigate to /api-docs in a web browser.

API Docs

(back to top)

Roadmap

Version 1

  • Create database connection
  • Add route, controller, service and data layers for items
  • Add API documentation
  • Write unit and integration tests
  • Write project documentation

See the open issues for a full list of proposed features (and known issues).

(back to top)

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

(back to top)

Testing

This project uses Mocha, Chai, and Sinon for testing, and Istanbul for test coverage reports

yarn test

(back to top)

Linting & Formatting

This project uses ESLint for linting using AirBNB's style guide, and Prettier for formatting.

yarn lint
yarn lint:fix

yarn format
yarn format:write

(back to top)

License

Distributed under the MIT License. See LICENSE.txt for more information.

(back to top)

Contact

Jake Clayton

  e: [email protected]

  w: jakec.dev

  LinkedIn

(back to top)

Acknowledgments

(back to top)

About

Backend server for Travel Planner app

Resources

License

Stars

Watchers

Forks