Git Product home page Git Product logo

product-api's Introduction

product-api

This application contains the APIs to create, list and find the product.

Prerequisites

This app uses :-

  • Java 11.0.16
  • Spring Boot 2.4.1
  • In-Memory H2 Database

Installation

  • Clone the repository
  • mvn install
  • mvn spring-boot:run

After running, the project, switch to your browser and hit http://localhost:8080/api/products. We should see the list of Products.

Swagger documentation is also added, and it will be up at http://localhost:8080/swagger-ui.html#/

Below is the list of APIs that are implemented :-

To create the Product

POST:- http://localhost:8080/api/products

Request Body:-

{
    "title": "Soap",
    "details": "Cleanising agent",
    "price": 40.5,
    "size": "small",
    "category": "domestic"
}

Response:-


{
    "details": "Cleanising agent",
    "title": "Soap",
    "id": "8c3ed1bb-166c-47dd-a3bb-afd501f0f13a",
    "category": "domestic",
    "size": "small",
    "price": 40.5
}

If details, title or price has null value, it returns an error

{
    "timestamp": "2022-12-04",
    "status": 400,
    "errors": [
        "title must not be null"
    ]
}

To list the Products

GET:- http://localhost:8080/api/products

Response :-

 [
    {
        "details": "Cleanising agent",
        "title": "Soap",
        "id": "8c3ed1bb-166c-47dd-a3bb-afd501f0f13a",
        "category": "domestic",
        "size": "small",
        "price": 40.5
    },
    {
        "details": "Cleanising agent",
        "title": "Detergent",
        "id": "05bb82bf-392f-489c-85d7-6743e8679212",
        "category": "domestic",
        "size": "small",
        "price": 40.5
    },
    {
        "details": "Healthy and Protenious bar",
        "title": "Proteing Bar",
        "id": "c4cd58ce-dade-4112-a6e1-e11afa4e625b",
        "category": "food",
        "size": null,
        "price": 100.5
    }
]

If products are not available it returns an error

{
    "timestamp": "2022-12-04T18:30:07.350255",
    "message": "No Product found"
}

To fetch product details by Id

GET:- http://localhost:8080/api/products/fc231103-ded1-4a25-8d2a-d174bde3057c

Response :-

{
    "details": "Healthy and Protenious bar",
    "title": "Proteing Bar",
    "id": "fc231103-ded1-4a25-8d2a-d174bde3057c",
    "category": "food",
    "size": null,
    "price": 100.5
}

If product is not found then it returns an error

{
    "timestamp": "2022-12-04T18:31:55.83154",
    "message": "Product not found"
}

Design decisions.

I used Spring boot to create the APIs as I just wanted to make APIs more readable as initially I thought of using Rails but it does so many things under the hood convention over config :) and looking at the code, things are not obvious.

I used h2 in memory database in order to persist record in memory, It is really flexible and works really well with JPA (an ORM for java).

I created a Product model, with all its basic details like details, title etc. Also, added validations on each if its fields, I choose Id as UUID as they are always unique and can help us if we go to distributed environments etc.

I have written the 100% testcases for the Controller, Repository and Service too, they can be found in src/test

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.