Skip to content

Commit

Permalink
Feat: 사용자는 링크를 삭제할 수 있다.
Browse files Browse the repository at this point in the history
Feat: 사용자는 링크를 삭제할 수 있다.
  • Loading branch information
hseong3243 authored Apr 21, 2024
2 parents 00d9162 + ebf2c68 commit 494809e
Show file tree
Hide file tree
Showing 13 changed files with 347 additions and 141 deletions.
10 changes: 10 additions & 0 deletions src/docs/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ operation::link-controller-test/find-hub-links[snippets='http-request,request-he

operation::link-controller-test/find-hub-links[snippets='http-response,response-fields']

=== 회원 링크 삭제

==== request

operation::link-controller-test/delete-link[snippets='http-request,request-headers,path-parameters']

==== response

operation::link-controller-test/delete-link[snippets='http-response,response-fields']

== 허브

=== 허브 생성
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@
import com.seong.shoutlink.domain.link.service.LinkUseCase;
import com.seong.shoutlink.domain.link.service.request.CreateHubLinkCommand;
import com.seong.shoutlink.domain.link.service.request.CreateLinkCommand;
import com.seong.shoutlink.domain.link.service.request.DeleteLinkCommand;
import com.seong.shoutlink.domain.link.service.request.FindHubLinksCommand;
import com.seong.shoutlink.domain.link.service.request.FindLinksCommand;
import com.seong.shoutlink.domain.link.service.response.CreateHubLinkResponse;
import com.seong.shoutlink.domain.link.service.response.CreateLinkResponse;
import com.seong.shoutlink.domain.link.service.response.DeleteLinkResponse;
import com.seong.shoutlink.domain.link.service.response.FindLinksResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
Expand Down Expand Up @@ -82,4 +85,13 @@ public ResponseEntity<FindLinksResponse> findHubLinks(
request.size()));
return ResponseEntity.ok(response);
}

