Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SIS-1: Created skeleton class files and updated project structure #6

Merged
merged 2 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ build/

### VS Code ###
.vscode/
src/main/java/sg/com/smartinventory/logfile
72 changes: 0 additions & 72 deletions src/main/java/sg/com/smartinventory/DataLoader.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.springframework.web.bind.annotation.RestController;

import jakarta.validation.Valid;

import sg.com.smartinventory.entities.Customer;
import sg.com.smartinventory.services.CustomerService;

Expand All @@ -24,7 +25,6 @@ public CustomerController(CustomerService customerService) {
// CREATE.
@PostMapping("")
public ResponseEntity<Customer> createCustomer(@Valid @RequestBody Customer customer) {

// if(bindingResult.hasErrors()) {
// return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
// }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package sg.com.smartinventory.controllers;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.validation.Valid;
import sg.com.smartinventory.entities.Product;
import sg.com.smartinventory.services.ProductService;

@RestController
@RequestMapping("/products")
public class ProductController {
private ProductService productService;

// @Autowired
public ProductController(ProductService productService) {
this.productService = productService;
}

// CREATE.
@PostMapping("")
public ResponseEntity<Product> createProduct(@Valid @RequestBody Product product) {
// if(bindingResult.hasErrors()) {
// return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
// }

Product newProduct = productService.createProduct(product);
return new ResponseEntity<>(newProduct, HttpStatus.CREATED);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package sg.com.smartinventory.controllers;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.validation.Valid;
import sg.com.smartinventory.entities.Review;
import sg.com.smartinventory.services.ReviewService;

@RestController
@RequestMapping("/reviews")
public class ReviewController {
private ReviewService reviewService;

// @Autowired
public ReviewController(ReviewService reviewService) {
this.reviewService = reviewService;
}

// CREATE.
@PostMapping("")
public ResponseEntity<Review> createReview(@Valid @RequestBody Review review) {
// if(bindingResult.hasErrors()) {
// return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
// }

Review newReview = reviewService.createReview(review);
return new ResponseEntity<>(newReview, HttpStatus.CREATED);
}
}
1 change: 1 addition & 0 deletions src/main/java/sg/com/smartinventory/entities/Customer.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import jakarta.validation.constraints.Digits;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/sg/com/smartinventory/entities/Product.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/sg/com/smartinventory/entities/Review.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package sg.com.smartinventory.exceptions;

public class CustomerNotFoundException extends RuntimeException {
public CustomerNotFoundException(Long id) {
super("Could not find customer with id: " + id + ". ");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package sg.com.smartinventory.exceptions;

public class ProductNotFoundException extends RuntimeException {
public ProductNotFoundException(String id) {
super("Could not find product with id: " + id + ". ");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package sg.com.smartinventory.exceptions;

public class ReviewNotFoundException extends RuntimeException {
public ReviewNotFoundException(String id) {
super("Could not find review with id: " + id + ". ");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import sg.com.smartinventory.entities.Customer;

//CustomerRepository simply extends JpaRepository, giving us a lot of default methods to access db without the hardwork
// CustomerRepository simply extends JpaRepository, giving us a lot of default methods to access db without the hard work.
public interface CustomerRepository extends JpaRepository<Customer, Long> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package sg.com.smartinventory.serviceImpls;

import org.springframework.stereotype.Service;

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

@Service
public class ProductServiceImpl implements ProductService {
private ProductRepository productRepository;

// @Autowired
public ProductServiceImpl(ProductRepository productRepository) {
this.productRepository = productRepository;
}

@Override
public Product createProduct(Product product) {
Product newProduct = productRepository.save(product);

return newProduct;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package sg.com.smartinventory.serviceImpls;

import org.springframework.stereotype.Service;

import sg.com.smartinventory.entities.Review;
import sg.com.smartinventory.repositories.ReviewRepository;
import sg.com.smartinventory.services.ReviewService;

@Service
public class ReviewServiceImpl implements ReviewService {
private ReviewRepository reviewRepository;

// @Autowired
public ReviewServiceImpl(ReviewRepository reviewRepository) {
this.reviewRepository = reviewRepository;
}

@Override
public Review createReview(Review review) {
Review newReview = reviewRepository.save(review);

return newReview;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package sg.com.smartinventory.services;

import sg.com.smartinventory.entities.Product;

public interface ProductService {
Product createProduct(Product product);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package sg.com.smartinventory.services;

import sg.com.smartinventory.entities.Review;

public interface ReviewService {
Review createReview(Review review);
}
72 changes: 72 additions & 0 deletions src/main/java/sg/com/smartinventory/utility/DataLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package sg.com.smartinventory.utility;

// import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import jakarta.annotation.PostConstruct;

import sg.com.smartinventory.entities.Customer;
import sg.com.smartinventory.entities.Product;
import sg.com.smartinventory.entities.Review;
import sg.com.smartinventory.repositories.CustomerRepository;
import sg.com.smartinventory.repositories.ProductRepository;
import sg.com.smartinventory.repositories.ReviewRepository;

@Component
public class DataLoader {
// Inject all repositories.
private CustomerRepository customerRepository;
private ProductRepository productRepository;
private ReviewRepository reviewRepository;

// @Autowired
public DataLoader(CustomerRepository customerRepository, ProductRepository productRepository,
ReviewRepository reviewRepository) {
this.customerRepository = customerRepository;
this.productRepository = productRepository;
this.reviewRepository = reviewRepository;
}

@PostConstruct
public void loadData() {
// Clear all data.
customerRepository.deleteAll();
productRepository.deleteAll();
reviewRepository.deleteAll();

// Create fake data.
customerRepository.save(new Customer("John", "Doe", "USA", "123 Main St", 123456, 12345678,
"[email protected]", 101));

customerRepository.save(new Customer("Alice", "Smith", "Canada", "456 Maple Ave", 543210, 98765432,
"[email protected]", 102));
customerRepository.save(new Customer("Michael", "Johnson", "UK", "789 Oak Rd", 567890, 98761234,
"[email protected]", 103));
customerRepository.save(new Customer("Emily", "Brown", "Australia", "321 Elm St", 135790, 45678912,
"[email protected]", 104));
customerRepository.save(new Customer("David", "Wilson", "Germany", "654 Pine Rd", 987655, 36985214,
"[email protected]", 105));

productRepository.save(new Product("Electronics", "Smartphone",
"High-end smartphone with advanced features. ", 999.99, 100, 101));
productRepository.save(new Product("Clothing", "Men's T-Shirt",
"Comfortable cotton t-shirt for everyday wear. ", 29.99, 500, 102));
productRepository.save(new Product("Home & Kitchen", "Coffee Maker",
"Automatic coffee maker with programmable settings. ", 49.99, 50, 103));
productRepository.save(new Product("Beauty", "Perfume",
"Elegant fragrance with floral and citrus notes. ", 79.99, 200, 104));
productRepository.save(new Product("Books", "Science Fiction Novel",
"Bestselling sci-fi novel set in a dystopian future. ", 14.99, 300, 105));

reviewRepository.save(new Review("Electronics", "Great smartphone with excellent features. ",
5, 101, 201));
reviewRepository.save(new Review("Clothing", "Very comfortable t-shirt, fits perfectly. ",
4, 102, 202));
reviewRepository.save(new Review("Home & Kitchen", "Makes delicious coffee, easy to use. ",
4, 103, 203));
reviewRepository.save(new Review("Beauty", "Lovely fragrance, long-lasting. ",
5, 104, 204));
reviewRepository.save(new Review("Books", "Intriguing plot, couldn't put it down. ",
5, 105, 205));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sg.com.smartinventory;

import org.junit.jupiter.api.Test;

import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package sg.com.smartinventory;
package sg.com.smartinventory.controllers;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
Expand Down
Loading