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.
- 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
Category Command Controller
Category Query Controller
Product Command Controller
Product Query Controller
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. |
[ 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
- Spring Initializr
- Spring REST CRUD JPA Example
- Spring Boot CRUD REST API JPA H2 Example
- Integrating H2 with Spring Boot
- Best way to map 1-n relation in JPA-Hibernate
- Setting up Swagger
- ModelMapper documentation
- Query language Spring Querydsl
- Spring Boot documenting Swagger
- Swagger read docs from *.md files
- Online regular expressions tester
Template generated using EzGitDoc