Skip to content

Commit

Permalink
[OING-341] feature: 가족 이름 수정 API 추가 및 조회 API 응답 수정 (#264)
Browse files Browse the repository at this point in the history
* feature: add familyName field in api

* feature: add UpdateFamilyName API

* fix: fix failed test related to family

* test: add updateFamilyName test code

* refactor: add familyNameEditorId to FamilyResponse
  • Loading branch information
Ji-soo708 committed Jun 21, 2024
1 parent e3cd40f commit 55ed365
Show file tree
Hide file tree
Showing 13 changed files with 177 additions and 5 deletions.
11 changes: 11 additions & 0 deletions family/src/main/java/com/oing/controller/FamilyController.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.oing.controller;

import com.oing.domain.Family;
import com.oing.dto.request.UpdateFamilyNameRequest;
import com.oing.dto.response.FamilyResponse;
import com.oing.exception.AuthorizationFailedException;
import com.oing.restapi.FamilyApi;
Expand Down Expand Up @@ -28,4 +29,14 @@ public FamilyResponse getFamilyCreatedAt(String familyId, String loginFamilyId)
Family family = familyService.getFamilyById(familyId);
return FamilyResponse.of(family);
}

@Override
public FamilyResponse updateFamilyName(String familyId, String loginFamilyId, String loginMemberId, UpdateFamilyNameRequest request) {
if (!familyId.equals(loginFamilyId)) {
throw new AuthorizationFailedException();
}

Family family = familyService.updateFamilyName(familyId, loginMemberId, request.familyName());
return FamilyResponse.of(family);
}
}
25 changes: 24 additions & 1 deletion family/src/main/java/com/oing/domain/Family.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.security.InvalidParameterException;

