-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProductServiceImpl.java
More file actions
143 lines (134 loc) · 6.28 KB
/
ProductServiceImpl.java
File metadata and controls
143 lines (134 loc) · 6.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package com.ecommerece.productservice.services.impl;
import com.cloudinary.Cloudinary;
import com.cloudinary.utils.ObjectUtils;
import com.ecommerece.productservice.dtos.CommentDto;
import com.ecommerece.productservice.dtos.PostProductDto;
import com.ecommerece.productservice.dtos.ProductDto;
import com.ecommerece.productservice.entities.ImageModel;
import com.ecommerece.productservice.entities.Product;
import com.ecommerece.productservice.exceptions.ResourceNotFoundException;
import com.ecommerece.productservice.external.services.CommentService;
import com.ecommerece.productservice.repositories.ProductRepository;
import com.ecommerece.productservice.services.ImageService;
import com.ecommerece.productservice.services.ProductService;
import org.modelmapper.ModelMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.*;
@Service
public class ProductServiceImpl implements ProductService {
private final ProductRepository productRepository;
private final ModelMapper modelMapper;
private final CommentService commentService;
private final ImageService imageService;
private final Cloudinary cloudinary;
public ProductServiceImpl(ProductRepository productRepository, ModelMapper modelMapper, CommentService commentService, ImageService imageService, Cloudinary cloudinary) {
this.productRepository = productRepository;
this.modelMapper = modelMapper;
this.commentService = commentService;
this.imageService = imageService;
this.cloudinary = cloudinary;
}
@Override
public ProductDto saveProduct(String userId, PostProductDto postProductDto) {
ProductDto product = modelMapper.map(postProductDto, ProductDto.class);
//generate unique userid
String randomUserId = UUID.randomUUID().toString();
product.setId(randomUserId);
product.setUserId(userId);
try {
List<ImageModel> images = uploadImage(postProductDto.getImageFiles(), product.getId());
product.setImages(images);
} catch (Exception e) {
throw new RuntimeException(e);
}
return getProductDtofromProduct(productRepository.save(getProductfromProductDto(product)));
}
@Override
public ProductDto updateProduct(String productId, PostProductDto postProductDto) {
ProductDto product = modelMapper.map(postProductDto, ProductDto.class);
product.setId(productId);
ProductDto product1 = getProduct(productId);
if (!postProductDto.getImageFiles()[0].isEmpty())
{
try {
List<ImageModel> images = uploadImage(postProductDto.getImageFiles(), product.getId());
product.setImages(images);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
else {
product.setImages(product1.getImages());
}
return getProductDtofromProduct(productRepository.save(getProductfromProductDto(product)));
}
@Override
public List<ProductDto> getAllProducts() {
//implement RATING SERVICE CALL: USING REST TEMPLATE
List<ProductDto> productDtos = productRepository.findAll().stream().map(this::getProductDtofromProduct).toList();
return productDtos.stream().map( productDto -> {
List<CommentDto> commentDtos = commentService.getProductComments(productDto.getId()).getBody();
productDto.setComments(commentDtos);
return productDto;
}).toList();
// return productDtos;
}
@Override
public List<ProductDto> getUserProducts(String userId) {
List<ProductDto> productDtos = productRepository.findAllByUserId(userId).stream().map(this::getProductDtofromProduct).toList();
// return productDtos.stream().map( productDto -> {
// List<CommentDto> commentDtos = commentService.getProductComments(productDto.getId()).getBody();
// productDto.setComments(commentDtos);
// return productDto;
// }).toList();
return productDtos;
}
@Override
public ProductDto getProduct(String productId) {
Product product = productRepository.findById(productId).orElseThrow(() -> new ResourceNotFoundException("Product with given id is not found on server !! : " + productId));
List<CommentDto> comments = commentService.getProductComments(product.getId()).getBody();
ProductDto productDto = getProductDtofromProduct(product);
productDto.setComments(comments);
return productDto;
}
@Override
public void deleteProduct(String productId) {
productRepository.deleteById(productId);
}
@Override
public ProductDto updateProductFields(Map<String, Object> fields, String id) {
ProductDto currentProduct = getProduct(id);
fields.forEach((key,value) -> {
Field field = ReflectionUtils.findField(ProductDto.class, key);
field.setAccessible(true);
ReflectionUtils.setField(field,currentProduct,value);
});
return getProductDtofromProduct(productRepository.save(getProductfromProductDto(currentProduct)));
}
private List<ImageModel> uploadImage(MultipartFile[] multipartFiles, String productId) throws IOException {
// String imagePath = "/home/dev/Downloads/AyubAhmad.png";
List<ImageModel> imageModels = new ArrayList<ImageModel>();
for (MultipartFile file : multipartFiles) {
Map uploadResult = cloudinary.uploader().upload(file.getBytes(), ObjectUtils.emptyMap());
ImageModel imageModel = new ImageModel(
uploadResult.get("secure_url").toString(),
productId
);
imageModels.add(imageModel);
}
return imageModels;
}
private ProductDto getProductDtofromProduct(Product product){
return modelMapper.map(product, ProductDto.class);
}
private Product getProductfromProductDto(ProductDto product){
return modelMapper.map(product, Product.class);
}
// private Product getProductfromPostProductDto(PostProductDto product){
// return modelMapper.map(product, Product.class);
// }
}