Skip to content

Commit

Permalink
refact: 그룹 초대 서비스 수정
Browse files Browse the repository at this point in the history
- 그룹 초대 엔티티 수정
- 그룹 생성 시 그룹 초대 쿼리 추가
  • Loading branch information
GaBaljaintheroom committed May 8, 2024
1 parent 4d0dcda commit b0b03dc
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;
import java.time.ZonedDateTime;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
Expand All @@ -19,8 +21,8 @@ public void bulkSave(final Long ownerId, final List<Long> friendIds) {
jdbcTemplate.batchUpdate(
"""
INSERT INTO friend_invite (
friend_invite_id, owner_id, friend_id
) values (?, ?, ?)
friend_invite_id, owner_id, friend_id, created_at, updated_at
) values (?, ?, ?, ?, ?)
""",
new BatchPreparedStatementSetter() {

Expand All @@ -30,6 +32,8 @@ public void setValues(final PreparedStatement ps, final int i) throws SQLExcepti
ps.setNull(1, Types.BIGINT);
ps.setLong(2, ownerId);
ps.setLong(3, friendId);
ps.setTimestamp(4, Timestamp.valueOf(ZonedDateTime.now().toLocalDateTime()));
ps.setTimestamp(5, Timestamp.valueOf(ZonedDateTime.now().toLocalDateTime()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,20 @@ public class GroupInvite extends BaseEntity {
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id", nullable = false)
private Member member;
@JoinColumn(name = "group_owner_id", nullable = false)
private Member groupOwner;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "group_member_id", nullable = false)
private Member groupMember;

private GroupInvite(Member groupOwner, Member groupMember) {
this.groupOwner = groupOwner;
this.groupMember = groupMember;
}

public static GroupInvite createOf(Member groupOwner, Member groupMember) {
return new GroupInvite(groupOwner, groupMember);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package site.timecapsulearchive.core.domain.group.repository;

import java.util.List;

public interface GroupInviteQueryRepository {

void bulkSave(final Long groupOwnerId, final List<Long> groupMemberIds);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package site.timecapsulearchive.core.domain.group.repository;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;
import java.time.ZonedDateTime;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
@RequiredArgsConstructor
public class GroupInviteQueryRepositoryImpl implements GroupInviteQueryRepository {

private final JdbcTemplate jdbcTemplate;

@Override
public void bulkSave(Long groupOwnerId, List<Long> groupMemberIds) {
jdbcTemplate.batchUpdate(
"""
INSERT INTO group_invite (
group_invite_id, group_owner_id, group_member_id, created_at, updated_at
) values (?, ?, ?, ?, ?)
""",
new BatchPreparedStatementSetter() {

@Override
public void setValues(final PreparedStatement ps, final int i) throws SQLException {
final Long groupMember = groupMemberIds.get(i);
ps.setNull(1, Types.BIGINT);
ps.setLong(2, groupOwnerId);
ps.setLong(3, groupMember);
ps.setTimestamp(4, Timestamp.valueOf(ZonedDateTime.now().toLocalDateTime()));
ps.setTimestamp(5, Timestamp.valueOf(ZonedDateTime.now().toLocalDateTime()));
}

@Override
public int getBatchSize() {
return groupMemberIds.size();
}
}
);
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package site.timecapsulearchive.core.domain.group.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.Repository;
import site.timecapsulearchive.core.domain.group.entity.GroupInvite;

public interface GroupInviteRepository extends JpaRepository<GroupInvite, Long> {
public interface GroupInviteRepository extends Repository<GroupInvite, Long>,
GroupInviteQueryRepository {

void save(GroupInvite groupInvite);
}

Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
import site.timecapsulearchive.core.domain.group.data.dto.GroupCreateDto;
import site.timecapsulearchive.core.domain.group.data.dto.GroupOwnerSummaryDto;
import site.timecapsulearchive.core.domain.group.entity.Group;
import site.timecapsulearchive.core.domain.group.entity.GroupInvite;
import site.timecapsulearchive.core.domain.group.entity.MemberGroup;
import site.timecapsulearchive.core.domain.group.exception.GroupNotFoundException;
import site.timecapsulearchive.core.domain.group.exception.GroupOwnerAuthenticateException;
import site.timecapsulearchive.core.domain.group.repository.GroupInviteRepository;
import site.timecapsulearchive.core.domain.group.repository.GroupRepository;
import site.timecapsulearchive.core.domain.group.repository.MemberGroupRepository;
import site.timecapsulearchive.core.domain.member.entity.Member;
Expand All @@ -23,9 +25,10 @@
@RequiredArgsConstructor
public class GroupWriteServiceImpl implements GroupWriteService {

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

Expand All @@ -42,6 +45,7 @@ public void createGroup(final Long memberId, final GroupCreateDto dto) {
protected void doInTransactionWithoutResult(TransactionStatus status) {
groupRepository.save(group);
memberGroupRepository.save(memberGroup);
groupInviteRepository.bulkSave(memberId, dto.targetIds());
}
});

Expand All @@ -51,15 +55,32 @@ protected void doInTransactionWithoutResult(TransactionStatus status) {

@Override
public void inviteGroup(final Long memberId, final Long groupId, final Long targetId) {
final GroupOwnerSummaryDto summaryDto = memberGroupRepository.findOwnerInMemberGroup(
groupId, memberId).orElseThrow(GroupNotFoundException::new);
final Member groupOwner = memberRepository.findMemberById(memberId).orElseThrow(
MemberNotFoundException::new);

final Member groupMember = memberRepository.findMemberById(targetId).orElseThrow(
MemberNotFoundException::new);

final GroupInvite groupInvite = GroupInvite.createOf(groupOwner, groupMember);

final GroupOwnerSummaryDto[] summaryDto = new GroupOwnerSummaryDto[1];

transactionTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
summaryDto[0] = memberGroupRepository.findOwnerInMemberGroup(
groupId, memberId).orElseThrow(GroupNotFoundException::new);

if (!summaryDto.isOwner()) {
throw new GroupOwnerAuthenticateException();
}
if (!summaryDto[0].isOwner()) {
throw new GroupOwnerAuthenticateException();
}

groupInviteRepository.save(groupInvite);
}
});

socialNotificationManager.sendGroupInviteMessage(summaryDto.nickname(),
summaryDto.groupProfileUrl(), List.of(targetId));
socialNotificationManager.sendGroupInviteMessage(summaryDto[0].nickname(),
summaryDto[0].groupProfileUrl(), List.of(targetId));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,6 @@ public class Member extends BaseEntity {
@OneToMany(mappedBy = "member", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Capsule> capsules;

@OneToMany(mappedBy = "member", cascade = CascadeType.ALL, orphanRemoval = true)
private List<GroupInvite> groupInvites;

@OneToMany(mappedBy = "member", cascade = CascadeType.ALL, orphanRemoval = true)
private List<MemberGroup> groups;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
alter table group_invite
drop foreign key fk_group_invite_member_id;

alter table group_invite
drop column member_id;


ALTER TABLE group_invite
ADD COLUMN group_owner_id BIGINT;
ALTER TABLE group_invite
ADD COLUMN group_member_id BIGINT;

ALTER TABLE group_invite
ADD CONSTRAINT fk_group_invite_group_owner_id FOREIGN KEY (group_owner_id) REFERENCES member (member_id);

ALTER TABLE group_invite
ADD CONSTRAINT fk_group_invite_group_member_id FOREIGN KEY (group_member_id) REFERENCES member (member_id);

ALTER TABLE group_invite
ADD CONSTRAINT unique_owner_member_pair UNIQUE (group_owner_id, group_member_id);

0 comments on commit b0b03dc

Please sign in to comment.