|
1 | 1 | package com.tave.PromptMate.controller; |
2 | 2 |
|
3 | 3 | import com.tave.PromptMate.auth.dto.request.CustomUserDetails; |
| 4 | +import com.tave.PromptMate.common.S3Uploader; |
| 5 | +import com.tave.PromptMate.domain.Platform; |
| 6 | +import com.tave.PromptMate.domain.PromptCategory; |
4 | 7 | import com.tave.PromptMate.dto.community.CommunityPostResponse; |
5 | 8 | import com.tave.PromptMate.dto.library.CreateLibraryRequest; |
6 | 9 | import com.tave.PromptMate.dto.library.LibraryResponse; |
7 | 10 | import com.tave.PromptMate.service.LibraryService; |
8 | 11 | import io.swagger.v3.oas.annotations.Operation; |
9 | 12 | import io.swagger.v3.oas.annotations.tags.Tag; |
10 | | -import jakarta.validation.Valid; |
11 | 13 | import lombok.RequiredArgsConstructor; |
12 | 14 | import org.springframework.data.domain.Page; |
| 15 | +import org.springframework.http.MediaType; |
13 | 16 | import org.springframework.http.ResponseEntity; |
14 | 17 | import org.springframework.security.core.annotation.AuthenticationPrincipal; |
15 | 18 | import org.springframework.web.bind.annotation.*; |
| 19 | +import org.springframework.web.multipart.MultipartFile; |
16 | 20 |
|
| 21 | +import java.io.IOException; |
17 | 22 | import java.net.URI; |
18 | 23 | import java.util.List; |
19 | 24 |
|
|
24 | 29 | public class LibraryController { |
25 | 30 |
|
26 | 31 | private final LibraryService libraryService; |
| 32 | + private final S3Uploader s3Uploader; |
27 | 33 |
|
28 | 34 | // 라이브러리에 리라이팅 결과 저장하기 |
29 | | - @PostMapping |
30 | | - @Operation(summary = "리라이팅 결과 저장", description = "리라이팅된 결과를 라이브러리에 저장합니다.") |
| 35 | + @PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE) |
| 36 | + @Operation(summary = "리라이팅 결과 저장", description = "리라이팅된 결과를 라이브러리에 저장합니다. 이미지 첨부 가능.") |
31 | 37 | public ResponseEntity<LibraryResponse> save( |
32 | 38 | @AuthenticationPrincipal CustomUserDetails principal, |
33 | | - @Valid @RequestBody CreateLibraryRequest req){ |
34 | | - Long userId= principal.getUserId(); |
35 | | - LibraryResponse res=libraryService.save(userId,req); |
| 39 | + @RequestParam("rewriteResultId") Long rewriteResultId, |
| 40 | + @RequestParam(value = "savedTitle", required = false) String savedTitle, |
| 41 | + @RequestParam("platform") Platform platform, |
| 42 | + @RequestParam("category") PromptCategory category, |
| 43 | + @RequestPart(value = "image", required = false) MultipartFile image |
| 44 | + ) throws IOException { |
| 45 | + Long userId = principal.getUserId(); |
| 46 | + |
| 47 | + // 이미지 업로드 |
| 48 | + String imageUrl = null; |
| 49 | + if (image != null && !image.isEmpty()) { |
| 50 | + imageUrl = s3Uploader.uploadImage(image, "library"); |
| 51 | + } |
| 52 | + |
| 53 | + CreateLibraryRequest req = new CreateLibraryRequest( |
| 54 | + rewriteResultId, savedTitle, platform, category, imageUrl |
| 55 | + ); |
| 56 | + |
| 57 | + LibraryResponse res = libraryService.save(userId, req); |
36 | 58 |
|
37 | 59 | return ResponseEntity.created(URI.create("/api/libraries/" + res.id())) |
38 | 60 | .body(res); |
|
0 commit comments