Commit df72a694 by Thaswin Muralikaran

updated readme file

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