Commit df72a694 by Thaswin Muralikaran

updated readme file

parent 4ec171d2
...@@ -107,7 +107,8 @@ my-product-management-svc ...@@ -107,7 +107,8 @@ my-product-management-svc
- In the application.properties file under the resources folder add the database configuration. - In the application.properties file under the resources folder add the database configuration.
```java properties ```java
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/productmngm?createDatabaseIfNotExist=true&useSSL=false&serverTimezone=UTC spring.datasource.url=jdbc:mysql://127.0.0.1:3306/productmngm?createDatabaseIfNotExist=true&useSSL=false&serverTimezone=UTC
spring.datasource.username=root spring.datasource.username=root
spring.datasource.password=01234 spring.datasource.password=01234
...@@ -148,16 +149,19 @@ import java.util.List; ...@@ -148,16 +149,19 @@ import java.util.List;
##### ii. Adding annotations ##### ii. Adding annotations
```java ```
@Getter @Getter
@Setter @Setter
@Entity @Entity
@Table(name = "category") @Table(name = "category")
``` ```
##### iii. Defining class and fields ##### iii. Defining class and fields
```java ```java
public class Category { public class Category {
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
...@@ -177,6 +181,7 @@ public class Category { ...@@ -177,6 +181,7 @@ public class Category {
private LocalDateTime updatedAt; private LocalDateTime updatedAt;
} }
``` ```
#### Product Entity: #### Product Entity:
...@@ -184,6 +189,7 @@ public class Category { ...@@ -184,6 +189,7 @@ public class Category {
##### i. Importing packages and classes ##### i. Importing packages and classes
```java ```java
package com.ctsb.my_product_management_svc.product; package com.ctsb.my_product_management_svc.product;
import com.ctsb.my_product_management_svc.category.Category; import com.ctsb.my_product_management_svc.category.Category;
...@@ -193,20 +199,24 @@ import lombok.Setter; ...@@ -193,20 +199,24 @@ import lombok.Setter;
import org.hibernate.annotations.CreationTimestamp; import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp; import org.hibernate.annotations.UpdateTimestamp;
import java.time.LocalDateTime; import java.time.LocalDateTime;
``` ```
##### ii. Adding annotations ##### ii. Adding annotations
```java ```
@Getter @Getter
@Setter @Setter
@Entity @Entity
@Table(name = "product") @Table(name = "product")
``` ```
##### iii. Defining class and fields ##### iii. Defining class and fields
```java ```java
public class Product { public class Product {
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
...@@ -227,11 +237,13 @@ public class Product { ...@@ -227,11 +237,13 @@ public class Product {
@UpdateTimestamp @UpdateTimestamp
private LocalDateTime updatedAt; private LocalDateTime updatedAt;
} }
``` ```
##### iv. Creating Enum for product entity ##### iv. Creating Enum for product entity
```Java ```java
package com.ctsb.my_product_management_svc.product; package com.ctsb.my_product_management_svc.product;
public enum Status { public enum Status {
...@@ -239,11 +251,13 @@ public enum Status { ...@@ -239,11 +251,13 @@ public enum Status {
OUT_OF_STOCK, OUT_OF_STOCK,
DISCONTINUED DISCONTINUED
} }
``` ```
##### v. Declaring relationship between category and product ##### v. Declaring relationship between category and product
```java ```java
//Declare one-to-many relationship to product in category //Declare one-to-many relationship to product in category
@OneToMany(mappedBy = "category", orphanRemoval = true) @OneToMany(mappedBy = "category", orphanRemoval = true)
@JsonIgnore @JsonIgnore
...@@ -253,6 +267,7 @@ public enum Status { ...@@ -253,6 +267,7 @@ public enum Status {
@ManyToOne @ManyToOne
@JoinColumn(name = "category_id", nullable = false) @JoinColumn(name = "category_id", nullable = false)
private Category category; private Category category;
``` ```
<details> <details>
...@@ -361,24 +376,28 @@ public class Product { ...@@ -361,24 +376,28 @@ public class Product {
#### i. Creating CategoryRepository #### i. Creating CategoryRepository
```Java ```java
package com.ctsb.my_product_management_svc.category; package com.ctsb.my_product_management_svc.category;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
public interface CategoryRepository extends JpaRepository<Category, Long> { public interface CategoryRepository extends JpaRepository<Category, Long> {
} }
``` ```
#### ii. Creating ProductRepository #### ii. Creating ProductRepository
```Java ```java
package com.ctsb.my_product_management_svc.product; package com.ctsb.my_product_management_svc.product;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
public interface ProductRepository extends JpaRepository<Product, Long> { public interface ProductRepository extends JpaRepository<Product, Long> {
} }
``` ```
### 6. Creating Data Transfer Object(DTO) (10 minutes) ### 6. Creating Data Transfer Object(DTO) (10 minutes)
...@@ -390,7 +409,8 @@ public interface ProductRepository extends JpaRepository<Product, Long> { ...@@ -390,7 +409,8 @@ public interface ProductRepository extends JpaRepository<Product, Long> {
#### i. Creating CategoryDto #### i. Creating CategoryDto
```Java ```java
package com.ctsb.my_product_management_svc.category; package com.ctsb.my_product_management_svc.category;
public record CategoryDto( public record CategoryDto(
...@@ -399,11 +419,13 @@ public record CategoryDto( ...@@ -399,11 +419,13 @@ public record CategoryDto(
Boolean active Boolean active
) { ) {
} }
``` ```
#### ii. Creating ProductDto #### ii. Creating ProductDto
```Java ```java
package com.ctsb.my_product_management_svc.product; package com.ctsb.my_product_management_svc.product;
public record ProductDto( public record ProductDto(
...@@ -429,7 +451,8 @@ public record ProductDto( ...@@ -429,7 +451,8 @@ public record ProductDto(
##### i. Importing necessary packages and classes ##### i. Importing necessary packages and classes
``` ```java
package com.ctsb.my_product_management_svc.category; package com.ctsb.my_product_management_svc.category;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
...@@ -439,28 +462,34 @@ import org.springframework.web.server.ResponseStatusException; ...@@ -439,28 +462,34 @@ import org.springframework.web.server.ResponseStatusException;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.List; import java.util.List;
``` ```
##### ii. Adding necessary annotations ##### ii. Adding necessary annotations
``` ```
@Service @Service
@AllArgsConstructor // Lombok annotation for constructor-based dependency injection @AllArgsConstructor // Lombok annotation for constructor-based dependency injection
``` ```
##### iii. Calling repository and declare error message ##### iii. Calling repository and declare error message
``` ```java
public class CategoryService { public class CategoryService {
private final CategoryRepository categoryRepository; // Handles database operations private final CategoryRepository categoryRepository; // Handles database operations
private static final String NOT_FOUND = "Category with %s id is not found!"; // Error message template private static final String NOT_FOUND = "Category with %s id is not found!"; // Error message template
} }
``` ```
##### iv. Creating mapper method for create operation ##### iv. Creating mapper method for create operation
``` ```java
// Maps DTO to Entity // Maps DTO to Entity
private Category mapper(CategoryDto categoryDto){ private Category mapper(CategoryDto categoryDto){
var category = new Category(); var category = new Category();
...@@ -469,11 +498,13 @@ public class CategoryService { ...@@ -469,11 +498,13 @@ public class CategoryService {
category.setActive(categoryDto.active()); category.setActive(categoryDto.active());
return category; return category;
} }
``` ```
##### v. Creating logics for CRUD operations ##### v. Creating logics for CRUD operations
``` ```java
// Get all categories // Get all categories
public List <Category> findAll(){ public List <Category> findAll(){
return categoryRepository.findAll(); return categoryRepository.findAll();
...@@ -526,13 +557,15 @@ public class CategoryService { ...@@ -526,13 +557,15 @@ public class CategoryService {
); );
categoryRepository.deleteById(id); categoryRepository.deleteById(id);
} }
``` ```
<details> <details>
<summary> Full code of Category Service </summary> <summary> Full code of Category Service </summary>
```Java ```java
package com.ctsb.my_product_management_svc.category; package com.ctsb.my_product_management_svc.category;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
...@@ -607,7 +640,8 @@ public class CategoryService { ...@@ -607,7 +640,8 @@ public class CategoryService {
##### i. Importing necessary packages and classes ##### i. Importing necessary packages and classes
``` ```java
package com.ctsb.my_product_management_svc.product; package com.ctsb.my_product_management_svc.product;
import com.ctsb.my_product_management_svc.category.Category; import com.ctsb.my_product_management_svc.category.Category;
...@@ -618,29 +652,35 @@ import org.springframework.stereotype.Service; ...@@ -618,29 +652,35 @@ import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException; import org.springframework.web.server.ResponseStatusException;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.List; import java.util.List;
``` ```
##### ii. Adding necessary annotations ##### ii. Adding necessary annotations
``` ```
@Service @Service
@AllArgsConstructor @AllArgsConstructor
``` ```
##### iii. Calling repository and declare error message ##### iii. Calling repository and declare error message
``` ```java
public class ProductService { public class ProductService {
private final ProductRepository productRepository; // Handles product database operations private final ProductRepository productRepository; // Handles product database operations
private final CategoryRepository categoryRepository; // Handles category lookups private final CategoryRepository categoryRepository; // Handles category lookups
private static final String NOT_FOUND = "Product with %s id is not found!"; // Error message template private static final String NOT_FOUND = "Product with %s id is not found!"; // Error message template
} }
``` ```
##### iv. Creating mapper method for create operation ##### iv. Creating mapper method for create operation
``` ```java
// Maps DTO to Product entity (without category) // Maps DTO to Product entity (without category)
private Product mapper(ProductDto productDto){ private Product mapper(ProductDto productDto){
var product = new Product(); var product = new Product();
...@@ -651,11 +691,13 @@ public class ProductService { ...@@ -651,11 +691,13 @@ public class ProductService {
product.setStatus(productDto.status()); product.setStatus(productDto.status());
return product; return product;
} }
``` ```
##### v. Creating logics for CRUD operations ##### v. Creating logics for CRUD operations
``` ```java
// Get all products // Get all products
public List<Product> findAll(){ public List<Product> findAll(){
return productRepository.findAll(); return productRepository.findAll();
...@@ -730,13 +772,15 @@ public class ProductService { ...@@ -730,13 +772,15 @@ public class ProductService {
); );
productRepository.deleteById(id); productRepository.deleteById(id);
} }
``` ```
<details> <details>
<summary> Full code of Product Service </summary> <summary> Full code of Product Service </summary>
```Java ```java
package com.ctsb.my_product_management_svc.product; package com.ctsb.my_product_management_svc.product;
import com.ctsb.my_product_management_svc.category.Category; import com.ctsb.my_product_management_svc.category.Category;
...@@ -842,35 +886,42 @@ public class ProductService { ...@@ -842,35 +886,42 @@ public class ProductService {
##### i. Importing necessary packages and classes ##### i. Importing necessary packages and classes
``` ```java
package com.ctsb.my_product_management_svc.category; package com.ctsb.my_product_management_svc.category;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.List; import java.util.List;
``` ```
##### ii. Adding necessary annotations ##### ii. Adding necessary annotations
``` ```
@RestController @RestController
@RequestMapping("/categories") // Base path for all endpoints in this controller @RequestMapping("/categories") // Base path for all endpoints in this controller
@AllArgsConstructor @AllArgsConstructor
``` ```
##### iii. Calling category service class ##### iii. Calling category service class
``` ```java
public class CategoryController { public class CategoryController {
private final CategoryService categoryService; // Handles business logic private final CategoryService categoryService; // Handles business logic
} }
``` ```
##### iv. Creating endpoints for logic in service layer ##### iv. Creating endpoints for logic in service layer
``` ```java
// GET endpoint to retrieve all categories // GET endpoint to retrieve all categories
@GetMapping("/find") @GetMapping("/find")
public ResponseEntity<List<Category>> findAll(){ public ResponseEntity<List<Category>> findAll(){
...@@ -905,13 +956,15 @@ public class CategoryController { ...@@ -905,13 +956,15 @@ public class CategoryController {
categoryService.delete(id); categoryService.delete(id);
return ResponseEntity.ok("Category deleted!"); return ResponseEntity.ok("Category deleted!");
} }
``` ```
<details> <details>
<summary> Full code for Category Controller </summary> <summary> Full code for Category Controller </summary>
```Java ```java
package com.ctsb.my_product_management_svc.category; package com.ctsb.my_product_management_svc.category;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
...@@ -965,35 +1018,42 @@ public class CategoryController { ...@@ -965,35 +1018,42 @@ public class CategoryController {
##### i. Importing necessary packages and classes ##### i. Importing necessary packages and classes
``` ```java
package com.ctsb.my_product_management_svc.product; package com.ctsb.my_product_management_svc.product;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.List; import java.util.List;
``` ```
##### ii. Adding necessary annotations ##### ii. Adding necessary annotations
``` ```
@RestController @RestController
@RequestMapping("/products") @RequestMapping("/products")
@AllArgsConstructor @AllArgsConstructor
``` ```
##### iii. Calling product service class ##### iii. Calling product service class
``` ```java
public class ProductController { public class ProductController {
private final ProductService productService; // Handles business logic private final ProductService productService; // Handles business logic
} }
``` ```
##### iv. Creating endpoints for logic in service layer ##### iv. Creating endpoints for logic in service layer
``` ```java
// GET endpoint to retrieve all products // GET endpoint to retrieve all products
@GetMapping("/find") @GetMapping("/find")
public ResponseEntity<List<Product>> findAll(){ public ResponseEntity<List<Product>> findAll(){
...@@ -1031,13 +1091,14 @@ public class ProductController { ...@@ -1031,13 +1091,14 @@ public class ProductController {
productService.delete(id); productService.delete(id);
return ResponseEntity.ok("Product deleted!"); return ResponseEntity.ok("Product deleted!");
} }
``` ```
<details> <details>
<summary> Full code for Product Controller </summary> <summary> Full code for Product Controller </summary>
```Java ```java
package com.ctsb.my_product_management_svc.product; package com.ctsb.my_product_management_svc.product;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
...@@ -1102,7 +1163,8 @@ public class ProductController { ...@@ -1102,7 +1163,8 @@ public class ProductController {
- Add OpenApi dependency in build.gradle file. - Add OpenApi dependency in build.gradle file.
- Build the project file. - Build the project file.
``` ```java
dependencies { dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-web'
...@@ -1113,13 +1175,15 @@ dependencies { ...@@ -1113,13 +1175,15 @@ dependencies {
testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher' testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
} }
``` ```
#### Setting up OpenApiConfig file #### Setting up OpenApiConfig file
##### i. Packages and classes ##### i. Packages and classes
``` ```java
package com.ctsb.my_product_management_svc.config; package com.ctsb.my_product_management_svc.config;
import io.swagger.v3.oas.annotations.OpenAPIDefinition; import io.swagger.v3.oas.annotations.OpenAPIDefinition;
...@@ -1129,11 +1193,13 @@ import io.swagger.v3.oas.annotations.info.Info; ...@@ -1129,11 +1193,13 @@ import io.swagger.v3.oas.annotations.info.Info;
import io.swagger.v3.oas.annotations.info.License; import io.swagger.v3.oas.annotations.info.License;
import io.swagger.v3.oas.annotations.security.SecurityRequirement; import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.security.SecurityScheme; import io.swagger.v3.oas.annotations.security.SecurityScheme;
``` ```
##### ii. OpenApi Definition ##### ii. OpenApi Definition
``` ```java
@OpenAPIDefinition( @OpenAPIDefinition(
info = @Info( info = @Info(
// Contact information for support // Contact information for support
...@@ -1159,11 +1225,13 @@ import io.swagger.v3.oas.annotations.security.SecurityScheme; ...@@ -1159,11 +1225,13 @@ import io.swagger.v3.oas.annotations.security.SecurityScheme;
) )
} }
) )
``` ```
##### iii. Security Configuration ##### iii. Security Configuration
``` ```java
@SecurityScheme( @SecurityScheme(
name = "bearerAuth", // Name referenced in SecurityRequirement name = "bearerAuth", // Name referenced in SecurityRequirement
description = "Input JWT token for authentication", description = "Input JWT token for authentication",
...@@ -1171,22 +1239,26 @@ import io.swagger.v3.oas.annotations.security.SecurityScheme; ...@@ -1171,22 +1239,26 @@ import io.swagger.v3.oas.annotations.security.SecurityScheme;
scheme = "bearer", // Bearer token scheme scheme = "bearer", // Bearer token scheme
bearerFormat = "JWT" // Specifies JWT token format bearerFormat = "JWT" // Specifies JWT token format
) )
``` ```
##### iii. OpenApi config class ##### iii. OpenApi config class
``` ```java
public class OpenApiConfig { public class OpenApiConfig {
// Configuration-only class - no methods needed // Configuration-only class - no methods needed
// Spring will automatically pick up these annotations // Spring will automatically pick up these annotations
} }
``` ```
<details> <details>
<summary> Full code of OpenApi configuration </summary> <summary> Full code of OpenApi configuration </summary>
```Java ```java
package com.ctsb.my_product_management_svc.config; package com.ctsb.my_product_management_svc.config;
import io.swagger.v3.oas.annotations.OpenAPIDefinition; import io.swagger.v3.oas.annotations.OpenAPIDefinition;
...@@ -1235,7 +1307,8 @@ public class OpenApiConfig { ...@@ -1235,7 +1307,8 @@ public class OpenApiConfig {
#### Setting up SwaggerConfig file #### Setting up SwaggerConfig file
```Java ```java
package com.ctsb.my_product_management_svc.config; package com.ctsb.my_product_management_svc.config;
import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.OpenAPI;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment