Skip to content
This repository has been archived by the owner on Nov 10, 2022. It is now read-only.

trolit/grocery-store-themed-API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

77 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Grocery Store API (v1.2.2)

Maven project made in Java 14 and 2.3.4.RELEASE Spring Boot. API can serve for e.g. as groundwork to practise front-end implementation and develop functionalities like products filtering, shopping cart(rendering items in it, adding buttons to reassign amount), making purchase, decorating products that are on promotion etc. There is no payment system included. API by one of the PATCH requests serves products that user bought and reduces stock size on each of them on the backend(provided that purchase was successful ~> meaning if ordered products stocks were enough to make requested order).

Example of front-end implementation in Angular 10 can be found here.


Notes

- Accessing Swagger

To access Swagger enter: http://localhost:8080/swagger-ui/ in your browser (pay attention to the last slash)

- H2 Database usage

If API is running, access database console by entering: http://localhost:8080/h2-console in your browser (credentials are stored in application.properties file). Project is using persistent mode. Database sample with some precreated objects is stored in the repository and will be read as soon as you launch project. If you want to have empty database on each run, you can do one of the steps described below:

  1) Remove line spring.jpa.hibernate.ddl-auto=update from application.properties.
  In result on every app launch H2 will perform DROP TABLE operation.

  2) Overwrite line spring.datasource.url=jdbc:h2:file:./data/sample with jdbc:h2:mem:testdb
  This way app will use nonpersistent, "in-memory" database.

- Filtering products

Except percentagePriceDiff, every property from ProductQueryDto showed below can be requested to be filtered in key{operation}value scheme. Available operations are: :(equal), >(greater or equal), <(less or equal).

public class ProductQueryDto {

    private Integer id;
    private String name;
    private BigDecimal price;
    private Integer stock;
    private String category;
    private Integer categoryId;
    private String measurement;
    private BigDecimal previousPrice;   
    private Integer percentagePriceDiff;   // returns % difference between price and previousPrice
    private String priceStatus;
    
    // getters and setters skipped for brievity
}

To return products that category is "Alcoholic Drinks" you can use categoryId or human readable category. When using name with whitespace, whitespace must be replaced within %20 encoding. See examples below.

http://localhost:8080/api/v1/products?search=categoryId:1

http://localhost:8080/api/v1/products?search=category:Alcoholic%20Drinks

Filters can be chained to shorten results like below:

http://localhost:8080/api/v1/products?search=categoryId:1,price>15

If you would like to get products that are on discount you would send request:

http://localhost:8080/api/v1/products?search=priceStatus:discount

- Explained Stock update PATCH requests

Request /products/{id}/stock updates stock for single product overwriting currently stored data while the other one -> /api/v1/products/order - allows to update database information when someone orders chosen product(s). To use the second method, you need to send in body an array with specific order: {ProductId}, {Amount}. Note that before API applies changes to sent products stocks, it will verify if there is enough of each product to make order. See JSON example below for /api/v1/products/order action on how order should look like:

{
  "order": [
    "97", "68", "102", "44"
  ]
}
// 97 -> product Id
// 68 -> amount of product identified with 97 that user ordered

Available actions

Category Command Controller
Sr. No. Operation HTTP Method Path
/api/v1
Status Code Description
(1) Create Category POST /categories 201 New Category is created.
(2) Update Category PUT /categories/{id} 200 Category resource is updated.
(3) Delete Category DELETE /categories/{id} 204 Category is deleted.
Category Query Controller
Sr. No. Operation HTTP Method Path
/api/v1
Status Code Description
(1) Return Categories GET /categories 200 Fetches all categories.
(2) Return Category GET /categories/{id} 200 One category is fetched.
Product Command Controller
Sr. No. Operation HTTP Method Path
/api/v1
Status Code Description
(1) Create Product POST /products 201 New Product is created.
(2) Update Product PUT /products/{id} 200 Product is updated.
(3) Delete Product DELETE /products/{id} 204 Product is deleted.
(4) Change Product's price by percentage PATCH /products/{id}/price 200 Product price is updated according to given percentage.
(5) Update Product's stock PATCH /products/{id}/stock 204 Requested Product's stock is updated.
(6) Update Products stocks PATCH /products/order 204 Given products stocks are reduced by requested amount.
Product Query Controller
Sr. No. Operation HTTP Method Path
/api/v1
Status Code Description
(1) Return Products (can be filtered) GET /products 200 Fetches all products.
(2) Return Product within given id GET /products/{id} 200 Returns single product.
Connection Controller
Sr. No. Operation HTTP Method Path
/api/v1
Status Code Description
(1) Returns OK HttpStatus GET /online 200 Returns OK if API processes the request.

Changelog

[ 27.11.2020, 1.2.2 ]

  • Added simple action within ConnectionController that allows to check if API is online
  • Added action to ProductsController that returns all available measurements
  • Fixed case of null previous price in getProduct function
  • Changed return value for changeProductPriceByPercentage(from int to custom dto)
  • Fixed checkIfProductsOrderIsPossible and buyProducts functions
  • Changed filtering rule from greater-equal/less-equal to greater-than/less-than

[ 01.11.2020, 1.1.2 ]

  • Added default CORS policy

[ 04.10.2020, 1.1.1 ]

  • Extended product model with previousPrice property
  • Extended product query dto with previousPrice, priceStatus(discount/rise/unchanged) and percentagePriceDiff
  • Updated Product HTTP PUT, GET methods to supply 3 variables mentioned above
  • Implemented price difference counting(in percents) in ProductCommonMethods.java
  • Implemented priceStatus filtering
  • Optimized getAllProducts()
    • Moved categoryId, category, priceStatus predicates into ProductPredicate(categoryId, category are called within Product model)
    • Extraced setting percentagePriceDiff and adding params to ProductPredicateBuilder into separate methods
  • Added 400(Bad request) response for getAllProducts() in case of trying to filter via percentagePriceDiff

- Used tools and technologies
  • Spring Boot 2.3.4.RELEASE
  • Open JDK 14
  • Hibernate (spring-boot-starter-data-jpa)
  • JPA
  • Maven
  • H2 Database
  • Springfox Swagger 3.0.0
  • ModelMapper 2.3.8
  • Querydsl
  • IDE - IntelliJ IDEA CE 2020.1.1
- References

Template generated using EzGitDoc