Skip to content

Commit

Permalink
Merge pull request #557 from tukcomCD2024/feat/delete_capsule-B-#552
Browse files Browse the repository at this point in the history
feat : 캡슐 삭제 기능 구현
  • Loading branch information
GaBaljaintheroom authored Jul 8, 2024
2 parents 5936c33 + b255b4a commit 07bb758
Show file tree
Hide file tree
Showing 33 changed files with 190 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,12 @@
import java.security.SecureRandom;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import site.timecapsulearchive.core.domain.auth.data.dto.VerificationMessageSendDto;
import site.timecapsulearchive.core.domain.auth.exception.CertificationNumberNotFoundException;
import site.timecapsulearchive.core.domain.auth.exception.CertificationNumberNotMatchException;
import site.timecapsulearchive.core.domain.auth.repository.MessageAuthenticationCacheRepository;
import site.timecapsulearchive.core.domain.member.entity.Member;
import site.timecapsulearchive.core.domain.member.entity.MemberTemporary;
import site.timecapsulearchive.core.domain.member.exception.MemberNotFoundException;
import site.timecapsulearchive.core.domain.member.exception.MemberTagDuplicatedException;
import site.timecapsulearchive.core.domain.member.repository.MemberRepository;
import site.timecapsulearchive.core.domain.member.repository.MemberTemporaryRepository;
import site.timecapsulearchive.core.global.security.encryption.AESEncryptionManager;
import site.timecapsulearchive.core.global.security.encryption.HashEncryptionManager;
import site.timecapsulearchive.core.infra.sms.data.response.SmsApiResponse;
import site.timecapsulearchive.core.infra.sms.manager.SmsApiManager;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package site.timecapsulearchive.core.domain.capsule.entity;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
Expand Down Expand Up @@ -40,45 +39,33 @@
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Capsule extends BaseEntity {

@OneToMany(mappedBy = "capsule")
private final List<Image> images = new ArrayList<>();
@OneToMany(mappedBy = "capsule")
private final List<Video> videos = new ArrayList<>();
@OneToMany(mappedBy = "capsule")
private final List<GroupCapsuleOpen> groupCapsuleOpens = new ArrayList<>();
@Id
@Column(name = "capsule_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(name = "due_date")
private ZonedDateTime dueDate;

@Column(name = "point", columnDefinition = "SRID 3857")
private Point point;

@Column(name = "title", nullable = false)
private String title;

@Column(name = "content", nullable = false)
private String content;

@Column(name = "type", nullable = false)
@Enumerated(EnumType.STRING)
private CapsuleType type;

@Column(name = "is_opened", nullable = false)
private Boolean isOpened;

@Embedded
private Address address;

@Column(name = "declaration_count")
private Long declarationCount = 0L;

@OneToMany(mappedBy = "capsule")
private final List<Image> images = new ArrayList<>();

@OneToMany(mappedBy = "capsule")
private final List<Video> videos = new ArrayList<>();

@OneToMany(mappedBy = "capsule")
private final List<GroupCapsuleOpen> groupCapsuleOpens = new ArrayList<>();

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "group_id")
private Group group;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ public enum CapsuleType {
TREASURE,
ALL;

public boolean isGroupCapsule() {
return this.equals(CapsuleType.GROUP);
}

public boolean isPublicOrGroup() {
return this == PUBLIC || this == GROUP;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,38 @@ ResponseEntity<ApiSpec<CapsuleOpenedResponse>> updateCapsuleOpened(
Long capsuleId
);

@Operation(
summary = "캡슐 삭제",
description = "캡슐을 삭제한다.",
security = {@SecurityRequirement(name = "user_token")},
tags = {"capsule"}
)
@ApiResponses(value = {
@ApiResponse(
responseCode = "200",
description = "처리 완료"
),
@ApiResponse(
responseCode = "404",
description = "해당 캡슐을 찾을 수 없을 경우 발생하는 예외",
content = @Content(schema = @Schema(implementation = ErrorResponse.class))
),
@ApiResponse(
responseCode = "403",
description = "그룹 캡슐일 경우 그룹장이 아닐 때 발생하는 예외",
content = @Content(schema = @Schema(implementation = ErrorResponse.class))
)
})
ResponseEntity<ApiSpec<String>> deleteCapsule(
Long memberId,

@Parameter(in = ParameterIn.PATH, description = "캡슐 아이디", required = true)
Long capsuleId,

@Parameter(in = ParameterIn.PATH, description = "캡슐 타입", required = true)
CapsuleType capsuleType
);

@Operation(
summary = "공개, 그룹 캡슐 신고",
description = "공개, 그룹 캡슐을 신고한다.",
Expand Down Expand Up @@ -226,4 +258,3 @@ ResponseEntity<ApiSpec<String>> declarationCapsule(
);

}

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
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.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
Expand Down Expand Up @@ -151,6 +152,21 @@ public ResponseEntity<ApiSpec<CapsuleOpenedResponse>> updateCapsuleOpened(
);
}

@DeleteMapping(value = "/{capsule_id}/{capsule_type}", produces = {"application/json"})
@Override
public ResponseEntity<ApiSpec<String>> deleteCapsule(
@AuthenticationPrincipal final Long memberId,
@PathVariable("capsule_id") final Long capsuleId,
@PathVariable("capsule_type") final CapsuleType capsuleType
) {
capsuleService.deleteCapsule(memberId, capsuleId, capsuleType);
return ResponseEntity.ok(
ApiSpec.empty(
SuccessCode.SUCCESS
)
);
}

@PatchMapping(value = "/{capsule_id}/declaration", produces = {"application/json"})
@Override
public ResponseEntity<ApiSpec<String>> declarationCapsule(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,12 @@ void deleteExcludeGroupCapsuleByMemberId(

@Query("select c from Capsule c where c.group.id in :groupIds")
List<Capsule> findCapsulesByGroupIds(@Param("groupIds") List<Long> groupIds);

@Query("UPDATE Capsule c SET c.deletedAt = :deletedAt WHERE c.member.id = :memberId and c.id = :capsuleId")
@Modifying
void deleteByMemberIdAndCapsuleId(
@Param("memberId") Long memberId,
@Param("capsuleId") Long capsuleId,
@Param("deletedAt") ZonedDateTime deletedAt
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,12 @@ void deleteByCapsuleIds(
@Param("groupCapsuleIds") List<Long> groupCapsuleIds,
@Param("deletedAt") ZonedDateTime deletedAt
);

@Query("UPDATE Image i SET i.deletedAt = :deletedAt WHERE i.member.id =:memberId and i.capsule.id =:capsuleId")
@Modifying
void deleteByMemberIdAndCapsuleId(
@Param("memberId") Long memberId,
@Param("capsuleId") Long capsuleId,
@Param("deletedAt") ZonedDateTime deletedAt
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,12 @@ void deleteByCapsuleIds(
@Param("groupCapsuleIds") List<Long> groupCapsuleIds,
@Param("deletedAt") ZonedDateTime deletedAt
);

@Query("UPDATE Video v SET v.deletedAt = :deletedAt WHERE v.member.id = :memberId and v.capsule.id = :capsuleId")
@Modifying
void deleteByMemberIdAndCapsuleId(
@Param("memberId") Long memberId,
@Param("capsuleId") Long capsuleId,
@Param("deletedAt") ZonedDateTime deletedAt
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@
import site.timecapsulearchive.core.domain.capsule.generic_capsule.repository.capsule.CapsuleRepository;
import site.timecapsulearchive.core.domain.capsule.generic_capsule.repository.image.ImageRepository;
import site.timecapsulearchive.core.domain.capsule.generic_capsule.repository.video.VideoRepository;
import site.timecapsulearchive.core.domain.capsule.group_capsule.repository.GroupCapsuleOpenRepository;
import site.timecapsulearchive.core.domain.capsuleskin.entity.CapsuleSkin;
import site.timecapsulearchive.core.domain.capsuleskin.repository.CapsuleSkinRepository;
import site.timecapsulearchive.core.domain.friend.repository.member_friend.MemberFriendRepository;
import site.timecapsulearchive.core.domain.member.entity.Member;
import site.timecapsulearchive.core.domain.member_group.entity.MemberGroup;
import site.timecapsulearchive.core.domain.member_group.repository.member_group_repository.MemberGroupRepository;
import site.timecapsulearchive.core.global.common.supplier.ZonedDateTimeSupplier;
import site.timecapsulearchive.core.global.geography.GeoTransformManager;

@Service
Expand All @@ -30,6 +34,8 @@ public class CapsuleService {

private final CapsuleRepository capsuleRepository;
private final MemberFriendRepository memberFriendRepository;
private final MemberGroupRepository memberGroupRepository;
private final GroupCapsuleOpenRepository groupCapsuleOpenRepository;
private final GeoTransformManager geoTransformManager;
private final ImageRepository imageRepository;
private final VideoRepository videoRepository;
Expand Down Expand Up @@ -152,15 +158,51 @@ public List<NearbyARCapsuleSummaryDto> findFriendsARCapsulesByCurrentLocation(
}

@Transactional
public void deleteRelatedAllCapsuleByMemberId(final Long memberId,
final ZonedDateTime deletedAt) {
public void deleteRelatedAllCapsuleByMemberId(
final Long memberId,
final ZonedDateTime deletedAt
) {
imageRepository.deleteByMemberId(memberId, deletedAt);
videoRepository.deleteByMemberId(memberId, deletedAt);
capsuleSkinRepository.deleteByMemberId(memberId, deletedAt);
capsuleRepository.deleteExcludeGroupCapsuleByMemberId(memberId, deletedAt);
}

@Transactional
public void deleteCapsule(
final Long memberId,
final Long capsuleId,
final CapsuleType capsuleType
) {
final ZonedDateTime deletedAt = ZonedDateTimeSupplier.utc().get();

if (capsuleType.isGroupCapsule()) {
validateGroupCapsuleOwnership(memberId, capsuleId);
}

deleteCapsuleAssets(memberId, capsuleId, deletedAt);
}

private void validateGroupCapsuleOwnership(final Long memberId, final Long capsuleId) {
final Capsule capsule = capsuleRepository.findCapsuleByMemberIdAndCapsuleId(memberId,
capsuleId)
.orElseThrow(CapsuleNotFondException::new);
final Long groupId = capsule.getGroup().getId();

final MemberGroup memberGroup = memberGroupRepository.findMemberGroupByMemberIdAndGroupId(
memberId, groupId)
.orElseThrow(CapsuleNotFondException::new);
memberGroup.checkDeleteGroupCapsuleAuthority();
}

private void deleteCapsuleAssets(final Long memberId, final Long capsuleId,
final ZonedDateTime deletedAt) {
imageRepository.deleteByMemberIdAndCapsuleId(memberId, capsuleId, deletedAt);
videoRepository.deleteByMemberIdAndCapsuleId(memberId, capsuleId, deletedAt);
groupCapsuleOpenRepository.deleteByMemberIdAndCapsuleId(memberId, capsuleId, deletedAt);
capsuleRepository.deleteByMemberIdAndCapsuleId(memberId, capsuleId, deletedAt);
}

public void declarationCapsule(final Long capsuleId) {
Capsule capsuleToDeclaration = capsuleRepository.findById(capsuleId)
.orElseThrow(CapsuleNotFondException::new);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.response.GroupCapsuleMembersResponse;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.response.GroupCapsuleOpenStateResponse;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.response.GroupCapsuleSliceResponse;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.response.GroupSpecificCapsuleSliceResponse;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.response.GroupCapsuleSummaryResponse;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.response.GroupSpecificCapsuleSliceResponse;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.response.MyGroupCapsuleSliceResponse;
import site.timecapsulearchive.core.global.common.response.ApiSpec;
import site.timecapsulearchive.core.global.error.ErrorResponse;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package site.timecapsulearchive.core.domain.capsule.group_capsule.api;

import jakarta.validation.Valid;
import jakarta.validation.constraints.Max;
import java.time.ZonedDateTime;
import java.util.List;
import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public record GroupCapsuleResponse(
@Schema(description = "캡슐 타입")
CapsuleType capsuleType
) {

public GroupCapsuleResponse {
if (dueDate != null) {
dueDate = dueDate.withZoneSameInstant(ResponseMappingConstant.ZONE_ID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,12 @@ void deleteByCapsuleIds(
@Param("groupCapsuleIds") List<Long> groupCapsuleIds,
@Param("deletedAt") ZonedDateTime deletedAt
);

@Query("UPDATE GroupCapsuleOpen gco SET gco.deletedAt = :deletedAt WHERE gco.member.id = :memberId and gco.capsule.id = :capsuleId")
@Modifying
void deleteByMemberIdAndCapsuleId(
@Param("memberId") Long memberId,
@Param("capsuleId") Long capsuleId,
@Param("deletedAt") ZonedDateTime deletedAt
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.dto.GroupCapsuleDetailDto;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.dto.GroupCapsuleDto;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.dto.GroupCapsuleMemberSummaryDto;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.dto.GroupSpecificCapsuleSliceRequestDto;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.dto.GroupCapsuleSummaryDto;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.dto.GroupCapsuleWithMemberDetailDto;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.dto.GroupSpecificCapsuleSliceRequestDto;
import site.timecapsulearchive.core.domain.member.entity.QMember;
import site.timecapsulearchive.core.global.util.SliceUtil;

Expand Down Expand Up @@ -177,7 +177,8 @@ public Long findGroupCapsuleCount(final Long groupId) {
.fetchOne();
}

public Slice<CapsuleBasicInfoDto> findGroupSpecificCapsuleSlice(final GroupSpecificCapsuleSliceRequestDto dto) {
public Slice<CapsuleBasicInfoDto> findGroupSpecificCapsuleSlice(
final GroupSpecificCapsuleSliceRequestDto dto) {
final List<CapsuleBasicInfoDto> groupCapsuleDtos = jpaQueryFactory
.select(
Projections.constructor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.dto.GroupCapsuleDto;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.dto.GroupCapsuleMemberDto;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.dto.GroupCapsuleOpenStateDto;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.dto.GroupSpecificCapsuleSliceRequestDto;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.dto.GroupCapsuleSummaryDto;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.dto.GroupCapsuleWithMemberDetailDto;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.dto.GroupSpecificCapsuleSliceRequestDto;
import site.timecapsulearchive.core.domain.capsule.group_capsule.repository.GroupCapsuleOpenRepository;
import site.timecapsulearchive.core.domain.capsule.group_capsule.repository.GroupCapsuleQueryRepository;
import site.timecapsulearchive.core.domain.capsuleskin.entity.CapsuleSkin;
Expand Down Expand Up @@ -201,7 +201,8 @@ public Slice<GroupCapsuleDto> findGroupCapsuleSlice(
groupIds);
}

public Slice<CapsuleBasicInfoDto> findGroupSpecificCapsuleSlice(final GroupSpecificCapsuleSliceRequestDto dto) {
public Slice<CapsuleBasicInfoDto> findGroupSpecificCapsuleSlice(
final GroupSpecificCapsuleSliceRequestDto dto) {
checkGroupAuthority(dto.memberId(), dto.groupId());

return groupCapsuleQueryRepository.findGroupSpecificCapsuleSlice(dto);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
import java.time.ZonedDateTime;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.SliceImpl;
import org.springframework.stereotype.Repository;
import site.timecapsulearchive.core.domain.capsuleskin.data.dto.CapsuleSkinSummaryDto;
import site.timecapsulearchive.core.global.util.SliceUtil;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import site.timecapsulearchive.core.domain.member.data.reqeust.UpdateMemberPhoneSearchAvailableRequest;
import site.timecapsulearchive.core.domain.member.data.reqeust.UpdateMemberTagSearchAvailableRequest;
import site.timecapsulearchive.core.domain.auth.data.request.VerificationMessageSendRequest;
import site.timecapsulearchive.core.domain.auth.data.request.VerificationNumberValidRequest;
import site.timecapsulearchive.core.domain.member.data.dto.MemberDetailDto;
import site.timecapsulearchive.core.domain.member.data.dto.MemberStatusDto;
import site.timecapsulearchive.core.domain.member.data.reqeust.CheckStatusRequest;
import site.timecapsulearchive.core.domain.member.data.reqeust.UpdateFCMTokenRequest;
import site.timecapsulearchive.core.domain.member.data.reqeust.UpdateMemberDataRequest;
import site.timecapsulearchive.core.domain.member.data.reqeust.UpdateMemberPhoneSearchAvailableRequest;
import site.timecapsulearchive.core.domain.member.data.reqeust.UpdateMemberTagSearchAvailableRequest;
import site.timecapsulearchive.core.domain.member.data.reqeust.UpdateNotificationEnabledRequest;
import site.timecapsulearchive.core.domain.member.data.response.MemberDetailResponse;
import site.timecapsulearchive.core.domain.member.data.response.MemberNotificationStatusResponse;
Expand Down
Loading

0 comments on commit 07bb758

Please sign in to comment.