Skip to content

Commit

Permalink
Merge pull request #75 from HongDam-org/feat/group-invite-fix
Browse files Browse the repository at this point in the history
[FEAT] group 초대 api 및 위치 공유 여부 변경 api 수정
  • Loading branch information
ohksj77 authored Dec 5, 2023
2 parents 6ec77e8 + 30ec4b4 commit 9046d4a
Show file tree
Hide file tree
Showing 18 changed files with 178 additions and 156 deletions.
7 changes: 5 additions & 2 deletions backend/src/docs/group.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ operation::post join group[snippets='http-request,http-response']
=== 그룹 초대
operation::post invite group[snippets='http-request,http-response']

=== 위치 공유 여부 수정
operation::post change share[snippets='http-request,http-response']
=== 위치 공유 ON
operation::post share location[snippets='http-request,http-response']

=== 위치 공유 OFF
operation::post unshare location[snippets='http-request,http-response']

=== 위치 공유 여부 조회
operation::get share[snippets='http-request,http-response']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,14 @@ public ResponseEntity<GroupInfoResponse> inviteGroup(
}

@PostMapping("/share/{id}")
public ResponseEntity<Void> changeShare(@PathVariable UUID id) {
groupService.changeShare(id);
public ResponseEntity<Void> shareLocation(@PathVariable UUID id) {
groupService.shareLocation(id);
return ResponseEntity.noContent().build();
}

@PostMapping("/unshare/{id}")
public ResponseEntity<Void> unShareLocation(@PathVariable UUID id) {
groupService.unShareLocation(id);
return ResponseEntity.noContent().build();
}

Expand All @@ -57,7 +63,7 @@ public ResponseEntity<ShareInfoResponse> getShare(@PathVariable UUID id) {
return ResponseEntity.ok(groupService.getShare(id));
}

@GetMapping()
@GetMapping
public ResponseEntity<List<GroupInfoResponse>> getMyGroups() {
return ResponseEntity.ok(groupService.getMyGroups());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.UUID;

@Getter
@NoArgsConstructor
@AllArgsConstructor
public class InviteGroupRequest {
private UUID friendMemberId;
private List<UUID> friendMemberIds;
private UUID groupId;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.twtw.backend.domain.group.entity;

import com.twtw.backend.domain.member.entity.Member;
import com.twtw.backend.domain.plan.entity.Plan;
import com.twtw.backend.global.audit.AuditListener;
import com.twtw.backend.global.audit.Auditable;
Expand Down Expand Up @@ -52,13 +53,26 @@ public class Group implements Auditable {
@Column(nullable = false)
private BaseTime baseTime;

public Group(String name, String groupImage, UUID leaderId) {
@Builder
public Group(final String name, final String groupImage, final Member leader) {
this.name = name;
this.groupImage = groupImage;
this.leaderId = leaderId;
this.leaderId = leader.getId();

final GroupMember groupMember = new GroupMember(this, leader);
this.groupMembers.add(groupMember);
groupMember.acceptInvite();
}

public void addToGroup(final Plan plan) {
public void addPlan(final Plan plan) {
this.groupPlans.add(plan);
}

public void inviteAll(final List<Member> friends) {
friends.forEach(friend -> addGroupMember(new GroupMember(this, friend)));
}

private boolean addGroupMember(final GroupMember groupMember) {
return this.groupMembers.add(groupMember);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,23 @@ public GroupMember(Group group, Member member) {
this.group = group;
this.member = member;
this.share = true;
group.getGroupMembers().add(this);
member.getGroupMembers().add(this);
member.addGroupMember(this);
this.groupInviteCode = GroupInviteCode.REQUESTED;
}

public void changeShare() {
this.share = !this.share;
public void share() {
this.share = true;
}

public void unShare() {
this.share = false;
}

public void acceptInvite() {
this.groupInviteCode = GroupInviteCode.ACCEPTED;
}

public void changeGroupCode(GroupInviteCode groupInviteCode) {
this.groupInviteCode = groupInviteCode;
public UUID getGroupId() {
return this.group.getId();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,19 @@
import com.twtw.backend.domain.group.entity.GroupMember;
import com.twtw.backend.domain.member.entity.Member;

import org.mapstruct.*;
import org.mapstruct.IterableMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingConstants;
import org.mapstruct.Named;

import java.util.List;
import java.util.UUID;

@Mapper(componentModel = MappingConstants.ComponentModel.SPRING)
public interface GroupMapper {
@Mapping(target = "baseTime", ignore = true)
GroupMember connectGroupMember(Group group, Member member);

@Mapping(target = "baseTime", ignore = true)
@Mapping(target = "groupMembers", ignore = true)
@Mapping(target = "groupPlans", ignore = true)
@Mapping(target = "name", source = "groupDto.name")
@Mapping(target = "groupImage", source = "groupDto.groupImage")
@Mapping(target = "leaderId", source = "leaderId")
Group toGroupEntity(MakeGroupRequest groupDto, UUID leaderId);
Group toGroupEntity(MakeGroupRequest groupDto, Member leader);

@Mapping(target = "groupId", source = "id")
GroupInfoResponse toGroupInfo(Group group);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.twtw.backend.domain.group.dto.response.ShareInfoResponse;
import com.twtw.backend.domain.group.dto.response.SimpleGroupInfoResponse;
import com.twtw.backend.domain.group.entity.Group;
import com.twtw.backend.domain.group.entity.GroupInviteCode;
import com.twtw.backend.domain.group.entity.GroupMember;
import com.twtw.backend.domain.group.mapper.GroupMapper;
import com.twtw.backend.domain.group.repository.GroupMemberRepository;
Expand All @@ -22,6 +21,7 @@

import java.util.List;
import java.util.UUID;
import java.util.function.Consumer;

@Service
public class GroupService {
Expand Down Expand Up @@ -63,9 +63,7 @@ public GroupMember getGroupMemberEntity(UUID groupId, UUID memberId) {
@Transactional
public GroupInfoResponse makeGroup(MakeGroupRequest groupDto) {
Member member = authService.getMemberByJwt();
Group group = groupMapper.toGroupEntity(groupDto, member.getId());
GroupMember groupMember = groupMapper.connectGroupMember(group, member);
groupMember.changeGroupCode(GroupInviteCode.ACCEPTED);
Group group = groupMapper.toGroupEntity(groupDto, member);

return groupMapper.toGroupInfo(groupRepository.save(group));
}
Expand All @@ -76,18 +74,27 @@ public SimpleGroupInfoResponse joinGroup(JoinGroupRequest joinGroupRequest) {
GroupMember groupMember =
getGroupMemberEntity(joinGroupRequest.getGroupId(), member.getId());

groupMember.changeGroupCode(GroupInviteCode.ACCEPTED);
groupMember.acceptInvite();

return new SimpleGroupInfoResponse(groupMember.getGroup().getId());
return new SimpleGroupInfoResponse(groupMember.getGroupId());
}

@Transactional
public void changeShare(UUID id) {
public void shareLocation(final UUID id) {
changeShare(GroupMember::share, id);
}

@Transactional
public void unShareLocation(final UUID id) {
changeShare(GroupMember::unShare, id);
}

private void changeShare(final Consumer<GroupMember> changeShare, final UUID id) {
Member member = this.authService.getMemberByJwt();
GroupInfoResponse groupInfo = getGroupById(id);

GroupMember groupMember = getGroupMemberEntity(groupInfo.getGroupId(), member.getId());
groupMember.changeShare();
changeShare.accept(groupMember);
}

@Transactional
Expand All @@ -103,8 +110,9 @@ public ShareInfoResponse getShare(UUID id) {
@Transactional
public GroupInfoResponse inviteGroup(InviteGroupRequest inviteGroupRequest) {
Group group = getGroupEntity(inviteGroupRequest.getGroupId());
Member friend = memberService.getMemberById(inviteGroupRequest.getFriendMemberId());
groupMapper.connectGroupMember(group, friend);
List<Member> friends =
memberService.getMembersByIds(inviteGroupRequest.getFriendMemberIds());
group.inviteAll(friends);

return groupMapper.toGroupInfo(group);
}
Expand All @@ -117,7 +125,7 @@ public GroupInfoResponse getGroupInfoResponse(Group group) {
public List<GroupInfoResponse> getMyGroups() {
Member loginMember = authService.getMemberByJwt();

if (loginMember.getGroupMembers().isEmpty()) {
if (loginMember.hasNoGroupMember()) {
return List.of();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,12 @@ public Member(String nickname, String profileImage, OAuth2Info oauthInfo) {
this.role = Role.ROLE_USER;
this.oauthInfo = oauthInfo;
}

public void addGroupMember(final GroupMember groupMember) {
this.groupMembers.add(groupMember);
}

public boolean hasNoGroupMember() {
return this.groupMembers.isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,8 @@ public MemberResponse getResponseByMember(Member member) {
public List<MemberResponse> getMemberResponses(final Plan plan) {
return memberMapper.toMemberResponses(plan.getPlanMembers());
}

public List<Member> getMembersByIds(final List<UUID> friendMemberIds) {
return memberRepository.findAllById(friendMemberIds);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ private PlanMember findPlanMember(final Member member) {
.orElseThrow(InvalidPlanMemberException::new);
}

public void addPlace(final Place place) {
private void addPlace(final Place place) {
this.place = place;
}

public void addGroup(final Group group) {
this.group = group;
this.group.addToGroup(this);
this.group.addPlan(this);
}

public UUID getPlanMakerId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ void inviteGroup() throws Exception {
.content(
toRequestBody(
new InviteGroupRequest(
UUID.randomUUID(), UUID.randomUUID())))
List.of(UUID.randomUUID()),
UUID.randomUUID())))
.header(
"Authorization",
"Bearer wefa3fsdczf32.gaoiuergf92.gb5hsa2jgh"));
Expand All @@ -153,7 +154,7 @@ void inviteGroup() throws Exception {

@Test
@DisplayName("위치 공유 수정 API가 수행되는가")
void changeShare() throws Exception {
void shareLocation() throws Exception {
// when
final ResultActions perform =
mockMvc.perform(
Expand All @@ -162,7 +163,8 @@ void changeShare() throws Exception {
.content(
toRequestBody(
new InviteGroupRequest(
UUID.randomUUID(), UUID.randomUUID())))
List.of(UUID.randomUUID()),
UUID.randomUUID())))
.header(
"Authorization",
"Bearer wefa3fsdczf32.gaoiuergf92.gb5hsa2jgh"));
Expand All @@ -172,7 +174,40 @@ void changeShare() throws Exception {

// docs
perform.andDo(print())
.andDo(document("post change share", getDocumentRequest(), getDocumentResponse()));
.andDo(
document(
"post share location",
getDocumentRequest(),
getDocumentResponse()));
}

@Test
@DisplayName("위치 공유 수정 API가 수행되는가")
void unShareLocation() throws Exception {
// when
final ResultActions perform =
mockMvc.perform(
post("/group/unshare/" + UUID.randomUUID())
.contentType(MediaType.APPLICATION_JSON)
.content(
toRequestBody(
new InviteGroupRequest(
List.of(UUID.randomUUID()),
UUID.randomUUID())))
.header(
"Authorization",
"Bearer wefa3fsdczf32.gaoiuergf92.gb5hsa2jgh"));

// then
perform.andExpect(status().isNoContent());

// docs
perform.andDo(print())
.andDo(
document(
"post unshare location",
getDocumentRequest(),
getDocumentResponse()));
}

@Test
Expand All @@ -191,7 +226,8 @@ void getShare() throws Exception {
.content(
toRequestBody(
new InviteGroupRequest(
UUID.randomUUID(), UUID.randomUUID())))
List.of(UUID.randomUUID()),
UUID.randomUUID())))
.header(
"Authorization",
"Bearer wefa3fsdczf32.gaoiuergf92.gb5hsa2jgh"));
Expand Down
Loading

0 comments on commit 9046d4a

Please sign in to comment.