Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GOORM-72]- 가족 그룹 목록 조회 API #17

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package goorm.kgu.familynote.domain.family.family.application;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import goorm.kgu.familynote.domain.family.family.domain.Family;
import goorm.kgu.familynote.domain.family.family.domain.FamilyRepository;
import goorm.kgu.familynote.domain.family.family.presentation.exception.FamilyNotFoundException;
import goorm.kgu.familynote.domain.family.family.presentation.response.FamilyListResponse;
import goorm.kgu.familynote.domain.family.family.presentation.response.FamilyResponse;
import goorm.kgu.familynote.domain.family.member.domain.FamilyMember;
import goorm.kgu.familynote.domain.user.application.UserService;
import goorm.kgu.familynote.domain.user.domain.User;
import lombok.RequiredArgsConstructor;

@Service
Expand All @@ -24,11 +29,21 @@ public FamilyResponse getFamily(Long familyId) {
}

@Transactional
public Family createFamily() {
Family family = Family.createNewFamily();
public Family createFamily(String familyName) {
Family family = Family.createNewFamily(familyName);
return familyRepository.save(family);
}

@Transactional
public FamilyListResponse getMyFamilyList() {
User user = userService.me();
List<FamilyMember> familyMembers = user.getFamilyMembers();
List<Family> family = familyMembers.stream()
.map(FamilyMember::getFamily)
.toList();
return FamilyListResponse.of(family, user.getNickname());
}

public Family getFamilyById(Long familyId) {
return familyRepository.findById(familyId)
.orElseThrow(FamilyNotFoundException::new);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import goorm.kgu.familynote.common.domain.BaseTimeEntity;
import goorm.kgu.familynote.domain.family.member.domain.FamilyMember;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
Expand All @@ -30,11 +31,16 @@ public class Family extends BaseTimeEntity {
@GeneratedValue(strategy = IDENTITY)
private Long id;

@Column(nullable = false)
private String familyName;

@OneToMany(mappedBy = "family", fetch = FetchType.LAZY)
private List<FamilyMember> familyMembers;

public static Family createNewFamily() {
return Family.builder().build();
public static Family createNewFamily(String familyName) {
return Family.builder()
.familyName(familyName)
.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import goorm.kgu.familynote.common.exception.ExceptionResponse;
import goorm.kgu.familynote.domain.family.family.application.FamilyService;
import goorm.kgu.familynote.domain.family.family.presentation.response.FamilyListResponse;
import goorm.kgu.familynote.domain.family.family.presentation.response.FamilyResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
Expand Down Expand Up @@ -51,4 +52,28 @@ public ResponseEntity<FamilyResponse> getFamily(
FamilyResponse response = familyService.getFamily(familyId);
return ResponseEntity.ok(response);
}

@Operation(summary = "가족 그룹 목록 조회", description = "내가 속한 가족 그룹 목록을 조회합니다.")
@ApiResponses({
@ApiResponse(
responseCode = "200",
description = "가족 그룹 목록 조회 성공",
content = @Content(schema = @Schema(implementation = FamilyListResponse.class))
),
@ApiResponse(
responseCode = "403",
description = "사용자 인증에 실패하였습니다.",
content = @Content(schema = @Schema(implementation = ExceptionResponse.class))
),
@ApiResponse(
responseCode = "404",
description = "가족 그룹이 존재하지 않습니다./ 사용자를 찾을 수 없습니다.",
content = @Content(schema = @Schema(implementation = ExceptionResponse.class))
)
})
@GetMapping("/list")
public ResponseEntity<FamilyListResponse> getFamilyList() {
FamilyListResponse response = familyService.getMyFamilyList();
return ResponseEntity.ok(response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package goorm.kgu.familynote.domain.family.family.presentation.response;

import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED;

import java.util.List;
import java.util.stream.Collectors;

import goorm.kgu.familynote.domain.family.family.domain.Family;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Builder;

@Builder
public record FamilyListResponse(
@Schema(
description = "가족 그룹 목록",
example = "[{\"familyId\": 1, \"familyName\": \"행복한 우리집\", \"myName\": \"이한음\", \"familyMembers\": [{\"familyMemberId\": 1, \"nickName\": \"이한음\"}, {\"familyMemberId\": 2, \"nickName\": \"전민주\"}]}]",
requiredMode = REQUIRED
)
List<FamilyResponse> contents
) {
public static FamilyListResponse of(List<Family> familyList, String myName) {
return FamilyListResponse.builder()
.contents(familyList.stream()
.map(family -> FamilyResponse.of(family, myName))
.collect(Collectors.toList())
)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,37 @@
package goorm.kgu.familynote.domain.family.family.presentation.response;

import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED;

import java.util.List;

import goorm.kgu.familynote.domain.family.family.domain.Family;
import goorm.kgu.familynote.domain.family.member.presentation.response.FamilyMemberResponse;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Builder;

@Builder
public record FamilyResponse(
@Schema(description = "가족 그룹 ID", example = "1", requiredMode = REQUIRED)
Long familyId,

@Schema(description = "가족 그룹 이름", example = "행복한 우리집", requiredMode = REQUIRED)
String familyName,

@Schema(description = "내 이름", example = "이한음", requiredMode = REQUIRED)
String myName,

@Schema(
description = "가족 구성원 목록",
example = "[{\"familyMemberId\": 1, \"nickName\": \"이한음\"}, {\"familyMemberId\": 2, \"nickName\": \"전민주\"}]",
requiredMode = REQUIRED
)
List<FamilyMemberResponse> familyMembers
) {

public static FamilyResponse of(Family family, String myName) {
return FamilyResponse.builder()
.familyId(family.getId())
.familyName(family.getFamilyName())
.myName(myName)
.familyMembers(FamilyMemberResponse.of(family.getFamilyMembers()))
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ public class FamilyMemberService {

@Transactional
public FamilyPersistResponse saveFamilyMember(FamilyMemberCreateRequest request) {
Family family = familyService.createFamily();
Family family = familyService.createFamily(request.familyName());
List<Long> users = request.userIds();
users.forEach(userId -> {
User user = userService.getUserById(userId);
FamilyMember familyMember = FamilyMember.create(family, user);
familyMemberRepository.save(familyMember);
});
FamilyMember me = FamilyMember.create(family, userService.me());
familyMemberRepository.save(me);
return FamilyPersistResponse.of(family.getId());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ public class FamilyMember extends BaseTimeEntity {
@GeneratedValue(strategy = IDENTITY)
private Long id;

private String role;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

public record FamilyMemberCreateRequest(
@Schema(description = "가족 구성원 유저 ID", example = "[1, 2, 3]", requiredMode = REQUIRED)
List<Long> userIds
List<Long> userIds,

@Schema(description = "가족 그룹 이름", example = "행복한 우리집", requiredMode = REQUIRED)
String familyName
) {
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package goorm.kgu.familynote.domain.family.member.presentation.response;

import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED;

import java.util.List;
import java.util.stream.Collectors;

import goorm.kgu.familynote.domain.family.member.domain.FamilyMember;
import io.swagger.v3.oas.annotations.media.Schema;

public record FamilyMemberResponse(
@Schema(description = "가족 구성원 ID", example = "1", requiredMode = REQUIRED)
Long familyMemberId,
String nickName,
String role

@Schema(description = "닉네임", example = "이한음", requiredMode = REQUIRED)
String nickName
) {

public static List<FamilyMemberResponse> of(List<FamilyMember> familyMembers) {
Expand All @@ -20,8 +25,7 @@ public static List<FamilyMemberResponse> of(List<FamilyMember> familyMembers) {
public static FamilyMemberResponse of(FamilyMember familyMember) {
return new FamilyMemberResponse(
familyMember.getId(),
familyMember.getUser().getNickname(),
familyMember.getRole()
familyMember.getUser().getNickname()
);
}
}
11 changes: 11 additions & 0 deletions src/main/java/goorm/kgu/familynote/domain/user/domain/User.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
package goorm.kgu.familynote.domain.user.domain;

import static jakarta.persistence.CascadeType.ALL;
import static jakarta.persistence.FetchType.LAZY;
import static jakarta.persistence.GenerationType.IDENTITY;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import goorm.kgu.familynote.common.domain.BaseTimeEntity;
import goorm.kgu.familynote.domain.family.member.domain.FamilyMember;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.OneToOne;
import jakarta.persistence.Table;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
Expand Down Expand Up @@ -41,6 +49,9 @@ public class User extends BaseTimeEntity implements UserDetails {

private String refreshToken;

@OneToMany(mappedBy = "user", fetch = LAZY, cascade = ALL, orphanRemoval = true)
List<FamilyMember> familyMembers = new ArrayList<>();

public static User create(String nickname, String password) {
return User.builder()
.nickname(nickname)
Expand Down