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

[HANSEL-119] Set Version Column & Refactor RegisterReviewStats #122

Open
wants to merge 7 commits into
base: release/v0.2.0
Choose a base branch
from
Open
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
Expand Up @@ -18,7 +18,9 @@
import com.peauty.persistence.designer.workspace.WorkspaceEntity;
import com.peauty.persistence.designer.workspace.WorkspaceRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.orm.ObjectOptimisticLockingFailureException;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

Expand Down Expand Up @@ -100,18 +102,33 @@ public Workspace findByDesignerId(Long userId) {
}

@Override
public Workspace registerReviewStats(Long designerId, ReviewRating newRating) {
WorkspaceEntity workspaceEntity = workspaceRepository.getByDesignerId(designerId)
.orElseThrow(() -> new PeautyException(PeautyResponseCode.NOT_EXIST_WORKSPACE));
List<BannerImageEntity> bannerImageEntities = bannerImageRepository.findByWorkspaceId(workspaceEntity.getId());
Workspace workspace = WorkspaceMapper.toDomain(workspaceEntity, bannerImageEntities);
// ๋ฆฌ๋ทฐ ์ž‘์„ฑ ๋กœ์ง
workspace.registerReviewStats(newRating);
// ์—”ํ‹ฐํ‹ฐ ๋ณ€ํ™˜ ํ›„ ์ €์žฅ
workspaceEntity = WorkspaceMapper.toEntity(workspace, designerId);
workspaceRepository.save(workspaceEntity);

return workspace;
@Transactional
public Workspace registerReviewStats(Long designerId, ReviewRating newRating
// TODO: InterruptedException์„ WorkspacePort์—์„œ Workspace registerReviewStats(Long designerId, ReviewRating newRating) throws InterruptedException; ์ด๋ ‡๊ฒŒ ๊ฑธ์–ด์ฃผ๋Š” ๊ฒƒ์ด ๋งž์„๊นŒ?
// ์•„๋‹ˆ๋ฉด ๋‚ด๋ถ€ try-catch์—์„œ InterruptedException์„ ๊ฑฐ๋Š”๊ฒŒ ๋งž์„๊นŒ?
) {
while (true) {
try {
WorkspaceEntity workspaceEntity = workspaceRepository.findByDesignerIdWithOptimisticLock(designerId) // Workspace ์—”ํ‹ฐํ‹ฐ์—์„œ id๋ฅผ ํ˜ธ์ถœํ•ด์˜ฌ ๋•Œ, ๋ฒ„์ „์„ ํ™•์ธ
.orElseThrow(() -> new PeautyException(PeautyResponseCode.NOT_EXIST_WORKSPACE));
List<BannerImageEntity> bannerImageEntities = bannerImageRepository.findByWorkspaceId(workspaceEntity.getId());
Workspace workspace = WorkspaceMapper.toDomain(workspaceEntity, bannerImageEntities);
// ๋ฆฌ๋ทฐ ์ž‘์„ฑ ๋กœ์ง
workspace.registerReviewStats(newRating);
// ์—”ํ‹ฐํ‹ฐ ๋ณ€ํ™˜ ํ›„ ์ €์žฅ
workspaceEntity = WorkspaceMapper.toEntity(workspace, designerId);
workspaceRepository.save(workspaceEntity);

return workspace;
} catch (ObjectOptimisticLockingFailureException e) {
try {
Thread.sleep(10); // 10ms ๋Œ€๊ธฐ // ์ €์žฅ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ํ•˜๋‚˜์˜ ์‹คํ–‰ ๋‹จ์œ„(Thread)
} catch (InterruptedException ie) {
Thread.currentThread().interrupt(); // ํ˜„์žฌ ์Šค๋ ˆ๋“œ์˜ ์ธํ„ฐ๋ŸฝํŠธ ์ƒํƒœ ๋ณต๊ตฌ
throw new PeautyException(PeautyResponseCode.INTERNAL_SERVER_MAINTENANCE);
}
}
}
}

public Workspace updateReviewStats(Long designerId, ReviewRating oldRating, ReviewRating newRating) {
Expand Down
1 change: 1 addition & 0 deletions peauty-domain/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ dependencies {
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
testImplementation 'org.mockito:mockito-core'
testImplementation 'org.assertj:assertj-core'

}

test {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public enum PeautyResponseCode {
INVALID_BIRTHDATE("1229", "Invalid Your Puppy Birthday", "ํ˜„์žฌ ๋‚ ์งœ๋ณด๋‹ค ์ดํ›„๋ฅผ ์„ ํƒํ–ˆ์Šต๋‹ˆ๋‹ค."),
CONTAINS_NON_EXISTING_DESIGNERS("1230", "Designer Not Found", "์กด์žฌํ•˜์ง€ ์•Š๋Š” ๋””์ž์ด๋„ˆ๊ฐ€ ํฌํ•จ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค."),
NOT_FOUND_REVIEWER_WRITTEN_REVIEW("1231", "Not found reviewer written review", "ํ•ด๋‹น ๋ฆฌ๋ทฐ๋ฅผ ์ž‘์„ฑํ•œ ์‚ฌ์šฉ์ž๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."),
INTERNAL_SERVER_MAINTENANCE("1232", "Performing Internal Server Maintenance. Try Later", "๋‚ด๋ถ€ ์„œ๋ฒ„ ์ ๊ฒ€ ์ค‘์ด๋‹ˆ, ์ž ์‹œ ํ›„ ๋‹ค์‹œ ๋“ฑ๋กํ•ด์ฃผ์„ธ์š”."),

// ๋น„๋”ฉ ๊ด€๋ จ (1300 ~ 1350)
WRONG_BIDDING_PROCESS_STEP_DESCRIPTION("1300", "Wrong Bidding Process Step Description", "์ž˜๋ชป๋œ ์ž…์ฐฐ ํ”„๋กœ์„ธ์Šค์ž…๋‹ˆ๋‹ค."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,7 @@ public class WorkspaceEntity extends BaseTimeEntity {
@Column(name = "phone_number", nullable = false)
private String phoneNumber;

@Version
private Integer version;

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.peauty.persistence.designer.workspace;

import jakarta.persistence.LockModeType;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Lock;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
Expand All @@ -21,4 +23,8 @@
@Query("SELECT w FROM WorkspaceEntity w WHERE w.address LIKE CONCAT(:baseAddress, '%')")
List<WorkspaceEntity> findByBaseAddress(@Param("baseAddress") String baseAddress);

@Lock(LockModeType.OPTIMISTIC)
@Query("SELECT w FROM WorkspaceEntity w WHERE w.designerId = :designerId")
Optional<WorkspaceEntity> findByDesignerIdWithOptimisticLock(@Param("designerId") Long designerId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ public static Review toReviewDomain(
.toList())
.reviewRating(reviewEntity.getReviewRating())
.build();


}

}
Expand Down
Loading