Skip to content

Commit

Permalink
Merge pull request #11 from nhkhai/main
Browse files Browse the repository at this point in the history
SIS-27 & SIS-29: Added Basic CRUD for Customer And Product.
  • Loading branch information
nhkhai authored Mar 22, 2024
2 parents d0a4cf4 + 682f930 commit 1d747ed
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 14 deletions.
29 changes: 15 additions & 14 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
Expand All @@ -20,7 +21,9 @@
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand All @@ -30,6 +33,15 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- <dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
Expand All @@ -39,21 +51,10 @@
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
Expand Down Expand Up @@ -98,4 +99,4 @@
</plugins>
</pluginManagement>
</build>
</project>
</project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package sg.com.smartinventory.serviceImpls;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import sg.com.smartinventory.entities.Customer;
Expand All @@ -24,6 +27,51 @@ public Customer createCustomer(Customer customer) {
return newCustomer;
}

@Override
public Customer getCustomer(long id) {
// Optional<Customer> optionalCustomer = customerRepository.findById(id);
// if(optionalCustomer.isPresent()) {
// Customer foundCustomer = optionalCustomer.get();
// return foundCustomer;
// }
// throw new CustomerNotFoundException(id);
// return customerRepository.findById(id).orElseThrow(() -> new
// CustomerNotFoundException(id));
return customerRepository.findById(id).get(0);
}

@Override
public ArrayList<Customer> getAllCustomers() {
List<Customer> allCustomers = customerRepository.findAll();
return (ArrayList<Customer>) allCustomers;
}

@Override
public Customer updateCustomer(long id, Customer customer) {
// Retrieve the customer from the database.
// Customer customerToUpdate = customerRepository.findById(id).orElseThrow(() ->
// new
// CustomerNotFoundException(id));
Customer customerToUpdate = customerRepository.findById(id).get(0);

// Update the customer retrieved from the database.
customerToUpdate.setFirstName(customer.getFirstName());
customerToUpdate.setLastName(customer.getLastName());
customerToUpdate.setCountry(customer.getCountry());
customerToUpdate.setAddress(customer.getAddress());
customerToUpdate.setPostalCode(customer.getPostalCode());
customerToUpdate.setPhoneNumber(customer.getPhoneNumber());
customerToUpdate.setEmail(customer.getEmail());

// Save the updated customer back to the database.
return customerRepository.save(customerToUpdate);
}

@Override
public void deleteCustomer(long id) {
customerRepository.deleteById(id);
}

// @Override
// public Review addReviewToCustomer(long customerId, long productId, Review
// review) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package sg.com.smartinventory.serviceImpls;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import sg.com.smartinventory.entities.Product;
// import sg.com.smartinventory.exceptions.ProductNotFoundException;
import sg.com.smartinventory.repositories.ProductRepository;
import sg.com.smartinventory.services.ProductService;

Expand All @@ -21,4 +25,47 @@ public Product createProduct(Product product) {

return newProduct;
}

@Override
public Product getProduct(long id) {
// Optional<Product> optionalProduct = productRepository.findById(id);
// if(optionalProduct.isPresent()) {
// Product foundProduct = optionalProduct.get();
// return foundProduct;
// }
// throw new ProductNotFoundException(id);
// return productRepository.findById(id).orElseThrow(() -> new
// ProductNotFoundException(id));
return productRepository.findById(id).get(0);
}

@Override
public ArrayList<Product> getAllProducts() {
List<Product> allProducts = productRepository.findAll();
return (ArrayList<Product>) allProducts;
}

@Override
public Product updateProduct(long id, Product product) {
// Retrieve the product from the database.
// Product productToUpdate = productRepository.findById(id).orElseThrow(() ->
// new
// ProductNotFoundException(id));
Product productToUpdate = productRepository.findById(id).get(0);

// Update the product retrieved from the database.
productToUpdate.setCategory(product.getCategory());
productToUpdate.setName(product.getName());
productToUpdate.setDescription(product.getDescription());
productToUpdate.setPrice(product.getPrice());
productToUpdate.setStockQuantity(product.getStockQuantity());

// Save the updated product back to the database.
return productRepository.save(productToUpdate);
}

@Override
public void deleteProduct(long id) {
productRepository.deleteById(id);
}
}
10 changes: 10 additions & 0 deletions src/main/java/sg/com/smartinventory/services/CustomerService.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
package sg.com.smartinventory.services;

import java.util.ArrayList;

import sg.com.smartinventory.entities.Customer;
// import sg.com.smartinventory.entities.Review;

public interface CustomerService {
Customer createCustomer(Customer customer);

Customer getCustomer(long id);

ArrayList<Customer> getAllCustomers();

Customer updateCustomer(long id, Customer customer);

void deleteCustomer(long id);

// Review addReviewToCustomer(long customerId, long productId, Review review);
}
10 changes: 10 additions & 0 deletions src/main/java/sg/com/smartinventory/services/ProductService.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
package sg.com.smartinventory.services;

import java.util.ArrayList;

import sg.com.smartinventory.entities.Product;

public interface ProductService {
Product createProduct(Product product);

Product getProduct(long id);

ArrayList<Product> getAllProducts();

Product updateProduct(long id, Product product);

void deleteProduct(long id);
}

0 comments on commit 1d747ed

Please sign in to comment.