Skip to content

Commit

Permalink
[BE] FEAT & REFACTOR : Modify LentController and startLentShareCabine…
Browse files Browse the repository at this point in the history
…t in LentServiceImpl
  • Loading branch information
LeeDaeWook committed Aug 23, 2023
1 parent eec47f2 commit a1b0325
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 반복문 돌면서 수행
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -13,20 +15,54 @@ public class TicketingSharedCabinet {

@Autowired
private RedisTemplate<String, String> 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);
}
Expand Down

0 comments on commit a1b0325

Please sign in to comment.