Skip to content

Commit f5612de

Browse files
committed
Product CRUD is done successfully.
1 parent e0fcea1 commit f5612de

File tree

6 files changed

+219
-0
lines changed

6 files changed

+219
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.manir.springbootecommercerestapi.controller;
2+
3+
import com.manir.springbootecommercerestapi.dto.ProductDto;
4+
import com.manir.springbootecommercerestapi.service.ProductService;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.http.HttpStatus;
7+
import org.springframework.http.ResponseEntity;
8+
import org.springframework.web.bind.annotation.*;
9+
10+
import java.util.List;
11+
12+
@RestController
13+
@RequestMapping(value = "api/v1/products")
14+
public class ProductController {
15+
16+
@Autowired
17+
private ProductService productService;
18+
19+
//product create api
20+
@PostMapping("/createProduct")
21+
public ResponseEntity<ProductDto> createProduct(@RequestBody ProductDto productDto){
22+
ProductDto responseProduct = productService.createProduct(productDto);
23+
return new ResponseEntity<>(responseProduct, HttpStatus.CREATED);
24+
}
25+
26+
//get all products api
27+
@GetMapping("/getAllProduct")
28+
public List<ProductDto> getAllProduct(){
29+
List<ProductDto> responseProductDtoList = productService.getAllProduct();
30+
return responseProductDtoList;
31+
}
32+
33+
//get product by id api
34+
@GetMapping("/{productId}")
35+
public ResponseEntity<ProductDto> getProductById(@PathVariable Long productId){
36+
ProductDto responseProduct = productService.getProductById(productId);
37+
return ResponseEntity.ok(responseProduct);
38+
}
39+
40+
//update product api
41+
@PutMapping("/updateProduct/{productId}")
42+
public ResponseEntity<ProductDto> updateProduct(@RequestBody ProductDto productDto,
43+
@PathVariable Long productId){
44+
ProductDto responseProduct = productService.updateProduct(productDto, productId);
45+
return ResponseEntity.ok(responseProduct);
46+
}
47+
48+
//delete product api
49+
@DeleteMapping("/deleteProduct/{productId}")
50+
public ResponseEntity<String> deleteProduct(@PathVariable Long productId){
51+
productService.deleteProduct(productId);
52+
return new ResponseEntity<>("Product with id: "+ productId +" is deleted successfully:)", HttpStatus.OK);
53+
}
54+
55+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.manir.springbootecommercerestapi.dto;
2+
import lombok.Data;
3+
4+
@Data
5+
public class ProductDto {
6+
private Long id;
7+
private String title;
8+
private String keywords;
9+
private String description;
10+
private String detail;
11+
private double price;
12+
private Integer quantity;
13+
private String status;
14+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.manir.springbootecommercerestapi.repository;
2+
3+
import com.manir.springbootecommercerestapi.resource.Product;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
6+
public interface ProductRepository extends JpaRepository<Product, Long> {
7+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.manir.springbootecommercerestapi.resource;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
7+
import javax.persistence.*;
8+
9+
@AllArgsConstructor
10+
@NoArgsConstructor
11+
@Data
12+
@Entity
13+
@Table(name = "products")
14+
public class Product {
15+
@Id
16+
@GeneratedValue(strategy = GenerationType.IDENTITY)
17+
private Long id;
18+
@Column(name = "title")
19+
private String title;
20+
@Column(name = "keywords")
21+
private String keywords;
22+
@Column(name = "description")
23+
private String description;
24+
@Column(name = "detail")
25+
private String detail;
26+
@Column(name = "price")
27+
private double price;
28+
@Column(name = "quantity")
29+
private Integer quantity;
30+
@Column(name = "status")
31+
private String status;
32+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package com.manir.springbootecommercerestapi.service.Impl;
2+
3+
import com.manir.springbootecommercerestapi.dto.ProductDto;
4+
import com.manir.springbootecommercerestapi.exception.ResourceNotFoundException;
5+
import com.manir.springbootecommercerestapi.repository.ProductRepository;
6+
import com.manir.springbootecommercerestapi.resource.Product;
7+
import com.manir.springbootecommercerestapi.service.ProductService;
8+
import org.modelmapper.ModelMapper;
9+
import org.springframework.stereotype.Service;
10+
11+
import javax.annotation.Resource;
12+
import java.util.List;
13+
import java.util.stream.Collectors;
14+
15+
@Service
16+
public class ProductServiceImpl implements ProductService {
17+
18+
@Resource(name = "modelMapper")
19+
private ModelMapper modelMapper;
20+
@Resource(name = "productRepository")
21+
private ProductRepository productRepository;
22+
23+
@Override
24+
public ProductDto createProduct(ProductDto productDto) {
25+
//map to entity
26+
Product product = mapToEntity(productDto);
27+
Product createdProduct = productRepository.save(product);
28+
//map to dto
29+
ProductDto responseProduct = mapToDto(createdProduct);
30+
return responseProduct;
31+
}
32+
33+
@Override
34+
public List<ProductDto> getAllProduct() {
35+
36+
List<Product> products = productRepository.findAll();
37+
//map to dtos
38+
List<ProductDto> productDtoList = products.stream()
39+
.map(product -> mapToDto(product))
40+
.collect(Collectors.toList());
41+
42+
return productDtoList;
43+
}
44+
45+
@Override
46+
public ProductDto getProductById(Long productId) {
47+
48+
Product product = productRepository.findById(productId)
49+
.orElseThrow(
50+
()->new ResourceNotFoundException("Product", productId)
51+
);
52+
//map to dto
53+
ProductDto responseProduct = mapToDto(product);
54+
55+
return responseProduct;
56+
}
57+
58+
@Override
59+
public ProductDto updateProduct(ProductDto productDto, Long productId) {
60+
Product product = productRepository.findById(productId)
61+
.orElseThrow(
62+
()->new ResourceNotFoundException("Product", productId)
63+
);
64+
product.setTitle(productDto.getTitle());
65+
product.setDescription(productDto.getDescription());
66+
product.setKeywords(productDto.getKeywords());
67+
product.setPrice(productDto.getPrice());
68+
product.setDetail(productDto.getDetail());
69+
product.setQuantity(productDto.getQuantity());
70+
product.setStatus(productDto.getStatus());
71+
72+
Product updatedProduct = productRepository.save(product);
73+
//map to dto
74+
ProductDto responseProduct = mapToDto(updatedProduct);
75+
return responseProduct;
76+
}
77+
78+
@Override
79+
public void deleteProduct(Long productId) {
80+
Product product = productRepository.findById(productId)
81+
.orElseThrow(
82+
()->new ResourceNotFoundException("Product", productId)
83+
);
84+
productRepository.delete(product);
85+
}
86+
87+
//map to dto
88+
private ProductDto mapToDto(Product product){
89+
ProductDto productDto = modelMapper.map(product, ProductDto.class);
90+
return productDto;
91+
}
92+
//map to entity
93+
private Product mapToEntity(ProductDto productDto){
94+
Product product = modelMapper.map(productDto, Product.class);
95+
return product;
96+
}
97+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.manir.springbootecommercerestapi.service;
2+
3+
import com.manir.springbootecommercerestapi.dto.CategoryDto;
4+
import com.manir.springbootecommercerestapi.dto.ProductDto;
5+
6+
import java.util.List;
7+
8+
public interface ProductService {
9+
ProductDto createProduct(ProductDto productDto);
10+
List<ProductDto> getAllProduct();
11+
ProductDto getProductById(Long productId);
12+
ProductDto updateProduct(ProductDto productDto, Long productId);
13+
void deleteProduct(Long productId);
14+
}

0 commit comments

Comments
 (0)