Skip to content
Open
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
Binary file added CreateAProduct.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
139 changes: 139 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
# Ecommerce Store Spring Boot

## About the Application
- This application consists of products stored in database, and CRUD can be implemented.
- This application runs on port 4444.
- It uses MySql as Database.
- It uses Thymeleaf Template Engine for UI.
- It uses UUID format.
- In this application both Controller and RestController is implemented.

## How to Access
### This app contains 4 Controllers
- ProductController (Rest Controller)
- Create Product
- Returns GenericProductDto
-
> POST
> localhost:4444/products/
- Get all Products
- Returns List of GenericProductDto
-
> GET
> localhost:4444/products/
- Get Product By Id
- Returns a GenericProductDto
-
> GET
> localhost:4444/products/{id}
- Update Product
- Returns GenericProductDto
-
> PUT
> localhost:4444/products/{id}
- Delete Product
- Returns ResponseEntity of GenericProductDto
-
> DELETE
> localhost:4444/products/{id}
- CategoryController (Rest Controller)
- Get All Products in Category
- Returns List of GenericProductDto
-
> GET
> localhost:4444/categories/{id}
- Get All Categories
- Returns List of CategoryDto
-
> GET
> localhost:4444/categories/{id}

- UIProductDBController (Controller) (Browser)
- Get all Products
- Returns String of Products
-
> GET
> localhost:4444/productservice/get-all-products
- Get Single Product
- Returns Product
-
> GET
> localhost:4444/productservice/get-single-product
> localhost:4444/productservice/get-all-products
- Create Product
- Redirects to Get all Products
-
> POST
> localhost:4444/productservice/create-a-product
- Update Product
- Redirects to Get all Products
-
> PUT
> localhost:4444/productservice/update-a-product
> localhost:4444/productservice/create-a-product
- Delete Product
- Redirects to Get all Products
-
> DELETE
> localhost:4444/productservice/delete-a-product