@Entity(name = "family")
@NoArgsConstructor(access = lombok.AccessLevel.PROTECTED)
@AllArgsConstructor
Expand All @@ -20,13 +22,17 @@ public class Family extends BaseEntity {
@Column(name = "family_name", columnDefinition = "CHAR(10)")
private String familyName;

@Column(name = "family_name_editor_id", columnDefinition = "CHAR(26)")
private String familyNameEditorId;

@Column(name = "score", nullable = false)
private Integer score = 0;


public Family(String id, String familyName) {
public Family(String id, String familyName, String familyNameEditorId) {
this.id = id;
this.familyName = familyName;
this.familyNameEditorId = familyNameEditorId;
}

public static final int NEW_POST_SCORE = 20;
Expand Down Expand Up @@ -86,4 +92,21 @@ public void subtractScore(int score) {
public void resetScore() {
this.score = 0;
}

public void updateFamilyName(String familyName, String loginFamilyId) {
if (familyName == null) {
this.familyName = null;
this.familyNameEditorId = null;
} else {
validateFamilyName(familyName);
this.familyName = familyName;
this.familyNameEditorId = loginFamilyId;
}
}

private void validateFamilyName(String familyName) {
if ((familyName.codePoints().count() > 10) || familyName.isBlank()) {
throw new InvalidParameterException();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.oing.dto.request;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.Size;

@Schema(description = "가족 이름 수정 요청")
public record UpdateFamilyNameRequest(

@Size(max = 10)
@Schema(description = "가족 이름", example = "오잉")
String familyName
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,20 @@ public record FamilyResponse(
@Schema(description = "가족 ID", example = "01HGW2N7EHJVJ4CJ999RRS2E97")
String familyId,

@Schema(description = "가족 이름", example = "미밍이네")
String familyName,

@Schema(description = "가족 이름 마지막 수정자 ID", example = "01HGW2N7EHJVJ4CJ999RRS2E97")
String familyNameEditorId,

@Schema(description = "가족 생성 시간", example = "2021-12-05T12:30:00.000+09:00")
ZonedDateTime createdAt
) {
public static FamilyResponse of(Family family) {
return new FamilyResponse(
family.getId(),
family.getFamilyName(),
family.getFamilyNameEditorId(),
family.getCreatedAt().atZone(ZoneId.systemDefault())
);
}
Expand Down
21 changes: 21 additions & 0 deletions family/src/main/java/com/oing/restapi/FamilyApi.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.oing.restapi;

import com.oing.dto.request.UpdateFamilyNameRequest;
import com.oing.dto.response.FamilyResponse;
import com.oing.util.security.LoginFamilyId;
import com.oing.util.security.LoginMemberId;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
Expand All @@ -28,4 +30,23 @@ FamilyResponse getFamilyCreatedAt(
@LoginFamilyId
String loginFamilyId
);

@Operation(summary = "가족 이름 변경", description = "가족 이름을 변경합니다.")
@PutMapping("/{familyId}/name")
FamilyResponse updateFamilyName(
@Parameter(description = "가족 ID", example = "01HGW2N7EHJVJ4CJ999RRS2E97")
@PathVariable
String familyId,

@Parameter(hidden = true)
@LoginFamilyId
String loginFamilyId,

@Parameter(hidden = true)
@LoginMemberId
String loginMemberId,

@RequestBody
UpdateFamilyNameRequest request
);
}
9 changes: 8 additions & 1 deletion family/src/main/java/com/oing/service/FamilyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class FamilyService {

@Transactional
public Family createFamily() {
Family family = new Family(identityGenerator.generateIdentity(), null);
Family family = new Family(identityGenerator.generateIdentity(), null, null);
return familyRepository.save(family);
}

Expand Down Expand Up @@ -57,4 +57,11 @@ private int calculateLiveFamilyTopPercentage(String familyId) {
// score 를 통한 순위를 통해 전체 가족들 중 상위 백분율 계산 (1%에 가까울수록 고순위)
return familyScoreBridge.calculateFamilyTopPercentage(rank, familiesCount);
}

@Transactional
public Family updateFamilyName(String familyId, String loginMemberId, String familyName) {
Family family = getFamilyById(familyId);
family.updateFamilyName(familyName, loginMemberId);
return family;
}
}
80 changes: 80 additions & 0 deletions family/src/test/java/com/oing/service/FamilyServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.oing.service;

import com.oing.domain.Family;
import com.oing.repository.FamilyRepository;
import com.oing.util.IdentityGenerator;
import jakarta.transaction.Transactional;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.test.context.ActiveProfiles;

import java.security.InvalidParameterException;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.when;

@Transactional
@ActiveProfiles("test")
@ExtendWith(MockitoExtension.class)
public class FamilyServiceTest {

@InjectMocks
private FamilyService familyService;
@Mock
private FamilyRepository familyRepository;
@Mock
private FamilyTopPercentageHistoryService familyTopPercentageHistoryService;
@Mock
private FamilyScoreBridge familyScoreBridge;
@Mock
private IdentityGenerator identityGenerator;

@Test
void _이름_수정_테스트() {
// given
String newName = "new-name";
String memberId = "1";
String familyId = "1";

// when
when(familyRepository.findById(familyId)).thenReturn(Optional.of(new Family(familyId, null, null)));
Family family = familyService.updateFamilyName(familyId, memberId, newName);

// then
assertEquals(newName, family.getFamilyName());
assertEquals(memberId, family.getFamilyNameEditorId());
}

@Test
void__초과_가_이름_수정_예외_검증() {
// given
String newName = "wrong-length-name";
String memberId = "1";
String familyId = "1";

// when
when(familyRepository.findById(familyId)).thenReturn(Optional.of(new Family(familyId, null, null)));

// then
assertThrows(InvalidParameterException.class, () -> familyService.updateFamilyName(familyId, memberId, newName));
}

@Test
void 공백만을_포함한_이름_수정_예외_검증() {
// given
String newName = " ";
String memberId = "1";
String familyId = "1";

// when
when(familyRepository.findById(familyId)).thenReturn(Optional.of(new Family(familyId, null, null)));

// then
assertThrows(InvalidParameterException.class, () -> familyService.updateFamilyName(familyId, memberId, newName));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class MainViewController implements MainViewApi {
private final MemberBridge memberBridge;
private final MissionBridge missionBridge;
private final PostController postController;
private final FamilyBridge familyBridge;

private static final int PAGE_FETCH_SIZE = 1000;

Expand Down Expand Up @@ -75,10 +76,12 @@ public DaytimePageResponse getDaytimePage(
.isMeMissionUploadedToday();
int leftUploadCountUntilMissionUnlock = postController.getRemainingSurvivalPostCount(loginMemberId, loginMemberId, familyId)
.leftUploadCountUntilMissionUnlock();
String familyName = familyBridge.findFamilyNameByFamilyId(familyId);

return new DaytimePageResponse(
members.stream().sorted(comparator).map((member) -> new MainPageTopBarResponse(
member.memberId(),
familyName,
member.imageUrl(),
String.valueOf(member.name().charAt(0)),
member.name(),
Expand Down Expand Up @@ -143,8 +146,10 @@ public DaytimePageResponse getDaytimePage(
public NighttimePageResponse getNighttimePage(String loginMemberId, String loginFamilyId) {
Page<FamilyMemberProfileResponse> members = memberService.getFamilyMembersProfilesByFamilyId(loginFamilyId, 1, PAGE_FETCH_SIZE);
LocalDate today = ZonedDateTime.now().toLocalDate();
String familyName = familyBridge.findFamilyNameByFamilyId(loginFamilyId);
List<MainPageTopBarResponse> mainPageTopBarResponses = members.stream().map((member) -> new MainPageTopBarResponse(
member.memberId(),
familyName,
member.imageUrl(),
String.valueOf(member.name().charAt(0)),
member.name(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public record MainPageTopBarResponse(
@Schema(description = "멤버ID", example = "01HGW2N7EHJVJ4CJ999RRS2E97")
String memberId,

@Schema(description = "가족 이름", example = "미밍이네")
String familyName,

@Schema(description = "이미지 URL", example = "https://no5ing.com/image/01HGW2N7EHJVJ4CJ999RRS2E97")
String imageUrl,

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE `family` ADD COLUMN (`family_name_editor_id` CHAR(26) COMMENT 'ULID');
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class FamilyScoreEventListenerTest {

@BeforeEach
void setUp() {
familyRepository.save(new Family(testMember1.getFamilyId(), null));
familyRepository.save(new Family(testMember1.getFamilyId(), null, null));
memberRepository.save(testMember1);
memberRepository.save(testMember2);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public class MemberRepositoryCustomTest {
@BeforeEach
void setup() {
// Family & Members
familyRepository.save(new Family("testFamily", null));
familyRepository.save(new Family("testFamily", null, null));
memberRepository.save(testMember1);
memberRepository.save(testMember2);
memberRepository.save(testMember3);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class PostRepositoryCustomTest {
@BeforeEach
void setup() {
// Family & Members
familyRepository.save(new Family("testFamily", null));
familyRepository.save(new Family("testFamily", null, null));
memberRepository.save(testMember1);
memberRepository.save(testMember2);
memberRepository.save(testMember3);
Expand Down

0 comments on commit 55ed365

Please sign in to comment.