/RobotDreams-Spring-Course-Assignments

Assignments completed as part of the RobotDreams Java & Spring Framework Course

Primary LanguageJava

The repository includes the completed assignments in the Java & Spring Framework Course (February 14 - April 17 2024) provided by RobotDreams.

Assignment 2

Task

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.

Solution

The Example folder contains the classes and interfaces present in the UML diagram.

Assignment2-UML

Assignment 3

Task

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).


Solution Notes

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.

Assignment3

Assignment 4

Task

  • 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.

Solution Notes

An example Post Request to create a resource Assignment4-PostRequest

An example Get Request after the previous post request Assignment4-FinalGetRequest



Assignment 5

Task

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.


Solution Notes

Requests


Save a product ProductSave

Save an order OrderCreated




Table Content


Product Table
ProductsTable

Order Table
OrdersTable

OrderProducts Table
OrderProductsTable



Assignment 6

Task

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.


Solution Notes

Examples were created using 'CascadeType.REMOVE' and 'CascadeType.PERSIST' to demonstrate the impact of the cascade type on entities.


CascadeType.PERSIST

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...

image


Product Table product-table


ProductDetail Table productDetail-table




CascadeType.REMOVE

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

Post an order image


Orders Table
image


OrderProducts Table
image


Delete request to delete created Order
image


Orders Table
image


OrderProducts Table
image



Assignment 7

Task

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.


Solution Notes

  • package customFunctions includes the smsFunction which is a functional interface

SmsService includes the method sendSms which runs asynchronously

  • package util includes the class UtilTest to used persist test data


Assignment 8

Task

Implement exception handling in the order and product services where you deem necessary. Create your own exceptions and handle them accordingly.


Solution Notes

InsufficentOrderAmountException



1- A post request was sent to create product with price "1" image



2- A post request was sent to create an order with a product which was created in previous step image

Observed Console Output image

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 ");
        }


InvalidPriceException

1- A post request was sent to create product with a price "-100" image

Observed Console Output image

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");
        }


Assignment 9

Task

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.


Solution Notes

The unit tests existed in

OrderServiceUnitTests.java



Assignment 10

Task

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.


Solution Notes

Both Sms and Shipping features are completed. They locates in package sms and shipping



Assignment 11

Task

Dockerize the developed java application.


Solution Notes

Docker Image
https://hub.docker.com/repository/docker/canberkt/assignment11-rd/general