8
8
import org .springframework .beans .factory .annotation .Autowired ;
9
9
import org .springframework .http .HttpStatus ;
10
10
import org .springframework .http .ResponseEntity ;
11
+ import org .springframework .security .access .method .P ;
11
12
import org .springframework .web .bind .annotation .CrossOrigin ;
12
13
import org .springframework .web .bind .annotation .DeleteMapping ;
13
14
import org .springframework .web .bind .annotation .GetMapping ;
17
18
import org .springframework .web .bind .annotation .RestController ;
18
19
import org .springframework .web .multipart .MultipartFile ;
19
20
21
+ import com .amazonaws .services .s3 .AmazonS3Client ;
20
22
import com .spooder .weshlist .Model .Product ;
21
23
import com .spooder .weshlist .Model .ProductDetail ;
22
24
import com .spooder .weshlist .dto .RatingDto ;
25
+ import com .spooder .weshlist .service .FileService ;
23
26
import com .spooder .weshlist .service .ProductService ;
24
27
25
28
import org .slf4j .Logger ;
@@ -38,14 +41,19 @@ public class ProductController {
38
41
@ Autowired
39
42
private ProductService productService ;
40
43
44
+ @ Autowired
45
+ private FileService fileService ;
46
+
41
47
private static final Logger logger = LoggerFactory .getLogger (ProductService .class );
42
48
43
49
@ PostMapping
44
50
public ResponseEntity <Product > addProduct (@ ModelAttribute Product product , @ RequestPart (name = "imageFile" , required = false ) MultipartFile imageFile ) {
45
51
if (product == null ) {
46
52
return new ResponseEntity <>(HttpStatus .BAD_REQUEST );
47
53
}
48
- Product savedProduct = productService .addProduct (product , imageFile );
54
+ Product savedProduct = replaceImage (product , imageFile ); // Image 처리 후, Service 단에 등록
55
+ savedProduct = productService .addProduct (savedProduct );
56
+
49
57
return new ResponseEntity <>(savedProduct , HttpStatus .CREATED );
50
58
}
51
59
@@ -68,6 +76,8 @@ public Product getProductById(@PathVariable Long product_id) {
68
76
69
77
@ PutMapping ("/{product_id}" )
70
78
public ResponseEntity <String > updateProduct (@ PathVariable Long product_id , @ ModelAttribute Product updatedProduct , @ RequestPart (name = "imageFile" , required = false ) MultipartFile imageFile ) {
79
+
80
+ // 기존의 Product 불러온 후 새로운 Product의 정보를 주입해줌.
71
81
Product product = productService .getProductById (product_id );
72
82
Calendar calendar = Calendar .getInstance ();
73
83
calendar .setTimeZone (TimeZone .getTimeZone ("Asia/Seoul" ));
@@ -80,8 +90,8 @@ public ResponseEntity<String> updateProduct(@PathVariable Long product_id, @Mode
80
90
else
81
91
product .setUploader (updatedProduct .getUploader ());
82
92
83
- if ( updatedProduct . getImage_name () == null ) productService . replaceImageFile ( product , imageFile );
84
- System . out . println ( updatedProduct . getBrand ());
93
+ replaceImage ( product , imageFile ); // 이미지 없으면 알아서 걸러주므로, 일단 보내기
94
+
85
95
product .setName (updatedProduct .getName ());
86
96
product .setPrice (updatedProduct .getPrice ());
87
97
product .setBrand (updatedProduct .getBrand ());
@@ -118,5 +128,18 @@ public ResponseEntity<String> deleteProduct(@PathVariable Long product_id) {
118
128
return new ResponseEntity <>("Error on deleting image" , HttpStatus .INTERNAL_SERVER_ERROR );
119
129
}
120
130
}
131
+
132
+ private Product replaceImage (Product product , MultipartFile imageFile ) { // Product와 imageFile을 보내면 기존 Product에 이미지 업로드 및 등록 처리
133
+ if (imageFile == null || imageFile .isEmpty ()) {
134
+ return product ; // imageFile이 없으면 기존 product 반환
135
+ }
136
+ try {
137
+ fileService .uploadImage (imageFile );
138
+ product .setImage_name (imageFile .getOriginalFilename ());
139
+ } catch (Exception e ) {
140
+ e .printStackTrace ();
141
+ }
142
+ return product ;
143
+ }
121
144
}
122
145
0 commit comments