Skip to content

Commit

Permalink
[OING-193] feat: 회원 리얼이모지 familyId 컬럼 추가 및 로직 수정 (#152)
Browse files Browse the repository at this point in the history
* feature: add familyId column to MemberRealEmoji

* feature: add familyId parameter to MemberRealEmoji logic

* refactor: refact MemberRealEmojiApi logic

* test: fix test error due to added familyId

* style: delete unused method

* test: fix MemberPostRealEmoji test due to added familyId parameter

* test: add getMemberRealEmojis when member quit group
  • Loading branch information
Ji-soo708 authored and CChuYong committed Feb 11, 2024
1 parent 5891721 commit 59e309a
Show file tree
Hide file tree
Showing 13 changed files with 115 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE `member_real_emoji` ADD COLUMN (`family_id` CHAR(26) NOT NULL DEFAULT '0' COMMENT 'ULID');
UPDATE member_real_emoji JOIN member ON member_real_emoji.member_id = member.member_id
SET member_real_emoji.family_id = member.family_id;
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ public class MemberPostRealEmojiApiTest {

@BeforeEach
void setUp() {
memberRepository.save(new Member(TEST_MEMBER_ID, "testUser1", LocalDate.now(), "",
memberRepository.save(new Member(TEST_MEMBER_ID, TEST_FAMILY_ID, LocalDate.now(), "",
"", "",
LocalDateTime.now()));
TEST_MEMBER_TOKEN = tokenGenerator.generateTokenPair(TEST_MEMBER_ID).accessToken();

memberPostRepository.save(new MemberPost(TEST_POST_ID, TEST_MEMBER_ID, TEST_FAMILY_ID, "img", "img",
"content"));

memberRealEmojiRepository.save(new MemberRealEmoji(TEST_REAL_EMOJI_ID, TEST_MEMBER_ID, Emoji.EMOJI_1,
memberRealEmojiRepository.save(new MemberRealEmoji(TEST_REAL_EMOJI_ID, TEST_MEMBER_ID, TEST_FAMILY_ID, Emoji.EMOJI_1,
"https://test.com/bucket/real-emoji.jpg", "bucket/real-emoji.jpg"));

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class MemberRealEmojiApiTest {
private ObjectMapper objectMapper;

private String TEST_MEMBER_ID = "01HGW2N7EHJVJ4CJ999RRS2E97";
private String TEST_FAMILY_ID = "01HGW2N7EHJVJ4CJ999RRS2E44";
private String TEST_MEMBER_REAL_EMOJI_ID = "01HGW2N7EHJVJ4CJ999RRS2A97";
private String TEST_MEMBER_TOKEN;

Expand All @@ -55,7 +56,7 @@ void setUp() {
memberRepository.save(
new Member(
TEST_MEMBER_ID,
"testUser1",
TEST_FAMILY_ID,
LocalDate.now(),
"", "", "",
LocalDateTime.now()
Expand Down Expand Up @@ -116,6 +117,7 @@ void setUp() {
new MemberRealEmoji(
TEST_MEMBER_REAL_EMOJI_ID,
TEST_MEMBER_ID,
TEST_FAMILY_ID,
Emoji.EMOJI_1,
"https://test.com/bucket/images/defaultEmoji.jpg",
"images/defaultEmoji.jpg"
Expand Down Expand Up @@ -144,6 +146,7 @@ void setUp() {
new MemberRealEmoji(
TEST_MEMBER_REAL_EMOJI_ID,
TEST_MEMBER_ID,
TEST_FAMILY_ID,
Emoji.EMOJI_1,
realEmojiImageUrl,
"images/defaultEmoji.jpg"
Expand Down
4 changes: 0 additions & 4 deletions member/src/main/java/com/oing/domain/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ public class Member extends DeletableBaseAuditEntity {
private LocalDateTime familyJoinAt;


public void setProfileImgKey(String profileImgKey) {
this.profileImgKey = profileImgKey;
}


public void updateProfileImg(String profileImgUrl, String profileImgKey) {
this.profileImgUrl = profileImgUrl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ public class MemberPostRealEmojiController implements MemberPostRealEmojiApi {
*/
@Transactional
@Override
public PostRealEmojiResponse createPostRealEmoji(String postId, PostRealEmojiRequest request) {
public PostRealEmojiResponse createPostRealEmoji(String postId, String familyId, PostRealEmojiRequest request) {
String memberId = authenticationHolder.getUserId();
MemberPost post = memberPostService.getMemberPostById(postId);
if (!memberBridge.isInSameFamily(memberId, post.getMemberId()))
throw new AuthorizationFailedException();

MemberRealEmoji realEmoji = memberRealEmojiService.getMemberRealEmojiById(request.realEmojiId());
MemberRealEmoji realEmoji = memberRealEmojiService.getMemberRealEmojiByIdAndFamilyId(request.realEmojiId(), familyId);
validatePostRealEmojiForAddition(post, memberId, realEmoji);
MemberPostRealEmoji postRealEmoji = new MemberPostRealEmoji(identityGenerator.generateIdentity(), realEmoji,
post, memberId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,40 +42,40 @@ public PreSignedUrlResponse requestPresignedUrl(String memberId, PreSignedUrlReq

@Transactional
@Override
public RealEmojiResponse createMemberRealEmoji(String memberId, CreateMyRealEmojiRequest request) {
public RealEmojiResponse createMemberRealEmoji(String memberId, String familyId, CreateMyRealEmojiRequest request) {
validateMemberId(memberId);
String emojiId = identityGenerator.generateIdentity();
String emojiImgKey = preSignedUrlGenerator.extractImageKey(request.imageUrl());
Emoji emoji = Emoji.fromString(request.type());
if (isExistsSameRealEmojiType(emoji, memberId)) {
if (isExistsSameRealEmojiType(emoji, memberId, familyId)) {
throw new DuplicateRealEmojiException();
}

MemberRealEmoji realEmoji = new MemberRealEmoji(emojiId, memberId, emoji, request.imageUrl(), emojiImgKey);
MemberRealEmoji realEmoji = new MemberRealEmoji(emojiId, memberId, familyId, emoji, request.imageUrl(), emojiImgKey);
MemberRealEmoji addedRealEmoji = memberRealEmojiService.save(realEmoji);
return RealEmojiResponse.from(addedRealEmoji);
}

private boolean isExistsSameRealEmojiType(Emoji emoji, String memberId) {
return memberRealEmojiService.findRealEmojiByEmojiTypeAndMemberId(emoji, memberId);
private boolean isExistsSameRealEmojiType(Emoji emoji, String memberId, String familyId) {
return memberRealEmojiService.findRealEmojiByEmojiTypeAndMemberIdAndFamilyId(emoji, memberId, familyId);
}

@Transactional
@Override
public RealEmojiResponse changeMemberRealEmoji(String memberId, String realEmojiId, UpdateMyRealEmojiRequest request) {
public RealEmojiResponse changeMemberRealEmoji(String memberId, String familyId, String realEmojiId, UpdateMyRealEmojiRequest request) {
validateMemberId(memberId);
String emojiImgKey = preSignedUrlGenerator.extractImageKey(request.imageUrl());

MemberRealEmoji findEmoji = memberRealEmojiService.findRealEmojiById(realEmojiId);
MemberRealEmoji findEmoji = memberRealEmojiService.getMemberRealEmojiByIdAndFamilyId(realEmojiId, familyId);
findEmoji.updateRealEmoji(request.imageUrl(), emojiImgKey);
return RealEmojiResponse.from(findEmoji);
}

@Override
public RealEmojisResponse getMemberRealEmojis(String memberId) {
public RealEmojisResponse getMemberRealEmojis(String memberId, String familyId) {
validateMemberId(memberId);

List<MemberRealEmoji> realEmojis = memberRealEmojiService.findRealEmojisByMemberId(memberId);
List<MemberRealEmoji> realEmojis = memberRealEmojiService.findRealEmojisByMemberIdAndFamilyId(memberId, familyId);
List<RealEmojiResponse> emojiResponses = realEmojis.stream()
.map(RealEmojiResponse::from)
.collect(Collectors.toList());
Expand Down
3 changes: 3 additions & 0 deletions post/src/main/java/com/oing/domain/MemberRealEmoji.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public class MemberRealEmoji extends BaseAuditEntity {
@Column(name = "member_id", columnDefinition = "CHAR(26)", nullable = false)
private String memberId;

@Column(name = "family_id", columnDefinition = "CHAR(26)", nullable = false)
private String familyId;

@Enumerated(EnumType.STRING)
@Column(name = "type", nullable = false)
private Emoji type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

public interface MemberRealEmojiRepository extends JpaRepository<MemberRealEmoji, String> {

Optional<MemberRealEmoji> findByTypeAndMemberId(Emoji emoji, String memberId);
Optional<MemberRealEmoji> findByIdAndFamilyId(String realEmojiId, String familyId);

List<MemberRealEmoji> findAllByMemberId(String memberId);
Optional<MemberRealEmoji> findByTypeAndMemberIdAndFamilyId(Emoji emoji, String memberId, String familyId);

List<MemberRealEmoji> findAllByMemberIdAndFamilyId(String memberId, String familyId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.oing.dto.request.PostRealEmojiRequest;
import com.oing.dto.response.*;
import com.oing.util.security.FamilyId;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
Expand All @@ -21,6 +22,10 @@ PostRealEmojiResponse createPostRealEmoji(
@PathVariable
String postId,

@Parameter(hidden = true)
@FamilyId
String familyId,

@Valid
@RequestBody
PostRealEmojiRequest request
Expand Down
15 changes: 14 additions & 1 deletion post/src/main/java/com/oing/restapi/MemberRealEmojiApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.oing.dto.response.PreSignedUrlResponse;
import com.oing.dto.response.RealEmojiResponse;
import com.oing.dto.response.RealEmojisResponse;
import com.oing.util.security.FamilyId;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
Expand Down Expand Up @@ -37,6 +38,10 @@ RealEmojiResponse createMemberRealEmoji(
@PathVariable
String memberId,

@Parameter(hidden = true)
@FamilyId
String familyId,

@Valid
@RequestBody
CreateMyRealEmojiRequest request
Expand All @@ -49,6 +54,10 @@ RealEmojiResponse changeMemberRealEmoji(
@PathVariable
String memberId,

@Parameter(hidden = true)
@FamilyId
String familyId,

@Parameter(description = "리얼 이모지 ID", example = "01HGW2N7EHJVJ4CJ999RRS2E97")
@PathVariable
String realEmojiId,
Expand All @@ -63,6 +72,10 @@ RealEmojiResponse changeMemberRealEmoji(
RealEmojisResponse getMemberRealEmojis(
@Parameter(description = "회원 ID", example = "01HGW2N7EHJVJ4CJ999RRS2E97")
@PathVariable
String memberId
String memberId,

@Parameter(hidden = true)
@FamilyId
String familyId
);
}
20 changes: 7 additions & 13 deletions post/src/main/java/com/oing/service/MemberRealEmojiService.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,23 @@ public class MemberRealEmojiService {
private final MemberRealEmojiRepository memberRealEmojiRepository;


public MemberRealEmoji getMemberRealEmojiById(String realEmojiId) {
return memberRealEmojiRepository.findById(realEmojiId)
public MemberRealEmoji getMemberRealEmojiByIdAndFamilyId(String realEmojiId, String familyId) {
return memberRealEmojiRepository
.findByIdAndFamilyId(realEmojiId, familyId)
.orElseThrow(RealEmojiNotFoundException::new);
}

public MemberRealEmoji save(MemberRealEmoji emoji) {
return memberRealEmojiRepository.save(emoji);
}

public MemberRealEmoji findRealEmojiById(String realEmojiId) {
return memberRealEmojiRepository
.findById(realEmojiId)
.orElseThrow(RealEmojiNotFoundException::new);
}

public boolean findRealEmojiByEmojiTypeAndMemberId(Emoji emoji, String memberId) {
public boolean findRealEmojiByEmojiTypeAndMemberIdAndFamilyId(Emoji emoji, String memberId, String familyId) {
return memberRealEmojiRepository
.findByTypeAndMemberId(emoji, memberId)
.findByTypeAndMemberIdAndFamilyId(emoji, memberId, familyId)
.isPresent();
}

public List<MemberRealEmoji> findRealEmojisByMemberId(String memberId) {
return memberRealEmojiRepository.findAllByMemberId(memberId);
public List<MemberRealEmoji> findRealEmojisByMemberIdAndFamilyId(String memberId, String familyId) {
return memberRealEmojiRepository.findAllByMemberIdAndFamilyId(memberId, familyId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,23 @@ public class MemberPostRealEmojiControllerTest {
void 게시물_리얼이모지_등록_테스트() {
//given
String memberId = "1";
String familyId = "1";
when(authenticationHolder.getUserId()).thenReturn(memberId);
when(memberBridge.isInSameFamily(memberId, memberId)).thenReturn(true);
MemberPost post = new MemberPost("1", memberId, "1", "https://oing.com/post.jpg", "post.jpg",
MemberPost post = new MemberPost("1", memberId, familyId, "https://oing.com/post.jpg", "post.jpg",
"안녕.오잉.");
MemberRealEmoji realEmoji = new MemberRealEmoji("1", memberId,
MemberRealEmoji realEmoji = new MemberRealEmoji("1", memberId, familyId,
Emoji.EMOJI_1, "https://oing.com/emoji.jpg", "emoji.jpg");
when(memberPostService.getMemberPostById(post.getId())).thenReturn(post);
when(memberRealEmojiService.getMemberRealEmojiById(realEmoji.getId())).thenReturn(realEmoji);
when(memberRealEmojiService.getMemberRealEmojiByIdAndFamilyId(realEmoji.getId(), familyId)).thenReturn(realEmoji);

MemberPostRealEmoji postRealEmoji = new MemberPostRealEmoji("1", realEmoji, post, memberId);
when(memberPostRealEmojiService.savePostRealEmoji(any(MemberPostRealEmoji.class))).thenReturn(postRealEmoji);
when(identityGenerator.generateIdentity()).thenReturn(postRealEmoji.getId());
PostRealEmojiRequest request = new PostRealEmojiRequest(realEmoji.getId());

//when
PostRealEmojiResponse response = memberPostRealEmojiController.createPostRealEmoji(post.getId(), request);
PostRealEmojiResponse response = memberPostRealEmojiController.createPostRealEmoji(post.getId(), familyId, request);

//then
assertEquals(post.getId(), response.postId());
Expand All @@ -83,10 +84,11 @@ public class MemberPostRealEmojiControllerTest {
void 권한없는_memberId로_게시물_리얼이모지_등록_예외_테스트() {
// given
String memberId = "1";
String familyId = "1";
when(authenticationHolder.getUserId()).thenReturn(memberId);
MemberPost post = new MemberPost("1", memberId, "1", "https://oing.com/post.jpg", "post.jpg",
MemberPost post = new MemberPost("1", memberId, familyId, "https://oing.com/post.jpg", "post.jpg",
"안녕.오잉.");
MemberRealEmoji realEmoji = new MemberRealEmoji("1", memberId,
MemberRealEmoji realEmoji = new MemberRealEmoji("1", memberId, familyId,
Emoji.EMOJI_1, "https://oing.com/emoji.jpg", "emoji.jpg");
when(memberPostService.getMemberPostById(post.getId())).thenReturn(post);

Expand All @@ -96,38 +98,40 @@ public class MemberPostRealEmojiControllerTest {

// then
assertThrows(AuthorizationFailedException.class,
() -> memberPostRealEmojiController.createPostRealEmoji(post.getId(), request));
() -> memberPostRealEmojiController.createPostRealEmoji(post.getId(), familyId, request));
}

@Test
void 게시물_중복된_리얼이모지_등록_예외_테스트() {
//given
String memberId = "1";
String familyId = "1";
when(authenticationHolder.getUserId()).thenReturn(memberId);
when(memberBridge.isInSameFamily(memberId, memberId)).thenReturn(true);
MemberPost post = new MemberPost("1", memberId, "1", "https://oing.com/post.jpg", "post.jpg",
MemberPost post = new MemberPost("1", memberId, familyId, "https://oing.com/post.jpg", "post.jpg",
"안녕.오잉.");
MemberRealEmoji realEmoji = new MemberRealEmoji("1", memberId, Emoji.EMOJI_1,
MemberRealEmoji realEmoji = new MemberRealEmoji("1", memberId, familyId, Emoji.EMOJI_1,
"https://oing.com/emoji.jpg", "emoji.jpg");
when(memberPostService.getMemberPostById(post.getId())).thenReturn(post);
when(memberRealEmojiService.getMemberRealEmojiById(realEmoji.getId())).thenReturn(realEmoji);
when(memberRealEmojiService.getMemberRealEmojiByIdAndFamilyId(realEmoji.getId(), familyId)).thenReturn(realEmoji);

//when
when(memberPostRealEmojiService.isMemberPostRealEmojiExists(post, memberId, realEmoji)).thenReturn(true);
PostRealEmojiRequest request = new PostRealEmojiRequest(realEmoji.getId());

//then
assertThrows(RealEmojiAlreadyExistsException.class,
() -> memberPostRealEmojiController.createPostRealEmoji(post.getId(), request));
() -> memberPostRealEmojiController.createPostRealEmoji(post.getId(), familyId, request));
}

@Test
void 게시물_리얼이모지_삭제_테스트() {
//given
String memberId = "1";
MemberPost post = new MemberPost("1", memberId, "1", "https://oing.com/post.jpg", "post.jpg",
String familyId = "1";
MemberPost post = new MemberPost("1", memberId, familyId, "https://oing.com/post.jpg", "post.jpg",
"안녕.오잉.");
MemberRealEmoji realEmoji = new MemberRealEmoji("1", memberId,
MemberRealEmoji realEmoji = new MemberRealEmoji("1", memberId, familyId,
Emoji.EMOJI_1, "https://oing.com/emoji.jpg", "emoji.jpg");
when(memberPostService.getMemberPostById(post.getId())).thenReturn(post);

Expand All @@ -142,10 +146,11 @@ public class MemberPostRealEmojiControllerTest {
void 게시물_등록되지_않은_리얼이모지_삭제_예외_테스트() {
//given
String memberId = "1";
String familyId = "1";
when(authenticationHolder.getUserId()).thenReturn(memberId);
MemberPost post = new MemberPost("1", memberId, "1","https://oing.com/post.jpg", "post.jpg",
MemberPost post = new MemberPost("1", memberId, familyId,"https://oing.com/post.jpg", "post.jpg",
"안녕.오잉.");
MemberRealEmoji realEmoji = new MemberRealEmoji("1", memberId,
MemberRealEmoji realEmoji = new MemberRealEmoji("1", memberId, familyId,
Emoji.EMOJI_1, "https://oing.com/emoji.jpg", "emoji.jpg");
when(memberPostService.getMemberPostById(post.getId())).thenReturn(post);

Expand Down
Loading

0 comments on commit 59e309a

Please sign in to comment.