Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/#66 #67

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ dependencies {
// S3
implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.mysql:mysql-connector-j'
implementation 'com.mysql:mysql-connector-j'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
package kr.hs.dgsw.SOPO_server_v2.domain.board.dto;

import kr.hs.dgsw.SOPO_server_v2.domain.board.entity.BoardEntity;
import kr.hs.dgsw.SOPO_server_v2.domain.file.dto.FileRes;
import kr.hs.dgsw.SOPO_server_v2.domain.file.entity.FileEntity;

import java.util.List;


public record BoardLoadRes (
Long boardId,
String boardTitle,
String boardContent,
Integer boardLikeCount,
String memberName
String memberName,
List<FileRes> files
){
public static BoardLoadRes of(BoardEntity board) {
public static BoardLoadRes of(BoardEntity board, List<FileEntity> fileEntities) {
return new BoardLoadRes(
board.getBoardId(),
board.getBoardTitle(),
board.getBoardTitle(),
board.getBoardLikeCount(),
board.getMember().getMemberName()
board.getMember().getMemberName(),
fileEntities.stream().map(FileRes::of).toList()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import kr.hs.dgsw.SOPO_server_v2.domain.board.dto.BoardUpdateReq;
import kr.hs.dgsw.SOPO_server_v2.domain.board.entity.BoardEntity;
import kr.hs.dgsw.SOPO_server_v2.domain.board.repository.BoardRepository;
import kr.hs.dgsw.SOPO_server_v2.domain.file.enums.FileCategory;
import kr.hs.dgsw.SOPO_server_v2.domain.file.repository.FileRepository;
import kr.hs.dgsw.SOPO_server_v2.domain.member.entity.MemberEntity;
import kr.hs.dgsw.SOPO_server_v2.domain.member.enums.MemberCategory;
import kr.hs.dgsw.SOPO_server_v2.global.error.custom.board.BoardNotFound;
Expand All @@ -27,13 +29,15 @@ public class BoardService {

private final BoardRepository boardRepository;
private final GetCurrentMember getCurrentMember;
private final FileRepository fileRepository;

// 게시글 전체 조회
public ResponseData<List<BoardLoadRes>> getBoards(PageRequest pageRequest) {
List<BoardEntity> boardList = boardRepository.findAll();

List<BoardLoadRes> boardLoadResList = boardList.stream()
.map(BoardLoadRes::of)
.map(b -> BoardLoadRes.of(b, fileRepository.findByFileCategoryAndFkId(
FileCategory.BOARD, b.getBoardId().toString())))
.skip((pageRequest.page() -1) * pageRequest.size())
.limit(pageRequest.size())
.collect(Collectors.toList());
Expand All @@ -49,7 +53,6 @@ public ResponseData<Long> createBoard() {
.boardTitle(null)
.boardContent(null)
.boardLikeCount(0)
.file(null)
.member(curMember)
.build();

Expand Down Expand Up @@ -79,7 +82,7 @@ public Response updateBoard(Long boardId, BoardUpdateReq updateReq) {
public ResponseData<BoardLoadRes> findOneBoard(Long boardId) {
BoardEntity board = boardRepository.findById(boardId)
.orElseThrow(() -> BoardNotFound.EXCEPTION);
BoardLoadRes boardLoadRes = BoardLoadRes.of(board);
BoardLoadRes boardLoadRes = BoardLoadRes.of(board, fileRepository.findByFileCategoryAndFkId(FileCategory.BOARD, boardId.toString()));
return ResponseData.of(HttpStatus.OK, "게시물 단일 조회 완료", boardLoadRes);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public ResponseData<Long> createContest() {
.contestLikeCount(0)
.contestDateTime(null)
.memberIdList(null)
.file(null)
.member(curMember)
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import kr.hs.dgsw.SOPO_server_v2.domain.file.service.FileService;
import kr.hs.dgsw.SOPO_server_v2.global.response.ResponseData;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
Expand All @@ -23,12 +21,12 @@ public class FileController {
private final FileService fileService;

@PostMapping
public ResponseData<List<FileRes>> fileUpload(@RequestParam Long id, @RequestParam FileCategory fileCategory, @RequestPart List<MultipartFile> fileList) {
public ResponseData<List<FileRes>> fileUpload(@RequestParam String id, @RequestParam FileCategory fileCategory, @RequestPart List<MultipartFile> fileList) {
return fileService.fileUpload(id, fileCategory, fileList);
}

@GetMapping("{id}")
public ResponseData<List<FileRes>> getFiles(@PathVariable Long id, @RequestParam FileCategory fileCategory) {
return fileService.getFiles(id, fileCategory);
}
// @GetMapping("{id}")
// public ResponseData<List<FileRes>> getFiles(@PathVariable Long id, @RequestParam FileCategory fileCategory) {
// return fileService.getFiles(id, fileCategory);
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,5 @@ public class FileEntity extends BaseTimeEntity {
@JoinColumn(name = "member_id")
private MemberEntity member;

private String fkId;
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package kr.hs.dgsw.SOPO_server_v2.domain.file.repository;

import kr.hs.dgsw.SOPO_server_v2.domain.file.entity.FileEntity;
import kr.hs.dgsw.SOPO_server_v2.domain.file.enums.FileCategory;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface FileRepository extends JpaRepository<FileEntity, Long> {
List<FileEntity> findByBoard_BoardId(Long boardId);
List<FileEntity> findByContest_ContestId(Long contestId);
List<FileEntity> findByFileCategoryAndFkId(FileCategory fileCategory, String fkId);
}
Original file line number Diff line number Diff line change
@@ -1,155 +1,36 @@
package kr.hs.dgsw.SOPO_server_v2.domain.file.service;

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import kr.hs.dgsw.SOPO_server_v2.domain.board.entity.BoardEntity;
import kr.hs.dgsw.SOPO_server_v2.domain.board.repository.BoardRepository;
import kr.hs.dgsw.SOPO_server_v2.domain.contest.entity.ContestEntity;
import kr.hs.dgsw.SOPO_server_v2.domain.contest.repository.ContestRepository;
import kr.hs.dgsw.SOPO_server_v2.domain.file.dto.FileRes;
import kr.hs.dgsw.SOPO_server_v2.domain.file.entity.FileEntity;
import kr.hs.dgsw.SOPO_server_v2.domain.file.enums.FileCategory;
import kr.hs.dgsw.SOPO_server_v2.domain.file.repository.FileRepository;
import kr.hs.dgsw.SOPO_server_v2.global.error.custom.board.BoardNotFound;
import kr.hs.dgsw.SOPO_server_v2.global.error.custom.contest.ContestNotFound;
import kr.hs.dgsw.SOPO_server_v2.global.infra.cloud.storagy.S3Uploader;
import kr.hs.dgsw.SOPO_server_v2.global.response.ResponseData;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class FileService {

@Value("${cloud.aws.s3.bucket}")
private String bucket;

private final AmazonS3 amazonS3;

private final S3Uploader s3Uploader;
private final FileRepository fileRepository;

private final BoardRepository boardRepository;

private final ContestRepository contestRepository;

@Transactional
public ResponseData<List<FileRes>> fileUpload(Long id, FileCategory fileCategory, List<MultipartFile> fileList) {

List<FileRes> fileLists = new ArrayList<>();

try {

for (MultipartFile file : fileList) {
String fileName = file.getOriginalFilename();

ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType(file.getContentType());
metadata.setContentLength(file.getSize());

// S3에 저장하는 코드
amazonS3.putObject(
new PutObjectRequest(bucket, fileName, file.getInputStream(), metadata)
);

// FileRes 형식으로 만들기
FileRes oneFile = FileRes.builder()
.fileName(fileName)
.fileUrl(amazonS3.getUrl(bucket, fileName).toString())
.build();

fileLists.add(oneFile);

// 파일 업데이트 하는 코드
if (fileCategory == FileCategory.BOARD) {

BoardEntity board = boardRepository.findById(id)
.orElseThrow(() -> BoardNotFound.EXCEPTION);

// DB에 저장하는 코드
FileEntity fileEntity = FileEntity.builder()
.fileName(fileName)
.fileCategory(fileCategory)
.fileUrl(amazonS3.getUrl(bucket, fileName).toString())
.board(board)
.build();
fileRepository.save(fileEntity);

// 현재 게시물의 파일 목록에 새로 업로드된 파일 추가
List<FileEntity> files = board.getFile();
if (files == null) {
files = new ArrayList<>();
}
files.add(fileEntity);
board.setFile(files); // BoardEntity의 파일 목록 직접 수정

// 게시물 엔티티를 DB에 저장하여 파일 목록 반영
boardRepository.save(board);

} else if (fileCategory == FileCategory.CONTEST) {
ContestEntity contest = contestRepository.findById(id)
.orElseThrow(() -> ContestNotFound.EXCEPTION);

// DB에 저장하는 코드
FileEntity fileEntity = FileEntity.builder()
.fileName(fileName)
.fileCategory(fileCategory)
.fileUrl(amazonS3.getUrl(bucket, fileName).toString())
.contest(contest)
.build();

fileRepository.save(fileEntity);

// 현재 게시물의 파일 목록에 새로 업로드된 파일 추가
List<FileEntity> files = contest.getFile();
if (files == null) {
files = new ArrayList<>();
}
files.add(fileEntity);
contest.setFile(files); // BoardEntity의 파일 목록 직접 수정

// 게시물 엔티티를 DB에 저장하여 파일 목록 반영
contestRepository.save(contest);
}
}
} catch (IOException e) {
throw new RuntimeException("Error uploading file to S3", e);
}
return ResponseData.of(HttpStatus.OK, "파일 업로드 완료", fileLists);
public ResponseData<List<FileRes>> fileUpload(String id, FileCategory fileCategory, List<MultipartFile> fileList) {
List<FileRes> fileRes = s3Uploader.uploadList(fileList);
fileRepository.saveAll(fileRes.stream()
.map(f -> FileEntity.builder()
.fileUrl(f.fileUrl())
.fileCategory(fileCategory)
.fileName(f.fileName())
.fkId(id)
.build())
.toList());
return ResponseData.of(HttpStatus.OK,"파일 업로드 성공",fileRes);
}

public ResponseData<List<FileRes>> getFiles(Long id, FileCategory fileCategory) {

List<FileRes> fileList = new ArrayList<>();

if (fileCategory == FileCategory.BOARD) {

fileList = fileRepository.findByBoard_BoardId(id).stream()
.map(file -> FileRes.builder()
.fileName(file.getFileName())
.fileUrl(file.getFileUrl())
.build())
.collect(Collectors.toList());

} else if (fileCategory == FileCategory.CONTEST) {
fileList = fileRepository.findByContest_ContestId(id).stream()
.map(file -> FileRes.builder()
.fileName(file.getFileName())
.fileUrl(file.getFileUrl())
.build())
.collect(Collectors.toList());
}

return ResponseData.of(HttpStatus.OK, "파일 조회 완료", fileList);
}


}
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
package kr.hs.dgsw.SOPO_server_v2.domain.member.entity;

import jakarta.persistence.*;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToOne;
import jakarta.persistence.Table;
import jakarta.validation.constraints.Size;
import kr.hs.dgsw.SOPO_server_v2.domain.file.entity.FileEntity;
import kr.hs.dgsw.SOPO_server_v2.domain.member.enums.MemberCategory;
import kr.hs.dgsw.SOPO_server_v2.domain.member.enums.MemberState;
import kr.hs.dgsw.SOPO_server_v2.domain.portfolio.entity.PortfolioEntity;
import kr.hs.dgsw.SOPO_server_v2.global.common.entity.BaseTimeEntity;
import lombok.*;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.SuperBuilder;
import org.hibernate.annotations.DynamicUpdate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
Expand Down
Loading