@DeleteMapping("/links/{linkId}")
public ResponseEntity<DeleteLinkResponse> deleteLink(
@LoginUser Long memberId,
@PathVariable("linkId") Long linkId) {
DeleteLinkResponse deleteLinkResponse = linkUseCase.deleteLink(
new DeleteLinkCommand(memberId, linkId));
return ResponseEntity.ok(deleteLinkResponse);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.seong.shoutlink.domain.domain.service.result.DomainLinkResult;
import java.util.List;
import java.util.Optional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
Expand All @@ -20,4 +21,9 @@ public interface LinkJpaRepository extends JpaRepository<LinkEntity, Long> {
Page<DomainLinkResult> findDomainLinks(@Param("domainId") Long domainId, Pageable pageable);

List<LinkEntity> findAllByLinkBundleIdIn(List<Long> linkBundleIds);

@Query("select l from LinkEntity l "
+ "join MemberLinkBundleEntity lb on l.linkBundleId = lb.linkBundleId "
+ "where l.linkId = :linkId and lb.memberId = :memberId")
Optional<LinkEntity> findByIdAndMemberId(@Param("linkId") Long linkId, @Param("memberId") Long memberId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.seong.shoutlink.domain.link.service.LinkRepository;
import com.seong.shoutlink.domain.link.service.result.LinkPaginationResult;
import com.seong.shoutlink.domain.linkbundle.LinkBundle;
import com.seong.shoutlink.domain.member.Member;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -68,4 +69,15 @@ public List<LinkWithLinkBundle> findAllByLinkBundlesIn(List<LinkBundle> linkBund
linkBundleIdAndLinkBundle.get(linkEntity.getLinkBundleId())))
.toList();
}

@Override
public Optional<Link> findMemberLink(Long linkId, Member member) {
return linkJpaRepository.findByIdAndMemberId(linkId, member.getMemberId())
.map(LinkEntity::toDomain);
}

@Override
public void delete(Link link) {
linkJpaRepository.deleteById(link.getLinkId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.seong.shoutlink.domain.link.LinkWithLinkBundle;
import com.seong.shoutlink.domain.link.service.result.LinkPaginationResult;
import com.seong.shoutlink.domain.linkbundle.LinkBundle;
import com.seong.shoutlink.domain.member.Member;
import java.util.List;
import java.util.Optional;

Expand All @@ -19,4 +20,8 @@ public interface LinkRepository {
Optional<Link> findById(Long linkId);

List<LinkWithLinkBundle> findAllByLinkBundlesIn(List<LinkBundle> linkBundles);

Optional<Link> findMemberLink(Long linkId, Member member);

void delete(Link link);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
import com.seong.shoutlink.domain.link.service.event.CreateMemberLinkEvent;
import com.seong.shoutlink.domain.link.service.request.CreateHubLinkCommand;
import com.seong.shoutlink.domain.link.service.request.CreateLinkCommand;
import com.seong.shoutlink.domain.link.service.request.DeleteLinkCommand;
import com.seong.shoutlink.domain.link.service.request.FindHubLinksCommand;
import com.seong.shoutlink.domain.link.service.request.FindLinksCommand;
import com.seong.shoutlink.domain.link.service.response.CreateHubLinkResponse;
import com.seong.shoutlink.domain.link.service.response.CreateLinkResponse;
import com.seong.shoutlink.domain.link.service.response.DeleteLinkResponse;
import com.seong.shoutlink.domain.link.service.response.FindLinksResponse;
import com.seong.shoutlink.domain.link.service.result.LinkPaginationResult;
import com.seong.shoutlink.domain.linkbundle.LinkBundle;
Expand Down Expand Up @@ -103,6 +105,15 @@ public FindLinksResponse findHubLinks(FindHubLinksCommand command) {
return FindLinksResponse.of(result.links(), result.totalElements(), result.hasNext());
}

@Override
public DeleteLinkResponse deleteLink(DeleteLinkCommand command) {
Member member = getMember(command.memberId());
Link link = linkRepository.findMemberLink(command.linkId(), member)
.orElseThrow(() -> new ShoutLinkException("존재하지 않는 링크입니다.", ErrorCode.NOT_FOUND));
linkRepository.delete(link);
return new DeleteLinkResponse(link.getLinkId());
}

private void checkAuthenticated(@Nullable Long nullableMemberId) {
if(Objects.isNull(nullableMemberId)) {
throw new ShoutLinkException("인증되지 않은 사용자입니다.", ErrorCode.UNAUTHENTICATED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import com.seong.shoutlink.domain.link.service.request.CreateHubLinkCommand;
import com.seong.shoutlink.domain.link.service.request.CreateLinkCommand;
import com.seong.shoutlink.domain.link.service.request.DeleteLinkCommand;
import com.seong.shoutlink.domain.link.service.request.FindHubLinksCommand;
import com.seong.shoutlink.domain.link.service.request.FindLinksCommand;
import com.seong.shoutlink.domain.link.service.response.CreateHubLinkResponse;
import com.seong.shoutlink.domain.link.service.response.CreateLinkResponse;
import com.seong.shoutlink.domain.link.service.response.DeleteLinkResponse;
import com.seong.shoutlink.domain.link.service.response.FindLinksResponse;

public interface LinkUseCase {
Expand All @@ -17,4 +19,6 @@ public interface LinkUseCase {
CreateHubLinkResponse createHubLink(CreateHubLinkCommand command);

FindLinksResponse findHubLinks(FindHubLinksCommand command);

DeleteLinkResponse deleteLink(DeleteLinkCommand command);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.seong.shoutlink.domain.link.service.request;

public record DeleteLinkCommand(Long memberId, Long linkId) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.seong.shoutlink.domain.link.service.response;

public record DeleteLinkResponse(Long linkId) {

}
Loading

0 comments on commit 494809e

Please sign in to comment.