From 4bf463cc98e447bbcb4f061cfd40e247fdcd0d48 Mon Sep 17 00:00:00 2001 From: hyeonda02 Date: Sun, 4 Aug 2024 01:34:46 +0900 Subject: [PATCH] =?UTF-8?q?Feat=20:=20Career=20Detail=20=ED=99=9C=EB=8F=99?= =?UTF-8?q?=EA=B8=B0=EB=A1=9D=20=EC=88=98=EC=A0=95=20API=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/CareerDetailController.java | 15 ++++++- .../careerdetail/domain/CareerDetail.java | 20 +++++++++ .../domain/mapping/CareerTag.java | 6 ++- .../dto/CareerDetailRequestDto.java | 24 ++++++++++ .../repository/CareerTagRepository.java | 7 +++ .../service/CareerDetailService.java | 1 + .../service/CareerDetailServiceImpl.java | 44 ++++++++++++++++--- 7 files changed, 108 insertions(+), 9 deletions(-) create mode 100644 src/main/java/umc/kkijuk/server/careerdetail/repository/CareerTagRepository.java diff --git a/src/main/java/umc/kkijuk/server/careerdetail/controller/CareerDetailController.java b/src/main/java/umc/kkijuk/server/careerdetail/controller/CareerDetailController.java index b8cb0d66..7a5395c1 100644 --- a/src/main/java/umc/kkijuk/server/careerdetail/controller/CareerDetailController.java +++ b/src/main/java/umc/kkijuk/server/careerdetail/controller/CareerDetailController.java @@ -37,7 +37,7 @@ public CareerDetailResponse create(@ } @DeleteMapping("/{careerId}/{detailId}") - @Operation(summary = "활동 기록을 삭제", description = "활동 기록 ID에 해당하는 활동을 삭제합니다.") + @Operation(summary = "활동 기록 삭제", description = "활동 기록 ID에 해당하는 활동을 삭제합니다.") @Parameters({ @Parameter(name = "careerId", description = "활동 Id, path variable 입니다."), @Parameter(name = "detailId", description = "활동 기록 Id, path variable 입니다.") @@ -46,5 +46,18 @@ public CareerDetailResponse delete(@PathVariable Long careerId, @PathVar careerDetailService.delete(requestMember, detailId); return CareerDetailResponse.success(HttpStatus.OK, "활동 기록을 성공적으로 삭제했습니다.",null); } + @PutMapping("/{careerId}/{detailId}") + @Operation(summary = "활동 기록 수정",description = "주어진 정보를 바탕으로 활동 기록 ID에 해당하는 활동을 수정합니다. ") + @Parameters({ + @Parameter(name = "careerId",description = "활동 Id, path variable 입니다."), + @Parameter(name = "detailId", description = "활동 기록 Id, path variable 입니다. ") + }) + public CareerDetailResponse update(@PathVariable Long careerId, + @PathVariable Long detailId, + @RequestBody @Valid CareerDetailRequestDto.CareerDetailUpdate request){ + CareerDetail updateCareerDetail = careerDetailService.update(requestMember, request, careerId ,detailId); + return CareerDetailResponse.success(HttpStatus.OK, "활동 기록을 성공적으로 수정했습니다.", CareerDetailConverter.toCareerDetailResult(updateCareerDetail)); + + } } diff --git a/src/main/java/umc/kkijuk/server/careerdetail/domain/CareerDetail.java b/src/main/java/umc/kkijuk/server/careerdetail/domain/CareerDetail.java index 9d6cc259..5baf308f 100644 --- a/src/main/java/umc/kkijuk/server/careerdetail/domain/CareerDetail.java +++ b/src/main/java/umc/kkijuk/server/careerdetail/domain/CareerDetail.java @@ -44,4 +44,24 @@ public void setCareer(Career career) { this.career = career; } + public void setTitle(String title) { + this.title = title; + } + + public void setContent(String content) { + this.content = content; + } + + public void setStartDate(LocalDate startDate) { + this.startDate = startDate; + } + + public void setEndDate(LocalDate endDate) { + this.endDate = endDate; + } + + public void setMemberId(Long memberId) { + this.memberId = memberId; + } + } diff --git a/src/main/java/umc/kkijuk/server/careerdetail/domain/mapping/CareerTag.java b/src/main/java/umc/kkijuk/server/careerdetail/domain/mapping/CareerTag.java index 1e2c8266..e21102af 100644 --- a/src/main/java/umc/kkijuk/server/careerdetail/domain/mapping/CareerTag.java +++ b/src/main/java/umc/kkijuk/server/careerdetail/domain/mapping/CareerTag.java @@ -26,10 +26,12 @@ public class CareerTag { public void setCareerDetail(CareerDetail careerDetail) { if (this.careerDetail != null) { - careerDetail.getCareerTagList().remove(this); + this.careerDetail.getCareerTagList().remove(this); } this.careerDetail = careerDetail; - careerDetail.getCareerTagList().add(this); + if (careerDetail != null) { + careerDetail.getCareerTagList().add(this); + } } diff --git a/src/main/java/umc/kkijuk/server/careerdetail/dto/CareerDetailRequestDto.java b/src/main/java/umc/kkijuk/server/careerdetail/dto/CareerDetailRequestDto.java index 4fcb9cec..97c9f906 100644 --- a/src/main/java/umc/kkijuk/server/careerdetail/dto/CareerDetailRequestDto.java +++ b/src/main/java/umc/kkijuk/server/careerdetail/dto/CareerDetailRequestDto.java @@ -39,4 +39,28 @@ public static class CareerDetailCreate{ @Schema(description = "태그 리스트") List tagList; } + + @Getter + @Builder + public static class CareerDetailUpdate { + @Size(max = 30) + @Schema(description = "활동 기록 제목", example = "수정된 아이디어톤", type="string") + String title; + + @Size(max = 800) + @Schema(description = "활동 기록 내용", example = "수정된 기획한 웹/앱 서비스를 발표하고 피드백을 교환함 투표 결과 우수상 수상", type="string") + String content; + + @JsonFormat(shape=JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", timezone = "Asia/Seoul") + @Schema(description = "활동 기록 시작 날짜", example = "2024-01-12", type="string") + LocalDate startDate; + + @JsonFormat(shape=JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", timezone = "Asia/Seoul") + @Schema(description = "활동 기록 종료 날짜", example = "2024-02-22", type="string") + LocalDate endDate; + + @Schema(description = "태그 리스트") + List tagList; + } + } diff --git a/src/main/java/umc/kkijuk/server/careerdetail/repository/CareerTagRepository.java b/src/main/java/umc/kkijuk/server/careerdetail/repository/CareerTagRepository.java new file mode 100644 index 00000000..7dccb899 --- /dev/null +++ b/src/main/java/umc/kkijuk/server/careerdetail/repository/CareerTagRepository.java @@ -0,0 +1,7 @@ +package umc.kkijuk.server.careerdetail.repository; + +import org.springframework.data.jpa.repository.JpaRepository; +import umc.kkijuk.server.careerdetail.domain.mapping.CareerTag; + +public interface CareerTagRepository extends JpaRepository { +} diff --git a/src/main/java/umc/kkijuk/server/careerdetail/service/CareerDetailService.java b/src/main/java/umc/kkijuk/server/careerdetail/service/CareerDetailService.java index 2e2b2790..3d76d618 100644 --- a/src/main/java/umc/kkijuk/server/careerdetail/service/CareerDetailService.java +++ b/src/main/java/umc/kkijuk/server/careerdetail/service/CareerDetailService.java @@ -7,5 +7,6 @@ public interface CareerDetailService { CareerDetail create(Member member, CareerDetailRequestDto.CareerDetailCreate request, Long careerId); void delete(Member member, Long careerDetailId); + CareerDetail update(Member member,CareerDetailRequestDto.CareerDetailUpdate request, Long careerId, Long detailId); } diff --git a/src/main/java/umc/kkijuk/server/careerdetail/service/CareerDetailServiceImpl.java b/src/main/java/umc/kkijuk/server/careerdetail/service/CareerDetailServiceImpl.java index 2c2367bf..c5c2d977 100644 --- a/src/main/java/umc/kkijuk/server/careerdetail/service/CareerDetailServiceImpl.java +++ b/src/main/java/umc/kkijuk/server/careerdetail/service/CareerDetailServiceImpl.java @@ -11,12 +11,14 @@ import umc.kkijuk.server.careerdetail.dto.converter.CareerDetailConverter; import umc.kkijuk.server.careerdetail.dto.converter.CareerTagConverter; import umc.kkijuk.server.careerdetail.repository.CareerDetailRepository; +import umc.kkijuk.server.careerdetail.repository.CareerTagRepository; import umc.kkijuk.server.common.domian.exception.OwnerMismatchException; import umc.kkijuk.server.common.domian.exception.ResourceNotFoundException; import umc.kkijuk.server.member.domain.Member; import umc.kkijuk.server.tag.domain.Tag; import umc.kkijuk.server.tag.repository.TagRepository; +import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; @@ -27,6 +29,7 @@ public class CareerDetailServiceImpl implements CareerDetailService{ private final CareerRepository careerRepository; private final TagRepository tagRepository; private final CareerDetailRepository careerDetailRepository; + private final CareerTagRepository careerTagRepositosy; @Override @Transactional @@ -40,22 +43,51 @@ public CareerDetail create(Member requestMember, CareerDetailRequestDto.CareerDe newCareerDetail.setCareer(career); career.getCareerDetailList().add(newCareerDetail); - List tagList = request.getTagList().stream().map(tagId -> { - return tagRepository.findById(tagId).orElseThrow(() -> new ResourceNotFoundException("Tag", tagId)); - }).collect(Collectors.toList()); - List careerTagList = CareerTagConverter.toCareerTagList(tagList); + List careerTagList = returnCareerTagList(request.getTagList()); careerTagList.forEach(careerTag -> careerTag.setCareerDetail(newCareerDetail)); return careerDetailRepository.save(newCareerDetail); } + @Override @Transactional - public void delete(Member reqeustMember ,Long careerDetailId) { - CareerDetail careerDetail = careerDetailRepository.findById(careerDetailId).orElseThrow(() -> new ResourceNotFoundException("CareerDetail", careerDetailId)); + public void delete(Member reqeustMember ,Long detailId) { + CareerDetail careerDetail = careerDetailRepository.findById(detailId).orElseThrow(() -> new ResourceNotFoundException("CareerDetail", detailId)); if(!careerDetail.getMemberId().equals(reqeustMember.getId())){ throw new OwnerMismatchException(); } careerDetailRepository.delete(careerDetail); } + @Override + @Transactional + public CareerDetail update(Member requestMember, CareerDetailRequestDto.CareerDetailUpdate request, Long careerId, Long detailId){ + CareerDetail updateCareerDetail = careerDetailRepository.findById(detailId).orElseThrow(() -> new ResourceNotFoundException("CareerDetail", detailId)); + if(!updateCareerDetail.getMemberId().equals(requestMember.getId())){ + throw new OwnerMismatchException(); + } + + List existTags = new ArrayList<>(updateCareerDetail.getCareerTagList()); + updateCareerDetail.getCareerTagList().clear(); + existTags.forEach(careerTag -> careerTagRepositosy.delete(careerTag)); + + updateCareerDetail.setTitle(request.getTitle()); + updateCareerDetail.setContent(request.getContent()); + updateCareerDetail.setStartDate(request.getStartDate()); + updateCareerDetail.setEndDate(request.getEndDate()); + + List careerTagList = returnCareerTagList(request.getTagList()); + careerTagList.forEach(careerTag -> careerTag.setCareerDetail(updateCareerDetail)); + + return careerDetailRepository.save(updateCareerDetail); + + + } + private List returnCareerTagList(List tagIdList) { + List tagList = tagIdList.stream().map(tagId -> { + return tagRepository.findById(tagId).orElseThrow(() -> new ResourceNotFoundException("Tag", tagId)); + }).collect(Collectors.toList()); + List careerTagList = CareerTagConverter.toCareerTagList(tagList); + return careerTagList; + } }