Skip to content

Commit

Permalink
Merge pull request #32 from Na-o-man/feature/#10/ShareGroup-join-api
Browse files Browse the repository at this point in the history
[FEAT] 공유그룹 참여 api 구현
  • Loading branch information
bflykky authored Jul 31, 2024
2 parents 92fbfee + 8577b24 commit 045a4fb
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,13 @@ public ResultResponse<ShareGroupResponse.ShareGroupInfo> getShareGroupInfo(@Path
ShareGroupConverter.toShareGroupDetailInfoDTO(shareGroup, profileList));
}

@PostMapping("/join")
@Operation(summary = "공유그룹 참여 API", description = "특정 공유그룹에 참여하는 API입니다.")
public ResultResponse<ShareGroupResponse.ShareGroupId> joinShareGroup(@Valid @RequestBody ShareGroupRequest.JoinShareGroupRequest request,
@LoginMember Member member) {
ShareGroup shareGroup = shareGroupService.joinShareGroup(request.getShareGroupId(), request.getProfileId(), member);
return ResultResponse.of(ShareGroupResultCode.JOIN_SHARE_GROUP,
ShareGroupConverter.toJoinShareGroupInfoDTO(shareGroup));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ public static ShareGroupResponse.ShareGroupInfo toShareGroupInfoDTO(ShareGroup s
.build();
}

// 그룹 참여 시 반환하는 DTO
public static ShareGroupResponse.ShareGroupId toJoinShareGroupInfoDTO(ShareGroup shareGroup) {
return ShareGroupResponse.ShareGroupId.builder()
.shareGroupId(shareGroup.getId())
.build();
}

// 조회 시, 디테일한 그룹 정보를 반환하는 DTO
public static ShareGroupResponse.ShareGroupInfo toShareGroupDetailInfoDTO(ShareGroup shareGroup, List<Profile> profiles) {
List<ShareGroupResponse.ProfileInfo> profileInfoList = profiles.stream()
Expand All @@ -45,6 +52,7 @@ public static ShareGroupResponse.ShareGroupInfo toShareGroupDetailInfoDTO(ShareG
.build();
}

// profile 정보 반환 DTO
public static ShareGroupResponse.ProfileInfo toProfileInfo(Profile profile) {
return ShareGroupResponse.ProfileInfo.builder()
.profileId(profile.getId())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,15 @@ public static class createShareGroupRequest {
private String place;
}

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class JoinShareGroupRequest {
@NotNull(message = "공유그룹 ID를 입력해야 합니다.")
private Long shareGroupId;
@NotNull(message = "프로필 ID를 입력해야 합니다.")
private Long profileId;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ public static class ShareGroupInfo {
private LocalDateTime createdAt;
}

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class ShareGroupId {
private Long shareGroupId;
}

@Getter
@Builder
@NoArgsConstructor
Expand Down
23 changes: 7 additions & 16 deletions src/main/java/com/umc/naoman/domain/shareGroup/entity/Profile.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,17 @@
package com.umc.naoman.domain.shareGroup.entity;

import com.umc.naoman.domain.member.entity.Member;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.SQLRestriction;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;

@Entity
@EntityListeners(AuditingEntityListener.class)
@Table(name = "profiles")
@SQLRestriction("deleted_at is NULL")
@Getter
Expand All @@ -49,7 +35,6 @@ public class Profile {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "share_group_id")
private ShareGroup shareGroup;
@CreatedDate
@Column(name = "joined_at", nullable = false, updatable = false)
private LocalDateTime joinedAt;
@Column(name = "deleted_at")
Expand All @@ -58,4 +43,10 @@ public class Profile {
public void delete() {
this.deletedAt = LocalDateTime.now();
}

public void setInfo(Member member) {
this.member = member;
this.image = member.getImage();
this.joinedAt = LocalDateTime.now();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ public interface ShareGroupService {
ShareGroup createShareGroup(ShareGroupRequest.createShareGroupRequest request, Member member);
ShareGroup findShareGroup(Long shareGroupId);
List<Profile> findProfileList(Long shareGroupId);
ShareGroup joinShareGroup(Long shareGroupId, Long profileId, Member member);
Profile findProfile(Long profileId);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.umc.naoman.domain.shareGroup.service;

import com.umc.naoman.domain.member.entity.Member;
import com.umc.naoman.domain.member.repository.MemberRepository;
import com.umc.naoman.domain.shareGroup.converter.ShareGroupConverter;
import com.umc.naoman.domain.shareGroup.dto.ShareGroupRequest;
import com.umc.naoman.domain.shareGroup.entity.Profile;
Expand All @@ -9,6 +10,7 @@
import com.umc.naoman.domain.shareGroup.repository.ProfileRepository;
import com.umc.naoman.domain.shareGroup.repository.ShareGroupRepository;
import com.umc.naoman.global.error.BusinessException;
import com.umc.naoman.global.error.code.MemberErrorCode;
import com.umc.naoman.global.error.code.ShareGroupErrorCode;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -25,6 +27,7 @@ public class ShareGroupServiceImpl implements ShareGroupService {

private final ShareGroupRepository shareGroupRepository;
private final ProfileRepository profileRepository;
private final MemberRepository memberRepository;

@Transactional
@Override
Expand Down Expand Up @@ -59,6 +62,27 @@ public ShareGroup createShareGroup(ShareGroupRequest.createShareGroupRequest req
return savedShareGroup;
}

@Transactional
@Override
public ShareGroup joinShareGroup(Long shareGroupId, Long profileId, Member member) {

ShareGroup shareGroup = findShareGroup(shareGroupId);

//repo에서 Profile 객체 꺼내오기
Profile profile = findProfile(profileId);

//공유그룹에 해당 profile이 존재하지 않으면
if (!profile.getShareGroup().getId().equals(shareGroupId)) {
throw new BusinessException(ShareGroupErrorCode.INVALID_PROFILE_FOR_GROUP);
}

//해당 멤버(본인)을 선택한 profile에 세팅, 저장
profile.setInfo(member);
profileRepository.save(profile);

return shareGroup;
}

@Override
public ShareGroup findShareGroup(Long shareGroupId) {
return shareGroupRepository.findById(shareGroupId)
Expand All @@ -69,4 +93,11 @@ public ShareGroup findShareGroup(Long shareGroupId) {
public List<Profile> findProfileList(Long shareGroupId) {
return profileRepository.findByShareGroupId(shareGroupId);
}
}

@Override
public Profile findProfile(Long profileId) {
return profileRepository.findById(profileId)
.orElseThrow(() -> new BusinessException(ShareGroupErrorCode.PROFILE_NOT_FOUND));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public enum ShareGroupErrorCode implements ErrorCode {

SHARE_GROUP_NOT_FOUND(404, "EG005", "공유 그룹을 찾을 수 없습니다."),

PROFILE_NOT_FOUND(404, "EG006", "프로필을 찾을 수 없습니다."),
INVALID_PROFILE_FOR_GROUP(400, "EG007", "해당 프로필은 이 공유 그룹에 속하지 않습니다."),

;

private final int status;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
@RequiredArgsConstructor
public enum ShareGroupResultCode implements ResultCode {
CREATE_SHARE_GROUP(200, "SG001", "성공적으로 공유 그룹을 생성하였습니다."),
SHARE_GROUP_INFO(200, "SG002", "공유 그룹을 성공적으로 조회했습니다.")
SHARE_GROUP_INFO(200, "SG002", "공유 그룹을 성공적으로 조회했습니다."),
JOIN_SHARE_GROUP(200, "SG003", "성공적으로 공유 그룹에 참여했습니다." )

;
private final int status;
private final String code;
Expand Down

0 comments on commit 045a4fb

Please sign in to comment.