Project built using Java 15 and the following tools:
- Maven tool for automatic building
- Spring Boot as server side framework
- Hibernate as ORM / JPA implementation
- MySQL as database implementation
- Spring Data JPA as the top layer over Hibernate
- Postman for testing
Model is organized in the model package. Model consists of entity classes. Entities use various annotations that describe the relationships between each other. All these annotations are used by JPA in order to map entities to database tables.
Repositories are organized in the repository package. They are interfaces that are responsible for data persistence. The repository layer is an abstraction that provides all CRUD functionality and keeps hidden the data related information (e.g. specific database implmentation) from the other layers.
Service is organized in the service package. Service layer depends on the repository layer and provides separation of concern, encapsulating all the business logic implementation. It is there to apply business rules on data sent to and from the repository layer. Service layer does not care about the specific database implementation. This technique makes the application flexible in a possible data source replacement.
Controllers are organized in the controller package. Their layer depends on the repository layer and is responsible for the incoming requests and the outgoing responses. A controller determines available endpoints that client side is able to call. This layer should not apply logic on the receiving or returning data.
CREATE DATABASE ordertheproduct
You may also import to MySQL provided file ordertheproduct.sql
In case you want to use a different database name, follow the next steps:
CREATE DATABASE DB_NAME;
Open src/main/resources/application.properties
file
Change db.name
property to match your preferred database name DB_NAME
Open src/main/resources/application.properties
file
Change spring.datasource.username
and spring.datasource.password
properties to match your MySQL connection
To launch the application, chose one from the folloging options:
-
click the public static void main in
OrderProductApplication
and selectRun
from your IDE; -
if you use Maven, you can run the application by using:
./mvnw clean spring-boot:run
- you can build the JAR file with
./mvnw clean package
and then run the JAR file, as follows:
java -jar target/order-product-0.0.1-SNAPSHOT.jar
The server will start running at http://localhost:8080.
For your convenience, I suggest using Postman app to check provided endpoints. The following REST endpoints are available upon deployment:
HTTP Verb | URL | Description | Status Codes |
---|---|---|---|
GET |
http://localhost:8080/products?orderBy={id/name/price; default=name} |
Obtains a list of all existing products |
|
GET |
http://localhost:8080/products/{productId} |
Obtains the single product corresponding to the provided productId |
|
GET |
http://localhost:8080/orders |
Obtains a list of all existing orders |
|
GET |
http://localhost:8080/orders/{from}/{to} |
Obtains finalized orders corresponding to the provided dates of order (date format: yyyy-mm-dd) |
|
POST |
http://localhost:8080/products |
Creates a new product based on the data contained in the request body - JSON { "name": "provided_name", "price": provided_price } |
|
POST |
http://localhost:8080/orders |
Creates a new order without provided request body |
|
POST |
http://localhost:8080/orders/{orderId}/products?productId={productId}&productId={productId}&... |
Adds a new products to chosen order based on the provided orderId and productIds |
|
PUT |
http://localhost:8080/orders/{orderId} |
Updates an existing order as finalized based on provided orderId |
|
PUT |
http://localhost:8080/products/{productId} |
Updates an existing product with the data contained in the request body - JSON { "name": "provided_name", "price": provided_price } |
|
PUT |
http://localhost:8080/orders/{orderId}/products |
Updates an existing order (only new, not finalized orders) with updated products prices based on provided orderId |
|