Skip to content

Commit

Permalink
feat : 프록시 패턴으로 서비스 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
GaBaljaintheroom committed May 8, 2024
1 parent fce88b5 commit 42efafb
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,14 @@ ResponseEntity<ApiSpec<GroupsSliceResponse>> findGroups(
description = "처리 시작"
)
})
@PostMapping(value = "/groups/{group_id}/members/{member_id}/invitation")
ResponseEntity<Void> inviteGroup(
@Parameter(in = ParameterIn.PATH, description = "그룹 아이디", required = true, schema = @Schema())
@PathVariable("group_id") Long groupId,
ResponseEntity<ApiSpec<String>> inviteGroup(
Long memberId,

@Parameter(in = ParameterIn.PATH, description = "대상 회원 아이디", required = true, schema = @Schema())
@PathVariable("member_id") Long memberId
@Parameter(in = ParameterIn.PATH, description = "그룹 아이디", required = true)
Long groupId,

@Parameter(in = ParameterIn.PATH, description = "대상 회원 아이디", required = true)
Long targetId
);

@Operation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ public ResponseEntity<ApiSpec<GroupDetailResponse>> findGroupDetailById(
@AuthenticationPrincipal final Long memberId,
@PathVariable("group_id") final Long groupId
) {
final GroupDetailDto groupDetailDto = groupService.findGroupDetailByGroupId(memberId, groupId);
final GroupDetailDto groupDetailDto = groupService.findGroupDetailByGroupId(memberId,
groupId);

return ResponseEntity.ok(
ApiSpec.success(
Expand Down Expand Up @@ -118,9 +119,20 @@ public ResponseEntity<ApiSpec<GroupsSliceResponse>> findGroups(
);
}

@PostMapping(value = "/invite/{group_id}/member/{target_id}")
@Override
public ResponseEntity<Void> inviteGroup(Long groupId, Long memberId) {
return null;
public ResponseEntity<ApiSpec<String>> inviteGroup(
@AuthenticationPrincipal final Long memberId,
@PathVariable("group_id") final Long groupId,
@PathVariable("target_id") final Long targetId
) {
groupService.inviteGroup(memberId, groupId, targetId);

return ResponseEntity.ok(
ApiSpec.empty(
SuccessCode.SUCCESS
)
);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package site.timecapsulearchive.core.domain.group.service;

import java.time.ZonedDateTime;
import org.springframework.data.domain.Slice;
import site.timecapsulearchive.core.domain.group.data.dto.GroupDetailDto;
import site.timecapsulearchive.core.domain.group.data.dto.GroupSummaryDto;
import site.timecapsulearchive.core.domain.group.entity.Group;

public interface GroupReadService {

Group findGroupById(final Long groupId);

Slice<GroupSummaryDto> findGroupsSlice(
final Long memberId,
final int size,
final ZonedDateTime createdAt
);

GroupDetailDto findGroupDetailByGroupId(final Long memberId, final Long groupId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package site.timecapsulearchive.core.domain.group.service;

import java.time.ZonedDateTime;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Slice;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import site.timecapsulearchive.core.domain.group.data.dto.GroupDetailDto;
import site.timecapsulearchive.core.domain.group.data.dto.GroupSummaryDto;
import site.timecapsulearchive.core.domain.group.entity.Group;
import site.timecapsulearchive.core.domain.group.exception.GroupNotFoundException;
import site.timecapsulearchive.core.domain.group.repository.GroupQueryRepository;
import site.timecapsulearchive.core.domain.group.repository.GroupRepository;

@Service
@RequiredArgsConstructor
public class GroupReadServiceImpl implements GroupReadService {

private final GroupQueryRepository groupQueryRepository;
private final GroupRepository groupRepository;


@Transactional(readOnly = true)
public Group findGroupById(final Long groupId) {
return groupRepository.findGroupById(groupId)
.orElseThrow(GroupNotFoundException::new);
}

@Transactional(readOnly = true)
public Slice<GroupSummaryDto> findGroupsSlice(
final Long memberId,
final int size,
final ZonedDateTime createdAt
) {
return groupQueryRepository.findGroupsSlice(memberId, size, createdAt);
}

@Transactional(readOnly = true)
public GroupDetailDto findGroupDetailByGroupId(final Long memberId, final Long groupId) {
final GroupDetailDto groupDetailDto = groupQueryRepository.findGroupDetailByGroupId(groupId)
.orElseThrow(GroupNotFoundException::new);

final boolean isGroupMember = groupDetailDto.members()
.stream()
.anyMatch(m -> m.memberId().equals(memberId));

if (!isGroupMember) {
throw new GroupNotFoundException();
}

return groupDetailDto;
}
}
Original file line number Diff line number Diff line change
@@ -1,87 +1,5 @@
package site.timecapsulearchive.core.domain.group.service;

import lombok.RequiredArgsConstructor;
import java.time.ZonedDateTime;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Slice;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
import site.timecapsulearchive.core.domain.group.data.dto.GroupCreateDto;
import site.timecapsulearchive.core.domain.group.data.dto.GroupDetailDto;
import site.timecapsulearchive.core.domain.group.data.dto.GroupSummaryDto;
import site.timecapsulearchive.core.domain.group.entity.Group;
import site.timecapsulearchive.core.domain.group.entity.MemberGroup;
import site.timecapsulearchive.core.domain.group.exception.GroupNotFoundException;
import site.timecapsulearchive.core.domain.group.repository.GroupQueryRepository;
import site.timecapsulearchive.core.domain.group.repository.GroupRepository;
import site.timecapsulearchive.core.domain.group.repository.MemberGroupRepository;
import site.timecapsulearchive.core.domain.member.entity.Member;
import site.timecapsulearchive.core.domain.member.exception.MemberNotFoundException;
import site.timecapsulearchive.core.domain.member.repository.MemberRepository;
import site.timecapsulearchive.core.infra.queue.manager.SocialNotificationManager;
public interface GroupService extends GroupReadService, GroupWriteService {

@Service
@RequiredArgsConstructor
public class GroupService {

private final GroupRepository groupRepository;
private final MemberRepository memberRepository;
private final MemberGroupRepository memberGroupRepository;
private final TransactionTemplate transactionTemplate;
private final SocialNotificationManager socialNotificationManager;
private final GroupQueryRepository groupQueryRepository;

public void createGroup(final Long memberId, final GroupCreateDto dto) {
final Member member = memberRepository.findMemberById(memberId)
.orElseThrow(MemberNotFoundException::new);

final Group group = dto.toEntity();

final MemberGroup memberGroup = MemberGroup.createGroupOwner(member, group);

transactionTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
groupRepository.save(group);
memberGroupRepository.save(memberGroup);
}
});

socialNotificationManager.sendGroupInviteMessage(member.getNickname(),
dto.groupProfileUrl(), dto.targetIds());
}

@Transactional(readOnly = true)
public Group findGroupById(Long groupId) {
return groupRepository.findGroupById(groupId)
.orElseThrow(GroupNotFoundException::new);
}

@Transactional(readOnly = true)
public Slice<GroupSummaryDto> findGroupsSlice(
final Long memberId,
final int size,
final ZonedDateTime createdAt
) {
return groupQueryRepository.findGroupsSlice(memberId, size, createdAt);
}

@Transactional(readOnly = true)
public GroupDetailDto findGroupDetailByGroupId(final Long memberId, final Long groupId) {
final GroupDetailDto groupDetailDto = groupQueryRepository.findGroupDetailByGroupId(groupId)
.orElseThrow(GroupNotFoundException::new);

final boolean isGroupMember = groupDetailDto.members()
.stream()
.anyMatch(m -> m.memberId().equals(memberId));

if (!isGroupMember) {
throw new GroupNotFoundException();
}

return groupDetailDto;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package site.timecapsulearchive.core.domain.group.service;

import java.time.ZonedDateTime;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Slice;
import org.springframework.stereotype.Service;
import site.timecapsulearchive.core.domain.group.data.dto.GroupCreateDto;
import site.timecapsulearchive.core.domain.group.data.dto.GroupDetailDto;
import site.timecapsulearchive.core.domain.group.data.dto.GroupSummaryDto;
import site.timecapsulearchive.core.domain.group.entity.Group;

@Service
@RequiredArgsConstructor
public class GroupServiceImpl implements GroupService {

private final GroupReadService groupReadService;
private final GroupWriteService groupWriteService;

@Override
public Group findGroupById(final Long groupId) {
return groupReadService.findGroupById(groupId);
}

@Override
public Slice<GroupSummaryDto> findGroupsSlice(
final Long memberId,
final int size,
final ZonedDateTime createdAt
) {
return groupReadService.findGroupsSlice(memberId, size, createdAt);
}

@Override
public GroupDetailDto findGroupDetailByGroupId(final Long memberId, final Long groupId) {
return groupReadService.findGroupDetailByGroupId(memberId, groupId);
}

@Override
public void createGroup(final Long memberId, final GroupCreateDto dto) {
groupWriteService.createGroup(memberId, dto);
}

@Override
public void inviteGroup(final Long memberId, final Long groupId, final Long targetId) {
groupWriteService.inviteGroup(memberId, groupId, targetId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package site.timecapsulearchive.core.domain.group.service;

import site.timecapsulearchive.core.domain.group.data.dto.GroupCreateDto;

public interface GroupWriteService {

void createGroup(final Long memberId, final GroupCreateDto dto);

void inviteGroup(final Long memberId, final Long groupId, final Long targetId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package site.timecapsulearchive.core.domain.group.service;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
import site.timecapsulearchive.core.domain.group.data.dto.GroupCreateDto;
import site.timecapsulearchive.core.domain.group.entity.Group;
import site.timecapsulearchive.core.domain.group.entity.MemberGroup;
import site.timecapsulearchive.core.domain.group.repository.GroupRepository;
import site.timecapsulearchive.core.domain.group.repository.MemberGroupRepository;
import site.timecapsulearchive.core.domain.member.entity.Member;
import site.timecapsulearchive.core.domain.member.exception.MemberNotFoundException;
import site.timecapsulearchive.core.domain.member.repository.MemberRepository;
import site.timecapsulearchive.core.infra.queue.manager.SocialNotificationManager;

@Service
@RequiredArgsConstructor
public class GroupWriteServiceImpl implements GroupWriteService {

private final GroupRepository groupRepository;
private final MemberRepository memberRepository;
private final MemberGroupRepository memberGroupRepository;
private final TransactionTemplate transactionTemplate;
private final SocialNotificationManager socialNotificationManager;

public void createGroup(final Long memberId, final GroupCreateDto dto) {
final Member member = memberRepository.findMemberById(memberId)
.orElseThrow(MemberNotFoundException::new);

final Group group = dto.toEntity();

final MemberGroup memberGroup = MemberGroup.createGroupOwner(member, group);

transactionTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
groupRepository.save(group);
memberGroupRepository.save(memberGroup);
}
});

socialNotificationManager.sendGroupInviteMessage(member.getNickname(),
dto.groupProfileUrl(), dto.targetIds());
}

@Override
public void inviteGroup(Long memberId, Long groupId, Long targetId) {

}

}

0 comments on commit 42efafb

Please sign in to comment.