Skip to content

Commit

Permalink
Refactored presentation layer for console interface
Browse files Browse the repository at this point in the history
- Implemented adapter pattern for console interface
  • Loading branch information
kaizerpwn committed May 2, 2024
1 parent 89cce03 commit ec02851
Show file tree
Hide file tree
Showing 17 changed files with 109 additions and 105 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.ibrahimokic.ordermanagement;

import com.ibrahimokic.ordermanagement.controller.console.UserConsoleController;
import com.ibrahimokic.ordermanagement.repository.AddressRepository;
import com.ibrahimokic.ordermanagement.repository.OrderRepository;
import com.ibrahimokic.ordermanagement.repository.ProductRepository;
import com.ibrahimokic.ordermanagement.repository.UserRepository;
import com.ibrahimokic.ordermanagement.adapters.UserConsoleAdapter;
import com.ibrahimokic.ordermanagement.service.AddressService;
import com.ibrahimokic.ordermanagement.service.OrderService;
import com.ibrahimokic.ordermanagement.service.ProductService;
import com.ibrahimokic.ordermanagement.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
Expand All @@ -15,10 +15,10 @@
@EnableJpaRepositories
@RequiredArgsConstructor
public class OrderManagementApplication implements CommandLineRunner {
private final UserRepository userRepository;
private final AddressRepository addressRepository;
private final ProductRepository productRepository;
private final OrderRepository orderRepository;
private final UserService userService;
private final AddressService addressService;
private final ProductService productService;
private final OrderService orderService;

public static void main(String[] args) {
SpringApplication.run(OrderManagementApplication.class, args);
Expand All @@ -28,9 +28,9 @@ public static void main(String[] args) {
public void run(String... args) {
String environment = System.getenv("ENVIRONMENT");
if (environment == null || !"github-actions".equals(environment)) {
UserConsoleController userConsoleController = new UserConsoleController(userRepository, addressRepository,
productRepository, orderRepository);
userConsoleController.userMainForm();
UserConsoleAdapter userConsoleAdapter = new UserConsoleAdapter(userService, addressService,
productService, orderService);
userConsoleAdapter.userMainForm();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
package com.ibrahimokic.ordermanagement.controller.console;
package com.ibrahimokic.ordermanagement.adapters;

import com.ibrahimokic.ordermanagement.controller.console.ui.ConsoleUserInterface;
import com.ibrahimokic.ordermanagement.domain.entity.Address;
import com.ibrahimokic.ordermanagement.domain.entity.User;
import com.ibrahimokic.ordermanagement.repository.AddressRepository;
import com.ibrahimokic.ordermanagement.repository.OrderRepository;
import com.ibrahimokic.ordermanagement.repository.ProductRepository;
import com.ibrahimokic.ordermanagement.repository.UserRepository;
import com.ibrahimokic.ordermanagement.service.impl.UserServiceImpl;
import com.ibrahimokic.ordermanagement.domain.entity.User;
import com.ibrahimokic.ordermanagement.service.AddressService;
import com.ibrahimokic.ordermanagement.service.OrderService;
import com.ibrahimokic.ordermanagement.service.ProductService;
import com.ibrahimokic.ordermanagement.service.UserService;
import com.ibrahimokic.ordermanagement.utils.Utils;
import lombok.RequiredArgsConstructor;

import java.time.LocalDate;

@RequiredArgsConstructor
public class AdminConsoleController extends ConsoleUserInterface {
public class AdminConsoleAdapter extends ConsoleUserInterface {
private final User loggedUser;
private final UserRepository userRepository;
private final AddressRepository addressRepository;
private final ProductRepository productRepository;
private final OrderRepository orderRepository;
private final UserService userService;
private final AddressService addressService;
private final ProductService productService;
private final OrderService orderService;

public void adminDashboard() {
Utils.clearConsole(20);
Expand All @@ -45,8 +44,8 @@ private void processAdminDashboardChoice(int choice) {
case 2 -> adminProductManagementOptions();
case 3 -> adminOrderManagementOptions();
case 4 -> {
UserConsoleController userConsoleController = new UserConsoleController(userRepository, addressRepository, productRepository, orderRepository);
userConsoleController.userMainForm();
UserConsoleAdapter userConsoleAdapter = new UserConsoleAdapter(userService, addressService, productService, orderService);
userConsoleAdapter.userMainForm();
}
}
}
Expand All @@ -55,8 +54,8 @@ public void adminOrderManagementOptions() {
Utils.clearConsole(20);
consoleHeader();

OrderConsoleController orderConsoleController = new OrderConsoleController(loggedUser, orderRepository);
orderConsoleController.displayAdminOrderManagementMenu();
OrderConsoleAdapter orderConsoleAdapter = new OrderConsoleAdapter(loggedUser, orderService);
orderConsoleAdapter.displayAdminOrderManagementMenu();

int choice = getValidInput( 3);
processAdminOrderManagementChoice(choice);
Expand All @@ -66,8 +65,8 @@ public void adminProductManagementOptions() {
Utils.clearConsole(20);
consoleHeader();

ProductConsoleController productConsoleController = new ProductConsoleController(loggedUser, productRepository);
productConsoleController.displayAdminProductManagementMenu();
ProductConsoleAdapter productConsoleAdapter = new ProductConsoleAdapter(loggedUser, productService);
productConsoleAdapter.displayAdminProductManagementMenu();

int choice = getValidInput( 4);
processAdminProductManagementChoice(choice);
Expand Down Expand Up @@ -121,8 +120,8 @@ public void adminUserListForm() {
Utils.clearConsole(20);
consoleHeader();

UserConsoleController userConsoleController = new UserConsoleController(userRepository, addressRepository, productRepository, orderRepository);
userConsoleController.showAllUsersTable();
UserConsoleAdapter userConsoleAdapter = new UserConsoleAdapter(userService, addressService, productService, orderService);
userConsoleAdapter.showAllUsersTable();

Utils.returnBackToTheMainMenu(scanner);
adminUserManagementOptions();
Expand All @@ -132,8 +131,8 @@ public void adminProductListForm() {
Utils.clearConsole(20);
consoleHeader();

ProductConsoleController productConsoleController = new ProductConsoleController(loggedUser, productRepository);
productConsoleController.showAllProductsTable();
ProductConsoleAdapter productConsoleAdapter = new ProductConsoleAdapter(loggedUser, productService);
productConsoleAdapter.showAllProductsTable();

Utils.returnBackToTheMainMenu(scanner);
adminProductManagementOptions();
Expand All @@ -143,8 +142,8 @@ public void adminOrderListForm() {
Utils.clearConsole(20);
consoleHeader();

OrderConsoleController orderConsoleController = new OrderConsoleController(loggedUser, orderRepository);
orderConsoleController.showAllOrdersList();
OrderConsoleAdapter orderConsoleAdapter = new OrderConsoleAdapter(loggedUser, orderService);
orderConsoleAdapter.showAllOrdersList();

Utils.returnBackToTheMainMenu(scanner);
adminOrderManagementOptions();
Expand All @@ -155,8 +154,8 @@ public void adminOrderExportForm() {
consoleHeader();
scanner.nextLine();

OrderConsoleController orderConsoleController = new OrderConsoleController(loggedUser, orderRepository);
orderConsoleController.exportOrdersAsExcelFile();
OrderConsoleAdapter orderConsoleAdapter = new OrderConsoleAdapter(loggedUser, orderService);
orderConsoleAdapter.exportOrdersAsExcelFile();

Utils.returnBackToTheMainMenu(scanner);
adminOrderManagementOptions();
Expand All @@ -167,8 +166,8 @@ public void adminProductCreationForm() {
consoleHeader();
scanner.nextLine();

ProductConsoleController productConsoleController = new ProductConsoleController(loggedUser, productRepository);
productConsoleController.createNewProduct();
ProductConsoleAdapter productConsoleAdapter = new ProductConsoleAdapter(loggedUser, productService);
productConsoleAdapter.createNewProduct();

adminProductManagementOptions();
}
Expand Down Expand Up @@ -218,10 +217,10 @@ private void createUserAccountForm() {

do {
email = Utils.promptUserInput(scanner,"email");
if (userRepository.findByEmail(email) != null) {
if (userService.findByEmail(email) != null) {
System.out.println("User with this email already exists. Please try a different email address.");
}
} while (userRepository.findByEmail(email) != null);
} while (userService.findByEmail(email) != null);

String name = Utils.promptUserInput(scanner,"first name");
String surname = Utils.promptUserInput(scanner,"last name");
Expand Down Expand Up @@ -257,8 +256,8 @@ private void deleteUserForm() {
consoleHeader();
scanner.nextLine();

UserConsoleController userConsoleController = new UserConsoleController(userRepository, addressRepository, productRepository, orderRepository);
userConsoleController.showAllUsersTable();
UserConsoleAdapter userConsoleAdapter = new UserConsoleAdapter(userService, addressService, productService, orderService);
userConsoleAdapter.showAllUsersTable();

System.out.println(">> Please enter 'ID' of the user you want to delete.");
System.out.println(">> Press 'ENTER ' if you want to go back to the main menu.");
Expand All @@ -279,9 +278,7 @@ private void deleteUserForm() {
return;
}

UserServiceImpl userService = new UserServiceImpl(userRepository);

if (userRepository.findById(userId).isPresent()) {
if (userService.findById(userId).isPresent()) {
userService.deleteUser(userId);
System.out.println("Successfully deleted user with ID: " + userId);
} else {
Expand All @@ -293,14 +290,13 @@ private void deleteUserForm() {
}

private void adminProductDeletionForm() {
ProductConsoleController productConsoleController = new ProductConsoleController(loggedUser, productRepository);
ProductConsoleAdapter productConsoleAdapter = new ProductConsoleAdapter(loggedUser, productService);

productConsoleController.deleteProductForm();
productConsoleAdapter.deleteProductForm();
adminProductManagementOptions();
}

private void saveUserAndReturnToMenu(User user) {
UserServiceImpl userService = new UserServiceImpl(userRepository);
userService.createUser(user);

Utils.returnBackToTheMainMenu(scanner);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.ibrahimokic.ordermanagement.controller.console;
package com.ibrahimokic.ordermanagement.adapters;

import com.ibrahimokic.ordermanagement.controller.console.ui.ConsoleUserInterface;
import com.ibrahimokic.ordermanagement.domain.entity.Address;
import com.ibrahimokic.ordermanagement.domain.entity.Order;
import com.ibrahimokic.ordermanagement.domain.entity.User;
import com.ibrahimokic.ordermanagement.repository.OrderRepository;
import com.ibrahimokic.ordermanagement.domain.entity.User;
import com.ibrahimokic.ordermanagement.service.OrderService;
import com.ibrahimokic.ordermanagement.utils.Utils;

import lombok.RequiredArgsConstructor;
Expand All @@ -16,12 +16,12 @@
import java.util.List;

@RequiredArgsConstructor
public class OrderConsoleController extends ConsoleUserInterface {
public class OrderConsoleAdapter extends ConsoleUserInterface {
private final User loggedUser;
private final OrderRepository orderRepository;
private final OrderService orderService;

public List<Order> showAllOrdersList() {
List<Order> orderList = orderRepository.findAll();
List<Order> orderList = orderService.getAllOrders();

System.out.println("|-------------|---------------------|---------------|---------------------|-------------------------------------------------------------|-------------------------------------------------------|");
System.out.println("| Order ID | User | Order Date | Total Amount | Delivery Address | Source Address |");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.ibrahimokic.ordermanagement.controller.console;
package com.ibrahimokic.ordermanagement.adapters;

import com.ibrahimokic.ordermanagement.controller.console.ui.ConsoleUserInterface;
import com.ibrahimokic.ordermanagement.domain.entity.Product;
import com.ibrahimokic.ordermanagement.domain.entity.User;
import com.ibrahimokic.ordermanagement.repository.ProductRepository;
import com.ibrahimokic.ordermanagement.service.impl.ProductServiceImpl;
import com.ibrahimokic.ordermanagement.domain.entity.User;
import com.ibrahimokic.ordermanagement.service.ProductService;
import com.ibrahimokic.ordermanagement.utils.Utils;
import lombok.RequiredArgsConstructor;

Expand All @@ -13,9 +12,9 @@
import java.util.List;

@RequiredArgsConstructor
public class ProductConsoleController extends ConsoleUserInterface {
public class ProductConsoleAdapter extends ConsoleUserInterface {
private final User loggedUser;
private final ProductRepository productRepository;
private final ProductService productService;

public void displayAdminProductManagementMenu() {
Utils.clearConsole(20);
Expand All @@ -29,7 +28,7 @@ public void displayAdminProductManagementMenu() {
}

public List<Product> showAllProductsTable() {
List<Product> productList = productRepository.findAll();
List<Product> productList = productService.getAllProducts();

System.out.println("|-------------|---------------------|---------------|---------------------|---------------------|---------------------|");
System.out.println("| Product ID | Product Name | Price | Available From | Available Until | Available Quantity |");
Expand Down Expand Up @@ -71,7 +70,7 @@ public void createNewProduct() {
newProduct.setAvailableUntil(availableUntil);
newProduct.setAvailableQuantity(availableQuantity);

productRepository.save(newProduct);
productService.createProduct(newProduct);
System.out.println("Product '"+productName+"' successfully created.");

Utils.returnBackToTheMainMenu(scanner);
Expand All @@ -98,8 +97,6 @@ public void deleteProductForm() {
}

if (productId != 0) {
ProductServiceImpl productService = new ProductServiceImpl(productRepository);

if (productList.stream().anyMatch(product -> product.getProductId().equals(productId))) {
productService.deleteProduct(productId);
System.out.println("Successfully deleted product with ID: " + productId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.ibrahimokic.ordermanagement.controller.console;
package com.ibrahimokic.ordermanagement.adapters;

import com.ibrahimokic.ordermanagement.controller.console.ui.ConsoleUserInterface;
import com.ibrahimokic.ordermanagement.domain.entity.User;
import com.ibrahimokic.ordermanagement.repository.AddressRepository;
import com.ibrahimokic.ordermanagement.repository.OrderRepository;
import com.ibrahimokic.ordermanagement.repository.ProductRepository;
import com.ibrahimokic.ordermanagement.repository.UserRepository;
import com.ibrahimokic.ordermanagement.service.AddressService;
import com.ibrahimokic.ordermanagement.service.OrderService;
import com.ibrahimokic.ordermanagement.service.ProductService;
import com.ibrahimokic.ordermanagement.service.UserService;
import com.ibrahimokic.ordermanagement.utils.Utils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
Expand All @@ -14,11 +14,11 @@

@Component
@RequiredArgsConstructor
public class UserConsoleController extends ConsoleUserInterface {
private final UserRepository userRepository;
private final AddressRepository addressRepository;
private final ProductRepository productRepository;
private final OrderRepository orderRepository;
public class UserConsoleAdapter extends ConsoleUserInterface {
private final UserService userService;
private final AddressService addressService;
private final ProductService productService;
private final OrderService orderService;

public void userMainForm() {
Utils.clearConsole(20);
Expand Down Expand Up @@ -68,15 +68,15 @@ public void userLoginForm() {
System.out.print(">> Please enter your password: ");
String password = scanner.nextLine();

User retrievedUser = userRepository.findByUsername(username);
User retrievedUser = userService.findByUsername(username).get();

if (retrievedUser != null && retrievedUser.checkUserPassword(password)) {
loggedIn = true;
AdminConsoleController adminConsoleController = new AdminConsoleController(retrievedUser, userRepository, addressRepository, productRepository, orderRepository);
AdminConsoleAdapter adminConsoleAdapter = new AdminConsoleAdapter(retrievedUser, userService, addressService, productService, orderService);

switch (retrievedUser.getRole()) {
case "user" -> userMainForm();
case "admin" -> adminConsoleController.adminDashboard();
case "admin" -> adminConsoleAdapter.adminDashboard();
}
} else {
System.out.println("Incorrect username or password");
Expand All @@ -94,7 +94,7 @@ public void userLoginForm() {
}

public List<User> showAllUsersTable() {
List<User> userList = userRepository.findAll();
List<User> userList = userService.getAllUsers();

System.out.println("|-------------|--------------|------------------------|----------|----------------|---------------|----------------|");
System.out.println("| User ID | Username | Email | Role | First Name | Last Name | Birth Date |");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ public class AddressController {
@Content(mediaType = "application/json", array = @ArraySchema(schema = @Schema(implementation = Address.class)))
})
public ResponseEntity<List<Address>> getAllAddresses() {
List<Address> addresses = addressService.getAllAddresses();
return ResponseEntity.ok(addresses);
return ResponseEntity.ok(addressService.getAllAddresses());
}

@GetMapping("/{addressId}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class ProductController {
@Content(mediaType = "application/json", array = @ArraySchema(schema = @Schema(implementation = Product.class)))
})
public ResponseEntity<?> getAllProducts() {
return productService.getAllProducts();
return ResponseEntity.ok(productService.getAllProducts());
}

@GetMapping("/{productId}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ public class UserController {
@Content(mediaType = "application/json", array = @ArraySchema(schema = @Schema(implementation = User.class)))
})
public ResponseEntity<List<User>> getAllUsers() {
List<User> users = userService.getAllUsers();
return ResponseEntity.ok(users);
return ResponseEntity.ok(userService.getAllUsers());
}

@GetMapping("/{userId}")
Expand Down
Loading

0 comments on commit ec02851

Please sign in to comment.