Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: 허브 마스터는 허브 링크를 삭제할 수 있다. #96

Merged
merged 4 commits into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/docs/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,16 @@ operation::link-controller-test/delete-link[snippets='http-request,request-heade

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

=== 허브 링크 삭제

==== request

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

==== response

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

== 허브

=== 허브 생성
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/seong/shoutlink/domain/hub/Hub.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ private String validateDescription(String description) {
return description;
}

public void checkMasterAuthority(Long memberId) {
if(Objects.equals(masterId, memberId)) {
public void checkMasterAuthority(Member member) {
if(Objects.equals(masterId, member.getMemberId())) {
return;
}
throw new ShoutLinkException("권한이 없습니다.", ErrorCode.UNAUTHORIZED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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.DeleteHubLinkCommand;
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;
Expand Down Expand Up @@ -94,4 +95,14 @@ public ResponseEntity<DeleteLinkResponse> deleteLink(
new DeleteLinkCommand(memberId, linkId));
return ResponseEntity.ok(deleteLinkResponse);
}

@DeleteMapping("/hubs/{hubId}/links/{linkId}")
public ResponseEntity<DeleteLinkResponse> deleteHubLink(
@LoginUser Long memberId,
@PathVariable("hubId") Long hubId,
@PathVariable("linkId") Long linkId) {
DeleteLinkResponse response = linkUseCase.deleteHubLink(
new DeleteHubLinkCommand(linkId, memberId, hubId));
return ResponseEntity.ok(response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,9 @@ public interface LinkJpaRepository extends JpaRepository<LinkEntity, Long> {
+ "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);

@Query("select l from LinkEntity l "
+ "join HubLinkBundleEntity lb on l.linkBundleId = lb.linkBundleId "
+ "where l.linkId = :linkId and lb.hubId = :hubId")
Optional<LinkEntity> findByIdAndHubId(@Param("linkId") Long linkId, @Param("hubId") Long hubId);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.seong.shoutlink.domain.link.repository;

import com.seong.shoutlink.domain.domain.Domain;
import com.seong.shoutlink.domain.hub.Hub;
import com.seong.shoutlink.domain.link.Link;
import com.seong.shoutlink.domain.link.LinkWithLinkBundle;
import com.seong.shoutlink.domain.link.service.LinkRepository;
Expand Down Expand Up @@ -76,6 +77,12 @@ public Optional<Link> findMemberLink(Long linkId, Member member) {
.map(LinkEntity::toDomain);
}

@Override
public Optional<Link> findHubLink(Long linkId, Hub hub) {
return linkJpaRepository.findByIdAndHubId(linkId, hub.getHubId())
.map(LinkEntity::toDomain);
}

@Override
public void delete(Link link) {
linkJpaRepository.deleteById(link.getLinkId());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.seong.shoutlink.domain.link.service;

import com.seong.shoutlink.domain.domain.Domain;
import com.seong.shoutlink.domain.hub.Hub;
import com.seong.shoutlink.domain.link.Link;
import com.seong.shoutlink.domain.link.LinkWithLinkBundle;
import com.seong.shoutlink.domain.link.service.result.LinkPaginationResult;
Expand All @@ -23,5 +24,7 @@ public interface LinkRepository {

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

Optional<Link> findHubLink(Long linkId, Hub hub);

void delete(Link link);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
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.DeleteHubLinkCommand;
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;
Expand Down Expand Up @@ -80,7 +81,8 @@ private Member getMember(Long memberId) {
@Transactional
public CreateHubLinkResponse createHubLink(CreateHubLinkCommand command) {
Hub hub = getHub(command.hubId());
hub.checkMasterAuthority(command.memberId());
Member member = getMember(command.memberId());
hub.checkMasterAuthority(member);

LinkBundle hubLinkBundle = getHubLinkBundle(command.linkBundleId(), hub);
Link link = new Link(command.url(), command.description());
Expand All @@ -106,6 +108,7 @@ public FindLinksResponse findHubLinks(FindHubLinksCommand command) {
}

@Override
@Transactional
public DeleteLinkResponse deleteLink(DeleteLinkCommand command) {
Member member = getMember(command.memberId());
Link link = linkRepository.findMemberLink(command.linkId(), member)
Expand All @@ -114,6 +117,19 @@ public DeleteLinkResponse deleteLink(DeleteLinkCommand command) {
return new DeleteLinkResponse(link.getLinkId());
}

@Override
@Transactional
public DeleteLinkResponse deleteHubLink(DeleteHubLinkCommand command) {
Hub hub = getHub(command.hubId());
Member member = getMember(command.memberId());
hub.checkMasterAuthority(member);
Link link = linkRepository.findHubLink(command.linkId(), hub)
.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,6 +2,7 @@

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.DeleteHubLinkCommand;
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;
Expand All @@ -21,4 +22,6 @@ public interface LinkUseCase {
FindLinksResponse findHubLinks(FindHubLinksCommand command);

DeleteLinkResponse deleteLink(DeleteLinkCommand command);

DeleteLinkResponse deleteHubLink(DeleteHubLinkCommand 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 DeleteHubLinkCommand(Long linkId, Long memberId, Long hubId) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ private Member getMember(Long memberId) {
@Transactional
public CreateLinkBundleResponse createHubLinkBundle(CreateHubLinkBundleCommand command) {
Hub hub = getHub(command.hubId());
hub.checkMasterAuthority(command.memberId());
Member member = getMember(command.memberId());
hub.checkMasterAuthority(member);
LinkBundle linkBundle = new LinkBundle(
command.description(),
command.isDefault());
Expand Down
Loading
Loading