Skip to content

Commit

Permalink
feat: GroupUser 조회 기능추가
Browse files Browse the repository at this point in the history
  • Loading branch information
SangWoon123 committed May 7, 2024
1 parent 66e0ed5 commit e7ffe21
Show file tree
Hide file tree
Showing 22 changed files with 110 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.tukorea.planding.common.CommonResponse;
import com.tukorea.planding.common.CommonUtils;
import com.tukorea.planding.domain.group.dto.GroupFavoriteResponse;
import com.tukorea.planding.domain.group.dto.response.GroupFavoriteResponse;
import com.tukorea.planding.domain.group.service.GroupFavoriteService;
import com.tukorea.planding.domain.user.dto.UserInfo;
import lombok.RequiredArgsConstructor;
Expand All @@ -16,15 +16,15 @@ public class GroupFavoriteController {

private final GroupFavoriteService groupFavoriteService;

@GetMapping("/{groupCode}")
public CommonResponse<GroupFavoriteResponse> addFavorite(@AuthenticationPrincipal UserInfo userInfo, @PathVariable String groupCode) {
GroupFavoriteResponse response = groupFavoriteService.addFavorite(userInfo, groupCode);
@GetMapping("/{groupId}")
public CommonResponse<GroupFavoriteResponse> addFavorite(@AuthenticationPrincipal UserInfo userInfo, @PathVariable Long groupId) {
GroupFavoriteResponse response = groupFavoriteService.addFavorite(userInfo, groupId);
return CommonUtils.success(response);
}

@DeleteMapping("/{groupCode}")
public CommonResponse<?> deleteFavorite(@AuthenticationPrincipal UserInfo userInfo, @PathVariable String groupCode) {
groupFavoriteService.deleteFavorite(userInfo, groupCode);
@DeleteMapping("/{groupId}")
public CommonResponse<?> deleteFavorite(@AuthenticationPrincipal UserInfo userInfo, @PathVariable Long groupId) {
groupFavoriteService.deleteFavorite(userInfo, groupId);
return CommonUtils.success("즐겨찾기 해제 완료.");
}

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

import com.tukorea.planding.common.CommonResponse;
import com.tukorea.planding.common.CommonUtils;
import com.tukorea.planding.domain.group.dto.GroupInviteRequest;
import com.tukorea.planding.domain.group.dto.GroupInviteResponse;
import com.tukorea.planding.domain.group.dto.request.GroupInviteRequest;
import com.tukorea.planding.domain.group.dto.response.GroupInviteResponse;
import com.tukorea.planding.domain.group.service.GroupInviteService;
import com.tukorea.planding.domain.user.dto.UserInfo;
import io.swagger.v3.oas.annotations.Operation;
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.tukorea.planding.domain.group.dto;
package com.tukorea.planding.domain.group.dto.request;

import lombok.Builder;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.tukorea.planding.domain.group.dto.request;

import lombok.Builder;

@Builder
public record GroupInviteRequest(
Long groupId,
String userCode
) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.tukorea.planding.domain.group.dto.request;

public record GroupUpdateRequest(
Long groupId,
String name,
String description
) {
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.tukorea.planding.domain.group.dto;
package com.tukorea.planding.domain.group.dto.response;

import com.tukorea.planding.domain.group.entity.GroupFavorite;
import lombok.Builder;
import lombok.Getter;

@Builder
public record GroupFavoriteResponse(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.tukorea.planding.domain.group.dto;
package com.tukorea.planding.domain.group.dto.response;

import com.tukorea.planding.domain.group.entity.InviteStatus;
import lombok.Builder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.tukorea.planding.domain.group.dto;
package com.tukorea.planding.domain.group.dto.response;

import com.tukorea.planding.domain.group.entity.GroupRoom;
import lombok.Builder;
import lombok.Getter;

@Builder
public record GroupResponse(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.tukorea.planding.domain.group.dto.response;

import com.tukorea.planding.domain.user.entity.User;
import lombok.Builder;
import lombok.Getter;

@Builder
public record GroupUserResponse(
String userCode,
String userName
) {
public static GroupUserResponse toGroupUserResponse(User user) {
return GroupUserResponse.builder()
.userCode(user.getUserCode())
.userName(user.getUsername())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.tukorea.planding.domain.group.entity;

import com.tukorea.planding.domain.group.dto.GroupInviteResponse;
import com.tukorea.planding.domain.group.dto.response.GroupInviteResponse;
import com.tukorea.planding.domain.user.entity.User;
import com.tukorea.planding.global.error.BusinessException;
import com.tukorea.planding.global.error.ErrorCode;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.tukorea.planding.domain.group.entity;

import com.tukorea.planding.domain.group.dto.GroupCreateRequest;
import com.tukorea.planding.domain.group.dto.request.GroupCreateRequest;
import com.tukorea.planding.domain.user.entity.User;
import com.tukorea.planding.global.audit.BaseEntity;
import com.tukorea.planding.domain.schedule.entity.Schedule;
Expand Down Expand Up @@ -32,7 +32,7 @@ public class GroupRoom extends BaseEntity {
private String groupCode; // 그룹방 고유 식별값

@OneToMany(mappedBy = "groupRoom", cascade = CascadeType.ALL, orphanRemoval = true)
private final Set<UserGroup> groupMemberships = new HashSet<>();
private final Set<UserGroup> userGroups = new HashSet<>();

@OneToMany(mappedBy = "groupRoom", cascade = CascadeType.ALL, orphanRemoval = true)
private final List<Schedule> schedules = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.tukorea.planding.domain.group.repository;

import com.tukorea.planding.domain.group.entity.GroupRoom;
import com.tukorea.planding.domain.user.entity.User;

import java.util.List;
import java.util.Optional;

public interface GroupRoomRepositoryCustom {
List<GroupRoom> findGroupRoomsByUserId(Long userId);
GroupRoom findByGroupCode(String groupCode);

GroupRoom findByGroupId(Long groupId);

List<User> getGroupUsers(Long groupId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,46 @@

import com.querydsl.jpa.impl.JPAQueryFactory;
import com.tukorea.planding.domain.group.entity.GroupRoom;
import com.tukorea.planding.domain.user.entity.User;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

import static com.tukorea.planding.domain.group.entity.QGroupRoom.groupRoom;
import static com.tukorea.planding.domain.group.entity.QUserGroup.userGroup;
import static com.tukorea.planding.domain.user.entity.QUser.user;


@Repository
@RequiredArgsConstructor
public class GroupRoomRepositoryCustomImpl implements GroupRoomRepositoryCustom{
public class GroupRoomRepositoryCustomImpl implements GroupRoomRepositoryCustom {

private final JPAQueryFactory queryFactory;

@Override
public List<GroupRoom> findGroupRoomsByUserId(Long userId) {
return queryFactory.select(groupRoom)
.from(groupRoom)
.innerJoin(groupRoom.groupMemberships)
.on(groupRoom.groupMemberships.any().user.id.eq(userId))
.innerJoin(groupRoom.userGroups)
.on(groupRoom.userGroups.any().user.id.eq(userId))
.fetch();
}

@Override
public GroupRoom findByGroupCode(String groupCode) {
public GroupRoom findByGroupId(Long groupId) {
return queryFactory.select(groupRoom)
.from(groupRoom)
.where(groupRoom.groupCode.eq(groupCode))
.where(groupRoom.id.eq(groupId))
.fetchOne();
}

@Override
public List<User> getGroupUsers(Long groupId) {
return queryFactory.select(user)
.from(userGroup)
.where(userGroup.groupRoom.id.eq(groupId))
.fetch();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public boolean existsByGroupRoomIdAndUserId(Long groupRoomId, Long userId) {
@Override
public List<User> findUserByIsConnectionFalse(Long groupRoomId) {
return queryFactory.selectFrom(user)
.join(user.groupMemberships, userGroup)
.join(user.userGroup, userGroup)
.where(userGroup.groupRoom.id.eq(groupRoomId)
.and(userGroup.isConnected.eq(false)))
.fetch();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.tukorea.planding.domain.group.service;

import com.tukorea.planding.domain.group.dto.GroupFavoriteResponse;
import com.tukorea.planding.domain.group.dto.response.GroupFavoriteResponse;
import com.tukorea.planding.domain.group.entity.GroupFavorite;
import com.tukorea.planding.domain.group.entity.GroupRoom;
import com.tukorea.planding.domain.group.repository.GroupFavoriteRepository;
Expand All @@ -24,9 +24,9 @@ public class GroupFavoriteService {
private final UserQueryService userQueryService;
private final GroupQueryService groupQueryService;

public GroupFavoriteResponse addFavorite(UserInfo userInfo, String groupCode) {
public GroupFavoriteResponse addFavorite(UserInfo userInfo, Long groupId) {
User user = userQueryService.getByUserInfo(userInfo.getUserCode());
GroupRoom groupRoom = groupQueryService.getGroupByCode(groupCode);
GroupRoom groupRoom = groupQueryService.getGroupById(groupId);

boolean exists = groupFavoriteRepositoryCustom.existsByUserAndGroupRoom(user.getUserCode());
if (exists) {
Expand All @@ -39,9 +39,9 @@ public GroupFavoriteResponse addFavorite(UserInfo userInfo, String groupCode) {
return GroupFavoriteResponse.from(save);
}

public void deleteFavorite(UserInfo userInfo, String groupCode) {
public void deleteFavorite(UserInfo userInfo, Long groupId) {
User user = userQueryService.getByUserInfo(userInfo.getUserCode());
GroupRoom groupRoom = groupQueryService.getGroupByCode(groupCode);
GroupRoom groupRoom = groupQueryService.getGroupById(groupId);

GroupFavorite groupFavorite = groupFavoriteRepositoryCustom.findByUserAndGroupRoom(user, groupRoom);
if (groupFavorite == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import com.tukorea.planding.domain.group.entity.GroupInvite;
import com.tukorea.planding.domain.group.entity.GroupRoom;
import com.tukorea.planding.domain.group.entity.UserGroup;
import com.tukorea.planding.domain.group.dto.GroupInviteRequest;
import com.tukorea.planding.domain.group.dto.GroupInviteResponse;
import com.tukorea.planding.domain.group.dto.request.GroupInviteRequest;
import com.tukorea.planding.domain.group.dto.response.GroupInviteResponse;
import com.tukorea.planding.domain.group.entity.InviteStatus;
import com.tukorea.planding.domain.group.repository.GroupInviteRepository;
import com.tukorea.planding.domain.notify.dto.NotificationScheduleRequest;
Expand Down Expand Up @@ -51,7 +51,7 @@ public GroupInviteResponse inviteGroupRoom(UserInfo userInfo, GroupInviteRequest
throw new BusinessException(ErrorCode.CANNOT_INVITE_YOURSELF);
}

GroupRoom groupRoom = groupQueryService.getGroupByCode(invitedUserInfo.inviteGroupCode());
GroupRoom groupRoom = groupQueryService.getGroupById(invitedUserInfo.groupId());
log.info("[그룹 초대] {} : {}", groupRoom.getName(), invitingUser.getUsername());

// 초대하는 유저가 방장인지 체크하는 로직
Expand Down Expand Up @@ -122,7 +122,7 @@ private void validInvitePermission(GroupRoom groupRoom, User invitingUser) {

private void checkUserAlreadyOrInvited(GroupRoom groupRoom, User invitedUser) {
// 초대한 유저가 이미 그룹에 속해 있는지 확인
if (groupRoom.getGroupMemberships().contains(invitedUser)) {
if (groupRoom.getUserGroups().contains(invitedUser)) {
throw new BusinessException(ErrorCode.USER_ALREADY_INVITED);
}
// 이미 보낸 초대인지 확인
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,17 @@ public GroupRoom createGroup(GroupRoom groupRoom) {
return groupRoomRepository.save(groupRoom);
}

public List<GroupRoom> findGroupsByUser(User user){
return groupRoomRepositoryCustom.findGroupRoomsByUserId(user.getId());
public List<GroupRoom> findGroupsByUserId(Long userId) {
return groupRoomRepositoryCustom.findGroupRoomsByUserId(userId);
}

public GroupRoom getGroupByCode(String groupCode) {
return groupRoomRepository.findByGroupCode(groupCode)
public GroupRoom getGroupById(Long groupId) {
return groupRoomRepository.findById(groupId)
.orElseThrow(() -> new BusinessException(ErrorCode.GROUP_ROOM_NOT_FOUND));
}

public List<User> getGroupUsers(Long groupId) {
return groupRoomRepositoryCustom.getGroupUsers(groupId);
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.tukorea.planding.domain.group.service;

import com.tukorea.planding.domain.group.dto.GroupCreateRequest;
import com.tukorea.planding.domain.group.dto.GroupResponse;
import com.tukorea.planding.domain.group.dto.GroupUpdateRequest;
import com.tukorea.planding.domain.group.dto.request.GroupCreateRequest;
import com.tukorea.planding.domain.group.dto.response.GroupResponse;
import com.tukorea.planding.domain.group.dto.request.GroupUpdateRequest;
import com.tukorea.planding.domain.group.dto.response.GroupUserResponse;
import com.tukorea.planding.domain.group.entity.GroupRoom;
import com.tukorea.planding.domain.group.entity.UserGroup;
import com.tukorea.planding.domain.group.repository.GroupRoomRepository;
import com.tukorea.planding.domain.user.dto.UserInfo;
import com.tukorea.planding.domain.user.entity.User;
import com.tukorea.planding.domain.user.service.UserQueryService;
Expand All @@ -24,6 +26,7 @@ public class GroupRoomService {
private final UserQueryService userQueryService;
private final UserGroupService userGroupService;
private final GroupQueryService groupQueryService;
private final GroupRoomRepository groupRoomRepository;

@Transactional
public GroupResponse createGroupRoom(UserInfo userInfo, GroupCreateRequest createGroupRoom) {
Expand All @@ -44,7 +47,7 @@ public GroupResponse createGroupRoom(UserInfo userInfo, GroupCreateRequest creat
public GroupResponse updateGroupNameOrDescription(UserInfo userInfo, GroupUpdateRequest groupUpdateRequest) {
User user = userQueryService.getUserByUserCode(userInfo);

GroupRoom groupRoom = groupQueryService.getGroupByCode(groupUpdateRequest.groupCode());
GroupRoom groupRoom = groupQueryService.getGroupById(groupUpdateRequest.groupId());

// TODO 그룹의 팀원도 변경가능하도록
if (!groupRoom.getOwner().equals(user.getUserCode())) {
Expand All @@ -60,13 +63,21 @@ public GroupResponse updateGroupNameOrDescription(UserInfo userInfo, GroupUpdate
public List<GroupResponse> getAllGroupRoomByUser(UserInfo userInfo) {
User user = userQueryService.getUserByUserCode(userInfo);

List<GroupRoom> groupRooms = groupQueryService.findGroupsByUser(user);
List<GroupRoom> groupRooms = groupQueryService.findGroupsByUserId(user.getId());

return groupRooms.stream()
.map(this::toGroupResponse)
.collect(Collectors.toList());
}

public List<GroupUserResponse> getGroupUsers(Long groupId) {
GroupRoom groupRoom = groupQueryService.getGroupById(groupId);
List<User> users = groupQueryService.getGroupUsers(groupRoom.getId());
return users.stream()
.map(GroupUserResponse::toGroupUserResponse)
.collect(Collectors.toList());
}

private GroupResponse toGroupResponse(GroupRoom groupRoom) {
return GroupResponse.from(groupRoom);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,5 @@ public CommonResponse<ScheduleResponse> updateScheduleByGroupRoom(@PathVariable
public ResponseEntity<ScheduleResponse> deleteScheduleByGroupRoom(@PathVariable Long groupRoomId, @PathVariable Long scheduleId, @AuthenticationPrincipal UserInfo userInfo) {
scheduleService.deleteScheduleByGroupRoom(groupRoomId, scheduleId, userInfo);
return new ResponseEntity<>(HttpStatus.OK);

}
}
Loading

0 comments on commit e7ffe21

Please sign in to comment.