diff --git a/src/main/java/com/example/helloworldmvc/config/SwaggerConfig.java b/src/main/java/com/example/helloworldmvc/config/SwaggerConfig.java index 35a796e..4928b9c 100644 --- a/src/main/java/com/example/helloworldmvc/config/SwaggerConfig.java +++ b/src/main/java/com/example/helloworldmvc/config/SwaggerConfig.java @@ -33,7 +33,7 @@ public OpenAPI helloWorldMVCAPI(){ .in(SecurityScheme.In.HEADER)); return new OpenAPI() - .addServersItem(new Server().url("/mvc")) + .addServersItem(new Server().url("/")) .info(info) .addSecurityItem(securityRequirement) .components(components); diff --git a/src/main/java/com/example/helloworldmvc/converter/CommunityConverter.java b/src/main/java/com/example/helloworldmvc/converter/CommunityConverter.java index 9d3829d..f836f72 100644 --- a/src/main/java/com/example/helloworldmvc/converter/CommunityConverter.java +++ b/src/main/java/com/example/helloworldmvc/converter/CommunityConverter.java @@ -5,6 +5,7 @@ import com.example.helloworldmvc.domain.enums.CommunityCategory; import com.example.helloworldmvc.web.dto.CommunityRequestDTO; import com.example.helloworldmvc.web.dto.CommunityResponseDTO; +import com.example.helloworldmvc.web.dto.FileDTO; import org.springframework.data.domain.Page; import java.util.ArrayList; @@ -27,14 +28,14 @@ public static CommunityResponseDTO.CreatedPostDTO toCreatedPostDTO(Community com .build(); } - public static CommunityResponseDTO.PostListDTO toPostListDTO(Page postList){ - List posts = postList.stream().map(CommunityConverter::toPostDTO).toList(); + public static CommunityResponseDTO.PostListDTO toPostListDTO(Page postList, Long categoryId){ + List posts = postList.stream().map(i -> CommunityConverter.toPostDTO(i, categoryId)).toList(); return CommunityResponseDTO.PostListDTO.builder() .postDTOList(posts) .build(); } - public static CommunityResponseDTO.PostDTO toPostDTO(Community community){ + public static CommunityResponseDTO.PostDTO toPostDTO(Community community, Long categoryId){ String imageUrl = null; if(!community.getFileList().isEmpty()){ imageUrl = community.getFileList().get(0).getUrl(); @@ -45,19 +46,24 @@ public static CommunityResponseDTO.PostDTO toPostDTO(Community community){ .created_at(community.getCreatedAt()) .commentNum(community.getCommentList().size()) .imageUrl(imageUrl) + .content(community.getContent()) + .category_id(categoryId) .build(); } public static CommunityResponseDTO.PostDetailDTO toPostDetailDTO(Community community, Page commentList){ - List list = new ArrayList<>(); + List list = new ArrayList(); if(!community.getFileList().isEmpty()){ - community.getFileList().stream().map(file -> list.add(file.getUrl())).collect(Collectors.toList()); + community.getFileList().stream().forEach(file -> { + list.add(FileConverter.toFileDetailRes(file.getUrl(), file.getFileType())); + }); } - else list.add("NULL"); + else list.add(FileConverter.toFileDetailRes("","")); List comments = commentList.stream().map(CommunityConverter::toCommentDTO).toList(); return CommunityResponseDTO.PostDetailDTO.builder() .title(community.getTitle()) .content(community.getContent()) + .communityWriterEmail(community.getUser().getEmail()) .created_at(community.getCreatedAt()) .fileList(list) .commentDTOList(comments) @@ -67,6 +73,7 @@ public static CommunityResponseDTO.PostDetailDTO toPostDetailDTO(Community commu public static CommunityResponseDTO.CommentDTO toCommentDTO(Comment comment){ return CommunityResponseDTO.CommentDTO.builder() .anonymousName(comment.getAnonymous()) + .commentWriterEmail(comment.getUser().getEmail()) .created_at(comment.getCreatedAt()) .content(comment.getContent()) .build(); diff --git a/src/main/java/com/example/helloworldmvc/converter/FileConverter.java b/src/main/java/com/example/helloworldmvc/converter/FileConverter.java index 02f7e6e..2f964d6 100644 --- a/src/main/java/com/example/helloworldmvc/converter/FileConverter.java +++ b/src/main/java/com/example/helloworldmvc/converter/FileConverter.java @@ -4,13 +4,21 @@ import com.example.helloworldmvc.domain.Community; import com.example.helloworldmvc.domain.File; import com.example.helloworldmvc.domain.User; +import com.example.helloworldmvc.web.dto.FileDTO; public class FileConverter { - public static File toFile(String pictureUrl, User user, Center center) { + public static File toFile(String pictureUrl, String fileType, User user, Center center) { return File.builder() .url(pictureUrl) + .fileType(fileType) .user(user) .center(center) .build(); } + public static FileDTO.FileDetailRes toFileDetailRes(String pictureUrl, String fileType) { + return FileDTO.FileDetailRes.builder() + .fileUrl(pictureUrl) + .fileType(fileType) + .build(); + } } diff --git a/src/main/java/com/example/helloworldmvc/domain/File.java b/src/main/java/com/example/helloworldmvc/domain/File.java index c5d94f1..c93a790 100644 --- a/src/main/java/com/example/helloworldmvc/domain/File.java +++ b/src/main/java/com/example/helloworldmvc/domain/File.java @@ -15,6 +15,8 @@ public class File { private String url; + private String fileType; + @OneToOne @JoinColumn(name = "user_id") private User user; diff --git a/src/main/java/com/example/helloworldmvc/service/CommunityServiceImpl.java b/src/main/java/com/example/helloworldmvc/service/CommunityServiceImpl.java index 073d456..10c5906 100644 --- a/src/main/java/com/example/helloworldmvc/service/CommunityServiceImpl.java +++ b/src/main/java/com/example/helloworldmvc/service/CommunityServiceImpl.java @@ -51,7 +51,7 @@ public CommunityResponseDTO.PostListDTO getCommunityList(String userId, Long cat User user = userRepository.findByEmail(userId).orElseThrow(() -> new GeneralException(ErrorStatus.USER_NOT_FOUND)); CommunityCategory communityCategory = CommunityConverter.toCommunityCategory(categoryId); Page communityList = communityRepository.findAllByCommunityCategory(communityCategory, PageRequest.of(page, size)); - return CommunityConverter.toPostListDTO(communityList); + return CommunityConverter.toPostListDTO(communityList, categoryId); } @Override diff --git a/src/main/java/com/example/helloworldmvc/service/S3Service.java b/src/main/java/com/example/helloworldmvc/service/S3Service.java index a57e6a8..3cb31e8 100644 --- a/src/main/java/com/example/helloworldmvc/service/S3Service.java +++ b/src/main/java/com/example/helloworldmvc/service/S3Service.java @@ -25,7 +25,8 @@ public class S3Service { public File setImage(MultipartFile file, User user){ String pictureUrl = s3Manager.uploadFile(s3Manager.generateUserKeyName(createFileName()), file); - return fileRepository.save(FileConverter.toFile(pictureUrl, user, null)); + String fileType = this.getFileExtension(file); + return fileRepository.save(FileConverter.toFile(pictureUrl, fileType ,user, null)); } @Transactional @@ -39,7 +40,8 @@ public File changeImage(MultipartFile file, User user) { public File setCommunityImage(MultipartFile file, Community community) { String url = s3Manager.uploadFile(s3Manager.generateUserKeyName(createFileName()), file); - File file1 = FileConverter.toFile(url, null, null); + String fileType = this.getFileExtension(file); + File file1 = FileConverter.toFile(url, fileType,null, null); file1.setCommunity(community); return fileRepository.save(file1); } @@ -47,4 +49,17 @@ public Uuid createFileName() { String uuid = UUID.randomUUID().toString(); return uuidRepository.save(Uuid.builder().uuid(uuid).build()); } + /** + * 파일 확장자 가져오기 + * @author 이승우 + * @param file MultipartFile 객체 + * @return 파일 확장자 (예: ".jpg", ".png") + */ + public String getFileExtension(MultipartFile file) { + String originalFilename = file.getOriginalFilename(); + if (originalFilename != null && originalFilename.contains(".")) { + return originalFilename.substring(originalFilename.lastIndexOf(".")); + } + return ""; // 확장자가 없는 경우 빈 문자열 반환 또는 예외 처리 + } } diff --git a/src/main/java/com/example/helloworldmvc/web/dto/CommunityResponseDTO.java b/src/main/java/com/example/helloworldmvc/web/dto/CommunityResponseDTO.java index 11db05d..4002157 100644 --- a/src/main/java/com/example/helloworldmvc/web/dto/CommunityResponseDTO.java +++ b/src/main/java/com/example/helloworldmvc/web/dto/CommunityResponseDTO.java @@ -34,6 +34,8 @@ public static class PostDTO{ LocalDateTime created_at; Integer commentNum; String imageUrl; + String content; + Long category_id; } @Builder @@ -43,8 +45,9 @@ public static class PostDTO{ public static class PostDetailDTO{ String title; String content; + String communityWriterEmail; LocalDateTime created_at; - List fileList; + List fileList; List commentDTOList; } @@ -54,6 +57,7 @@ public static class PostDetailDTO{ @AllArgsConstructor public static class CommentDTO{ Long anonymousName; + String commentWriterEmail; LocalDateTime created_at; String content; } diff --git a/src/main/java/com/example/helloworldmvc/web/dto/FileDTO.java b/src/main/java/com/example/helloworldmvc/web/dto/FileDTO.java index 8449eae..29a919c 100644 --- a/src/main/java/com/example/helloworldmvc/web/dto/FileDTO.java +++ b/src/main/java/com/example/helloworldmvc/web/dto/FileDTO.java @@ -10,4 +10,12 @@ public class FileDTO { private Long imageId; private String imageUrl; + @Builder + @Getter + @NoArgsConstructor + @AllArgsConstructor + public static class FileDetailRes{ + String fileUrl; + String fileType; + } } \ No newline at end of file