From a1b0325a5f970973b86dea7cfd428c1d0b98f800 Mon Sep 17 00:00:00 2001 From: ldw Date: Wed, 23 Aug 2023 18:24:49 +0900 Subject: [PATCH] [BE] FEAT & REFACTOR : Modify LentController and startLentShareCabinet in LentServiceImpl --- .../lent/controller/LentController.java | 5 +- .../cabinet/lent/service/LentServiceImpl.java | 21 +++++---- .../cabinet/redis/TicketingSharedCabinet.java | 46 +++++++++++++++++-- 3 files changed, 56 insertions(+), 16 deletions(-) diff --git a/backend/src/main/java/org/ftclub/cabinet/lent/controller/LentController.java b/backend/src/main/java/org/ftclub/cabinet/lent/controller/LentController.java index 8dac533f3..00ea928c9 100644 --- a/backend/src/main/java/org/ftclub/cabinet/lent/controller/LentController.java +++ b/backend/src/main/java/org/ftclub/cabinet/lent/controller/LentController.java @@ -42,9 +42,10 @@ public void startLentCabinet( @PostMapping("/cabinets/share/{cabinetId}") public void startLentShareCabinet( @UserSession UserSessionDto user, - @PathVariable Long cabinetId) { + @PathVariable Long cabinetId, + @Valid @RequestBody Long shareCode) { log.info("Called startLentShareCabinet user: {}, cabinetId: {}", user, cabinetId); - lentFacadeService.startLentShareCabinet(user.getUserId(), cabinetId); + lentFacadeService.startLentShareCabinet(user.getUserId(), cabinetId, shareCode); } @PatchMapping("/return") diff --git a/backend/src/main/java/org/ftclub/cabinet/lent/service/LentServiceImpl.java b/backend/src/main/java/org/ftclub/cabinet/lent/service/LentServiceImpl.java index d29b0126b..4c69f37e9 100644 --- a/backend/src/main/java/org/ftclub/cabinet/lent/service/LentServiceImpl.java +++ b/backend/src/main/java/org/ftclub/cabinet/lent/service/LentServiceImpl.java @@ -67,7 +67,7 @@ public void startLentCabinet(Long userId, Long cabinetId) { } @Override - public void startLentShareCabinet(Long userId, Long cabinetId) { + public void startLentShareCabinet(Long userId, Long cabinetId, Long shareCode) { log.info("Called startLentShareCabinet: {}, {}", userId, cabinetId); LocalDateTime now = LocalDateTime.now(); Cabinet cabinet = cabinetOptionalFetcher.getCabinetForUpdate(cabinetId); @@ -84,15 +84,18 @@ public void startLentShareCabinet(Long userId, Long cabinetId) { // cabinetId); // 방장인지 검사 -> 분기 // 1. 방장 - 대여 가능한 캐비넷인지 확인, shadowKey 생성, valueKey 생성 - lentOptionalFetcher.handlePolicyStatus(lentPolicy.verifyCabinetForLent(cabinet)); - if (!ticketingSharedCabinet.isShadowKey(cabinetId.toString())) { - ticketingSharedCabinet.setShadowKey(cabinetId.toString()); + String cabinetIdString = cabinetId.toString(); + if (!ticketingSharedCabinet.isShadowKey(cabinetIdString)) { + lentOptionalFetcher.handlePolicyStatus(lentPolicy.verifyCabinetForLent(cabinet)); + ticketingSharedCabinet.setShadowKey(cabinetIdString); +// ticketingSharedCabinet.saveValue(cabinetIdString, userId.toString()); } -// 2. 이후 유저 - shareCode 검사(front에서 함), valueKey 생성 - ticketingSharedCabinet.saveValue(cabinetId.toString(), userId.toString(), 1); -// // 대여 가능한 캐비넷인지 확인 -// lentOptionalFetcher.handlePolicyStatus( -// lentPolicy.verifyCabinetForLent(cabinet)); +// 2. 이후 유저 - shareCode 검사, valueKey 생성 + else { + ticketingSharedCabinet.checkSizeOfUsers(cabinetIdString); +// ticketingSharedCabinet.saveValue(cabinetIdString, userId.toString()); + } + ticketingSharedCabinet.saveValue(cabinetIdString, userId.toString()); // 만료 시간 적용 -> listener로 이동할 것 LocalDateTime expiredAt = lentPolicy.generateExpirationDate(now, cabinet); // userId 반복문 돌면서 수행 diff --git a/backend/src/main/java/org/ftclub/cabinet/redis/TicketingSharedCabinet.java b/backend/src/main/java/org/ftclub/cabinet/redis/TicketingSharedCabinet.java index 8535e68b3..0b41173e8 100644 --- a/backend/src/main/java/org/ftclub/cabinet/redis/TicketingSharedCabinet.java +++ b/backend/src/main/java/org/ftclub/cabinet/redis/TicketingSharedCabinet.java @@ -1,6 +1,8 @@ package org.ftclub.cabinet.redis; import java.util.concurrent.TimeUnit; +import org.ftclub.cabinet.lent.domain.LentPolicyStatus; +import org.ftclub.cabinet.lent.repository.LentOptionalFetcher; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; @@ -13,20 +15,54 @@ public class TicketingSharedCabinet { @Autowired private RedisTemplate shadowKeyRedisTemplate; + @Autowired + private LentOptionalFetcher lentOptionalFetcher; + +// /** +// * @param key : cabinetId + suffix +// * @param hashKey: ${userName} 또는 "userCount" +// * @param pwCount: ${passwordCount} 또는 "userCount" +// */ +// public void saveValue(String key, String hashKey, Integer pwCount) { +// valueRedisTemplate.opsForHash().put(key, hashKey, pwCount); +// } /** - * @param key : cabinetId + suffix - * @param hashKey: ${userName} 또는 "userCount" - * @param pwCount: ${passwordCount} 또는 "userCount" + * @param key : cabinetId + suffix + * @param hashKey : ${userName} 또는 "userCount" */ - public void saveValue(String key, String hashKey, Integer pwCount) { - valueRedisTemplate.opsForHash().put(key, hashKey, pwCount); + public void saveValue(String key, String hashKey) { + if (isUserInValueKey(key, hashKey)) { + valueRedisTemplate.opsForHash().put(key, hashKey, getValue(key, hashKey) + 1); + } else { + if (getSizeOfUsers(key) == 0) // 방장이 들어온 경우 + { + valueRedisTemplate.opsForHash().put(key, hashKey, -1); + } else // 방장 이후 유저가 들어올려고 시도한 경우 + { + valueRedisTemplate.opsForHash().put(key, hashKey, 1); + } + } + } + + public void validateShareCode() { + } public Boolean isUserInValueKey(String key, String hashKey) { return valueRedisTemplate.opsForHash().hasKey(key, hashKey); } + public Long getSizeOfUsers(String key) { + valueRedisTemplate.opsForHash().size(key); + } + + public void checkSizeOfUsers(String key) { + if (getSizeOfUsers(key) > 4) { + lentOptionalFetcher.handlePolicyStatus(LentPolicyStatus.FULL_CABINET); + } + } + public Integer getValue(String key, String hashKey) { return (Integer) valueRedisTemplate.opsForHash().get(key, hashKey); }