## Screenshots from Browser (Controller)
===========================
Get all products.
![GetAllProducts](https://github.com/jsbiresh/productservice-naman/assets/133354225/d662c475-94e6-43f8-8895-af640bffdb92)

Get single product.
![GetSingleProduct](https://github.com/jsbiresh/productservice-naman/assets/133354225/fb8e955d-99c9-49f7-8344-39d81f3b4e5d)

Get all Categories
![GetAllCategories](https://github.com/jsbiresh/productservice-naman/assets/133354225/2f289454-983d-4c10-bc60-bfdd3dbcd49b)

Get in Category
![GetInCategory](https://github.com/jsbiresh/productservice-naman/assets/133354225/08c45ba3-f9fe-4b58-ae21-22a01eefcd05)

Create a product
![CreateAProduct](https://github.com/jsbiresh/productservice-naman/assets/133354225/43522551-87ba-405a-8749-72dea172e409)

Update product
![UpdateAProduct](https://github.com/jsbiresh/productservice-naman/assets/133354225/dae550d7-eded-4c3a-b405-ab80103f873a)

Delete product
![DeleteAProduct](https://github.com/jsbiresh/productservice-naman/assets/133354225/279c6631-0a8a-4a80-ac08-31c9162945f1)


## Screenshots from Postman (RestController)
===========================
Get all products.
![GetAllProducts](https://github.com/jsbiresh/productservice-naman/assets/133354225/fbcdd28a-2b3d-459e-be0d-fd054ce17cf3)

Get single product.
![GetSingleProduct](https://github.com/jsbiresh/productservice-naman/assets/133354225/156588c0-94c5-449d-87c9-34464ec879ae)

Get all Categories
![GetAllCategories](https://github.com/jsbiresh/productservice-naman/assets/133354225/cc3851fd-ec62-4613-bceb-b81b8d241d3d)

Get in Category
![GetInCategory](https://github.com/jsbiresh/productservice-naman/assets/133354225/3a3a6475-91a0-481b-aeb5-b3d8477f28c2)

Create a product
![CreateAProduct](https://github.com/jsbiresh/productservice-naman/assets/133354225/afaa765b-75b6-457a-a876-f75e5fa1cbed)

Update product
![UpdateAProduct](https://github.com/jsbiresh/productservice-naman/assets/133354225/5c7f12a9-3967-4363-a541-f879ec78e121)

Delete product
![DeleteAProduct](https://github.com/jsbiresh/productservice-naman/assets/133354225/8a09111b-db85-4ac5-bf46-15be22c37742)











6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@
<artifactId>flyway-mysql</artifactId>
<version>9.22.2</version>
</dependency>
<!-- THYMELEAF DEPENDENCY -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>


</dependencies>

Expand Down
134 changes: 32 additions & 102 deletions src/main/java/dev/naman/productservice/ProductserviceApplication.java
Original file line number Diff line number Diff line change
@@ -1,119 +1,49 @@
package dev.naman.productservice;

import dev.naman.productservice.inheritancedemo.joinedtable.Mentor;
import dev.naman.productservice.inheritancedemo.joinedtable.MentorRepository;
import dev.naman.productservice.inheritancedemo.joinedtable.User;
import dev.naman.productservice.inheritancedemo.joinedtable.UserRepository;
import dev.naman.productservice.models.Category;
import dev.naman.productservice.models.Price;
import dev.naman.productservice.models.Product;
import dev.naman.productservice.repositories.CategoryRepository;
import dev.naman.productservice.repositories.PriceRepository;
import dev.naman.productservice.repositories.ProductRepository;
//import dev.naman.productservice.inheritancedemo.joinedtable.Mentor;
//import dev.naman.productservice.inheritancedemo.joinedtable.MentorRepository;
//import dev.naman.productservice.inheritancedemo.joinedtable.User;
//import dev.naman.productservice.inheritancedemo.joinedtable.UserRepository;
//import dev.naman.productservice.models.Category;
//import dev.naman.productservice.models.Price;
//import dev.naman.productservice.models.Product;
//import dev.naman.productservice.repositories.CategoryRepository;
//import dev.naman.productservice.repositories.PriceRepository;
//import dev.naman.productservice.repositories.ProductRepository;
//import jakarta.transaction.Transactional;
//import org.hibernate.Hibernate;
//import org.springframework.beans.factory.annotation.Qualifier;

import jakarta.transaction.Transactional;
import org.hibernate.Hibernate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.List;
import java.util.Optional;
import java.util.UUID;


@SpringBootApplication
public class ProductserviceApplication {// implements CommandLineRunner {
public class ProductserviceApplication implements CommandLineRunner {

// private MentorRepository mentorRepository;
//
//
// private UserRepository userRepository;
// private final ProductRepository productRepository;
// private final CategoryRepository categoryRepository;
// private final PriceRepository priceRepository;
//
// public ProductserviceApplication(@Qualifier("jt_mr") MentorRepository mentorRepository,
// @Qualifier("jt_ur") UserRepository userRepository,
// ProductRepository productRepository,
// CategoryRepository categoryRepository,
// PriceRepository priceRepository) {
// this.mentorRepository = mentorRepository;
// this.userRepository = userRepository;
// this.productRepository = productRepository;
// this.categoryRepository = categoryRepository;
// this.priceRepository = priceRepository;
// @Autowired
// private static Environment environment;
// public ProductserviceApplication(Environment environment) {
// this.environment = environment;
// }

// private static String serverPort = environment.getProperty("server.port");

public static void main(String[] args) {
SpringApplication.run(ProductserviceApplication.class, args);
// Retrieve server.port property
// System.out.println("SERVER RUNNING ON PORT : "+ serverPort);
System.out.println("******************************");
System.out.println("SERVER RUNNING ON PORT: "+ 4444);
System.out.println("******************************");
}

// @Transactional()
// @Override
// public void run(String... args) throws Exception {
//// Mentor mentor = new Mentor();
//// mentor.setName("Naman");
//// mentor.setEmail("Naman@scaler.com");
//// mentor.setAverageRating(4.65);
//// mentorRepository.save(mentor);
////
//// User user = new User();
//// user.setName("Sarath");
//// user.setEmail("sarathcool@yopmail.com");
//// userRepository.save(user);
////
//// List<User> users = userRepository.findAll();
//// for (User user1: users) {
//// System.out.println(user1);
//// }
//
// Category category = new Category();
// category.setName("Apple Devices");
//// Category savedCategory = categoryRepository.save(category);
//
// Price price = new Price("Rupee", 10);
//// Price savedPrice = priceRepository.save(price);
//
// Product product = new Product();
// product.setTitle("iPhone 15 Pro");
// product.setDescription("The best iPhone Ever");
// product.setCategory(category);
// product.setPrice(price);
//
// productRepository.save(product);
//
// productRepository.deleteById(UUID.fromString("95672ed6-3127-4248-ae33-97a261c0a6f4"));
//
// System.out.println(productRepository.countAllByPrice_Currency("Rupee"));
// List<Product> products = productRepository.findAllByPrice_Currency("Rupee");
//// Category category1 = categoryRepository.findById(UUID.fromString("5e6f679e-f326-44ae-b220-544b3822ab00")).get();
//// System.out.println("Category name is: " + category1.getName());
//// System.out.println("Printing all products in the category");
////// Thread.sleep(1000);
////
//// System.out.println(category1.getProducts().size());
//// category1.getProducts().forEach(
//// product1 -> System.out.println(product1.getTitle())
//// );
////
//// for (Product product1: category1.getProducts()) {
//// try {
//// System.out.println(product1.getTitle());
//// } catch (Exception e) {
//// System.out.println(e.getMessage());
//// }
//// }
//
// List<Product> products1 = productRepository.findAllByTitle("iPhone 15 Pro");
//
// System.out.println("Fetching category b8f1f459-f9e9-4d3d-b115-f1f5267bd54f");
// Thread.sleep(1000);
// Category category1 = categoryRepository.findById(UUID.fromString("b8f1f459-f9e9-4d3d-b115-f1f5267bd54f")).get();
//// Category category1 = category1Optional.get();
//
// System.out.println("Fetching products for category");
// Thread.sleep(1000);
// List<Product> products11 = category1.getProducts();
// }

@Transactional
@Override
public void run(String... args) throws Exception {
// System.out.println("From command line runner");
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package dev.naman.productservice.controllers;

import dev.naman.productservice.dtos.CategoryDto;
import dev.naman.productservice.dtos.GenericProductDto;
import dev.naman.productservice.dtos.GetProductTitlesRequestDto;
import dev.naman.productservice.dtos.ProductDto;
import dev.naman.productservice.exceptions.NotFoundException;
import dev.naman.productservice.models.Category;
import dev.naman.productservice.models.Product;
import dev.naman.productservice.services.CategoryService;
Expand All @@ -21,29 +24,40 @@ public CategoryController(CategoryService categoryService) {
}

@GetMapping("/{uuid}")
public List<ProductDto> getCategory(@PathVariable("uuid") String uuid) {
List<Product> products = categoryService.getCategory(uuid).getProducts();
public List<GenericProductDto> getAllProductsFromACategory(@PathVariable("uuid") String uuid) throws NotFoundException {

List<Product> products = categoryService.getAllProductsFromACategory(uuid).getProducts();
List<GenericProductDto> productDtos = new ArrayList<>();

List<ProductDto> productDtos = new ArrayList<>();

for (Product product: products) {
ProductDto productDto = new ProductDto();
GenericProductDto productDto = new GenericProductDto();
productDto.setDescription(product.getDescription());
productDto.setUuid(product.getUuid());
productDto.setTitle(product.getTitle());
productDto.setImage(product.getImage());
productDto.setPrice(product.getPrice());
productDto.setPrice(product.getPrice().getPrice());
productDto.setCurrency(product.getCurrency());
productDto.setCategory(product.getCategory().getName());
productDtos.add(productDto);
// productDto.se
}

return productDtos;
}

@GetMapping("/titles/")
public List<String> getProductTitles(@RequestBody GetProductTitlesRequestDto requestDto) {
@GetMapping("/")
public List<CategoryDto> getAllCategories() {
return categoryService.getAllCategories();
}


List<String> uuids = requestDto.getUuids();

return categoryService.getProductTitles(uuids);
}
// @GetMapping("/titles/")
// public List<String> getProductTitles(@RequestBody GetProductTitlesRequestDto requestDto) {
//
// System.out.println("getProductTitles() called");
// List<String> uuids = requestDto.getUuids();
//
// return categoryService.getProductTitles(uuids);
// }

}
Loading