-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProductsController.java
192 lines (160 loc) · 7.07 KB
/
ProductsController.java
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package com.bhargav.LappyStore.controllers;
import java.util.List;
import java.nio.file.*;
import java.util.Date;
import java.io.InputStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import com.bhargav.LappyStore.models.Product;
import com.bhargav.LappyStore.models.ProductDto;
import com.bhargav.LappyStore.services.ProductsRepository;
import jakarta.validation.Valid;
@Controller
@RequestMapping("/products")
public class ProductsController {
@Autowired
private ProductsRepository repo;
@GetMapping({"","/"})
public String showProductsList(Model model){
List<Product> products= repo.findAll(Sort.by(Sort.Direction.ASC, "id"));
model.addAttribute("products", products);
return "products/index";
}
@GetMapping("/create")
public String showCreatePage(Model model){
ProductDto productDto= new ProductDto();
model.addAttribute("productDto", productDto);
return "products/createProduct";
}
@PostMapping("/create")
public String createProduct(
@Valid @ModelAttribute ProductDto productDto,
BindingResult result){
if(productDto.getImageFile().isEmpty()){
result.addError(new FieldError("productDto", "imageFile", "The image file is required"));
}
if(result.hasErrors()){
return "products/createProduct";
}
MultipartFile image = productDto.getImageFile();
Date createdAt= new Date();
String storageFileName= createdAt.getTime()+ "_" + image.getOriginalFilename();
try{
String uploadDir= "public/";
Path uploadPath= Paths.get(uploadDir);
if(!Files.exists(uploadPath)){
Files.createDirectories(uploadPath);
}
try(InputStream inputStream = image.getInputStream()){
Files.copy(inputStream, Paths.get(uploadDir+ storageFileName),
StandardCopyOption.REPLACE_EXISTING);
}
} catch(Exception ex){
System.out.println("Exception"+ex.getMessage());
}
Product product= new Product();
product.setName(productDto.getName());
product.setBrand(productDto.getBrand());
product.setCategory(productDto.getCategory());
product.setPrice(productDto.getPrice());
product.setDescription(productDto.getDescription());
product.setCreatedAt(createdAt);
product.setImageFileName(storageFileName);
repo.save(product);
return "redirect:/products";
}
@GetMapping("/edit")
public String showEditPage(
Model model,
@RequestParam int id
){
try{
Product product = repo.findById(id).get();
model.addAttribute("product", product);
ProductDto productDto= new ProductDto();
productDto.setName(product.getName());
productDto.setBrand(product.getBrand());
productDto.setCategory(product.getCategory());
productDto.setPrice(product.getPrice());
productDto.setDescription(product.getDescription());
model.addAttribute("productDto", productDto);
}
catch(Exception ex){
System.out.println("Exception: "+ex.getMessage());
return "redirect:/products";
}
return "products/EditProduct";
}
@PostMapping("/edit")
public String updateProduct(
Model model,
@RequestParam int id,
@Valid @ModelAttribute ProductDto productDto,
BindingResult result
){
try{
Product product= repo.findById(id).get();
model.addAttribute("product", product);
if(result.hasErrors()){
return "products/EditProduct";
}
if(!productDto.getImageFile().isEmpty()){
String uploadDir= "public/";
Path oldImagePath= Paths.get(uploadDir+ product.getImageFileName());
try{
Files.delete(oldImagePath);
}
catch(Exception ex){
System.out.println("Exception: " +ex.getMessage());
}
MultipartFile image= productDto.getImageFile();
Date createdAt= new Date();
String storageFileName= createdAt.getTime()+"_"+image.getOriginalFilename();
try(InputStream inputStream = image.getInputStream()){
Files.copy(inputStream, Paths.get(uploadDir+ storageFileName),
StandardCopyOption.REPLACE_EXISTING);
}
product.setImageFileName(storageFileName);
}
product.setName(productDto.getName());
product.setBrand(productDto.getBrand());
product.setCategory(productDto.getCategory());
product.setPrice(productDto.getPrice());
product.setDescription(productDto.getDescription());
repo.save(product);
}
catch(Exception ex){
System.out.println("Exception: "+ex.getMessage());
}
return "redirect:/products";
}
@GetMapping("/delete")
public String deleteProduct(
@RequestParam int id
){
try {
Product product= repo.findById(id).get();
Path imagePath= Paths.get("public/"+product.getImageFileName());
try {
Files.delete(imagePath);
}
catch (Exception e) {
System.out.println("Exception : "+e.getMessage());
}
repo.delete(product);
} catch (Exception e) {
System.out.println("Exception : "+e.getMessage());
}
return "redirect:/products";
}
}