diff --git a/src/main/java/com/hackathon/TimeLapse/article/ArticleController.java b/src/main/java/com/hackathon/TimeLapse/article/ArticleController.java index 007cde9..26e8250 100644 --- a/src/main/java/com/hackathon/TimeLapse/article/ArticleController.java +++ b/src/main/java/com/hackathon/TimeLapse/article/ArticleController.java @@ -1,23 +1,61 @@ package com.hackathon.TimeLapse.article; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.springframework.http.MediaType; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestPart; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; + import com.hackathon.TimeLapse.apiPayload.ApiResponse; +import com.hackathon.TimeLapse.image.Image; +import com.hackathon.TimeLapse.image.ImageRequestDTO; +import com.hackathon.TimeLapse.image.ImageService; +import com.hackathon.TimeLapse.s3.S3Service; + import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; -import org.springframework.validation.annotation.Validated; -import org.springframework.web.bind.annotation.*; +import lombok.extern.slf4j.Slf4j; @RestController @RequiredArgsConstructor @Validated +@Slf4j @RequestMapping("/api/article") public class ArticleController { - private final ArticleService articleService; - - @PostMapping("/") - public ApiResponse createArticle(@RequestBody @Valid ArticleRequestDTO.createArticleDTO request, - @RequestParam(name = "memberId") Long memberId) { - Article article = articleService.createArticle(memberId, request); - return ApiResponse.onSuccess(ArticleConverter.toCreateReviewResultDTO(article)); - } + + private final ArticleService articleService; + private final S3Service s3Service; + private final ImageService imageService; + + @PostMapping(value = "", consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE}) + public ApiResponse createArticle( + @RequestPart @Valid ArticleRequestDTO.createArticleDTO request, + @RequestPart List files + ) throws IOException { + long memberId = Long.parseLong(SecurityContextHolder.getContext().getAuthentication().getName()); + log.info("[*] memberId: " + memberId); + System.out.println(files.get(0)); + List uploadedFilesLinks = s3Service.uploadMultipleFiles(files); + Article article = articleService.createArticle(memberId, request, uploadedFilesLinks); + + List images = new ArrayList<>(); + for (String url : uploadedFilesLinks) { + Image image = Image.builder() + .image_url(url) + .article(article) + .build(); + images.add(image); + } + imageService.addImages(article.getId(), new ImageRequestDTO.addImagesDTO(uploadedFilesLinks)); + + return ApiResponse.onSuccess(ArticleConverter.toCreateReviewResultDTO(article)); + } } diff --git a/src/main/java/com/hackathon/TimeLapse/article/ArticleRequestDTO.java b/src/main/java/com/hackathon/TimeLapse/article/ArticleRequestDTO.java index a818356..8b15192 100644 --- a/src/main/java/com/hackathon/TimeLapse/article/ArticleRequestDTO.java +++ b/src/main/java/com/hackathon/TimeLapse/article/ArticleRequestDTO.java @@ -1,5 +1,9 @@ package com.hackathon.TimeLapse.article; +import java.util.List; + +import org.springframework.web.multipart.MultipartFile; + import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; import lombok.Getter; @@ -18,7 +22,5 @@ public static class createArticleDTO { private String description; @NotNull private Long status; - @NotNull - private Long memberId; } } diff --git a/src/main/java/com/hackathon/TimeLapse/article/ArticleService.java b/src/main/java/com/hackathon/TimeLapse/article/ArticleService.java index 18b215a..bce11e2 100644 --- a/src/main/java/com/hackathon/TimeLapse/article/ArticleService.java +++ b/src/main/java/com/hackathon/TimeLapse/article/ArticleService.java @@ -1,5 +1,8 @@ package com.hackathon.TimeLapse.article; +import java.util.List; + import com.hackathon.TimeLapse.apiPayload.exception.MemberNotFoundException; +import com.hackathon.TimeLapse.image.ImageService; import com.hackathon.TimeLapse.member.MemberRepository; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -15,7 +18,8 @@ public ArticleService(ArticleRepository articleRepository, MemberRepository memb } @Transactional - public Article createArticle(Long memberId, ArticleRequestDTO.createArticleDTO request) { + public Article createArticle(Long memberId, ArticleRequestDTO.createArticleDTO request, + List uploadedFilesLinks) { Article article = ArticleConverter.toArticle(request); article.setMember(memberRepository.findById(memberId) @@ -23,4 +27,4 @@ public Article createArticle(Long memberId, ArticleRequestDTO.createArticleDTO r return articleRepository.save(article); } -} \ No newline at end of file +} diff --git a/src/main/java/com/hackathon/TimeLapse/image/ImageRequestDTO.java b/src/main/java/com/hackathon/TimeLapse/image/ImageRequestDTO.java index 5fd6838..4deae9e 100644 --- a/src/main/java/com/hackathon/TimeLapse/image/ImageRequestDTO.java +++ b/src/main/java/com/hackathon/TimeLapse/image/ImageRequestDTO.java @@ -1,12 +1,15 @@ package com.hackathon.TimeLapse.image; import jakarta.validation.constraints.NotNull; +import lombok.AllArgsConstructor; import lombok.Getter; +import lombok.NoArgsConstructor; import java.util.List; public class ImageRequestDTO { @Getter + @AllArgsConstructor public static class addImagesDTO { private List imageUrlList; diff --git a/src/main/java/com/hackathon/TimeLapse/oauth/AuthTokensGenerator.java b/src/main/java/com/hackathon/TimeLapse/oauth/AuthTokensGenerator.java index c654012..59c94ab 100644 --- a/src/main/java/com/hackathon/TimeLapse/oauth/AuthTokensGenerator.java +++ b/src/main/java/com/hackathon/TimeLapse/oauth/AuthTokensGenerator.java @@ -10,7 +10,7 @@ @RequiredArgsConstructor public class AuthTokensGenerator { private static final String BEARER_TYPE = "Bearer"; - private static final long ACCESS_TOKEN_EXPIRE_TIME = 1000 * 60 * 30; // 30분 + private static final long ACCESS_TOKEN_EXPIRE_TIME = 1000 * 60 * 30 * 24 * 7; // 30분 private static final long REFRESH_TOKEN_EXPIRE_TIME = 1000 * 60 * 60 * 24 * 7; // 7일 private final JwtTokenProvider jwtTokenProvider; diff --git a/src/main/java/com/hackathon/TimeLapse/oauth/MemberController.java b/src/main/java/com/hackathon/TimeLapse/oauth/MemberController.java index cd24ca4..f16dfb4 100644 --- a/src/main/java/com/hackathon/TimeLapse/oauth/MemberController.java +++ b/src/main/java/com/hackathon/TimeLapse/oauth/MemberController.java @@ -8,7 +8,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; -import com.hackathon.TimeLapse.domain.Member; +import com.hackathon.TimeLapse.member.Member; import com.hackathon.TimeLapse.member.MemberRepository; import lombok.RequiredArgsConstructor; diff --git a/src/main/java/com/hackathon/TimeLapse/oauth/OAuthLoginService.java b/src/main/java/com/hackathon/TimeLapse/oauth/OAuthLoginService.java index 9ea96f4..a61a30d 100644 --- a/src/main/java/com/hackathon/TimeLapse/oauth/OAuthLoginService.java +++ b/src/main/java/com/hackathon/TimeLapse/oauth/OAuthLoginService.java @@ -2,7 +2,7 @@ import org.springframework.stereotype.Service; -import com.hackathon.TimeLapse.domain.Member; +import com.hackathon.TimeLapse.member.Member; import com.hackathon.TimeLapse.member.MemberRepository; import lombok.RequiredArgsConstructor; diff --git a/src/main/java/com/hackathon/TimeLapse/s3/S3Controller.java b/src/main/java/com/hackathon/TimeLapse/s3/S3Controller.java index b81a58a..48fa98d 100644 --- a/src/main/java/com/hackathon/TimeLapse/s3/S3Controller.java +++ b/src/main/java/com/hackathon/TimeLapse/s3/S3Controller.java @@ -1,6 +1,7 @@ package com.hackathon.TimeLapse.s3; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @@ -8,6 +9,7 @@ import lombok.RequiredArgsConstructor; +import java.io.IOException; import java.util.List; @RestController @@ -23,8 +25,14 @@ public ResponseEntity uploadFile(@RequestParam("file") MultipartFile fil } @PostMapping(value = "/uploadMultiple", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) - public ResponseEntity> uploadMultipleFiles(@RequestParam("files") MultipartFile[] files) { - return s3Service.uploadMultipleFiles(files); + public ResponseEntity> uploadMultipleFiles(@RequestParam("files") List files) { + try{ + return ResponseEntity.ok(s3Service.uploadMultipleFiles(files)); + } + catch (IOException e) { + e.printStackTrace(); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); + } } @GetMapping("/url") diff --git a/src/main/java/com/hackathon/TimeLapse/s3/S3Service.java b/src/main/java/com/hackathon/TimeLapse/s3/S3Service.java index a5405d2..053fc03 100644 --- a/src/main/java/com/hackathon/TimeLapse/s3/S3Service.java +++ b/src/main/java/com/hackathon/TimeLapse/s3/S3Service.java @@ -46,23 +46,18 @@ public ResponseEntity uploadFile(@RequestParam("file") MultipartFile fil } } - public ResponseEntity> uploadMultipleFiles(@RequestParam("files") MultipartFile[] files) { + public List uploadMultipleFiles(@RequestParam("files") List files) throws IOException { List fileUrls = new ArrayList<>(); for (MultipartFile file : files) { - try { - String fileName = file.getOriginalFilename(); - String fileUrl = "https://" + bucket + "/test" + fileName; - ObjectMetadata metadata = new ObjectMetadata(); - metadata.setContentType(file.getContentType()); - metadata.setContentLength(file.getSize()); - amazonS3Client.putObject(bucket, fileName, file.getInputStream(), metadata); - fileUrls.add(fileUrl); - } catch (IOException e) { - e.printStackTrace(); - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); - } + String fileName = file.getOriginalFilename(); + String fileUrl = "https://" + bucket + "/test" + fileName; + ObjectMetadata metadata = new ObjectMetadata(); + metadata.setContentType(file.getContentType()); + metadata.setContentLength(file.getSize()); + amazonS3Client.putObject(bucket, fileName, file.getInputStream(), metadata); + fileUrls.add(fileUrl); } - return ResponseEntity.ok(fileUrls); + return fileUrls; } public void deleteFile(String fileName) {