The repository includes the completed assignments in the Java & Spring Framework Course (February 14 - April 17 2024) provided by RobotDreams.
We have designed a university organizational structure in code during the lecture. Introduce new methods through interfaces and create a new UML diagram to reflect the latest additions.The Example folder contains the classes and interfaces present in the UML diagram.
Consider an online store that uses a database to store its products. The columns of the database are specified as follows in the code below
private String name;
private String category;
private String photoUrl;
private String description;
private Double price;
Write code for an API that be able to list all products of the store by category. NOTE: The API you will create should take "category" as a parameter, and JDBC Template should be used to filter products by category (using the WHERE clause).
If the query string is proven, the api delivers the products based on the given category, otherwise (If the request does not include the query string for categoryName) The api delivers all of the products.
- Create an API to enable the creation of a new product entry. This API should accept product details, which are provided in the previous task. Use Hibernate to persist the incoming data. After the registration process, return a message to the customer like "The operation has been completed."
- Implement a layered architecture.
An example Post Request to create a resource
An example Get Request after the previous post request
Specify the fetch strategy for the product and order entities you created. Write an API to create a common entity for order and product and establish this relationship. Allow users to create a product using the product API. Then, through the order API, create an order associated with this product.
Using the Spring Framework, implement cascade operations in the product, order, and orderproduct entities where the relationships exist. For example, if there is an order record and it has a relationship with a product, when the order is deleted, the related entity should also be deleted. Implement this scenario using cascade.
Examples were created using 'CascadeType.REMOVE' and 'CascadeType.PERSIST' to demonstrate the impact of the cascade type on entities.
I have created a new entity named "ProductDetail" with a OneToOne relationship with the "Product" entity.
When saving a Product through a POST request, the ProductDetail information will also be provided in the request body. Since the Cascade type is set to persist, the ProductDetail entity will be saved to the database along with its one-to-one relation.
Product.java
@Table(name = "Products")
@Entity
public class Product extends BaseEntity implements Serializable {
// codes here...
@OneToOne(mappedBy = "product", fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
private ProductDetail productDetail;
// codes here...
When an order is deleted, the related orderProducts must also be deleted.
Order.java
@Table(name = "Orders")
@Entity
public class Order extends BaseEntity implements Serializable {
//codes here
@OneToMany(mappedBy = "order", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
private Set<OrderProduct> orderProducts;
//codes here
Delete request to delete created Order
When a user places an order in the application, the user should receive a notification via SMS. The SMS service sends information about the order and operates asynchronously.
- package
customFunctions
includes the smsFunction which is a functional interface
SmsService
includes the method sendSms
which runs asynchronously
- package
util
includes the classUtilTest
to used persist test data
Implement exception handling in the order and product services where you deem necessary. Create your own exceptions and handle them accordingly.
1- A post request was sent to create product with price "1"
2- A post request was sent to create an order with a product which was created in previous step
private void checkIfOrderAmountIsSufficent(List<Product> products)
throws InsufficientOrderAmountException{
double minOrderAmount = 50.0;
var total = products.stream()
.map(Product::getPrice)
.reduce(0.0,Double::sum);
if(minOrderAmount > total)
throw new InsufficientOrderAmountException(total);
}
try-catch block
try {
checkIfOrderAmountIsSufficent(products);
} catch (InsufficientOrderAmountException e) {
System.out.println("Logged !");
throw new GeneralException("The order amount is insufficient ");
}
1- A post request was sent to create product with a price "-100"
private void checkIfPriceIsValid(double price)
throws InvalidPriceException {
if(price <= 0.0)
throw new InvalidPriceException("The price must be greater than 0. The input was: " + price);
}
try-catch block
try {
checkIfPriceIsValid(createProductRequestDto.getPrice());
} catch (InvalidPriceException e) {
System.out.println("Logged!");
throw new GeneralException("Invalid price");
}
Write a unit tests for one of the methods in the service layer of the existing structure, either for order creation or for product APIs.
The unit tests existed in
OrderServiceUnitTests.java
In the existing project, use Lombok annotations appropriately. Implement the calculation of shipping fees when creating an order. You can apply the Strategy design pattern for this task. Similarly, create an API where users can modify their own user information. After changes are made to an existing user, implement a feature to send an SMS to the user.
When sending the SMS, provide two different options for companies. If the user is premium, use a different SMS provider. You can use the Strategy pattern for this structure.
NOTE: There are two tasks in this assignment, and completing one is sufficient. You can choose to either implement the shipping fee calculation or the user SMS sending feature.
Both Sms and Shipping features are completed. They locates in package
sms
and shipping
Dockerize the developed java application.
Docker Image
https://hub.docker.com/repository/docker/canberkt/assignment11-rd/general