From 7ac3b3173a9d65206de1392dc3cf76e554733eb6 Mon Sep 17 00:00:00 2001 From: asebn1 Date: Thu, 27 Oct 2022 14:56:08 +0900 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20=EB=A9=94=EC=84=B8=EC=A7=80=20?= =?UTF-8?q?=EC=A2=8B=EC=95=84=EC=9A=94=EC=8B=9C=20=EB=82=99=EA=B4=80?= =?UTF-8?q?=EC=A0=81=20=EB=9D=BD=EC=9D=84=20=EC=A0=81=EC=9A=A9=ED=95=9C?= =?UTF-8?q?=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../naepyeon/domain/message/Message.java | 4 ++++ .../message/MessageRepositoryCustom.java | 8 +++++++ .../message/MessageRepositoryImpl.java | 24 +++++++++++++------ .../naepyeon/service/MessageService.java | 2 +- 4 files changed, 30 insertions(+), 8 deletions(-) diff --git a/backend/src/main/java/com/woowacourse/naepyeon/domain/message/Message.java b/backend/src/main/java/com/woowacourse/naepyeon/domain/message/Message.java index d72db618..d6a3adb3 100644 --- a/backend/src/main/java/com/woowacourse/naepyeon/domain/message/Message.java +++ b/backend/src/main/java/com/woowacourse/naepyeon/domain/message/Message.java @@ -19,6 +19,7 @@ import lombok.AccessLevel; import lombok.Getter; import lombok.NoArgsConstructor; +import org.springframework.data.annotation.Version; @Entity @Getter @@ -58,6 +59,9 @@ public class Message extends BaseEntity { @Column(name = "likes", nullable = false) private Long likes; + @Version + private Integer version; + public Message(final String content, final String color, final Member author, final Rollingpaper rollingpaper, final boolean anonymous, final boolean secret, final Long likes) { validateContentLength(content); diff --git a/backend/src/main/java/com/woowacourse/naepyeon/repository/message/MessageRepositoryCustom.java b/backend/src/main/java/com/woowacourse/naepyeon/repository/message/MessageRepositoryCustom.java index 9bfa563c..67531913 100644 --- a/backend/src/main/java/com/woowacourse/naepyeon/repository/message/MessageRepositoryCustom.java +++ b/backend/src/main/java/com/woowacourse/naepyeon/repository/message/MessageRepositoryCustom.java @@ -3,12 +3,20 @@ import com.woowacourse.naepyeon.domain.message.Message; import com.woowacourse.naepyeon.service.dto.WrittenMessageResponseDto; import java.util.List; +import java.util.Optional; + import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; +import org.springframework.data.jpa.repository.Lock; + +import javax.persistence.LockModeType; public interface MessageRepositoryCustom { List findAllByRollingpaperId(final Long rollingpaperId); Page findAllByAuthorId(final Long authorId, final Pageable pageRequest); + + @Lock(LockModeType.OPTIMISTIC) + Optional findByIdForUpdate(Long id); } diff --git a/backend/src/main/java/com/woowacourse/naepyeon/repository/message/MessageRepositoryImpl.java b/backend/src/main/java/com/woowacourse/naepyeon/repository/message/MessageRepositoryImpl.java index 92ebfd61..b027cac1 100644 --- a/backend/src/main/java/com/woowacourse/naepyeon/repository/message/MessageRepositoryImpl.java +++ b/backend/src/main/java/com/woowacourse/naepyeon/repository/message/MessageRepositoryImpl.java @@ -1,10 +1,5 @@ package com.woowacourse.naepyeon.repository.message; -import static com.woowacourse.naepyeon.domain.QTeam.team; -import static com.woowacourse.naepyeon.domain.QTeamParticipation.teamParticipation; -import static com.woowacourse.naepyeon.domain.message.QMessage.message; -import static com.woowacourse.naepyeon.domain.rollingpaper.QRollingpaper.rollingpaper; - import com.querydsl.core.BooleanBuilder; import com.querydsl.core.types.ConstructorExpression; import com.querydsl.core.types.Projections; @@ -15,13 +10,20 @@ import com.woowacourse.naepyeon.domain.message.Message; import com.woowacourse.naepyeon.domain.rollingpaper.Recipient; import com.woowacourse.naepyeon.service.dto.WrittenMessageResponseDto; -import java.util.List; -import java.util.function.Supplier; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.support.PageableExecutionUtils; +import java.util.List; +import java.util.Optional; +import java.util.function.Supplier; + +import static com.woowacourse.naepyeon.domain.QTeam.team; +import static com.woowacourse.naepyeon.domain.QTeamParticipation.teamParticipation; +import static com.woowacourse.naepyeon.domain.message.QMessage.message; +import static com.woowacourse.naepyeon.domain.rollingpaper.QRollingpaper.rollingpaper; + @RequiredArgsConstructor public class MessageRepositoryImpl implements MessageRepositoryCustom { @@ -58,6 +60,14 @@ public Page findAllByAuthorId(final Long authorId, fi return PageableExecutionUtils.getPage(content, pageRequest, countQuery::fetchOne); } + @Override + public Optional findByIdForUpdate(Long id) { + return Optional.ofNullable(queryFactory + .selectFrom(message) + .where(message.id.eq(id)) + .fetchOne()); + } + private ConstructorExpression makeProjections() { return Projections.constructor(WrittenMessageResponseDto.class, message.id, diff --git a/backend/src/main/java/com/woowacourse/naepyeon/service/MessageService.java b/backend/src/main/java/com/woowacourse/naepyeon/service/MessageService.java index 88ca3b90..c26774bb 100644 --- a/backend/src/main/java/com/woowacourse/naepyeon/service/MessageService.java +++ b/backend/src/main/java/com/woowacourse/naepyeon/service/MessageService.java @@ -197,7 +197,7 @@ private void validateAuthor(final Long memberId, final Message message) { } public MessageLikeResponseDto likeMessage(Long memberId, Long rollingpaperId, Long messageId) { - final Message message = messageRepository.findById(messageId) + final Message message = messageRepository.findByIdForUpdate(messageId) .orElseThrow(() -> new NotFoundMessageException(messageId)); if (messageLikeRepository.existsByMemberIdAndMessageId(memberId, messageId)) { throw new InvalidLikeMessageException(messageId, messageId); From 1686baeaf421defdbbeeae35a81d3b68dee8dcde Mon Sep 17 00:00:00 2001 From: asebn1 Date: Thu, 27 Oct 2022 15:38:14 +0900 Subject: [PATCH 2/3] =?UTF-8?q?refactor:=20MessageRepo=20final=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../naepyeon/repository/message/MessageRepositoryCustom.java | 2 +- .../naepyeon/repository/message/MessageRepositoryImpl.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/src/main/java/com/woowacourse/naepyeon/repository/message/MessageRepositoryCustom.java b/backend/src/main/java/com/woowacourse/naepyeon/repository/message/MessageRepositoryCustom.java index 67531913..6deba4c6 100644 --- a/backend/src/main/java/com/woowacourse/naepyeon/repository/message/MessageRepositoryCustom.java +++ b/backend/src/main/java/com/woowacourse/naepyeon/repository/message/MessageRepositoryCustom.java @@ -18,5 +18,5 @@ public interface MessageRepositoryCustom { Page findAllByAuthorId(final Long authorId, final Pageable pageRequest); @Lock(LockModeType.OPTIMISTIC) - Optional findByIdForUpdate(Long id); + Optional findByIdForUpdate(final Long id); } diff --git a/backend/src/main/java/com/woowacourse/naepyeon/repository/message/MessageRepositoryImpl.java b/backend/src/main/java/com/woowacourse/naepyeon/repository/message/MessageRepositoryImpl.java index b027cac1..85f4161b 100644 --- a/backend/src/main/java/com/woowacourse/naepyeon/repository/message/MessageRepositoryImpl.java +++ b/backend/src/main/java/com/woowacourse/naepyeon/repository/message/MessageRepositoryImpl.java @@ -61,7 +61,7 @@ public Page findAllByAuthorId(final Long authorId, fi } @Override - public Optional findByIdForUpdate(Long id) { + public Optional findByIdForUpdate(final Long id) { return Optional.ofNullable(queryFactory .selectFrom(message) .where(message.id.eq(id)) From 60abfd030d18caa6cfc53fe928ede649440edaa1 Mon Sep 17 00:00:00 2001 From: asebn1 Date: Fri, 28 Oct 2022 16:12:53 +0900 Subject: [PATCH 3/3] =?UTF-8?q?refactor:=20message=EC=9D=98=20version?= =?UTF-8?q?=EC=9D=84=20flyway=EC=97=90=20=EC=B6=94=EA=B0=80=ED=95=9C?= =?UTF-8?q?=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/woowacourse/naepyeon/domain/message/Message.java | 2 +- .../src/main/resources/db/migration/V0.5.3__message_version.sql | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 backend/src/main/resources/db/migration/V0.5.3__message_version.sql diff --git a/backend/src/main/java/com/woowacourse/naepyeon/domain/message/Message.java b/backend/src/main/java/com/woowacourse/naepyeon/domain/message/Message.java index d6a3adb3..ac645abb 100644 --- a/backend/src/main/java/com/woowacourse/naepyeon/domain/message/Message.java +++ b/backend/src/main/java/com/woowacourse/naepyeon/domain/message/Message.java @@ -60,7 +60,7 @@ public class Message extends BaseEntity { private Long likes; @Version - private Integer version; + private Long version; public Message(final String content, final String color, final Member author, final Rollingpaper rollingpaper, final boolean anonymous, final boolean secret, final Long likes) { diff --git a/backend/src/main/resources/db/migration/V0.5.3__message_version.sql b/backend/src/main/resources/db/migration/V0.5.3__message_version.sql new file mode 100644 index 00000000..950e463b --- /dev/null +++ b/backend/src/main/resources/db/migration/V0.5.3__message_version.sql @@ -0,0 +1,2 @@ +ALTER TABLE message + ADD version bigint default 0;