Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
82everywin committed Jan 19, 2025
2 parents 6c4b1d6 + 241bb55 commit af99629
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 37 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# backend
도서 성취도 기록 서비스 backend 저장소입니다.
# backend
도서 성취도 기록 서비스 backend 저장소입니다.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
import com.bookmile.backend.domain.group.service.GroupService;
import com.bookmile.backend.domain.group.service.Impl.GroupMemberServiceImpl;
import com.bookmile.backend.global.common.CommonResponse;
import com.bookmile.backend.domain.user.entity.User;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import java.util.List;
Expand All @@ -33,26 +34,26 @@ public class GroupController {
@PostMapping
public ResponseEntity<CommonResponse<GroupCreateResponseDto>> createGroup(
@RequestBody @Valid GroupCreateRequestDto requestDto,
@RequestParam Long userId // userId를 직접 받음
@AuthenticationPrincipal UserDetails userDetails
) {
// User 정보를 UserRepository에서 조회
User user = groupService.getUserById(userId);
String userEmail = userDetails.getUsername();

// 그룹 생성 요청 처리
GroupCreateResponseDto responseDto = groupService.createGroup(requestDto, user);
GroupCreateResponseDto responseDto = groupService.createGroup(requestDto, userEmail);
return ResponseEntity.status(GROUP_CREATE.getStatus())
.body(CommonResponse.from(GROUP_CREATE.getMessage(), responseDto));
}

@Operation(summary = "그룹 참여하기", description = "이미 존재하는 그룹에 참여합니다. 참여한 유저는 자동으로 MEMBER 역할을 부여받으며 비공개 그룹의 경우 비밀번호가 필요합니다. <br>"
+ "공개 그룹의 경우 비밀번호를 null 값이 아닌 공백(그냥 띄어쓰기 한 칸)으로 입력해주세요")
@PostMapping("/{groupId}")
public ResponseEntity<CommonResponse<Void>> joinGroup(
public ResponseEntity<CommonResponse<GroupJoinResponseDto>> joinGroup(
@RequestBody @Valid GroupJoinRequestDto requestDto,
@RequestHeader("user_id") Long userId
@AuthenticationPrincipal UserDetails userDetails
) {
groupJoinService.joinGroup(userId, requestDto);
return ResponseEntity.ok(CommonResponse.from(GROUP_JOIN.getMessage(), null));
String userEmail = userDetails.getUsername();
GroupJoinResponseDto responseDto = groupJoinService.joinGroup(userEmail, requestDto);
return ResponseEntity.ok(CommonResponse.from(GROUP_JOIN.getMessage(), responseDto));
}

@Operation(summary = "그룹 멤버 조회"
Expand All @@ -67,9 +68,12 @@ public ResponseEntity<List<GroupMemberResponseDto>> getMembers(@PathVariable Lon
, description = "그룹 상태를 변경합니다. 그룹장만이 변경할 수 있습니다.")
@PatchMapping("/{groupId}")
public ResponseEntity<GroupStatusUpdateResponseDto> updateGroupStatus(
@PathVariable Long groupId,@RequestBody @Valid GroupStatusUpdateRequestDto requestDto
@PathVariable Long groupId,
@RequestBody @Valid GroupStatusUpdateRequestDto requestDto,
@AuthenticationPrincipal UserDetails userDetails
) {
GroupStatusUpdateResponseDto responseDto = groupService.updateGroupStatus(groupId, requestDto, requestDto.getUserId());
String userEmail = userDetails.getUsername();
GroupStatusUpdateResponseDto responseDto = groupService.updateGroupStatus(groupId, requestDto, userEmail);
return ResponseEntity.ok(responseDto);
}

Expand Down Expand Up @@ -115,9 +119,11 @@ public ResponseEntity<GroupDetailResponseDto> getGroupDetail(@PathVariable Long
@Operation(summary = "그룹 공개/비공개 전환", description = "그룹장은 그룹 공개여부를 변경할 수 있습니다.")
public ResponseEntity<CommonResponse<Object>> updateGroupVisibility(
@PathVariable Long groupId,
@RequestBody @Valid GroupPrivateRequestDto requestDto
@RequestBody @Valid GroupPrivateRequestDto requestDto,
@AuthenticationPrincipal UserDetails userDetails
) {
groupService.updateGroupPrivate(groupId, requestDto.getIsOpen(), requestDto.getUserId());
String userEmail = userDetails.getUsername();
groupService.updateGroupPrivate(groupId, requestDto.getIsOpen(), userEmail);
return ResponseEntity.ok(CommonResponse.from(GROUP_PRIVATE_UPDATE.getMessage()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@
@Getter
@NoArgsConstructor
public class GroupPrivateRequestDto {
private Long userId;
private Boolean isOpen;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,4 @@ public class GroupStatusUpdateRequestDto {
@Schema(description = "그룹 상태 (RECRUITING, IN_PROGRESS, COMPLETED)", example = "IN_PROGRESS")
@NotNull(message = "변경할 상태는 필수입니다.")
private GroupStatus status;

private Long userId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
import com.bookmile.backend.domain.group.dto.res.GroupJoinResponseDto;

public interface GroupJoinService {
GroupJoinResponseDto joinGroup(Long userId, GroupJoinRequestDto groupJoinRequestDto);
GroupJoinResponseDto joinGroup(String userEmail, GroupJoinRequestDto requestDto);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.bookmile.backend.domain.group.service;

import com.bookmile.backend.domain.group.dto.req.GroupCreateRequestDto;
import com.bookmile.backend.domain.group.dto.req.GroupSearchRequestDto;
import com.bookmile.backend.domain.group.dto.req.GroupStatusUpdateRequestDto;
import com.bookmile.backend.domain.group.dto.res.GroupCreateResponseDto;
import com.bookmile.backend.domain.group.dto.res.GroupDetailResponseDto;
Expand All @@ -13,14 +12,14 @@

public interface GroupService {
User getUserById(Long userId); // User 정보 조회
GroupCreateResponseDto createGroup(GroupCreateRequestDto requestDto, User user);
GroupStatusUpdateResponseDto updateGroupStatus(Long groupId, GroupStatusUpdateRequestDto requestDto, Long userId);
GroupCreateResponseDto createGroup(GroupCreateRequestDto requestDto, String userEmail);
GroupStatusUpdateResponseDto updateGroupStatus(Long groupId, GroupStatusUpdateRequestDto requestDto, String userEmail);

List<GroupListResponseDto> getRecruitingGroups(String isbn13);
List<GroupListResponseDto> getInProgressGroups(String isbn13);
List<GroupListResponseDto> getCompletedGroups(String isbn13);

GroupDetailResponseDto getGroupDetail(Long groupId);

void updateGroupPrivate(Long groupId, Boolean isOpen, Long userId);
void updateGroupPrivate(Long groupId, Boolean isOpen, String userEmail);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.bookmile.backend.domain.group.repository.GroupRepository;
import com.bookmile.backend.domain.group.service.GroupJoinService;
import com.bookmile.backend.domain.user.entity.User;
import com.bookmile.backend.domain.user.repository.UserRepository;
import com.bookmile.backend.domain.userGroup.entity.Role;
import com.bookmile.backend.domain.userGroup.entity.UserGroup;
import com.bookmile.backend.domain.userGroup.repository.UserGroupRepository;
Expand All @@ -20,18 +21,20 @@ public class GroupJoinServiceImpl implements GroupJoinService {

private final GroupRepository groupRepository;
private final UserGroupRepository userGroupRepository;
private final UserRepository userRepository;

@Override
public GroupJoinResponseDto joinGroup(Long userId, GroupJoinRequestDto groupJoinRequestDto) {
public GroupJoinResponseDto joinGroup(String userEmail, GroupJoinRequestDto requestDto) {
User user = validateUserByEmail(userEmail);

Group group = findGroup(groupJoinRequestDto.getGroupId());
Group group = findGroup(requestDto.getGroupId());

checkUserAlreadyJoined(userId, group.getId());
checkUserAlreadyJoined(user.getId(), group.getId());
checkGroupCapacity(group);
checkGroupPassword(group, groupJoinRequestDto.getPassword());
checkGroupPassword(group, requestDto.getPassword());

UserGroup userGroup = UserGroup.builder()
.user(new User(userId))
.user(user)
.group(group)
.role(Role.MEMBER)
.build();
Expand Down Expand Up @@ -64,4 +67,9 @@ private void checkGroupPassword(Group group, String password) {
throw new CustomException(StatusCode.INVALID_GROUP_PASSWORD);
}
}

private User validateUserByEmail(String email) {
return userRepository.findByEmail(email)
.orElseThrow(() -> new CustomException(StatusCode.USER_NOT_FOUND));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.bookmile.backend.domain.userGroup.entity.UserGroup;
import com.bookmile.backend.domain.userGroup.entity.Role;
import com.bookmile.backend.domain.userGroup.repository.UserGroupRepository;
import com.bookmile.backend.global.common.StatusCode;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.bookmile.backend.global.exception.CustomException;
Expand Down Expand Up @@ -48,7 +49,9 @@ public User getUserById(Long userId) {

@Override
@Transactional
public GroupCreateResponseDto createGroup(GroupCreateRequestDto requestDto, User user) {
public GroupCreateResponseDto createGroup(GroupCreateRequestDto requestDto, String userEmail) {
User user = validateUserByEmail(userEmail);

Book book = bookService.saveBook(requestDto.getIsbn13());

Template template = checkTemplate(requestDto);
Expand All @@ -57,15 +60,15 @@ public GroupCreateResponseDto createGroup(GroupCreateRequestDto requestDto, User

Group group = groupRepository.save(requestDto.toEntity(book, goalType, goalContent));

Long templateId;
Long templateId = null;
if (requestDto.getTemplateId() == null) {
template = Template.builder()
.group(group)
.goalType(goalType)
.goalContent(goalContent)
.isTemplate(true)
.build();
templateId = template.getId();
templateRepository.save(template);
} else {
templateId = requestDto.getTemplateId();
}
Expand All @@ -76,16 +79,17 @@ public GroupCreateResponseDto createGroup(GroupCreateRequestDto requestDto, User

@Override
@Transactional
public GroupStatusUpdateResponseDto updateGroupStatus(Long groupId, GroupStatusUpdateRequestDto requestDto, Long userId) {
public GroupStatusUpdateResponseDto updateGroupStatus(Long groupId, GroupStatusUpdateRequestDto requestDto, String userEmail) {
User user = validateUserByEmail(userEmail);

Group group = findGroupById(groupId);
UserGroup userGroup = findUserGroupById(userId, groupId);
UserGroup userGroup = findUserGroupById(user.getId(), groupId);

validateGroupMaster(userGroup);
updateGroupStatus(group, requestDto.getStatus());

return GroupStatusUpdateResponseDto.toDto(group);
}

private List<GroupListResponseDto> findGroupsByStatus(String isbn13, GroupStatus status, boolean isRecent) {
List<Group> groups;
if (isRecent) {
Expand Down Expand Up @@ -131,10 +135,11 @@ public GroupDetailResponseDto getGroupDetail(Long groupId) {

@Override
@Transactional
public void updateGroupPrivate(Long groupId, Boolean isOpen, Long userId) {
public void updateGroupPrivate(Long groupId, Boolean isOpen, String userEmail) {
User user = validateUserByEmail(userEmail);
Group group = findGroupById(groupId);

UserGroup userGroup = findUserGroupById(userId, groupId);
UserGroup userGroup = findUserGroupById(user.getId(), groupId);
validateGroupMaster(userGroup);

group.setIsOpen(isOpen);
Expand All @@ -145,6 +150,11 @@ private Group findGroupById(Long groupId) {
.orElseThrow(() -> new CustomException(GROUP_NOT_FOUND));
}

private User validateUserByEmail(String email) {
return userRepository.findByEmail(email)
.orElseThrow(() -> new CustomException(StatusCode.USER_NOT_FOUND));
}

private UserGroup findUserGroupById(Long userId, Long groupId) {
return userGroupRepository.findByUserIdAndGroupId(userId, groupId)
.orElseThrow(() -> new CustomException(NOT_MEMBER));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public interface UserGroupRepository extends JpaRepository<UserGroup, Long> {

List<UserGroup> findByGroupId(Long groupId);

Optional<UserGroup> findByUserIdAndGroupId(Long userId, Long groupId);
Optional<UserGroup> findByUserIdAndGroupId(Long groupId, Long userId);

@Query("SELECT ug FROM UserGroup ug WHERE ug.group.id = :groupId AND ug.role = 'MASTER'")
Optional<UserGroup> findMasterByGroupId(@Param("groupId") Long groupId);
Expand Down

0 comments on commit af99629

Please sign in to comment.