Skip to content

Commit

Permalink
Feat : Career Detail 활동기록 수정 API 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
hyeonda02 committed Aug 3, 2024
1 parent a2e69c1 commit 4bf463c
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public CareerDetailResponse<CareerDetailResponseDto.CareerDetailResult> 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 입니다.")
Expand All @@ -46,5 +46,18 @@ public CareerDetailResponse<Object> 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<Object> 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));

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,28 @@ public static class CareerDetailCreate{
@Schema(description = "태그 리스트")
List<Long> 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<Long> tagList;
}

}
Original file line number Diff line number Diff line change
@@ -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<CareerTag, Long> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand All @@ -40,22 +43,51 @@ public CareerDetail create(Member requestMember, CareerDetailRequestDto.CareerDe
newCareerDetail.setCareer(career);
career.getCareerDetailList().add(newCareerDetail);

List<Tag> tagList = request.getTagList().stream().map(tagId -> {
return tagRepository.findById(tagId).orElseThrow(() -> new ResourceNotFoundException("Tag", tagId));
}).collect(Collectors.toList());
List<CareerTag> careerTagList = CareerTagConverter.toCareerTagList(tagList);
List<CareerTag> 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<CareerTag> 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<CareerTag> careerTagList = returnCareerTagList(request.getTagList());
careerTagList.forEach(careerTag -> careerTag.setCareerDetail(updateCareerDetail));

return careerDetailRepository.save(updateCareerDetail);


}
private List<CareerTag> returnCareerTagList(List<Long> tagIdList) {
List<Tag> tagList = tagIdList.stream().map(tagId -> {
return tagRepository.findById(tagId).orElseThrow(() -> new ResourceNotFoundException("Tag", tagId));
}).collect(Collectors.toList());
List<CareerTag> careerTagList = CareerTagConverter.toCareerTagList(tagList);
return careerTagList;
}
}

0 comments on commit 4bf463c

Please sign in to comment.