From b8ae356c7384d1392793fb4b2e5592b5216c675e Mon Sep 17 00:00:00 2001 From: sycuuui <102959791+sycuuui@users.noreply.github.com> Date: Sat, 21 Sep 2024 18:50:39 +0900 Subject: [PATCH 1/6] #213 feat : existPorfolio save api --- .../controller/PortfolioController.java | 19 +++++----- .../domain/portfolio/entity/Portfolio.java | 17 ++++++++- .../repository/PortfolioRepository.java | 9 ++++- .../portfolio/service/PortfolioService.java | 36 ++++++++++++++++--- 4 files changed, 66 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/gongjakso/server/domain/portfolio/controller/PortfolioController.java b/src/main/java/com/gongjakso/server/domain/portfolio/controller/PortfolioController.java index 3b1d1f57..6b307499 100644 --- a/src/main/java/com/gongjakso/server/domain/portfolio/controller/PortfolioController.java +++ b/src/main/java/com/gongjakso/server/domain/portfolio/controller/PortfolioController.java @@ -1,5 +1,7 @@ package com.gongjakso.server.domain.portfolio.controller; +import com.gongjakso.server.domain.contest.dto.request.UpdateContestDto; +import com.gongjakso.server.domain.contest.dto.response.ContestRes; import com.gongjakso.server.domain.portfolio.dto.request.PortfolioReq; import com.gongjakso.server.domain.portfolio.dto.response.PortfolioRes; import com.gongjakso.server.domain.portfolio.dto.response.SimplePortfolioRes; @@ -11,14 +13,8 @@ import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; import org.springframework.security.core.annotation.AuthenticationPrincipal; -import org.springframework.web.bind.annotation.DeleteMapping; -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.PutMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; import java.util.List; @@ -66,4 +62,11 @@ public ApplicationResponse deletePortfolio(@AuthenticationPrincipal Princi public ApplicationResponse> getMyPortfolios(@AuthenticationPrincipal PrincipalDetails principalDetails) { return ApplicationResponse.ok(portfolioService.getMyPortfolios(principalDetails.getMember())); } + + @Operation(description = "포트폴리오 파일 및 링크 업로드 API") + @PostMapping("/exist-protfolio") + public ApplicationResponse update(@AuthenticationPrincipal PrincipalDetails principalDetails, @RequestPart(required = false) MultipartFile image, @RequestPart(required = false) String notionUri){ + portfolioService.saveExistPortfolio(principalDetails.getMember(),image,notionUri); + return ApplicationResponse.ok(); + } } diff --git a/src/main/java/com/gongjakso/server/domain/portfolio/entity/Portfolio.java b/src/main/java/com/gongjakso/server/domain/portfolio/entity/Portfolio.java index ff563f67..094adaf9 100644 --- a/src/main/java/com/gongjakso/server/domain/portfolio/entity/Portfolio.java +++ b/src/main/java/com/gongjakso/server/domain/portfolio/entity/Portfolio.java @@ -12,6 +12,7 @@ import org.hibernate.annotations.JdbcTypeCode; import org.hibernate.annotations.SQLDelete; import org.hibernate.type.SqlTypes; +import org.springframework.data.redis.connection.ReactiveGeoCommands; @Getter @Entity @@ -36,13 +37,27 @@ public class Portfolio extends BaseTimeEntity { @JdbcTypeCode(SqlTypes.JSON) private PortfolioData portfolioData; - @Builder + @Column(name = "file_uri",columnDefinition = "text") + private String fileUri; + + @Column(name = "notion_uri",columnDefinition = "text") + private String notionUri; + + @Builder(builderMethodName = "portfolioBuilder") public Portfolio(Member member, String portfolioName, PortfolioData portfolioData) { this.member = member; this.portfolioName = portfolioName; this.portfolioData = portfolioData; } + @Builder(builderMethodName = "existPortfolioBuilder") + public Portfolio(Member member, String fileUri, String notionUri){ + this.member = member; + this.fileUri = fileUri; + this.notionUri = notionUri; + } + + public void updateName(String updatedName) { this.portfolioName = updatedName; } diff --git a/src/main/java/com/gongjakso/server/domain/portfolio/repository/PortfolioRepository.java b/src/main/java/com/gongjakso/server/domain/portfolio/repository/PortfolioRepository.java index cb0cc04b..d9030f8c 100644 --- a/src/main/java/com/gongjakso/server/domain/portfolio/repository/PortfolioRepository.java +++ b/src/main/java/com/gongjakso/server/domain/portfolio/repository/PortfolioRepository.java @@ -1,11 +1,18 @@ package com.gongjakso.server.domain.portfolio.repository; +import com.gongjakso.server.domain.member.entity.Member; import com.gongjakso.server.domain.portfolio.entity.Portfolio; import java.util.Optional; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; public interface PortfolioRepository extends JpaRepository, PortfolioRepositoryCustom{ Optional findByIdAndDeletedAtIsNull(Long portfolioId); - long countByDeletedAtIsNull(); + @Query("SELECT EXISTS (" + + "SELECT 1 FROM Portfolio po " + + "WHERE po.member = :member AND (po.fileUri IS NOT NULL OR po.notionUri IS NOT NULL))") + Boolean existsExistPortfolioByMember(@Param("member") Member member); + Optional findPortfolioByMember(Member member); } diff --git a/src/main/java/com/gongjakso/server/domain/portfolio/service/PortfolioService.java b/src/main/java/com/gongjakso/server/domain/portfolio/service/PortfolioService.java index f94554fd..ab3a1ebc 100644 --- a/src/main/java/com/gongjakso/server/domain/portfolio/service/PortfolioService.java +++ b/src/main/java/com/gongjakso/server/domain/portfolio/service/PortfolioService.java @@ -10,9 +10,13 @@ import com.gongjakso.server.global.exception.ApplicationException; import com.gongjakso.server.global.exception.ErrorCode; import java.util.List; + +import com.gongjakso.server.global.util.s3.S3Client; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.bind.annotation.RequestPart; +import org.springframework.web.multipart.MultipartFile; @Service @Transactional(readOnly = true) @@ -20,6 +24,9 @@ public class PortfolioService { private final PortfolioRepository portfolioRepository; + private final S3Client s3Client; + private final String S3_PORTFOLIO_DIR_NAME = "portfolio"; + // PortfolioName 생성 로직을 분리 private String generatePortfolioName(String portfolioName) { @@ -104,11 +111,7 @@ public PortfolioRes registerPortfolio(Member member, PortfolioReq portfolioReq) String portfolioName = generatePortfolioName(portfolioReq.portfolioName()); PortfolioData portfolioData = convertToPortfolioData(portfolioReq); - Portfolio portfolio = Portfolio.builder() - .member(member) - .portfolioName(portfolioName) - .portfolioData(portfolioData) - .build(); + Portfolio portfolio = new Portfolio(member,portfolioName,portfolioData); Portfolio savedPortfolio = portfolioRepository.save(portfolio); @@ -165,4 +168,27 @@ public List getMyPortfolios(Member member) { .map(SimplePortfolioRes::of) .toList(); } + + @Transactional + public void saveExistPortfolio(Member member, MultipartFile image, String notionUri){ + //등록된 파일이나 노션 링크 있는지 확인 + //Validation + Boolean isExist = portfolioRepository.existsExistPortfolioByMember(member); + if (!isExist){ + throw new ApplicationException(ErrorCode.ALREADY_EXIST_EXCEPTION); + } + if(image.isEmpty() && notionUri.isEmpty()){ + throw new ApplicationException(ErrorCode.PORTFOLIO_SAVE_FAILED_EXCEPTION); + } + //Business + String s3Url = null; + if (image != null && !image.isEmpty()) { + s3Url = s3Client.upload(image, S3_PORTFOLIO_DIR_NAME); + } + if (notionUri.isEmpty()){ + notionUri=null; + } + Portfolio portfolio = new Portfolio(member,s3Url,notionUri); + portfolioRepository.save(portfolio); + } } \ No newline at end of file From 4e9960b5ae8d90dc05e1fb34d98c26cacf5b40cc Mon Sep 17 00:00:00 2001 From: sycuuui <102959791+sycuuui@users.noreply.github.com> Date: Sat, 21 Sep 2024 19:34:55 +0900 Subject: [PATCH 2/6] #213 update,delete,find api --- .../controller/PortfolioController.java | 27 +++++++++-- .../dto/response/ExistPortfolioRes.java | 15 +++++++ .../domain/portfolio/entity/Portfolio.java | 5 +++ .../portfolio/service/PortfolioService.java | 45 +++++++++++++++++-- 4 files changed, 85 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/gongjakso/server/domain/portfolio/dto/response/ExistPortfolioRes.java diff --git a/src/main/java/com/gongjakso/server/domain/portfolio/controller/PortfolioController.java b/src/main/java/com/gongjakso/server/domain/portfolio/controller/PortfolioController.java index 6b307499..627d69f0 100644 --- a/src/main/java/com/gongjakso/server/domain/portfolio/controller/PortfolioController.java +++ b/src/main/java/com/gongjakso/server/domain/portfolio/controller/PortfolioController.java @@ -1,8 +1,7 @@ package com.gongjakso.server.domain.portfolio.controller; -import com.gongjakso.server.domain.contest.dto.request.UpdateContestDto; -import com.gongjakso.server.domain.contest.dto.response.ContestRes; import com.gongjakso.server.domain.portfolio.dto.request.PortfolioReq; +import com.gongjakso.server.domain.portfolio.dto.response.ExistPortfolioRes; import com.gongjakso.server.domain.portfolio.dto.response.PortfolioRes; import com.gongjakso.server.domain.portfolio.dto.response.SimplePortfolioRes; import com.gongjakso.server.domain.portfolio.service.PortfolioService; @@ -65,8 +64,30 @@ public ApplicationResponse> getMyPortfolios(@Authentica @Operation(description = "포트폴리오 파일 및 링크 업로드 API") @PostMapping("/exist-protfolio") - public ApplicationResponse update(@AuthenticationPrincipal PrincipalDetails principalDetails, @RequestPart(required = false) MultipartFile image, @RequestPart(required = false) String notionUri){ + public ApplicationResponse updateExistPortfolio(@AuthenticationPrincipal PrincipalDetails principalDetails, @RequestPart(required = false) MultipartFile image, @RequestPart(required = false) String notionUri){ portfolioService.saveExistPortfolio(principalDetails.getMember(),image,notionUri); return ApplicationResponse.ok(); } + + @Operation(description = "포트폴리오 파일 및 링크 업로드 삭제 API") + @PostMapping("/exist-protfolio/{portfolio_id}") + public ApplicationResponse deleteExistPortfolio(@AuthenticationPrincipal PrincipalDetails principalDetails,@PathVariable("portfolio_id") Long portfolioId){ + portfolioService.deleteExistPortfolio(principalDetails.getMember(),portfolioId); + return ApplicationResponse.ok(); + } + + @Operation(description = "포트폴리오 파일 및 링크 업로드 업데이트 API") + @PostMapping("/exist-protfolio/{portfolio_id}") + public ApplicationResponse updateExistPortfolio(@AuthenticationPrincipal PrincipalDetails principalDetails,@PathVariable("portfolio_id") Long portfolioId, @RequestPart(required = false) MultipartFile image, @RequestPart(required = false) String notionUri){ + portfolioService.updateExistPortfolio(principalDetails.getMember(),portfolioId,image,notionUri); + return ApplicationResponse.ok(); + } + + @Operation(description = "포트폴리오 파일 및 링크 업로드 가져오기 API") + @PostMapping("/exist-protfolio/{portfolio_id}") + public ApplicationResponse findExistPortfolio(@AuthenticationPrincipal PrincipalDetails principalDetails, @PathVariable("portfolio_id") Long portfolioId, @RequestPart(required = false) MultipartFile image, @RequestPart(required = false) String notionUri){ + return ApplicationResponse.ok(portfolioService.findExistPorfolio(principalDetails.getMember(),portfolioId)); + } + + } diff --git a/src/main/java/com/gongjakso/server/domain/portfolio/dto/response/ExistPortfolioRes.java b/src/main/java/com/gongjakso/server/domain/portfolio/dto/response/ExistPortfolioRes.java new file mode 100644 index 00000000..5143d332 --- /dev/null +++ b/src/main/java/com/gongjakso/server/domain/portfolio/dto/response/ExistPortfolioRes.java @@ -0,0 +1,15 @@ +package com.gongjakso.server.domain.portfolio.dto.response; + +import com.gongjakso.server.domain.portfolio.entity.Portfolio; + +public record ExistPortfolioRes( + String fileUri, + String notionUri +) { + public static ExistPortfolioRes of(Portfolio portfolio) { + return new ExistPortfolioRes( + portfolio.getFileUri(), + portfolio.getNotionUri() + ); + } +} diff --git a/src/main/java/com/gongjakso/server/domain/portfolio/entity/Portfolio.java b/src/main/java/com/gongjakso/server/domain/portfolio/entity/Portfolio.java index 094adaf9..031a6642 100644 --- a/src/main/java/com/gongjakso/server/domain/portfolio/entity/Portfolio.java +++ b/src/main/java/com/gongjakso/server/domain/portfolio/entity/Portfolio.java @@ -1,5 +1,6 @@ package com.gongjakso.server.domain.portfolio.entity; +import com.gongjakso.server.domain.contest.dto.request.UpdateContestDto; import com.gongjakso.server.domain.member.entity.Member; import com.gongjakso.server.domain.portfolio.vo.PortfolioData; import com.gongjakso.server.global.common.BaseTimeEntity; @@ -56,6 +57,10 @@ public Portfolio(Member member, String fileUri, String notionUri){ this.fileUri = fileUri; this.notionUri = notionUri; } + public void updateExistPortfolio(Portfolio portfolio, String fileUri, String notionUri) { + this.fileUri = (fileUri == null) ? this.fileUri : portfolio.getFileUri(); + this.notionUri = (notionUri == null) ? this.notionUri : portfolio.getNotionUri(); + } public void updateName(String updatedName) { diff --git a/src/main/java/com/gongjakso/server/domain/portfolio/service/PortfolioService.java b/src/main/java/com/gongjakso/server/domain/portfolio/service/PortfolioService.java index ab3a1ebc..fbb25523 100644 --- a/src/main/java/com/gongjakso/server/domain/portfolio/service/PortfolioService.java +++ b/src/main/java/com/gongjakso/server/domain/portfolio/service/PortfolioService.java @@ -2,6 +2,7 @@ import com.gongjakso.server.domain.member.entity.Member; import com.gongjakso.server.domain.portfolio.dto.request.PortfolioReq; +import com.gongjakso.server.domain.portfolio.dto.response.ExistPortfolioRes; import com.gongjakso.server.domain.portfolio.dto.response.PortfolioRes; import com.gongjakso.server.domain.portfolio.dto.response.SimplePortfolioRes; import com.gongjakso.server.domain.portfolio.entity.Portfolio; @@ -15,7 +16,6 @@ import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import org.springframework.web.bind.annotation.RequestPart; import org.springframework.web.multipart.MultipartFile; @Service @@ -185,10 +185,47 @@ public void saveExistPortfolio(Member member, MultipartFile image, String notion if (image != null && !image.isEmpty()) { s3Url = s3Client.upload(image, S3_PORTFOLIO_DIR_NAME); } - if (notionUri.isEmpty()){ - notionUri=null; - } Portfolio portfolio = new Portfolio(member,s3Url,notionUri); portfolioRepository.save(portfolio); } + + @Transactional + public void deleteExistPortfolio(Member member, Long id){ + Portfolio portfolio = portfolioRepository.findById(id).orElseThrow(()-> new ApplicationException(ErrorCode.NOT_FOUND_EXCEPTION)); + if(!member.getId().equals(portfolio.getMember().getId())){ + throw new ApplicationException(ErrorCode.UNAUTHORIZED_EXCEPTION); + } + if(portfolio.getFileUri() != null){ + s3Client.delete(portfolio.getFileUri()); + } + portfolioRepository.delete(portfolio); + } + + @Transactional + public void updateExistPortfolio(Member member, Long id, MultipartFile image, String notionUri){ + //등록된 파일이나 노션 링크 있는지 확인 + //Validation + Portfolio portfolio = portfolioRepository.findById(id).orElseThrow(()-> new ApplicationException(ErrorCode.NOT_FOUND_EXCEPTION)); + if(!member.getId().equals(portfolio.getMember().getId())){ + throw new ApplicationException(ErrorCode.UNAUTHORIZED_EXCEPTION); + } + //Business + String s3Url = null; + if (image != null && !image.isEmpty()) { + s3Client.delete(portfolio.getFileUri()); + s3Url = s3Client.upload(image, S3_PORTFOLIO_DIR_NAME); + } + portfolio.updateExistPortfolio(portfolio,s3Url,notionUri); + portfolioRepository.save(portfolio); + } + + public ExistPortfolioRes findExistPorfolio(Member member, Long id){ + //Validation + Portfolio portfolio = portfolioRepository.findById(id).orElseThrow(()-> new ApplicationException(ErrorCode.NOT_FOUND_EXCEPTION)); + if (!portfolio.getMember().getId().equals(member.getId())) { + throw new ApplicationException(ErrorCode.FORBIDDEN_EXCEPTION); + } + return new ExistPortfolioRes(portfolio.getFileUri(),portfolio.getNotionUri()); + } + } \ No newline at end of file From cc7ca735a4815d9ff539e0fba2af2b12a4172420 Mon Sep 17 00:00:00 2001 From: sycuuui <102959791+sycuuui@users.noreply.github.com> Date: Sat, 21 Sep 2024 19:41:53 +0900 Subject: [PATCH 3/6] #213 fix : controller method --- .../domain/portfolio/controller/PortfolioController.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/gongjakso/server/domain/portfolio/controller/PortfolioController.java b/src/main/java/com/gongjakso/server/domain/portfolio/controller/PortfolioController.java index 627d69f0..a8f69358 100644 --- a/src/main/java/com/gongjakso/server/domain/portfolio/controller/PortfolioController.java +++ b/src/main/java/com/gongjakso/server/domain/portfolio/controller/PortfolioController.java @@ -70,21 +70,21 @@ public ApplicationResponse updateExistPortfolio(@AuthenticationPrincipal P } @Operation(description = "포트폴리오 파일 및 링크 업로드 삭제 API") - @PostMapping("/exist-protfolio/{portfolio_id}") + @DeleteMapping("/exist-protfolio/{portfolio_id}") public ApplicationResponse deleteExistPortfolio(@AuthenticationPrincipal PrincipalDetails principalDetails,@PathVariable("portfolio_id") Long portfolioId){ portfolioService.deleteExistPortfolio(principalDetails.getMember(),portfolioId); return ApplicationResponse.ok(); } @Operation(description = "포트폴리오 파일 및 링크 업로드 업데이트 API") - @PostMapping("/exist-protfolio/{portfolio_id}") + @PatchMapping("/exist-protfolio/{portfolio_id}") public ApplicationResponse updateExistPortfolio(@AuthenticationPrincipal PrincipalDetails principalDetails,@PathVariable("portfolio_id") Long portfolioId, @RequestPart(required = false) MultipartFile image, @RequestPart(required = false) String notionUri){ portfolioService.updateExistPortfolio(principalDetails.getMember(),portfolioId,image,notionUri); return ApplicationResponse.ok(); } @Operation(description = "포트폴리오 파일 및 링크 업로드 가져오기 API") - @PostMapping("/exist-protfolio/{portfolio_id}") + @GetMapping("/exist-protfolio/{portfolio_id}") public ApplicationResponse findExistPortfolio(@AuthenticationPrincipal PrincipalDetails principalDetails, @PathVariable("portfolio_id") Long portfolioId, @RequestPart(required = false) MultipartFile image, @RequestPart(required = false) String notionUri){ return ApplicationResponse.ok(portfolioService.findExistPorfolio(principalDetails.getMember(),portfolioId)); } From 277010dc07bf2ee8a4d872a70f0ad498c7d14cca Mon Sep 17 00:00:00 2001 From: sycuuui <102959791+sycuuui@users.noreply.github.com> Date: Sat, 21 Sep 2024 23:43:21 +0900 Subject: [PATCH 4/6] #213 fix: add portfolioName --- .../domain/portfolio/controller/PortfolioController.java | 6 +++--- .../gongjakso/server/domain/portfolio/entity/Portfolio.java | 3 ++- .../domain/portfolio/repository/PortfolioRepository.java | 2 +- .../server/domain/portfolio/service/PortfolioService.java | 6 ++++-- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/gongjakso/server/domain/portfolio/controller/PortfolioController.java b/src/main/java/com/gongjakso/server/domain/portfolio/controller/PortfolioController.java index a8f69358..c459c5ba 100644 --- a/src/main/java/com/gongjakso/server/domain/portfolio/controller/PortfolioController.java +++ b/src/main/java/com/gongjakso/server/domain/portfolio/controller/PortfolioController.java @@ -64,7 +64,7 @@ public ApplicationResponse> getMyPortfolios(@Authentica @Operation(description = "포트폴리오 파일 및 링크 업로드 API") @PostMapping("/exist-protfolio") - public ApplicationResponse updateExistPortfolio(@AuthenticationPrincipal PrincipalDetails principalDetails, @RequestPart(required = false) MultipartFile image, @RequestPart(required = false) String notionUri){ + public ApplicationResponse updateExistPortfolio(@AuthenticationPrincipal PrincipalDetails principalDetails, @RequestPart(required = false,name = "image") MultipartFile image, @RequestPart(required = false,name = "notionUri") String notionUri){ portfolioService.saveExistPortfolio(principalDetails.getMember(),image,notionUri); return ApplicationResponse.ok(); } @@ -78,14 +78,14 @@ public ApplicationResponse deleteExistPortfolio(@AuthenticationPrincipal P @Operation(description = "포트폴리오 파일 및 링크 업로드 업데이트 API") @PatchMapping("/exist-protfolio/{portfolio_id}") - public ApplicationResponse updateExistPortfolio(@AuthenticationPrincipal PrincipalDetails principalDetails,@PathVariable("portfolio_id") Long portfolioId, @RequestPart(required = false) MultipartFile image, @RequestPart(required = false) String notionUri){ + public ApplicationResponse updateExistPortfolio(@AuthenticationPrincipal PrincipalDetails principalDetails,@PathVariable("portfolio_id") Long portfolioId, @RequestPart(required = false,name = "image") MultipartFile image, @RequestPart(required = false,name = "notionUri") String notionUri){ portfolioService.updateExistPortfolio(principalDetails.getMember(),portfolioId,image,notionUri); return ApplicationResponse.ok(); } @Operation(description = "포트폴리오 파일 및 링크 업로드 가져오기 API") @GetMapping("/exist-protfolio/{portfolio_id}") - public ApplicationResponse findExistPortfolio(@AuthenticationPrincipal PrincipalDetails principalDetails, @PathVariable("portfolio_id") Long portfolioId, @RequestPart(required = false) MultipartFile image, @RequestPart(required = false) String notionUri){ + public ApplicationResponse findExistPortfolio(@AuthenticationPrincipal PrincipalDetails principalDetails, @PathVariable("portfolio_id") Long portfolioId){ return ApplicationResponse.ok(portfolioService.findExistPorfolio(principalDetails.getMember(),portfolioId)); } diff --git a/src/main/java/com/gongjakso/server/domain/portfolio/entity/Portfolio.java b/src/main/java/com/gongjakso/server/domain/portfolio/entity/Portfolio.java index 031a6642..22f6a7a3 100644 --- a/src/main/java/com/gongjakso/server/domain/portfolio/entity/Portfolio.java +++ b/src/main/java/com/gongjakso/server/domain/portfolio/entity/Portfolio.java @@ -52,8 +52,9 @@ public Portfolio(Member member, String portfolioName, PortfolioData portfolioDat } @Builder(builderMethodName = "existPortfolioBuilder") - public Portfolio(Member member, String fileUri, String notionUri){ + public Portfolio(Member member, String portfolioName, String fileUri, String notionUri){ this.member = member; + this.portfolioName = portfolioName; this.fileUri = fileUri; this.notionUri = notionUri; } diff --git a/src/main/java/com/gongjakso/server/domain/portfolio/repository/PortfolioRepository.java b/src/main/java/com/gongjakso/server/domain/portfolio/repository/PortfolioRepository.java index d9030f8c..55408763 100644 --- a/src/main/java/com/gongjakso/server/domain/portfolio/repository/PortfolioRepository.java +++ b/src/main/java/com/gongjakso/server/domain/portfolio/repository/PortfolioRepository.java @@ -14,5 +14,5 @@ public interface PortfolioRepository extends JpaRepository, Por "SELECT 1 FROM Portfolio po " + "WHERE po.member = :member AND (po.fileUri IS NOT NULL OR po.notionUri IS NOT NULL))") Boolean existsExistPortfolioByMember(@Param("member") Member member); - Optional findPortfolioByMember(Member member); + Optional findPortfolioById(Long portfolioId); } diff --git a/src/main/java/com/gongjakso/server/domain/portfolio/service/PortfolioService.java b/src/main/java/com/gongjakso/server/domain/portfolio/service/PortfolioService.java index fbb25523..3c609334 100644 --- a/src/main/java/com/gongjakso/server/domain/portfolio/service/PortfolioService.java +++ b/src/main/java/com/gongjakso/server/domain/portfolio/service/PortfolioService.java @@ -174,7 +174,7 @@ public void saveExistPortfolio(Member member, MultipartFile image, String notion //등록된 파일이나 노션 링크 있는지 확인 //Validation Boolean isExist = portfolioRepository.existsExistPortfolioByMember(member); - if (!isExist){ + if (isExist){ throw new ApplicationException(ErrorCode.ALREADY_EXIST_EXCEPTION); } if(image.isEmpty() && notionUri.isEmpty()){ @@ -185,7 +185,7 @@ public void saveExistPortfolio(Member member, MultipartFile image, String notion if (image != null && !image.isEmpty()) { s3Url = s3Client.upload(image, S3_PORTFOLIO_DIR_NAME); } - Portfolio portfolio = new Portfolio(member,s3Url,notionUri); + Portfolio portfolio = new Portfolio(member,generatePortfolioName(null),s3Url,notionUri); portfolioRepository.save(portfolio); } @@ -205,6 +205,7 @@ public void deleteExistPortfolio(Member member, Long id){ public void updateExistPortfolio(Member member, Long id, MultipartFile image, String notionUri){ //등록된 파일이나 노션 링크 있는지 확인 //Validation + System.out.println(id); Portfolio portfolio = portfolioRepository.findById(id).orElseThrow(()-> new ApplicationException(ErrorCode.NOT_FOUND_EXCEPTION)); if(!member.getId().equals(portfolio.getMember().getId())){ throw new ApplicationException(ErrorCode.UNAUTHORIZED_EXCEPTION); @@ -215,6 +216,7 @@ public void updateExistPortfolio(Member member, Long id, MultipartFile image, St s3Client.delete(portfolio.getFileUri()); s3Url = s3Client.upload(image, S3_PORTFOLIO_DIR_NAME); } + System.out.println(notionUri); portfolio.updateExistPortfolio(portfolio,s3Url,notionUri); portfolioRepository.save(portfolio); } From 41f6ead584394e58aacae4518419fc34f2ba02a8 Mon Sep 17 00:00:00 2001 From: sycuuui <102959791+sycuuui@users.noreply.github.com> Date: Sun, 22 Sep 2024 00:22:12 +0900 Subject: [PATCH 5/6] #213 fix : exists profolio uri --- .../server/domain/portfolio/entity/Portfolio.java | 6 +++--- .../portfolio/repository/PortfolioRepository.java | 2 +- .../domain/portfolio/service/PortfolioService.java | 12 ++++++------ 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/gongjakso/server/domain/portfolio/entity/Portfolio.java b/src/main/java/com/gongjakso/server/domain/portfolio/entity/Portfolio.java index 22f6a7a3..a7682519 100644 --- a/src/main/java/com/gongjakso/server/domain/portfolio/entity/Portfolio.java +++ b/src/main/java/com/gongjakso/server/domain/portfolio/entity/Portfolio.java @@ -58,9 +58,9 @@ public Portfolio(Member member, String portfolioName, String fileUri, String not this.fileUri = fileUri; this.notionUri = notionUri; } - public void updateExistPortfolio(Portfolio portfolio, String fileUri, String notionUri) { - this.fileUri = (fileUri == null) ? this.fileUri : portfolio.getFileUri(); - this.notionUri = (notionUri == null) ? this.notionUri : portfolio.getNotionUri(); + public void updatePortfolioUri(Portfolio portfolio, String fileUri, String notionUri) { + this.fileUri = (fileUri == null) ? this.fileUri : fileUri; + this.notionUri = (notionUri == null) ? this.notionUri : notionUri; } diff --git a/src/main/java/com/gongjakso/server/domain/portfolio/repository/PortfolioRepository.java b/src/main/java/com/gongjakso/server/domain/portfolio/repository/PortfolioRepository.java index 55408763..b49d6399 100644 --- a/src/main/java/com/gongjakso/server/domain/portfolio/repository/PortfolioRepository.java +++ b/src/main/java/com/gongjakso/server/domain/portfolio/repository/PortfolioRepository.java @@ -12,7 +12,7 @@ public interface PortfolioRepository extends JpaRepository, Por long countByDeletedAtIsNull(); @Query("SELECT EXISTS (" + "SELECT 1 FROM Portfolio po " + - "WHERE po.member = :member AND (po.fileUri IS NOT NULL OR po.notionUri IS NOT NULL))") + "WHERE po.member = :member AND (po.fileUri IS NOT NULL OR po.notionUri IS NOT NULL) AND po.deletedAt IS NULL )") Boolean existsExistPortfolioByMember(@Param("member") Member member); Optional findPortfolioById(Long portfolioId); } diff --git a/src/main/java/com/gongjakso/server/domain/portfolio/service/PortfolioService.java b/src/main/java/com/gongjakso/server/domain/portfolio/service/PortfolioService.java index 3c609334..c2ba8d14 100644 --- a/src/main/java/com/gongjakso/server/domain/portfolio/service/PortfolioService.java +++ b/src/main/java/com/gongjakso/server/domain/portfolio/service/PortfolioService.java @@ -195,7 +195,7 @@ public void deleteExistPortfolio(Member member, Long id){ if(!member.getId().equals(portfolio.getMember().getId())){ throw new ApplicationException(ErrorCode.UNAUTHORIZED_EXCEPTION); } - if(portfolio.getFileUri() != null){ + if(portfolio.getFileUri() != null&& !portfolio.getFileUri().isEmpty()){ s3Client.delete(portfolio.getFileUri()); } portfolioRepository.delete(portfolio); @@ -205,19 +205,19 @@ public void deleteExistPortfolio(Member member, Long id){ public void updateExistPortfolio(Member member, Long id, MultipartFile image, String notionUri){ //등록된 파일이나 노션 링크 있는지 확인 //Validation - System.out.println(id); - Portfolio portfolio = portfolioRepository.findById(id).orElseThrow(()-> new ApplicationException(ErrorCode.NOT_FOUND_EXCEPTION)); + Portfolio portfolio = portfolioRepository.findById(id).orElseThrow(()-> new ApplicationException(ErrorCode.PORTFOLIO_NOT_FOUND_EXCEPTION)); if(!member.getId().equals(portfolio.getMember().getId())){ throw new ApplicationException(ErrorCode.UNAUTHORIZED_EXCEPTION); } //Business String s3Url = null; if (image != null && !image.isEmpty()) { - s3Client.delete(portfolio.getFileUri()); + if(portfolio.getFileUri()!=null && !portfolio.getFileUri().isEmpty()){ + s3Client.delete(portfolio.getFileUri()); + } s3Url = s3Client.upload(image, S3_PORTFOLIO_DIR_NAME); } - System.out.println(notionUri); - portfolio.updateExistPortfolio(portfolio,s3Url,notionUri); + portfolio.updatePortfolioUri(portfolio,s3Url,notionUri); portfolioRepository.save(portfolio); } From 529a1b9350059e9dce5c33700d734417ab734563 Mon Sep 17 00:00:00 2001 From: sycuuui <102959791+sycuuui@users.noreply.github.com> Date: Sun, 22 Sep 2024 00:31:19 +0900 Subject: [PATCH 6/6] =?UTF-8?q?#213=20fix=20:=20image->file=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../portfolio/controller/PortfolioController.java | 8 ++++---- .../domain/portfolio/service/PortfolioService.java | 14 +++++++------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/gongjakso/server/domain/portfolio/controller/PortfolioController.java b/src/main/java/com/gongjakso/server/domain/portfolio/controller/PortfolioController.java index c459c5ba..b30c1e2f 100644 --- a/src/main/java/com/gongjakso/server/domain/portfolio/controller/PortfolioController.java +++ b/src/main/java/com/gongjakso/server/domain/portfolio/controller/PortfolioController.java @@ -64,8 +64,8 @@ public ApplicationResponse> getMyPortfolios(@Authentica @Operation(description = "포트폴리오 파일 및 링크 업로드 API") @PostMapping("/exist-protfolio") - public ApplicationResponse updateExistPortfolio(@AuthenticationPrincipal PrincipalDetails principalDetails, @RequestPart(required = false,name = "image") MultipartFile image, @RequestPart(required = false,name = "notionUri") String notionUri){ - portfolioService.saveExistPortfolio(principalDetails.getMember(),image,notionUri); + public ApplicationResponse updateExistPortfolio(@AuthenticationPrincipal PrincipalDetails principalDetails, @RequestPart(required = false,name = "file") MultipartFile file, @RequestPart(required = false,name = "notionUri") String notionUri){ + portfolioService.saveExistPortfolio(principalDetails.getMember(),file,notionUri); return ApplicationResponse.ok(); } @@ -78,8 +78,8 @@ public ApplicationResponse deleteExistPortfolio(@AuthenticationPrincipal P @Operation(description = "포트폴리오 파일 및 링크 업로드 업데이트 API") @PatchMapping("/exist-protfolio/{portfolio_id}") - public ApplicationResponse updateExistPortfolio(@AuthenticationPrincipal PrincipalDetails principalDetails,@PathVariable("portfolio_id") Long portfolioId, @RequestPart(required = false,name = "image") MultipartFile image, @RequestPart(required = false,name = "notionUri") String notionUri){ - portfolioService.updateExistPortfolio(principalDetails.getMember(),portfolioId,image,notionUri); + public ApplicationResponse updateExistPortfolio(@AuthenticationPrincipal PrincipalDetails principalDetails,@PathVariable("portfolio_id") Long portfolioId, @RequestPart(required = false,name = "file") MultipartFile file, @RequestPart(required = false,name = "notionUri") String notionUri){ + portfolioService.updateExistPortfolio(principalDetails.getMember(),portfolioId,file,notionUri); return ApplicationResponse.ok(); } diff --git a/src/main/java/com/gongjakso/server/domain/portfolio/service/PortfolioService.java b/src/main/java/com/gongjakso/server/domain/portfolio/service/PortfolioService.java index c2ba8d14..6e19868e 100644 --- a/src/main/java/com/gongjakso/server/domain/portfolio/service/PortfolioService.java +++ b/src/main/java/com/gongjakso/server/domain/portfolio/service/PortfolioService.java @@ -170,20 +170,20 @@ public List getMyPortfolios(Member member) { } @Transactional - public void saveExistPortfolio(Member member, MultipartFile image, String notionUri){ + public void saveExistPortfolio(Member member, MultipartFile file, String notionUri){ //등록된 파일이나 노션 링크 있는지 확인 //Validation Boolean isExist = portfolioRepository.existsExistPortfolioByMember(member); if (isExist){ throw new ApplicationException(ErrorCode.ALREADY_EXIST_EXCEPTION); } - if(image.isEmpty() && notionUri.isEmpty()){ + if(file.isEmpty() && notionUri.isEmpty()){ throw new ApplicationException(ErrorCode.PORTFOLIO_SAVE_FAILED_EXCEPTION); } //Business String s3Url = null; - if (image != null && !image.isEmpty()) { - s3Url = s3Client.upload(image, S3_PORTFOLIO_DIR_NAME); + if (file != null && !file.isEmpty()) { + s3Url = s3Client.upload(file, S3_PORTFOLIO_DIR_NAME); } Portfolio portfolio = new Portfolio(member,generatePortfolioName(null),s3Url,notionUri); portfolioRepository.save(portfolio); @@ -202,7 +202,7 @@ public void deleteExistPortfolio(Member member, Long id){ } @Transactional - public void updateExistPortfolio(Member member, Long id, MultipartFile image, String notionUri){ + public void updateExistPortfolio(Member member, Long id, MultipartFile file, String notionUri){ //등록된 파일이나 노션 링크 있는지 확인 //Validation Portfolio portfolio = portfolioRepository.findById(id).orElseThrow(()-> new ApplicationException(ErrorCode.PORTFOLIO_NOT_FOUND_EXCEPTION)); @@ -211,11 +211,11 @@ public void updateExistPortfolio(Member member, Long id, MultipartFile image, St } //Business String s3Url = null; - if (image != null && !image.isEmpty()) { + if (file != null && !file.isEmpty()) { if(portfolio.getFileUri()!=null && !portfolio.getFileUri().isEmpty()){ s3Client.delete(portfolio.getFileUri()); } - s3Url = s3Client.upload(image, S3_PORTFOLIO_DIR_NAME); + s3Url = s3Client.upload(file, S3_PORTFOLIO_DIR_NAME); } portfolio.updatePortfolioUri(portfolio,s3Url,notionUri); portfolioRepository.save(portfolio);