From 75846d5967c148b05c700b3371e508423431d15a Mon Sep 17 00:00:00 2001 From: Seungmin-develop Date: Tue, 5 Sep 2023 14:45:53 +0900 Subject: [PATCH 1/4] =?UTF-8?q?community=20upload=20api=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 1 + src/main/java/FirebaseInitializer.java | 36 ++++++++++ .../controller/CommunityController.java | 6 +- .../walkie/controller/HistoryController.java | 72 +++++++++---------- .../com/whyranoid/walkie/domain/Post.java | 8 +++ .../walkie/dto/request/PostRequest.java | 12 +++- .../walkie/service/CommunityService.java | 4 ++ .../walkie/service/WalkingService.java | 2 +- 8 files changed, 99 insertions(+), 42 deletions(-) create mode 100644 src/main/java/FirebaseInitializer.java diff --git a/build.gradle b/build.gradle index 9d2a9c8..ef9a331 100644 --- a/build.gradle +++ b/build.gradle @@ -43,6 +43,7 @@ dependencies { implementation "com.querydsl:querydsl-jpa:${queryDslVersion}" annotationProcessor "com.querydsl:querydsl-apt:${queryDslVersion}" + implementation group: 'com.google.firebase', name: 'firebase-admin', version: '8.1.0' } tasks.named('test') { diff --git a/src/main/java/FirebaseInitializer.java b/src/main/java/FirebaseInitializer.java new file mode 100644 index 0000000..af2237c --- /dev/null +++ b/src/main/java/FirebaseInitializer.java @@ -0,0 +1,36 @@ +import com.google.auth.oauth2.GoogleCredentials; +import com.google.firebase.FirebaseApp; +import com.google.firebase.FirebaseOptions; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.io.ClassPathResource; +import org.springframework.stereotype.Service; + +import javax.annotation.PostConstruct; +import java.io.IOException; + +@Slf4j +@Service +public class FirebaseInitializer { + @Value("${app.firebase-configuration-file}") + private String firebaseConfigPath; + + @PostConstruct + public void initialize() { + try { + FirebaseOptions options = new FirebaseOptions.Builder() + .setCredentials( + GoogleCredentials.fromStream( + new ClassPathResource(firebaseConfigPath).getInputStream() + ) + ).build(); + + if (FirebaseApp.getApps().isEmpty()) { + FirebaseApp.initializeApp(options); + log.info("Firebase application has been initialized"); + } + } catch (IOException e) { + log.error(e.getMessage()); + } + } +} diff --git a/src/main/java/com/whyranoid/walkie/controller/CommunityController.java b/src/main/java/com/whyranoid/walkie/controller/CommunityController.java index 0f6074d..f1c24a5 100644 --- a/src/main/java/com/whyranoid/walkie/controller/CommunityController.java +++ b/src/main/java/com/whyranoid/walkie/controller/CommunityController.java @@ -3,7 +3,6 @@ import com.whyranoid.walkie.dto.request.PostRequest; import com.whyranoid.walkie.service.CommunityService; import io.swagger.v3.oas.annotations.Operation; -import io.swagger.v3.oas.annotations.media.ArraySchema; import io.swagger.v3.oas.annotations.media.Content; import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.responses.ApiResponse; @@ -24,13 +23,12 @@ public class CommunityController { HttpHeaders httpHeaders = new HttpHeaders(); private final CommunityService communityService; - @Operation(summary = "게시글 올리기", description = "커뮤니티에 게시글을 업로드합니다.") - @ApiResponse(responseCode = "200", description = "업로드 성공", content = @Content(array = @ArraySchema(schema = @Schema(implementation = com.whyranoid.walkie.dto.response.ApiResponse.class)))) + @Operation(summary = "게시글 올리기", description = "커뮤니티에 게시글을 업로드합니다. colorMode는 글자색으로 다음과 같이 표현합니다. -> (0: 검정색, 1: 흰색) historyFlag는 history 정보 포함 여부로 다음과 같이 표현합니다. -> (0: history 정보 미포함, 1: history 정보 포함)") + @ApiResponse(responseCode = "200", description = "업로드 성공", content = @Content(schema = @Schema(implementation = com.whyranoid.walkie.dto.response.ApiResponse.class))) @PostMapping("/upload-post") public ResponseEntity uploadPost( @RequestBody PostRequest postRequest ) { return ResponseEntity.ok(communityService.uploadPost(postRequest)); } - } diff --git a/src/main/java/com/whyranoid/walkie/controller/HistoryController.java b/src/main/java/com/whyranoid/walkie/controller/HistoryController.java index 3404463..35bfa9b 100644 --- a/src/main/java/com/whyranoid/walkie/controller/HistoryController.java +++ b/src/main/java/com/whyranoid/walkie/controller/HistoryController.java @@ -1,36 +1,36 @@ -package com.whyranoid.walkie.controller; - -import com.whyranoid.walkie.dto.response.HistoryResponse; -import com.whyranoid.walkie.service.HistoryService; -import io.swagger.v3.oas.annotations.Operation; -import io.swagger.v3.oas.annotations.media.ArraySchema; -import io.swagger.v3.oas.annotations.media.Content; -import io.swagger.v3.oas.annotations.media.Schema; -import io.swagger.v3.oas.annotations.responses.ApiResponse; -import io.swagger.v3.oas.annotations.tags.Tag; -import lombok.RequiredArgsConstructor; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; - -@Tag(name = "history", description = "운동기록 API") -@RestController -@RequiredArgsConstructor -@RequestMapping("/api/history") -public class HistoryController { - HttpHeaders httpHeaders = new HttpHeaders(); - private final HistoryService historyService; - - @Operation(summary = "운동기록 가져오기", description = "해당 유저의 운동기록을 가져옵니다.") - @ApiResponse(responseCode = "200", description = "호출 성공", content = @Content(array = @ArraySchema(schema = @Schema(implementation = HistoryResponse.class)))) - @GetMapping("/my-history") - public ResponseEntity getHistory( - @RequestParam Long walkieId - ) { - return new ResponseEntity<>(historyService.getHistory(walkieId), httpHeaders, HttpStatus.OK); - } -} +//package com.whyranoid.walkie.controller; +// +//import com.whyranoid.walkie.dto.response.HistoryResponse; +//import com.whyranoid.walkie.service.HistoryService; +//import io.swagger.v3.oas.annotations.Operation; +//import io.swagger.v3.oas.annotations.media.ArraySchema; +//import io.swagger.v3.oas.annotations.media.Content; +//import io.swagger.v3.oas.annotations.media.Schema; +//import io.swagger.v3.oas.annotations.responses.ApiResponse; +//import io.swagger.v3.oas.annotations.tags.Tag; +//import lombok.RequiredArgsConstructor; +//import org.springframework.http.HttpHeaders; +//import org.springframework.http.HttpStatus; +//import org.springframework.http.ResponseEntity; +//import org.springframework.web.bind.annotation.GetMapping; +//import org.springframework.web.bind.annotation.RequestMapping; +//import org.springframework.web.bind.annotation.RequestParam; +//import org.springframework.web.bind.annotation.RestController; +// +//@Tag(name = "history", description = "운동기록 API") +//@RestController +//@RequiredArgsConstructor +//@RequestMapping("/api/history") +//public class HistoryController { +// HttpHeaders httpHeaders = new HttpHeaders(); +// private final HistoryService historyService; +// +// @Operation(summary = "운동기록 가져오기", description = "해당 유저의 운동기록을 가져옵니다.") +// @ApiResponse(responseCode = "200", description = "호출 성공", content = @Content(array = @ArraySchema(schema = @Schema(implementation = HistoryResponse.class)))) +// @GetMapping("/my-history") +// public ResponseEntity getHistory( +// @RequestParam Long walkieId +// ) { +// return new ResponseEntity<>(historyService.getHistory(walkieId), httpHeaders, HttpStatus.OK); +// } +//} diff --git a/src/main/java/com/whyranoid/walkie/domain/Post.java b/src/main/java/com/whyranoid/walkie/domain/Post.java index fd108ec..13b8ced 100644 --- a/src/main/java/com/whyranoid/walkie/domain/Post.java +++ b/src/main/java/com/whyranoid/walkie/domain/Post.java @@ -33,6 +33,14 @@ public class Post { @Column(name = "date", nullable = false) private String date; + @NotNull + @Column(name = "color_mode", nullable = false) + private Integer colorMode; + + @NotNull + @Column(name = "history_flag", nullable = false) + private Integer historyFlag; + @ManyToOne @JoinColumn(name = "user_id", nullable = false) // referencedColumnName는 디폴트가 pk private Walkie user; diff --git a/src/main/java/com/whyranoid/walkie/dto/request/PostRequest.java b/src/main/java/com/whyranoid/walkie/dto/request/PostRequest.java index 9286fa2..efe3726 100644 --- a/src/main/java/com/whyranoid/walkie/dto/request/PostRequest.java +++ b/src/main/java/com/whyranoid/walkie/dto/request/PostRequest.java @@ -23,10 +23,20 @@ public class PostRequest { @Schema(example = "오늘 진짜 많이 걸었다~~") private String content; + @NotNull + @Schema(example = "0") + private Integer colorMode; + + @NotNull + @Schema(example = "0") + private Integer historyFlag; + @Builder - public PostRequest(Long walkieId, String photo, String content) { + public PostRequest(Long walkieId, String photo, String content, Integer colorMode, Integer historyFlag) { this.walkieId = walkieId; this.photo = photo; this.content = content; + this.colorMode = colorMode; + this.historyFlag = historyFlag; } } diff --git a/src/main/java/com/whyranoid/walkie/service/CommunityService.java b/src/main/java/com/whyranoid/walkie/service/CommunityService.java index 0f57dec..e16bc9d 100644 --- a/src/main/java/com/whyranoid/walkie/service/CommunityService.java +++ b/src/main/java/com/whyranoid/walkie/service/CommunityService.java @@ -23,9 +23,13 @@ public ApiResponse uploadPost(PostRequest postRequest) { Long walkieId = postRequest.getWalkieId(); String photo = postRequest.getPhoto(); String content = postRequest.getContent(); + Integer colorMode = postRequest.getColorMode(); + Integer historyFlag = postRequest.getHistoryFlag(); post.setPhoto(photo); post.setContent(content); post.setDate(LocalDateTime.now().toString()); + post.setColorMode(colorMode); + post.setHistoryFlag(historyFlag); // 어떤 에러 던져야 할 지 논의해봐야 할 듯 post.setUser(walkieRepository.findByUserId(walkieId).orElseThrow()); communityRepository.uploadPost(post); diff --git a/src/main/java/com/whyranoid/walkie/service/WalkingService.java b/src/main/java/com/whyranoid/walkie/service/WalkingService.java index e36374f..9bdc5e3 100644 --- a/src/main/java/com/whyranoid/walkie/service/WalkingService.java +++ b/src/main/java/com/whyranoid/walkie/service/WalkingService.java @@ -33,7 +33,7 @@ public Long startWalking(WalkingDto walkingDto) { Walkie walkie = walkieRepository.findById(walkingDto.getWalkieId()).orElseThrow(EntityNotFoundException::new); History input = History.builder() - .startTime(walkingDto.getStartTime()) + .startTime(walkingDto.getStartTime().toString()) .user(walkie) .build(); From 97f0d2cc5a0e3ee926334a3cc3887851cb196600 Mon Sep 17 00:00:00 2001 From: Seungmin-develop Date: Tue, 5 Sep 2023 23:24:00 +0900 Subject: [PATCH 2/4] =?UTF-8?q?community=20upload=20api=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/.DS_Store | Bin 0 -> 6148 bytes .../java/com/whyranoid/walkie/domain/Post.java | 4 ++-- .../walkie/dto/request/PostRequest.java | 8 ++++---- .../walkie/service/CommunityService.java | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) create mode 100644 src/main/.DS_Store diff --git a/src/main/.DS_Store b/src/main/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..0e98056ead8b2b4ddd22a5b0719be234dbd12c0a GIT binary patch literal 6148 zcmeHKI|>3p3{BKRv9YwAD|mxJ^aNf&P_YpOMeD6Rmq+vEgD9(wU?VS(yqQeiEc=Sh zMnrUaUQa|uBGQK&%GE;K?A*L%oy;f@jyukBl$C7cbXl~j-zSVa01F&BJoAtH-C@6t zo8MEJr2a0lP&G#k4Lb5A>uO>X7ausK8$-puO2*HpMGtZ*9Gt_1Xeo!mZ{8H^bT~2;Po?-j1=cc0Bf? bs4KR{c};8rosPWIf&3XTU1(I`*9zPLYRVP2 literal 0 HcmV?d00001 diff --git a/src/main/java/com/whyranoid/walkie/domain/Post.java b/src/main/java/com/whyranoid/walkie/domain/Post.java index 13b8ced..24cfadb 100644 --- a/src/main/java/com/whyranoid/walkie/domain/Post.java +++ b/src/main/java/com/whyranoid/walkie/domain/Post.java @@ -38,8 +38,8 @@ public class Post { private Integer colorMode; @NotNull - @Column(name = "history_flag", nullable = false) - private Integer historyFlag; + @Column(name = "history_content", nullable = true) + private String historyContent; @ManyToOne @JoinColumn(name = "user_id", nullable = false) // referencedColumnName는 디폴트가 pk diff --git a/src/main/java/com/whyranoid/walkie/dto/request/PostRequest.java b/src/main/java/com/whyranoid/walkie/dto/request/PostRequest.java index efe3726..b360a2b 100644 --- a/src/main/java/com/whyranoid/walkie/dto/request/PostRequest.java +++ b/src/main/java/com/whyranoid/walkie/dto/request/PostRequest.java @@ -28,15 +28,15 @@ public class PostRequest { private Integer colorMode; @NotNull - @Schema(example = "0") - private Integer historyFlag; + @Schema(example = "07.51km 01:03:45 08'29 운동기록 안보낼거면 null로 보내주세요") + private String historyContent; @Builder - public PostRequest(Long walkieId, String photo, String content, Integer colorMode, Integer historyFlag) { + public PostRequest(Long walkieId, String photo, String content, Integer colorMode, String historyContent) { this.walkieId = walkieId; this.photo = photo; this.content = content; this.colorMode = colorMode; - this.historyFlag = historyFlag; + this.historyContent = historyContent; } } diff --git a/src/main/java/com/whyranoid/walkie/service/CommunityService.java b/src/main/java/com/whyranoid/walkie/service/CommunityService.java index e16bc9d..cf7368f 100644 --- a/src/main/java/com/whyranoid/walkie/service/CommunityService.java +++ b/src/main/java/com/whyranoid/walkie/service/CommunityService.java @@ -24,12 +24,12 @@ public ApiResponse uploadPost(PostRequest postRequest) { String photo = postRequest.getPhoto(); String content = postRequest.getContent(); Integer colorMode = postRequest.getColorMode(); - Integer historyFlag = postRequest.getHistoryFlag(); + String historyContent = postRequest.getHistoryContent(); post.setPhoto(photo); post.setContent(content); post.setDate(LocalDateTime.now().toString()); post.setColorMode(colorMode); - post.setHistoryFlag(historyFlag); + post.setHistoryContent(historyContent); // 어떤 에러 던져야 할 지 논의해봐야 할 듯 post.setUser(walkieRepository.findByUserId(walkieId).orElseThrow()); communityRepository.uploadPost(post); From 18c74cae288676ddec2b4b787deae6b614b0397a Mon Sep 17 00:00:00 2001 From: Seungmin-develop Date: Wed, 6 Sep 2023 01:11:18 +0900 Subject: [PATCH 3/4] =?UTF-8?q?=EC=9D=B4=EB=AF=B8=EC=A7=80=20=ED=8C=8C?= =?UTF-8?q?=EC=9D=BC=20=EC=97=85=EB=A1=9C=EB=93=9C=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../whyranoid/walkie/ApiBaseUrlSingleton.java | 25 ++++++++++++++ .../walkie}/FirebaseInitializer.java | 13 ++++--- .../controller/CommunityController.java | 19 +++++++---- .../walkie/dto/request/PostRequest.java | 5 --- .../walkie/service/CommunityService.java | 34 ++++++++++++++----- 5 files changed, 70 insertions(+), 26 deletions(-) create mode 100644 src/main/java/com/whyranoid/walkie/ApiBaseUrlSingleton.java rename src/main/java/{ => com/whyranoid/walkie}/FirebaseInitializer.java (76%) diff --git a/src/main/java/com/whyranoid/walkie/ApiBaseUrlSingleton.java b/src/main/java/com/whyranoid/walkie/ApiBaseUrlSingleton.java new file mode 100644 index 0000000..e1f392b --- /dev/null +++ b/src/main/java/com/whyranoid/walkie/ApiBaseUrlSingleton.java @@ -0,0 +1,25 @@ +package com.whyranoid.walkie; + +public class ApiBaseUrlSingleton { + private static ApiBaseUrlSingleton instance; + private String baseUrl; + + private ApiBaseUrlSingleton() { + // private 생성자로 외부에서 인스턴스를 생성하지 못하도록 함 + } + + public static ApiBaseUrlSingleton getInstance() { + if (instance == null) { + instance = new ApiBaseUrlSingleton(); + } + return instance; + } + + public String getBaseUrl() { + return baseUrl; + } + + public void setBaseUrl(String baseUrl) { + this.baseUrl = baseUrl; + } +} diff --git a/src/main/java/FirebaseInitializer.java b/src/main/java/com/whyranoid/walkie/FirebaseInitializer.java similarity index 76% rename from src/main/java/FirebaseInitializer.java rename to src/main/java/com/whyranoid/walkie/FirebaseInitializer.java index af2237c..ff760e5 100644 --- a/src/main/java/FirebaseInitializer.java +++ b/src/main/java/com/whyranoid/walkie/FirebaseInitializer.java @@ -1,3 +1,5 @@ +package com.whyranoid.walkie; + import com.google.auth.oauth2.GoogleCredentials; import com.google.firebase.FirebaseApp; import com.google.firebase.FirebaseOptions; @@ -18,13 +20,10 @@ public class FirebaseInitializer { @PostConstruct public void initialize() { try { - FirebaseOptions options = new FirebaseOptions.Builder() - .setCredentials( - GoogleCredentials.fromStream( - new ClassPathResource(firebaseConfigPath).getInputStream() - ) - ).build(); - + ApiBaseUrlSingleton.getInstance().setBaseUrl("https://walkie-5bfb3.appspot.com"); + FirebaseOptions options = new FirebaseOptions.Builder().setCredentials( + GoogleCredentials.fromStream( + new ClassPathResource(firebaseConfigPath).getInputStream())).build(); if (FirebaseApp.getApps().isEmpty()) { FirebaseApp.initializeApp(options); log.info("Firebase application has been initialized"); diff --git a/src/main/java/com/whyranoid/walkie/controller/CommunityController.java b/src/main/java/com/whyranoid/walkie/controller/CommunityController.java index f1c24a5..b27cbd0 100644 --- a/src/main/java/com/whyranoid/walkie/controller/CommunityController.java +++ b/src/main/java/com/whyranoid/walkie/controller/CommunityController.java @@ -1,6 +1,6 @@ package com.whyranoid.walkie.controller; -import com.whyranoid.walkie.dto.request.PostRequest; +import com.google.firebase.auth.FirebaseAuthException; import com.whyranoid.walkie.service.CommunityService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.media.Content; @@ -11,9 +11,12 @@ import org.springframework.http.HttpHeaders; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; + +import java.io.IOException; @Tag(name = "community", description = "커뮤니티 API") @RestController @@ -25,10 +28,14 @@ public class CommunityController { private final CommunityService communityService; @Operation(summary = "게시글 올리기", description = "커뮤니티에 게시글을 업로드합니다. colorMode는 글자색으로 다음과 같이 표현합니다. -> (0: 검정색, 1: 흰색) historyFlag는 history 정보 포함 여부로 다음과 같이 표현합니다. -> (0: history 정보 미포함, 1: history 정보 포함)") @ApiResponse(responseCode = "200", description = "업로드 성공", content = @Content(schema = @Schema(implementation = com.whyranoid.walkie.dto.response.ApiResponse.class))) - @PostMapping("/upload-post") + @PostMapping(value = "/upload-post") public ResponseEntity uploadPost( - @RequestBody PostRequest postRequest - ) { - return ResponseEntity.ok(communityService.uploadPost(postRequest)); + @RequestParam("image")MultipartFile image, + @RequestParam("walkieId")Long walkieId, + @RequestParam("content")String content, + @RequestParam("colorMode")Integer colorMode, + @RequestParam("historyContent")String historyContent + ) throws IOException, FirebaseAuthException { + return ResponseEntity.ok(communityService.uploadPost(image, walkieId, content, colorMode, historyContent)); } } diff --git a/src/main/java/com/whyranoid/walkie/dto/request/PostRequest.java b/src/main/java/com/whyranoid/walkie/dto/request/PostRequest.java index b360a2b..c7f51d4 100644 --- a/src/main/java/com/whyranoid/walkie/dto/request/PostRequest.java +++ b/src/main/java/com/whyranoid/walkie/dto/request/PostRequest.java @@ -15,10 +15,6 @@ public class PostRequest { @Schema(example = "123") private Long walkieId; - @NotNull - @Schema(example = "업로드할 사진 이미지 url") - private String photo; - @NotNull @Schema(example = "오늘 진짜 많이 걸었다~~") private String content; @@ -34,7 +30,6 @@ public class PostRequest { @Builder public PostRequest(Long walkieId, String photo, String content, Integer colorMode, String historyContent) { this.walkieId = walkieId; - this.photo = photo; this.content = content; this.colorMode = colorMode; this.historyContent = historyContent; diff --git a/src/main/java/com/whyranoid/walkie/service/CommunityService.java b/src/main/java/com/whyranoid/walkie/service/CommunityService.java index cf7368f..b035ea2 100644 --- a/src/main/java/com/whyranoid/walkie/service/CommunityService.java +++ b/src/main/java/com/whyranoid/walkie/service/CommunityService.java @@ -1,15 +1,25 @@ package com.whyranoid.walkie.service; +import com.google.cloud.storage.Blob; +import com.google.cloud.storage.Bucket; +import com.google.firebase.auth.FirebaseAuthException; +import com.google.firebase.cloud.StorageClient; +import com.whyranoid.walkie.ApiBaseUrlSingleton; import com.whyranoid.walkie.domain.Post; -import com.whyranoid.walkie.dto.request.PostRequest; import com.whyranoid.walkie.dto.response.ApiResponse; import com.whyranoid.walkie.repository.CommunityRepository; import com.whyranoid.walkie.repository.WalkieRepository; import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.multipart.MultipartFile; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; import java.time.LocalDateTime; +import java.util.UUID; @Service @Transactional @@ -18,24 +28,32 @@ public class CommunityService { private final CommunityRepository communityRepository; private final WalkieRepository walkieRepository; - public ApiResponse uploadPost(PostRequest postRequest) { + @Value("${app.firebase-bucket-name}") + private String firebaseBucket; + + public ApiResponse uploadPost(MultipartFile image, Long walkieId, String content, Integer colorMode, String historyContent) throws IOException, FirebaseAuthException { + String imageUrl = "post/" + UUID.randomUUID() + ".jpg"; Post post = new Post(); - Long walkieId = postRequest.getWalkieId(); - String photo = postRequest.getPhoto(); - String content = postRequest.getContent(); - Integer colorMode = postRequest.getColorMode(); - String historyContent = postRequest.getHistoryContent(); - post.setPhoto(photo); post.setContent(content); post.setDate(LocalDateTime.now().toString()); post.setColorMode(colorMode); post.setHistoryContent(historyContent); // 어떤 에러 던져야 할 지 논의해봐야 할 듯 post.setUser(walkieRepository.findByUserId(walkieId).orElseThrow()); + uploadImage(image, imageUrl); + post.setPhoto(ApiBaseUrlSingleton.getInstance().getBaseUrl()+ '/' + imageUrl); communityRepository.uploadPost(post); return ApiResponse.builder() .status(200) .message("게시글 업로드 완료!") .build(); } + + public String uploadImage(MultipartFile image, String nameFile) + throws IOException, FirebaseAuthException { + Bucket bucket = StorageClient.getInstance().bucket(firebaseBucket); + InputStream content = new ByteArrayInputStream(image.getBytes()); + Blob blob = bucket.create(nameFile, content, image.getContentType()); + return blob.getMediaLink(); + } } From efe1a70bf7d9e4dde1496d12ec6f439c5894244b Mon Sep 17 00:00:00 2001 From: Seungmin-develop Date: Wed, 6 Sep 2023 01:19:35 +0900 Subject: [PATCH 4/4] =?UTF-8?q?swagger=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/CommunityController.java | 11 +++++- .../walkie/dto/request/PostRequest.java | 37 ------------------- 2 files changed, 10 insertions(+), 38 deletions(-) delete mode 100644 src/main/java/com/whyranoid/walkie/dto/request/PostRequest.java diff --git a/src/main/java/com/whyranoid/walkie/controller/CommunityController.java b/src/main/java/com/whyranoid/walkie/controller/CommunityController.java index b27cbd0..f08ca45 100644 --- a/src/main/java/com/whyranoid/walkie/controller/CommunityController.java +++ b/src/main/java/com/whyranoid/walkie/controller/CommunityController.java @@ -3,6 +3,8 @@ import com.google.firebase.auth.FirebaseAuthException; import com.whyranoid.walkie.service.CommunityService; import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.Parameters; import io.swagger.v3.oas.annotations.media.Content; import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.responses.ApiResponse; @@ -26,8 +28,15 @@ public class CommunityController { HttpHeaders httpHeaders = new HttpHeaders(); private final CommunityService communityService; - @Operation(summary = "게시글 올리기", description = "커뮤니티에 게시글을 업로드합니다. colorMode는 글자색으로 다음과 같이 표현합니다. -> (0: 검정색, 1: 흰색) historyFlag는 history 정보 포함 여부로 다음과 같이 표현합니다. -> (0: history 정보 미포함, 1: history 정보 포함)") + @Operation(summary = "게시글 올리기", description = "커뮤니티에 게시글을 업로드합니다. colorMode는 글자색으로 다음과 같이 표현합니다. -> (0: 검정색, 1: 흰색) historyContent는 history 정보를 표현합니다. 없으면 null 넘겨주시면 돼요") @ApiResponse(responseCode = "200", description = "업로드 성공", content = @Content(schema = @Schema(implementation = com.whyranoid.walkie.dto.response.ApiResponse.class))) + @Parameters({ + @Parameter(name = "image", description = "업로드할 이미지 multipart", example = "image.jpg"), + @Parameter(name = "walkieId", description = "유저 아이디", example = "123"), + @Parameter(name = "content", description = "유저가 쓴 글의 내용", example = "아 상쾌하게 달렸다!"), + @Parameter(name = "colorMode", description = "글자색 카테고리(0은 검정, 1은 흰색)", example = "1"), + @Parameter(name = "historyContent", description = "운동기록 정보", example = "7.51km 01:03:45 08'29\"") + }) @PostMapping(value = "/upload-post") public ResponseEntity uploadPost( @RequestParam("image")MultipartFile image, diff --git a/src/main/java/com/whyranoid/walkie/dto/request/PostRequest.java b/src/main/java/com/whyranoid/walkie/dto/request/PostRequest.java deleted file mode 100644 index c7f51d4..0000000 --- a/src/main/java/com/whyranoid/walkie/dto/request/PostRequest.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.whyranoid.walkie.dto.request; - -import io.swagger.v3.oas.annotations.media.Schema; -import lombok.AccessLevel; -import lombok.Builder; -import lombok.Getter; -import lombok.NoArgsConstructor; - -import javax.validation.constraints.NotNull; - -@NoArgsConstructor(access = AccessLevel.PROTECTED) -@Getter -public class PostRequest { - @NotNull - @Schema(example = "123") - private Long walkieId; - - @NotNull - @Schema(example = "오늘 진짜 많이 걸었다~~") - private String content; - - @NotNull - @Schema(example = "0") - private Integer colorMode; - - @NotNull - @Schema(example = "07.51km 01:03:45 08'29 운동기록 안보낼거면 null로 보내주세요") - private String historyContent; - - @Builder - public PostRequest(Long walkieId, String photo, String content, Integer colorMode, String historyContent) { - this.walkieId = walkieId; - this.content = content; - this.colorMode = colorMode; - this.historyContent = historyContent; - } -}