Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,14 +28,14 @@ public static CommunityResponseDTO.CreatedPostDTO toCreatedPostDTO(Community com
.build();
}

public static CommunityResponseDTO.PostListDTO toPostListDTO(Page<Community> postList){
List<CommunityResponseDTO.PostDTO> posts = postList.stream().map(CommunityConverter::toPostDTO).toList();
public static CommunityResponseDTO.PostListDTO toPostListDTO(Page<Community> postList, Long categoryId){
List<CommunityResponseDTO.PostDTO> 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();
Expand All @@ -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<Comment> commentList){
List<String> list = new ArrayList<>();
List<FileDTO.FileDetailRes> list = new ArrayList<FileDTO.FileDetailRes>();
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<CommunityResponseDTO.CommentDTO> 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)
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/example/helloworldmvc/domain/File.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class File {

private String url;

private String fileType;

@OneToOne
@JoinColumn(name = "user_id")
private User user;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Community> communityList = communityRepository.findAllByCommunityCategory(communityCategory, PageRequest.of(page, size));
return CommunityConverter.toPostListDTO(communityList);
return CommunityConverter.toPostListDTO(communityList, categoryId);
}

@Override
Expand Down
19 changes: 17 additions & 2 deletions src/main/java/com/example/helloworldmvc/service/S3Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -39,12 +40,26 @@ 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);
}
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 ""; // 확장자가 없는 경우 빈 문자열 반환 또는 예외 처리
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public static class PostDTO{
LocalDateTime created_at;
Integer commentNum;
String imageUrl;
String content;
Long category_id;
}

@Builder
Expand All @@ -43,8 +45,9 @@ public static class PostDTO{
public static class PostDetailDTO{
String title;
String content;
String communityWriterEmail;
LocalDateTime created_at;
List<String> fileList;
List<FileDTO.FileDetailRes> fileList;
List<CommentDTO> commentDTOList;
}

Expand All @@ -54,6 +57,7 @@ public static class PostDetailDTO{
@AllArgsConstructor
public static class CommentDTO{
Long anonymousName;
String commentWriterEmail;
LocalDateTime created_at;
String content;
}
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/example/helloworldmvc/web/dto/FileDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Loading