Skip to content

Commit bb2ac8e

Browse files
committed
product crud updated
1 parent 15b53bf commit bb2ac8e

File tree

7 files changed

+53
-15
lines changed

7 files changed

+53
-15
lines changed

src/main/java/com/manir/springbootecommercerestapi/controller/ProductController.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ public ResponseEntity<ProductDto> createProduct(@RequestBody ProductDto productD
2323
return new ResponseEntity<>(responseProduct, HttpStatus.CREATED);
2424
}
2525

26+
//create product with category
27+
@PostMapping("/{categoryId}/saveProductByCategoryId")
28+
public ResponseEntity<ProductDto> saveProductByCategoryId(@PathVariable Long categoryId,
29+
@RequestBody ProductDto productDto){
30+
ProductDto responseProduct = productService.saveProductByCategoryId(categoryId, productDto);
31+
return new ResponseEntity<>(responseProduct, HttpStatus.CREATED);
32+
}
33+
34+
35+
2636
//get all products api
2737
@GetMapping("/getAllProduct")
2838
public List<ProductDto> getAllProduct(){
@@ -38,10 +48,11 @@ public ResponseEntity<ProductDto> getProductById(@PathVariable Long productId){
3848
}
3949

4050
//update product api
41-
@PutMapping("/updateProduct/{productId}")
42-
public ResponseEntity<ProductDto> updateProduct(@RequestBody ProductDto productDto,
51+
@PutMapping("/{categoryId}/updateProduct/{productId}")
52+
public ResponseEntity<ProductDto> updateProduct(@PathVariable Long categoryId,
53+
@RequestBody ProductDto productDto,
4354
@PathVariable Long productId){
44-
ProductDto responseProduct = productService.updateProduct(productDto, productId);
55+
ProductDto responseProduct = productService.updateProduct(categoryId, productDto, productId);
4556
return ResponseEntity.ok(responseProduct);
4657
}
4758

src/main/java/com/manir/springbootecommercerestapi/dto/CategoryDto.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.manir.springbootecommercerestapi.dto;
22

3+
import com.manir.springbootecommercerestapi.resource.Product;
34
import lombok.Data;
45

6+
import java.util.Set;
7+
58
@Data
69
public class CategoryDto {
710

@@ -10,4 +13,6 @@ public class CategoryDto {
1013
private String keywords;
1114
private String description;
1215
private String status;
16+
17+
//private Set<ProductDto> products;
1318
}

src/main/java/com/manir/springbootecommercerestapi/dto/ProductDto.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ public class ProductDto {
1111
private double price;
1212
private Integer quantity;
1313
private String status;
14+
15+
private CategoryDto category;
1416
}

src/main/java/com/manir/springbootecommercerestapi/resource/Category.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.manir.springbootecommercerestapi.resource;
22

3-
import lombok.AllArgsConstructor;
4-
import lombok.Data;
5-
import lombok.NoArgsConstructor;
3+
import lombok.*;
64

75
import javax.persistence.*;
86
import java.util.HashSet;
@@ -11,7 +9,8 @@
119

1210
@AllArgsConstructor
1311
@NoArgsConstructor
14-
@Data
12+
@Getter
13+
@Setter
1514
@Entity
1615
@Table(name = "categories")
1716
public class Category {

src/main/java/com/manir/springbootecommercerestapi/resource/Product.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package com.manir.springbootecommercerestapi.resource;
22

3-
import lombok.AllArgsConstructor;
4-
import lombok.Data;
5-
import lombok.NoArgsConstructor;
3+
import lombok.*;
64

75
import javax.persistence.*;
86

97
@AllArgsConstructor
108
@NoArgsConstructor
11-
@Data
9+
@Getter
10+
@Setter
1211
@Entity
1312
@Table(name = "products")
1413
public class Product {

src/main/java/com/manir/springbootecommercerestapi/service/Impl/ProductServiceImpl.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import com.manir.springbootecommercerestapi.dto.ProductDto;
44
import com.manir.springbootecommercerestapi.exception.ResourceNotFoundException;
5+
import com.manir.springbootecommercerestapi.repository.CategoryRepository;
56
import com.manir.springbootecommercerestapi.repository.ProductRepository;
7+
import com.manir.springbootecommercerestapi.resource.Category;
68
import com.manir.springbootecommercerestapi.resource.Product;
79
import com.manir.springbootecommercerestapi.service.ProductService;
810
import org.modelmapper.ModelMapper;
@@ -19,6 +21,8 @@ public class ProductServiceImpl implements ProductService {
1921
private ModelMapper modelMapper;
2022
@Resource(name = "productRepository")
2123
private ProductRepository productRepository;
24+
@Resource(name = "categoryRepository")
25+
private CategoryRepository categoryRepository;
2226

2327
@Override
2428
public ProductDto createProduct(ProductDto productDto) {
@@ -56,19 +60,23 @@ public ProductDto getProductById(Long productId) {
5660
}
5761

5862
@Override
59-
public ProductDto updateProduct(ProductDto productDto, Long productId) {
63+
public ProductDto updateProduct(Long categoryId, ProductDto productDto, Long productId) {
6064
Product product = productRepository.findById(productId)
6165
.orElseThrow(
6266
()->new ResourceNotFoundException("Product", productId)
6367
);
68+
Category category = categoryRepository.findById(categoryId)
69+
.orElseThrow(
70+
()->new ResourceNotFoundException("Category", categoryId)
71+
);
6472
product.setTitle(productDto.getTitle());
6573
product.setDescription(productDto.getDescription());
6674
product.setKeywords(productDto.getKeywords());
6775
product.setPrice(productDto.getPrice());
6876
product.setDetail(productDto.getDetail());
6977
product.setQuantity(productDto.getQuantity());
7078
product.setStatus(productDto.getStatus());
71-
79+
product.setCategory(category);
7280
Product updatedProduct = productRepository.save(product);
7381
//map to dto
7482
ProductDto responseProduct = mapToDto(updatedProduct);
@@ -84,6 +92,19 @@ public void deleteProduct(Long productId) {
8492
productRepository.delete(product);
8593
}
8694

95+
@Override
96+
public ProductDto saveProductByCategoryId(Long categoryId, ProductDto productDto) {
97+
Category category = categoryRepository.findById(categoryId)
98+
.orElseThrow( () -> new ResourceNotFoundException("Category", categoryId));
99+
Product product = mapToEntity(productDto);
100+
101+
product.setCategory(category);
102+
Product createdProduct = productRepository.save(product);
103+
ProductDto responseProduct = mapToDto(createdProduct);
104+
105+
return responseProduct;
106+
}
107+
87108
//map to dto
88109
private ProductDto mapToDto(Product product){
89110
ProductDto productDto = modelMapper.map(product, ProductDto.class);
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.manir.springbootecommercerestapi.service;
22

3-
import com.manir.springbootecommercerestapi.dto.CategoryDto;
43
import com.manir.springbootecommercerestapi.dto.ProductDto;
54

65
import java.util.List;
@@ -9,6 +8,8 @@ public interface ProductService {
98
ProductDto createProduct(ProductDto productDto);
109
List<ProductDto> getAllProduct();
1110
ProductDto getProductById(Long productId);
12-
ProductDto updateProduct(ProductDto productDto, Long productId);
11+
ProductDto updateProduct(Long categoryId, ProductDto productDto, Long productId);
1312
void deleteProduct(Long productId);
13+
14+
ProductDto saveProductByCategoryId(Long categoryId, ProductDto productDto);
1415
}

0 commit comments

Comments
 (0)