Skip to content
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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package com.example.lent;
package com.example.lent.controller;

import com.example.lent.dto.LentRequest;
import com.example.lent.dto.LentResponse;
import com.example.lent.service.LentService;
import lombok.RequiredArgsConstructor;

/*컨트롤러는 뭘까요?*/
/*컨트롤러는 왜 인터페이스가 없어도 될까요?*/
@RequiredArgsConstructor
public class LentController {
private LentService lentService;
private final LentService lentService;

public LentResponse lent(LentRequest request) {
return null;
return lentService.lent(request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,16 @@ public Cabinet(CabinetStatus status) {
public void id(Long id) {
this.cabinetId = id;
}

public boolean isAvailable() {
return this.cabinetStatus == CabinetStatus.AVAILABLE;
}

public void lent() {
this.cabinetStatus = CabinetStatus.FULL;
}

public void returned() {
this.cabinetStatus = CabinetStatus.AVAILABLE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ public LentHistory(Long cabinetId, Long userId, String lentUserName, LocalDateTi
this.createdAt = createdAt;
this.expiredAt = expiredAt;
}

public void id(Long lentHistoryId) {
this.lentHistoryId = lentHistoryId;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.lent;
package com.example.lent.repository;

import com.example.lent.domain.LentHistory;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.lent;
package com.example.lent.repository;

import com.example.lent.domain.LentHistory;

Expand All @@ -13,11 +13,18 @@ public class LentHistoryRepositoryImpl implements LentHistoryRepository {

@Override
public LentHistory save(LentHistory lentHistory) {
return null;
if (lentHistory.getLentHistoryId() != null) {
TABLE.removeIf(e -> e.getLentHistoryId().equals(lentHistory.getLentHistoryId()));
} else {
lentHistory.id(ID_SEQUENCE++);
}
TABLE.add(lentHistory);
return lentHistory;
}

@Override
public List<LentHistory> findAll() {
return null;
return List.copyOf(TABLE);
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.lent;
package com.example.lent.service;

import com.example.lent.dto.LentRequest;
import com.example.lent.dto.LentResponse;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.example.lent.service;

import com.example.lent.domain.Cabinet;
import com.example.lent.domain.LentHistory;
import com.example.lent.dto.LentRequest;
import com.example.lent.dto.LentResponse;
import com.example.lent.repository.LentHistoryRepository;
import com.example.lent.testutil.CabinetRepository;
import com.example.lent.testutil.UserRepository;
import lombok.RequiredArgsConstructor;

/*서비스는 뭘까요?*/
@RequiredArgsConstructor
public class LentServiceImpl implements LentService {
private static final int LENT_PERIOD = 31;

private final LentHistoryRepository lentHistoryRepository;
private final CabinetRepository cabinetRepository;
private final UserRepository userRepository;

@Override
public LentResponse lent(LentRequest request) {
if (isBannedUser(request.getUserId())) {
throw new RuntimeException("사용 정지상태인 유저입니다.");
}
if (isAlreadyLent(request.getCabinetId())) {
throw new RuntimeException("이미 사용 중인 사물함입니다.");
}
if (isAlreadyLentUser(request.getUserId())) {
throw new RuntimeException("이미 대여 중인 유저입니다.");
}

Cabinet cabinet = cabinetRepository.findAll().stream()
.filter(e -> e.getCabinetId().equals(request.getCabinetId()))
.findAny()
.orElseThrow(() -> new RuntimeException("존재하지 않는 사물함입니다."));
cabinet.lent();
LentHistory lentHistory = new LentHistory(
request.getCabinetId(),
request.getUserId(),
userRepository.findById(request.getUserId()).getName(),
request.getCreatedAt(),
request.getCreatedAt().plusDays(LENT_PERIOD)
);
lentHistoryRepository.save(lentHistory);

return new LentResponse(
lentHistory.getLentHistoryId(),
lentHistory.getCabinetId(),
lentHistory.getLentUserName(),
lentHistory.getCreatedAt(),
lentHistory.getExpiredAt()
);
}

private boolean isBannedUser(Long userId) {
return userRepository.findById(userId).isBanned();
}

private boolean isAlreadyLent(Long cabinetId) {
Cabinet cabinet = cabinetRepository.findAll().stream()
.filter(e -> e.getCabinetId().equals(cabinetId))
.findAny()
.orElse(null);
return (cabinet != null && !cabinet.isAvailable());
}

private boolean isAlreadyLentUser(Long userId) {
return lentHistoryRepository.findAll().stream()
.anyMatch(e -> e.getUserId().equals(userId));
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package com.example.lent;

import com.example.lent.controller.LentController;
import com.example.lent.domain.Cabinet;
import com.example.lent.domain.CabinetStatus;
import com.example.lent.domain.LentHistory;
import com.example.lent.domain.User;
import com.example.lent.dto.LentRequest;
import com.example.lent.dto.LentResponse;
import com.example.lent.repository.LentHistoryRepository;
import com.example.lent.repository.LentHistoryRepositoryImpl;
import com.example.lent.service.LentService;
import com.example.lent.service.LentServiceImpl;
import com.example.lent.testutil.CabinetRepository;
import com.example.lent.testutil.UserRepository;
import org.junit.jupiter.api.BeforeEach;
Expand Down