Skip to content

Commit

Permalink
feat(#79): housework add
Browse files Browse the repository at this point in the history
feat(#79): housework add
  • Loading branch information
ghdcksgml1 authored Oct 21, 2023
2 parents e31793d + 70c9400 commit 54d10d4
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public JsonResult<?> createHouseworkInfo(@RequestHeader(name = "Authorization")
// Auth 서버로 요청자 인증 요청 - 해당 그룹원인지 판별하고 상태가 ACCEPT인지 확인
try {
authExternalService.userAuthenticateAndGroupMatch(authorization, groupId);
houseworkInfoService.createHouseworkInfo(HouseworkInfoCreateServiceRequest.of(request));
houseworkInfoService.createHouseworkInfo(HouseworkInfoCreateServiceRequest.of(request, groupId));

return JsonResult.successOf("Housework Create Success.");
} catch (HeachiException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.heachi.housework.api.service.housework.info;

import com.heachi.admin.common.exception.ExceptionMessage;
import com.heachi.admin.common.exception.group.info.GroupInfoException;
import com.heachi.admin.common.exception.group.member.GroupMemberException;
import com.heachi.admin.common.exception.housework.HouseworkException;
import com.heachi.housework.api.service.housework.info.request.HouseworkInfoCreateServiceRequest;
import com.heachi.mysql.define.group.info.GroupInfo;
import com.heachi.mysql.define.group.info.repository.GroupInfoRepository;
import com.heachi.mysql.define.group.member.GroupMember;
import com.heachi.mysql.define.group.member.repository.GroupMemberRepository;
import com.heachi.mysql.define.housework.category.HouseworkCategory;
Expand All @@ -24,19 +27,19 @@
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Slf4j
@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class HouseworkInfoService {
private final GroupInfoRepository groupInfoRepository;
private final GroupMemberRepository groupMemberRepository;
private final HouseworkInfoRepository houseworkInfoRepository;
private final HouseworkTodoRepository houseworkTodoRepository;
private final HouseworkMemberRepository houseworkMemberRepository;
private final GroupMemberRepository groupMemberRepository;
private final HouseworkCategoryRepository houseworkCategoryRepository;
private final HouseworkTodoRepository houseworkTodoRepository;

private final TodoListRepository todoListRepository;

Expand All @@ -50,12 +53,19 @@ public void createHouseworkInfo(HouseworkInfoCreateServiceRequest request) {
throw new HouseworkException(ExceptionMessage.HOUSEWORK_CATEGORY_NOT_FOUND);
});

// GROUP_INFO 조회
GroupInfo groupInfo = groupInfoRepository.findById(request.getGroupId()).orElseThrow(() -> {
log.warn(">>>> GroupInfo Not Found : {}", ExceptionMessage.GROUP_NOT_FOUND.getText());

throw new GroupInfoException(ExceptionMessage.GROUP_NOT_FOUND);
});

// 담당자 지정 - HOUSEWORK_MEMBER 생성
List<GroupMember> groupMemberList = groupMemberRepository.findGroupMemberListByGroupMemberIdList(request.getGroupMemberIdList());

// 한 건이라도 조회 실패시 예외 발생
if (groupMemberList.size() != request.getGroupMemberIdList().size()) {
log.warn(">>>> GourpMember Not Found : {}", ExceptionMessage.GROUP_MEMBER_NOT_FOUND.getText());
log.warn(">>>> GroupMember Not Found : {}", ExceptionMessage.GROUP_MEMBER_NOT_FOUND.getText());

throw new GroupMemberException(ExceptionMessage.GROUP_MEMBER_NOT_FOUND);
}
Expand All @@ -66,7 +76,7 @@ public void createHouseworkInfo(HouseworkInfoCreateServiceRequest request) {
// HouseworkTodo 생성
houseworkTodoRepository.save(HouseworkTodo.builder()
.houseworkInfo(null) // 단건은 HouseworkInfo가 존재하지 않는다.
.groupInfo(groupMemberList.isEmpty() ? null : groupMemberList.get(0).getGroupInfo())
.groupInfo(groupInfo)
.houseworkMember(groupMemberList.stream()
.map(gm -> gm.getId().toString())
.collect(Collectors.joining(",")))
Expand All @@ -78,31 +88,39 @@ public void createHouseworkInfo(HouseworkInfoCreateServiceRequest request) {
.endTime(request.getEndTime())
.build());

// TODO: 해당 HouseworkTodo에 맞는 groupInfoId와 Date가 캐싱되어있다면, dirtyBit를 true로 바꿔줘야함.
// 해당 HouseworkTodo에 맞는 groupInfoId와 Date가 캐싱되어있다면, dirtyBit를 true로 바꿔줘야함.
todoListRepository.findById(TodoList.makeId(request.getGroupId(), request.getDayDate()))
// Todo가 캐싱되어 있다면, dirtyBit 체킹
.ifPresent(todoList -> {
todoList.checkDirtyBit();
todoListRepository.save(todoList);
log.info(">>>> dirtyBit Checking TodoList id: {}", todoList.getId());
});
} else {

// HOUSEWORK_INFO 저장
HouseworkInfo houseworkInfo = houseworkInfoRepository.save(HouseworkInfo.builder()
.houseworkCategory(category)
.title(request.getTitle())
.detail(request.getDetail())
.type(request.getType())
.dayDate(request.getDayDate())
.weekDate(request.getWeekDate())
.monthDate(request.getMonthDate())
.endTime(request.getEndTime())
.build());
log.info(">>>> HouseworkInfo Create: {}", houseworkInfo.getId());

return ;
}
groupMemberList.stream()
.map(gm -> HouseworkMember.builder()
.groupMember(gm)
.houseworkInfo(houseworkInfo)
.build())
// HOUSEWORK_MEMBER 저장
.forEach(houseworkMemberRepository::save);

// HOUSEWORK_INFO 저장
HouseworkInfo houseworkInfo = houseworkInfoRepository.save(HouseworkInfo.builder()
.houseworkCategory(category)
.title(request.getTitle())
.detail(request.getDetail())
.type(request.getType())
.dayDate(request.getDayDate())
.weekDate(request.getWeekDate())
.monthDate(request.getMonthDate())
.endTime(request.getEndTime())
.build());
log.info(">>>> HouseworkInfo Create: {}", houseworkInfo.getId());

groupMemberList.stream()
.map(gm -> HouseworkMember.builder()
.groupMember(gm)
.houseworkInfo(houseworkInfo)
.build())
// HOUSEWORK_MEMBER 저장
.forEach(houseworkMemberRepository::save);
// TODO: findByGroupInfoId를 통해 해당 그룹의 캐싱된 객체 조회
}

} catch (RuntimeException e) {
log.warn(">>>> Housework Add Fail : {}", e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ public class HouseworkInfoCreateServiceRequest {
private String monthDate;
private LocalTime endTime;

private Long groupId;

@Builder
private HouseworkInfoCreateServiceRequest(List<Long> groupMemberIdList, Long houseworkCategoryId, String title,
String detail, HouseworkPeriodType type, LocalDate dayDate, String weekDate,
String monthDate, LocalTime endTime) {
String monthDate, LocalTime endTime, Long groupId) {
this.groupMemberIdList = groupMemberIdList == null ? new ArrayList<>() : groupMemberIdList;
this.houseworkCategoryId = houseworkCategoryId;
this.title = title;
Expand All @@ -37,9 +39,10 @@ private HouseworkInfoCreateServiceRequest(List<Long> groupMemberIdList, Long hou
this.weekDate = weekDate;
this.monthDate = monthDate;
this.endTime = endTime;
this.groupId = groupId;
}

public static HouseworkInfoCreateServiceRequest of(HouseworkInfoCreateRequest request) {
public static HouseworkInfoCreateServiceRequest of(HouseworkInfoCreateRequest request, Long groupId) {

return HouseworkInfoCreateServiceRequest.builder()
.groupMemberIdList(request.getGroupMemberIdList())
Expand All @@ -51,6 +54,7 @@ public static HouseworkInfoCreateServiceRequest of(HouseworkInfoCreateRequest re
.weekDate(request.getWeekDate())
.monthDate(request.getMonthDate())
.endTime(request.getEndTime())
.groupId(groupId)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import com.heachi.mysql.define.housework.todo.repository.HouseworkTodoRepository;
import com.heachi.mysql.define.user.User;
import com.heachi.mysql.define.user.repository.UserRepository;
import com.heachi.redis.define.housework.todo.TodoList;
import com.heachi.redis.define.housework.todo.repository.TodoListRepository;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -55,14 +57,19 @@ class HouseworkInfoServiceTest extends TestConfig {
@Autowired private HouseworkMemberRepository houseworkMemberRepository;
@Autowired private HouseworkTodoRepository houseworkTodoRepository;

@Autowired private TodoListRepository todoListRepository;

@AfterEach
void tearDown() {
houseworkTodoRepository.deleteAllInBatch();
houseworkMemberRepository.deleteAllInBatch();
houseworkInfoRepository.deleteAllInBatch();
groupMemberRepository.deleteAllInBatch();
groupInfoRepository.deleteAllInBatch();
userRepository.deleteAllInBatch();
houseworkCategoryRepository.deleteAllInBatch();

todoListRepository.deleteAll();
}

@Test
Expand Down Expand Up @@ -94,6 +101,7 @@ void createHouseworkInfoSuccess() {
.weekDate("1")
.monthDate(null)
.endTime(LocalTime.now())
.groupId(groupInfo.getId())
.build();

// when
Expand Down Expand Up @@ -130,6 +138,7 @@ void createHouseworkInfoFailWhenNotFoundCategory() {
.weekDate("1")
.monthDate(null)
.endTime(LocalTime.now())
.groupId(groupInfo.getId())
.build();

// when & then
Expand Down Expand Up @@ -163,11 +172,11 @@ void createHouseworkInfoFailWhenNotFoundGroupMember() {
.weekDate("1")
.monthDate(null)
.endTime(LocalTime.now())
.groupId(groupInfo.getId())
.build();

// when & then
GroupMemberException exception = assertThrows(GroupMemberException.class, () -> houseworkInfoService.createHouseworkInfo(request));
assertEquals(exception.getMessage(), ExceptionMessage.GROUP_MEMBER_NOT_FOUND.getText());
assertThrows(GroupMemberException.class, () -> houseworkInfoService.createHouseworkInfo(request));
}

@Test
Expand Down Expand Up @@ -196,6 +205,7 @@ void createHouseworkInfoWhenPeriodDay() {
.weekDate(null)
.monthDate(null)
.endTime(LocalTime.now())
.groupId(groupInfo.getId())
.build();

// when
Expand All @@ -207,4 +217,47 @@ void createHouseworkInfoWhenPeriodDay() {
assertThat(infoList.size()).isEqualTo(0);
assertThat(todoList.size()).isEqualTo(1);
}

@Test
@DisplayName("딱 한번하는 집안일의 경우, HOUSEWORK_INFO가 생성되지 않고, HOUSEWORK_TODO가 바로 생성된다. " +
"만약, Redis에 이미 캐싱되어있던 TodoList일 경우 DirtyBit가 Check로 바뀐다.")
void createHouseworkInfoWhenPeriodDayDirtyBitChecking() {
// given
HouseworkCategory category = generateHouseworkCategory();
houseworkCategoryRepository.save(category);

User user = generateUser();
userRepository.save(user);

GroupInfo groupInfo = generateGroupInfo(user);
groupInfoRepository.save(groupInfo);

GroupMember groupMember = generateGroupMember(user, groupInfo);
groupMemberRepository.save(groupMember);

HouseworkInfoCreateServiceRequest request = HouseworkInfoCreateServiceRequest.builder()
.houseworkCategoryId(category.getId())
.groupMemberIdList(null)
.type(HouseworkPeriodType.HOUSEWORK_PERIOD_DAY) // 단건
.title("Test")
.detail("Test")
.dayDate(LocalDate.now())
.weekDate(null)
.monthDate(null)
.endTime(LocalTime.now())
.groupId(groupInfo.getId())
.build();

TodoList todoList = todoListRepository.save(TodoList.builder()
.groupInfoId(groupInfo.getId())
.date(LocalDate.now())
.build());

// when
houseworkInfoService.createHouseworkInfo(request);
TodoList savedTodoList = todoListRepository.findById(todoList.getId()).get();

// then
assertThat(savedTodoList.isDirtyBit()).isTrue();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.heachi.admin.common.exception.group.info;

import com.heachi.admin.common.exception.ExceptionMessage;
import com.heachi.admin.common.exception.HeachiException;

public class GroupInfoException extends HeachiException {
public GroupInfoException(ExceptionMessage message) {
super(message.getText());
}
}

0 comments on commit 54d10d4

Please sign in to comment.