Skip to content

Commit

Permalink
Merge pull request #68 from Sopo2023/feat/#64
Browse files Browse the repository at this point in the history
Feat/#64
  • Loading branch information
GayeongKimm authored Nov 1, 2024
2 parents 8f31806 + 4361904 commit f6c97a1
Show file tree
Hide file tree
Showing 18 changed files with 195 additions and 115 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ dependencies {
implementation 'io.jsonwebtoken:jjwt-api:0.11.2'
implementation 'io.jsonwebtoken:jjwt-impl:0.11.2'
implementation 'io.jsonwebtoken:jjwt-gson:0.11.2'
// runtimeOnly 'io.jsonwebtoken:jjwt-jackson:0.11.5'
}

tasks.named('test') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ public class FileController {
private final FileService fileService;

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

@GetMapping("{id}")
public ResponseData<List<FileRes>> getFiles(@PathVariable Long id, @RequestParam FileCategory fileCategory) {
return fileService.getFiles(id, fileCategory);
public ResponseData<List<FileRes>> getFiles(@PathVariable Long id, @RequestParam FileCategory file_category) {
return fileService.getFiles(id, file_category);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import kr.hs.dgsw.SOPO_server_v2.domain.file.entity.FileEntity;
import lombok.Builder;

@Builder
public record FileRes (
String fileName,
String fileUrl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
import kr.hs.dgsw.SOPO_server_v2.global.common.entity.BaseTimeEntity;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.SuperBuilder;

@Getter
@Setter
@Entity
@Table(name = "tbl_file")
@NoArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
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> findByBoardBoardId(Long boardId);
List<FileEntity> findByContestContestId(Long contestId);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
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;
Expand All @@ -11,119 +8,78 @@
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.common.aws.S3Service;
import kr.hs.dgsw.SOPO_server_v2.global.common.aws.dto.S3Res;
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.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.function.Consumer;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class FileService {

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

private final AmazonS3 amazonS3;

private final S3Service s3Service;
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);
}
List<FileRes> fileRes = new ArrayList<>();
for (MultipartFile file : fileList) {
S3Res s3Res = s3Service.upload(file, fileCategory.toString());
FileEntity fileEntity = createFileEntity(file, s3Res.url(), fileCategory);

if (fileCategory == FileCategory.BOARD) {
BoardEntity board = boardRepository.findById(id)
.orElseThrow(() -> BoardNotFound.EXCEPTION);
saveFileEntity(fileEntity, board.getFile(), board, boardRepository::save);
} else if (fileCategory == FileCategory.CONTEST) {
ContestEntity contest = contestRepository.findById(id)
.orElseThrow(() -> ContestNotFound.EXCEPTION);
saveFileEntity(fileEntity, contest.getFile(), contest, contestRepository::save);
}
} catch (IOException e) {
throw new RuntimeException("Error uploading file to S3", e);
fileRes.add(FileRes.of(fileEntity));
}

return ResponseData.of(HttpStatus.OK, "파일 업로드 완료", fileRes);
}

private FileEntity createFileEntity(MultipartFile file, String url, FileCategory fileCategory) {
return FileEntity.builder()
.fileName(file.getOriginalFilename())
.fileCategory(fileCategory)
.fileUrl(url)
.build();
}

private <T> void saveFileEntity(FileEntity fileEntity, List<FileEntity> files, T entity, Consumer<T> saveFunction) {
if (files == null) {
files = new ArrayList<>();
}
files.add(fileEntity);

if (entity instanceof BoardEntity) {
fileEntity.setBoard((BoardEntity) entity);
((BoardEntity) entity).setFile(files);
} else if (entity instanceof ContestEntity) {
fileEntity.setContest((ContestEntity) entity);
((ContestEntity) entity).setFile(files);
}
return ResponseData.of(HttpStatus.OK, "파일 업로드 완료", fileLists);

fileRepository.save(fileEntity);
saveFunction.accept(entity);
}

public ResponseData<List<FileRes>> getFiles(Long id, FileCategory fileCategory) {
Expand All @@ -132,19 +88,13 @@ public ResponseData<List<FileRes>> getFiles(Long id, FileCategory fileCategory)

if (fileCategory == FileCategory.BOARD) {

fileList = fileRepository.findByBoard_BoardId(id).stream()
.map(file -> FileRes.builder()
.fileName(file.getFileName())
.fileUrl(file.getFileUrl())
.build())
fileList = fileRepository.findByBoardBoardId(id).stream()
.map(FileRes::of)
.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())
fileList = fileRepository.findByContestContestId(id).stream()
.map(FileRes::of)
.collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ public class MemberController {
public Response deleteMember(){
return memberService.deleteMember();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class MemberProfileController {
private final MemberProfileService memberProfileService;

@Operation(description = "멤버 프로필 수정")
@PatchMapping("")
@PatchMapping
public Response modifyMember(
@RequestBody MemberModifyReq memberModifyReq) {
return memberProfileService.memberModify(memberModifyReq);
Expand All @@ -30,5 +30,7 @@ public Response modifyMember(
public ResponseData<ReadProfileRes> readProfile(){
return memberProfileService.readProfile();
}


}

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public record MemberModifyReq(
@Email
String memberEmail,
String authCode,
String memberPassword
String memberPassword,
String memberSchool
) {
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package kr.hs.dgsw.SOPO_server_v2.domain.member.service;

import kr.hs.dgsw.SOPO_server_v2.domain.auth.service.AuthEmailService;
import kr.hs.dgsw.SOPO_server_v2.domain.auth.service.AuthEmailService;
import kr.hs.dgsw.SOPO_server_v2.domain.member.entity.MemberEntity;
import kr.hs.dgsw.SOPO_server_v2.domain.member.presentation.dto.req.MemberModifyReq;
import kr.hs.dgsw.SOPO_server_v2.domain.member.presentation.dto.res.ReadProfileRes;
Expand All @@ -11,6 +11,7 @@
import kr.hs.dgsw.SOPO_server_v2.global.response.Response;
import kr.hs.dgsw.SOPO_server_v2.global.response.ResponseData;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;
Expand All @@ -30,8 +31,8 @@ public Response memberModify(MemberModifyReq memberModifyReq) {
String memberEmail = memberModifyReq.memberEmail();
String authCode = memberModifyReq.authCode();

if (memberEmail != null && !memberEmail.isEmpty()) {
if (authCode == null || authCode.isEmpty()) {
if (StringUtils.isNotBlank(memberEmail)) {
if (StringUtils.isBlank(authCode)) {
throw NeedAuthCode.EXCEPTION;
}
if (!authEmailService.verifiedCode(memberEmail, authCode)) {
Expand All @@ -41,15 +42,20 @@ public Response memberModify(MemberModifyReq memberModifyReq) {
}

String memberPassword = memberModifyReq.memberPassword();
if (memberPassword != null) {
if (StringUtils.isNotBlank(memberPassword)) {
member.setMemberPassword(new BCryptPasswordEncoder().encode(memberPassword));
}

String memberName = memberModifyReq.memberName();
if (memberName != null) {
if (StringUtils.isNotBlank(memberName)) {
member.setMemberName(memberName);
}

String memberSchool = memberModifyReq.memberSchool();
if (StringUtils.isNotBlank(memberSchool)) {
member.setMemberSchool(memberSchool);
}

memberRepository.save(member);

return Response.of(HttpStatus.OK, "성공");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public class PortfolioEntity extends BaseTimeEntity {
@Column(name = "portfolio_id")
private Long portfolioId;


@Column(name = "portfolio_title")
private String portfolioTitle;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,19 @@
public class PortfolioController {
private final PortfolioService portfolioService;

public ResponseData<Long> generatePortfolio() {
return portfolioService.generatePortfolio();
}



@Operation(description = "최신 포트폴리오 불러오기")
@GetMapping
public ResponseData<List<LoadPortfolioRes>> loadPortfolio(
@ModelAttribute PageRequest pageRequest
){
return portfolioService.pagingPortfolio(pageRequest);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package kr.hs.dgsw.SOPO_server_v2.domain.portfolio.presentation.dto.req;

import java.util.List;

public record ModifyPortfolioReq(
String title,
String content,
List<String> tag
) {
}
Loading

0 comments on commit f6c97a1

Please sign in to comment.