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: 링크 허브 생성 시 기본 링크 분류가 만들어진다. #35

Merged
merged 10 commits into from
Feb 14, 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
9 changes: 9 additions & 0 deletions src/docs/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ operation::link-bundle-controller-test/find-link-bundles[snippets='http-request,

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

=== 허브 링크 묶음 생성

==== request

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

==== response

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

== 링크

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public enum ErrorCode {
UNAUTHENTICATED("SL101", Constants.UNAUTHORIZED),
INVALID_ACCESS_TOKEN("SL102", Constants.UNAUTHORIZED),
EXPIRED_ACCESS_TOKEN("SL103", Constants.UNAUTHORIZED),
UNAUTHORIZED("SL301", Constants.FORBIDDEN),
NOT_FOUND("SL401", Constants.NOT_FOUND),
DUPLICATE_EMAIL("SL901", Constants.CONFLICT),
DUPLICATE_NICKNAME("SL902", Constants.BAD_REQUEST);
Expand All @@ -21,6 +22,7 @@ private static class Constants {

private static final int BAD_REQUEST = 400;
private static final int UNAUTHORIZED = 401;
private static final int FORBIDDEN = 401;
private static final int NOT_FOUND = 404;
private static final int CONFLICT = 409;
}
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/com/seong/shoutlink/domain/hub/Hub.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.seong.shoutlink.domain.exception.ErrorCode;
import com.seong.shoutlink.domain.exception.ShoutLinkException;
import com.seong.shoutlink.domain.member.Member;
import java.text.MessageFormat;
import java.util.Objects;
import lombok.Getter;
Expand All @@ -14,11 +15,18 @@ public class Hub {
private static final int DESCRIPTION_MAX_SIZE = 200;

private Long hubId;
private Long masterId;
private String name;
private String description;
private boolean isPrivate;

public Hub(String name, String description, boolean isPrivate) {
public Hub(Member master, String name, String description, boolean isPrivate) {
this(null, master.getMemberId(), name, description, isPrivate);
}

public Hub(Long hubId, Long memberId, String name, String description, boolean isPrivate) {
this.hubId = hubId;
this.masterId = memberId;
this.name = validateName(name);
this.description = validateDescription(description);
this.isPrivate = isPrivate;
Expand Down Expand Up @@ -47,4 +55,11 @@ private String validateDescription(String description) {
}
return description;
}

public void checkMasterAuthority(Long memberId) {
if(Objects.equals(masterId, memberId)) {
return;
}
throw new ShoutLinkException("권한이 없습니다.", ErrorCode.UNAUTHORIZED);
}
}
25 changes: 25 additions & 0 deletions src/main/java/com/seong/shoutlink/domain/hub/HubWithMaster.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.seong.shoutlink.domain.hub;

import com.seong.shoutlink.domain.exception.ErrorCode;
import com.seong.shoutlink.domain.exception.ShoutLinkException;
import com.seong.shoutlink.domain.member.Member;
import lombok.Getter;

@Getter
public class HubWithMaster {

private final Hub hub;
private final Member member;

public HubWithMaster(Hub hub, Member member) {
this.hub = hub;
this.member = member;
}

public void checkMasterAuthority(Long memberId) {
if(member.isEqualToMemberId(memberId)) {
return;
}
throw new ShoutLinkException("권한이 없습니다.", ErrorCode.UNAUTHORIZED);
}
}
16 changes: 0 additions & 16 deletions src/main/java/com/seong/shoutlink/domain/hub/HubWithMembers.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "hub")
public class HubEntity {

@Id
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.seong.shoutlink.domain.hub.repository;

import com.seong.shoutlink.domain.hub.HubWithMembers;
import com.seong.shoutlink.domain.hub.Hub;
import com.seong.shoutlink.domain.hub.service.HubRepository;
import com.seong.shoutlink.domain.hubMember.repository.HubMemberEntity;
import com.seong.shoutlink.domain.hubMember.repository.HubMemberJpaRepository;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
@RequiredArgsConstructor
Expand All @@ -15,12 +17,19 @@ public class HubRepositoryImpl implements HubRepository {
private final HubMemberJpaRepository hubMemberJpaRepository;

@Override
public Long save(HubWithMembers hubWithMembers) {
HubEntity hubEntity = HubEntity.create(hubWithMembers.getHub());
@Transactional
public Long save(Hub hub) {
HubEntity hubEntity = HubEntity.create(hub);
hubJpaRepository.save(hubEntity);
HubMemberEntity hubMemberEntity
= HubMemberEntity.create(hubWithMembers.getHub(), hubWithMembers.getMember());
= HubMemberEntity.create(hubEntity, hub.getMasterId());
hubMemberJpaRepository.save(hubMemberEntity);
return hubEntity.getHubId();
}

@Override
public Optional<Hub> findById(Long hubId) {
return hubMemberJpaRepository.findHubMasterByHubIdWithHub(hubId)
.map(HubMemberEntity::toHub);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.seong.shoutlink.domain.hub.service;

import com.seong.shoutlink.domain.hub.HubWithMembers;
import com.seong.shoutlink.domain.hub.Hub;
import java.util.Optional;

public interface HubRepository {

Long save(HubWithMembers hubWithMembers);
Long save(Hub hub);

Optional<Hub> findById(Long hubId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
import com.seong.shoutlink.domain.exception.ErrorCode;
import com.seong.shoutlink.domain.exception.ShoutLinkException;
import com.seong.shoutlink.domain.hub.Hub;
import com.seong.shoutlink.domain.hub.HubWithMembers;
import com.seong.shoutlink.domain.hub.service.event.CreateHubEvent;
import com.seong.shoutlink.domain.hub.service.request.CreateHubCommand;
import com.seong.shoutlink.domain.hub.service.response.CreateHubResponse;
import com.seong.shoutlink.domain.member.Member;
import com.seong.shoutlink.domain.member.service.MemberRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
Expand All @@ -21,11 +21,12 @@ public class HubService {
private final HubRepository hubRepository;
private final EventPublisher eventPublisher;

@Transactional
public CreateHubResponse createHub(CreateHubCommand command) {
Member member = getMember(command.memberId());
Hub hub = new Hub(command.name(), command.description(), command.isPrivate());
Long hubId = hubRepository.save(new HubWithMembers(hub, member));
eventPublisher.publishEvent(new CreateHubEvent(hubId));
Hub hub = new Hub(member, command.name(), command.description(), command.isPrivate());
Long hubId = hubRepository.save(hub);
eventPublisher.publishEvent(new CreateHubEvent(hubId, hub.getMasterId()));
return new CreateHubResponse(hubId);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

import com.seong.shoutlink.domain.common.Event;

public record CreateHubEvent(Long hubId) implements Event {
public record CreateHubEvent(Long hubId, Long memberId) implements Event {

}
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
package com.seong.shoutlink.domain.hubMember.repository;

import com.seong.shoutlink.domain.hub.Hub;
import com.seong.shoutlink.domain.hub.repository.HubEntity;
import com.seong.shoutlink.domain.hubMember.HubMemberRole;
import com.seong.shoutlink.domain.member.Member;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "hub_member")
public class HubMemberEntity {

@Id
Expand All @@ -26,20 +31,30 @@ public class HubMemberEntity {
@Column(nullable = false)
private Long memberId;

@Column(nullable = false)
private Long hubId;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "hub_id", nullable = false, updatable = false)
private HubEntity hubEntity;

@Column(nullable = false)
@Enumerated(EnumType.STRING)
private HubMemberRole hubMemberRole;

public HubMemberEntity(Long memberId, Long hubId, HubMemberRole hubMemberRole) {
public HubMemberEntity(Long memberId, HubEntity hubEntity, HubMemberRole hubMemberRole) {
this.memberId = memberId;
this.hubId = hubId;
this.hubEntity = hubEntity;
this.hubMemberRole = hubMemberRole;
}

public static HubMemberEntity create(Hub hub, Member member) {
return new HubMemberEntity(member.getMemberId(), hub.getHubId(), HubMemberRole.MASTER);
public static HubMemberEntity create(HubEntity hubEntity, Long masterId) {
return new HubMemberEntity(masterId, hubEntity, HubMemberRole.MASTER);
}

public Hub toHub() {
return new Hub(
hubEntity.getHubId(),
memberId,
hubEntity.getName(),
hubEntity.getDescription(),
hubEntity.isPrivate());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package com.seong.shoutlink.domain.hubMember.repository;

import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface HubMemberJpaRepository extends JpaRepository<HubMemberEntity, Long> {

@Query("select hm from HubMemberEntity hm "
+ "join hm.hubEntity h "
+ "where h.hubId = :hubId "
+ "and hm.hubMemberRole = com.seong.shoutlink.domain.hubMember.HubMemberRole.MASTER")
Optional<HubMemberEntity> findHubMasterByHubIdWithHub(@Param("hubId") Long hubId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "link")
public class LinkEntity {

@Id
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.seong.shoutlink.domain.linkbundle;

import com.seong.shoutlink.domain.hub.Hub;
import lombok.Getter;

@Getter
public class HubLinkBundle {

private final Hub hub;
private final LinkBundle linkBundle;

public HubLinkBundle(Hub hub, LinkBundle linkBundle) {
this.hub = hub;
this.linkBundle = linkBundle;
}

public void initLinkBundleId(Long linkBundleId) {
linkBundle.initId(linkBundleId);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.seong.shoutlink.domain.linkbundle;

import com.seong.shoutlink.domain.member.Member;
import java.util.Objects;
import lombok.Getter;

Expand All @@ -10,17 +9,15 @@ public class LinkBundle {
private Long linkBundleId;
private String description;
private boolean isDefault;
private Long memberId;

public LinkBundle(String description, boolean isDefault, Member member) {
this(null, description, isDefault, member.getMemberId());
public LinkBundle(String description, boolean isDefault) {
this(null, description, isDefault);
}

public LinkBundle(Long linkBundleId, String description, boolean isDefault, Long memberId) {
public LinkBundle(Long linkBundleId, String description, boolean isDefault) {
this.linkBundleId = linkBundleId;
this.description = description;
this.isDefault = isDefault;
this.memberId = memberId;
}

public void initId(Long linkBundleId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.seong.shoutlink.domain.linkbundle;

import com.seong.shoutlink.domain.member.Member;
import lombok.Getter;

@Getter
public class MemberLinkBundle {

private final Member member;
private final LinkBundle linkBundle;

public MemberLinkBundle(Member member, LinkBundle linkBundle) {
this.member = member;
this.linkBundle = linkBundle;
}

public void initLinkBundleId(Long linkBundleId) {
linkBundle.initId(linkBundleId);
}
}
Loading
Loading