Skip to content

Commit

Permalink
feat: 게시글 작성 기능을 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
oxdjww committed Jul 4, 2024
1 parent aa9a60f commit 02d21ac
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -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<ArticleResponseDTO.createArticleResultDTO> 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<ArticleResponseDTO.createArticleResultDTO> createArticle(
@RequestPart @Valid ArticleRequestDTO.createArticleDTO request,
@RequestPart List<MultipartFile> files
) throws IOException {
long memberId = Long.parseLong(SecurityContextHolder.getContext().getAuthentication().getName());
log.info("[*] memberId: " + memberId);
System.out.println(files.get(0));
List<String> uploadedFilesLinks = s3Service.uploadMultipleFiles(files);
Article article = articleService.createArticle(memberId, request, uploadedFilesLinks);

List<Image> 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));
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -18,7 +22,5 @@ public static class createArticleDTO {
private String description;
@NotNull
private Long status;
@NotNull
private Long memberId;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -15,12 +18,13 @@ public ArticleService(ArticleRepository articleRepository, MemberRepository memb
}

@Transactional
public Article createArticle(Long memberId, ArticleRequestDTO.createArticleDTO request) {
public Article createArticle(Long memberId, ArticleRequestDTO.createArticleDTO request,
List<String> uploadedFilesLinks) {
Article article = ArticleConverter.toArticle(request);

article.setMember(memberRepository.findById(memberId)
.orElseThrow(() -> new MemberNotFoundException("Member not found with id: " + memberId)));

return articleRepository.save(article);
}
}
}
Original file line number Diff line number Diff line change
@@ -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<String> imageUrlList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/com/hackathon/TimeLapse/s3/S3Controller.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
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.*;
import org.springframework.web.multipart.MultipartFile;

import lombok.RequiredArgsConstructor;

import java.io.IOException;
import java.util.List;

@RestController
Expand All @@ -23,8 +25,14 @@ public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile fil
}

@PostMapping(value = "/uploadMultiple", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<List<String>> uploadMultipleFiles(@RequestParam("files") MultipartFile[] files) {
return s3Service.uploadMultipleFiles(files);
public ResponseEntity<List<String>> uploadMultipleFiles(@RequestParam("files") List<MultipartFile> files) {
try{
return ResponseEntity.ok(s3Service.uploadMultipleFiles(files));
}
catch (IOException e) {
e.printStackTrace();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}

@GetMapping("/url")
Expand Down
23 changes: 9 additions & 14 deletions src/main/java/com/hackathon/TimeLapse/s3/S3Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,18 @@ public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile fil
}
}

public ResponseEntity<List<String>> uploadMultipleFiles(@RequestParam("files") MultipartFile[] files) {
public List<String> uploadMultipleFiles(@RequestParam("files") List<MultipartFile> files) throws IOException {
List<String> 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) {
Expand Down

0 comments on commit 02d21ac

Please sign in to comment.