diff --git a/src/main/java/com/hobak/happinessql/domain/activity/api/ActivityController.java b/src/main/java/com/hobak/happinessql/domain/activity/api/ActivityController.java index 474d8f8..749e6d5 100644 --- a/src/main/java/com/hobak/happinessql/domain/activity/api/ActivityController.java +++ b/src/main/java/com/hobak/happinessql/domain/activity/api/ActivityController.java @@ -5,6 +5,8 @@ import com.hobak.happinessql.domain.activity.dto.*; import com.hobak.happinessql.domain.user.application.UserFindService; import com.hobak.happinessql.domain.user.domain.User; +import com.hobak.happinessql.global.exception.GeneralException; +import com.hobak.happinessql.global.response.Code; import com.hobak.happinessql.global.response.DataResponseDto; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; @@ -46,17 +48,30 @@ public DataResponseDto createActivity(@RequestBody Activity @Operation(summary = "활동 삭제", description = "기타 카테고리에 있는 활동을 삭제합니다.") @DeleteMapping("/{activityId}") - public DataResponseDto deleteActivity(@PathVariable Long activityId){ - activityDeleteService.deleteActivity(activityId); - ActivityResponseDto responseDto = ActivityConverter.toActivityResponseDto(activityId); - return DataResponseDto.of(responseDto, "활동을 성공적으로 삭제했습니다."); + public DataResponseDto deleteActivity(@PathVariable Long activityId, @AuthenticationPrincipal UserDetails userDetails){ + User user = userFindService.findByUserDetails(userDetails); + Long deleteActivityId = activityDeleteService.deleteActivity(activityId, user); + if(deleteActivityId != null) { + ActivityResponseDto responseDto = ActivityConverter.toActivityResponseDto(deleteActivityId); + return DataResponseDto.of(responseDto, "활동을 성공적으로 삭제했습니다."); + } + else { + throw new GeneralException(Code.INTERNAL_ERROR,"본인이 추가하지 않은 활동은 삭제가 불가합니다."); + } + } @Operation(summary = "활동 수정", description = "기타 카테고리에 있는 활동을 수정합니다.") @PutMapping("/{activityId}") - public DataResponseDto updateActicity(@PathVariable Long activityId, @RequestBody ActivityUpdateRequestDto requestDto){ - ActivityUpdateResponseDto responseDto = activityUpdateService.updateActivity(activityId,requestDto); - return DataResponseDto.of(responseDto,"활동을 성공적으로 수정했습니다."); + public DataResponseDto updateActivity(@PathVariable Long activityId, @RequestBody ActivityUpdateRequestDto requestDto, @AuthenticationPrincipal UserDetails userDetails){ + User user = userFindService.findByUserDetails(userDetails); + ActivityUpdateResponseDto responseDto = activityUpdateService.updateActivity(activityId,requestDto, user); + if(responseDto != null){ + return DataResponseDto.of(responseDto,"활동을 성공적으로 수정했습니다."); + } + else { + throw new GeneralException(Code.INTERNAL_ERROR,"본인이 추가하지 않은 활동은 수정이 불가합니다."); + } } @Operation(summary = "활동 검색", description = "유저가 갖고 있는 활동을 검색합니다. (활동의 description에 있는 내용은 아직 검색되지 않습니다.)") diff --git a/src/main/java/com/hobak/happinessql/domain/activity/application/ActivityDeleteService.java b/src/main/java/com/hobak/happinessql/domain/activity/application/ActivityDeleteService.java index ab3d3d8..289c708 100644 --- a/src/main/java/com/hobak/happinessql/domain/activity/application/ActivityDeleteService.java +++ b/src/main/java/com/hobak/happinessql/domain/activity/application/ActivityDeleteService.java @@ -4,6 +4,7 @@ import com.hobak.happinessql.domain.activity.domain.Activity; import com.hobak.happinessql.domain.activity.exception.ActivityNotFoundException; import com.hobak.happinessql.domain.activity.repository.ActivityRepository; +import com.hobak.happinessql.domain.user.domain.User; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -13,9 +14,13 @@ @RequiredArgsConstructor public class ActivityDeleteService { private final ActivityRepository activityRepository; - public void deleteActivity(Long activityId){ + public Long deleteActivity(Long activityId, User user){ Activity activity = activityRepository.findById(activityId) .orElseThrow(()->new ActivityNotFoundException("Activity with id " + activityId)); - activityRepository.delete(activity); + if(activity.getCategory().getUserId().equals(user.getUserId())) { + activityRepository.delete(activity); + return activity.getActivityId(); + } + return null; } } diff --git a/src/main/java/com/hobak/happinessql/domain/activity/application/ActivityUpdateService.java b/src/main/java/com/hobak/happinessql/domain/activity/application/ActivityUpdateService.java index a7e2592..06102fc 100644 --- a/src/main/java/com/hobak/happinessql/domain/activity/application/ActivityUpdateService.java +++ b/src/main/java/com/hobak/happinessql/domain/activity/application/ActivityUpdateService.java @@ -6,6 +6,7 @@ import com.hobak.happinessql.domain.activity.dto.ActivityUpdateResponseDto; import com.hobak.happinessql.domain.activity.exception.ActivityNotFoundException; import com.hobak.happinessql.domain.activity.repository.ActivityRepository; +import com.hobak.happinessql.domain.user.domain.User; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -16,11 +17,14 @@ public class ActivityUpdateService { private final ActivityRepository activityRepository; - public ActivityUpdateResponseDto updateActivity(Long activityId, ActivityUpdateRequestDto requestDto){ + public ActivityUpdateResponseDto updateActivity(Long activityId, ActivityUpdateRequestDto requestDto, User user){ Activity activity = activityRepository.findById(activityId) .orElseThrow(()-> new ActivityNotFoundException("Activity with id " + activityId)); - activity.updateName(requestDto.getName()); - return ActivityConverter.toActivityUpdateResponseDto(activity); + if(activity.getCategory().getUserId().equals(user.getUserId())) { + activity.updateName(requestDto.getName()); + return ActivityConverter.toActivityUpdateResponseDto(activity); + } + return null; } }