Skip to content

wallysoncarvalho/b2w-sw-api-challenge

Repository files navigation

Challenge Backend - Star Wars - B2W

Application

This project is a simple REST API that makes a few operations (described further) on the resource planet, that is, Star Wars planets.

When creating a planet, this service communicates with SWAPI so it can query if SWAPI has the newly registered planet and, if so, on how many movies does this planet appears.

This API was implemented with JAVA 11, using Spring Boot and Mongo DB starter to handle persistence with Mongo DB.

The package planet is responsible to maintain the objects that handles the operations related to the planet. These operations are exposed through the class PlanetComponent.

The web layer is located on the package web where it controls all incoming http request related to the planet. The controller invokes the appropriate behavior that PlanetComponent exposes depending on the kind of request that was made.

Tests

As we do not have many business rules on this problem I opted to use the integration approach for the tests with the help of spring-boot-test to set up the environment and provide Junit 5 (among others). Also, on the persistence side, I use flapdoodle as the test database.

I also configured a simple pipeline on github action to build and run the tests: https://github.com/wallysoncarvalho/b2w-sw-api-challenge/actions

Run

Docker

The preferred way to run this application is by using docker-compose because it already sets up everything automatically.

If docker and docker-compose is available, one can simply run docker-compose up -d at the root of the project. The image of the API will be built by the Dockerfile, note that it also uses a multi-stage build, where the first stage is to generate the fat jar of the application and create a small image using the layered jars technique provided by spring boot. Also, keep in mind that this stage will need to download the dependencies to create the jar, so it may take a few minutes (you can also build the jar manually and change the Dockerfile to use it instead of building one).

REST API

A POSTMAN collection named B2W-BACKEND-CHEALLENGE.postman_collection.json is provided at the root of this project, so you can import it and easily call the services of the API.

Get all planets

Get all planets, paginated and ordered by the planet's name.

  • URL

    /api/planet

  • Method:

    GET

  • URL Params

    Not required:

    page=[integer] - page >= 1

    pageSize=[integer] - pageSize >= 5 or <= 20

  • Success Response:

    • Code: 200
      ** Content:** { items : [{id: <string>, name: <string>, climate: <string>, terrain: <string>, movieAppearances: <integer>},...], total : 100 }
  • Error Response:

    • Code: 400 BAD REQUEST
      Cause: When inserting invalid params
      Content: { status: <integer>, message : <string> }

Register a new planet

Register a new planet.

  • URL

    /api/planet

  • Method:

    POST

  • Body

    Required:

    [{name: <string>, climate: <string>, terrain: <string>}

  • Success Response:

    • Code: 201
  • Error Response:

    • Code: 400 BAD REQUEST
      Cause: When inserting invalid body or trying to create a planet with an existing name.
      Content: { status: <integer>, message : <string> }

Get a planet by it's ID

Get a planet by id

  • URL

    /api/planet/:id

  • Method:

    GET

  • Success Response:

    • Code: 200
      Content: {id: <string>, name: <string>, climate: <string>, terrain: <string>, movieAppearances: <integer>}
  • Error Response:

    • Code: 400 BAD REQUEST
      Cause: Id that do not exist
      Content: { status: <integer>, message : <string> }

Delete a planet by it's ID

Delete/remove a planet by it's ID.

  • URL

    /api/planet/:id

  • Method:

    DELETE

  • Success Response:

    • Code: 204

Get a planet by it's name

Get a planet by id

  • URL

    /api/planet/name/:name

  • Method:

    GET

  • Success Response:

    • Code: 200
      Content: {id: <string>, name: <string>, climate: <string>, terrain: <string>, movieAppearances: <integer>}
  • Error Response:

    • Code: 400 BAD REQUEST
      Cause: Planet with provided name do no exist
      Content: { status: <integer>, message : <string> }

About

This API is part of the backend developer challenge of B2W

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published