사실 프로젝트 시작은 약 3주정도 지난것 같다
PM, UI/UX 디자이너, 개발자 (BE/FE) 협업으로 이루어지는 프로젝트라
이제서야 기획이 잡혀 어제 API 명세서와 ERD 의 수정이 어느정도 마무리되었다.
우리의 서비스는 '1인 가구 맞춤 집안일 관리 서비스' 이다.
1인 가구만 해당하지는 않겠지만, 사용자들에게 계절, 트랜드 등에 맞춰 집안일을 추천해주거나, 개인이 추가하는 집안일을 설정된 주기에 맞춰 알림을 보내주면서 관리할 수 있다.
BE 개발자는 오늘 추가된 2인까지 총 4명이라 기존 MVP 외에도 더 많은 기능을 구현 해볼수 있을 것 같아 설렌다.
구현해야하는 기능을 2명이서만 나눴기 때문에 조정이 필요해보이지만, 우선 집안일에 관련된 기능을 먼저 구현해보려 한다.
집안일과 관련된 기능은 아래와 같다.
우선 이렇게 5가지의 기능을 구현 중이다.
기능 구현에 앞서 집안일의 Entity, DTO 를 먼저 작성해줬다.
@Transactional
public ChoresDto.Response createChores(Long userId,
ChoresDto.CreateRequest request) {
try {
Chores chores = Chores.builder()
.userId(userId)
.title(request.getTitle())
.notificationYn(request.getNotificationYn())
.notificationTime(
LocalTime.parse(request.getNotificationTime()))
.space(request.getSpace())
.repeatType(request.getRepeatType())
.repeatInterval(request.getRepeatInterval())
.startDate(request.getStartDate())
.endDate(request.getEndDate())
.isDeleted(false)
.build();
Chores savedChores = choresRepository.save(chores);
List<ChoreInstances> instances = choreInstanceGenerator.generateInstances(savedChores);
choreInstancesRepository.saveAll(instances);
return ChoresDto.Response.fromEntity(savedChores);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
인스턴스 생성 중 에러가 발생할수도 있으니 @Transactional 어노테이션을 사용했다.
토큰 값에서 user 의 id 를 추출하고 클라이언트로부터 입력받은 값을 저장한다.
@Component
public class ChoreInstanceGenerator {
private static final int MAX_INSTANCES = 1000; // 최대 인스턴스 수 제한
public List<ChoreInstances> generateInstances(Chores chores) {
try {
validateChoreData(chores);
List<ChoreInstances> instances = new ArrayList<>();
if (chores.getRepeatType() == Chores.RepeatType.NONE) {
instances.add(createInstance(chores, chores.getStartDate()));
} else {
LocalDate currentDate = chores.getStartDate();
LocalDate endDate = chores.getEndDate() != null ? chores.getEndDate() : chores.getStartDate().plusYears(1);
int instanceCount = 0;
while (!currentDate.isAfter(endDate) && instanceCount < MAX_INSTANCES) {
instances.add(createInstance(chores, currentDate));
currentDate = getNextDate(currentDate, chores.getRepeatType(), chores.getRepeatInterval());
instanceCount++;
}
if (instanceCount >= MAX_INSTANCES) {
throw new ChoreException(ErrorCode.TOO_MANY_INSTANCES);
}
}
return instances;
} catch (DateTimeParseException e) {
throw new ChoreException(ErrorCode.INVALID_DATE_FORMAT, "날짜 형식이 올바르지 않습니다: " + e.getMessage());
} catch (Exception e) {
if (e instanceof ChoreException) {
throw e;
}
throw new ChoreException(ErrorCode.INTERNAL_SERVER_ERROR, "인스턴스 생성 중 오류가 발생했습니다: " + e.getMessage());
}
}
지금은 우선 동시에 생성할 수 있는 인스턴스 수는 1000개로 제한해두었다.
아마 300개 정도로 줄이지 않을까 싶다.
집안일을 생성하고 해당 인스턴스를 추가하는 로직이다. 별건 없고 대부분 validation 코드다.
임시로 Jwt 관련 코드들을 작성해두었지만, 이 코드를 사용할건 아니고, User 개발하시는 분이 끝나면 그 코드에 맞춰서 수정이 필요해보인다.
Postman 으로 임시 토큰을 생성하고, /chores/create 를 호출하니 잘 호출되고 예외 사항을 발생시켜도 예상대로 잘 나온다.