Skip to content

Commit

Permalink
[COZY-209] feat: 오늘 Role을 생성했다면 Todo도 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
eple0329 authored Aug 23, 2024
1 parent b6f2258 commit 9b26d96
Showing 1 changed file with 62 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@
import com.cozymate.cozymate_server.domain.role.dto.RoleRequestDto.CreateRoleRequestDto;
import com.cozymate.cozymate_server.domain.role.enums.DayListBitmask;
import com.cozymate.cozymate_server.domain.role.repository.RoleRepository;
import com.cozymate.cozymate_server.domain.todo.converter.TodoConverter;
import com.cozymate.cozymate_server.domain.todo.repository.TodoRepository;
import com.cozymate.cozymate_server.global.response.code.status.ErrorStatus;
import com.cozymate.cozymate_server.global.response.exception.GeneralException;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -19,45 +23,63 @@
@RequiredArgsConstructor
public class RoleCommandService {

private final RoleRepository roleRepository;
private final MateRepository mateRepository;


@Transactional
public void createRole(
Member member,
Long roomId,
CreateRoleRequestDto requestDto
) {
// 해당 API를 호출한 사람
Mate mate = mateRepository.findByMemberIdAndRoomId(member.getId(), roomId)
.orElseThrow(() -> new GeneralException(ErrorStatus._MATE_OR_ROOM_NOT_FOUND));

List<DayListBitmask> repeatDayList = requestDto.getRepeatDayList().stream()
.map(DayListBitmask::valueOf).toList();
int repeatDayBitmast = RoleConverter.convertDayListToBitmask(repeatDayList);

// Role의 대상이 되는 사람
requestDto.getMateIdList().forEach(mateId -> {
Mate targerMate = mateRepository.findById(mateId) // TODO: mate를 한번에 가져오는 방식으로 변경해야함
.orElseThrow(() -> new GeneralException(ErrorStatus._MATE_NOT_FOUND));
Role role = RoleConverter.toEntity(targerMate, requestDto.getTitle(), repeatDayBitmast);
roleRepository.save(role);
});
}

public void deleteRole(Long roomId, Long roleId, Member member) {
Mate mate = mateRepository.findByMemberIdAndRoomId(member.getId(), roomId)
.orElseThrow(() -> new GeneralException(ErrorStatus._MATE_OR_ROOM_NOT_FOUND));

Role roleToDelete = roleRepository.findById(roleId)
.orElseThrow(() -> new GeneralException(ErrorStatus._ROLE_NOT_FOUND));

if (Boolean.FALSE.equals(
member.getId().equals(roleToDelete.getMate().getMember().getId())
)) {
throw new GeneralException(ErrorStatus._ROLE_MATE_MISMATCH);
private final RoleRepository roleRepository;
private final MateRepository mateRepository;
private final TodoRepository todoRepository;


@Transactional
public void createRole(
Member member,
Long roomId,
CreateRoleRequestDto requestDto
) {
// 해당 API를 호출한 사람
Mate mate = mateRepository.findByMemberIdAndRoomId(member.getId(), roomId)
.orElseThrow(
() -> new GeneralException(ErrorStatus._MATE_OR_ROOM_NOT_FOUND));

List<DayListBitmask> repeatDayList = requestDto.getRepeatDayList().stream()
.map(DayListBitmask::valueOf).toList();
int repeatDayBitmast = RoleConverter.convertDayListToBitmask(repeatDayList);

// 오늘 요일 가져오기
DayOfWeek dayOfWeek = LocalDate.now().getDayOfWeek();
int dayBitmask = DayListBitmask.getBitmaskByDayOfWeek(dayOfWeek);

// Role의 대상이 되는 사람
requestDto.getMateIdList().forEach(mateId -> {
Mate targerMate = mateRepository.findById(
mateId) // TODO: mate를 한번에 가져오는 방식으로 변경해야함
.orElseThrow(
() -> new GeneralException(ErrorStatus._MATE_NOT_FOUND));
Role role = RoleConverter.toEntity(targerMate, requestDto.getTitle(),
repeatDayBitmast);
if ((role.getRepeatDays() & dayBitmask) != 0) {
todoRepository.save(
TodoConverter.toEntity(role.getMate().getRoom(),
role.getMate(),
role.getContent(),
LocalDate.now(), role)
);
}
roleRepository.save(role);
});
}

public void deleteRole(Long roomId, Long roleId, Member member) {
Mate mate = mateRepository.findByMemberIdAndRoomId(member.getId(), roomId)
.orElseThrow(
() -> new GeneralException(ErrorStatus._MATE_OR_ROOM_NOT_FOUND));

Role roleToDelete = roleRepository.findById(roleId)
.orElseThrow(() -> new GeneralException(ErrorStatus._ROLE_NOT_FOUND));

if (Boolean.FALSE.equals(
member.getId().equals(roleToDelete.getMate().getMember().getId())
)) {
throw new GeneralException(ErrorStatus._ROLE_MATE_MISMATCH);
}
roleRepository.delete(roleToDelete);
}
roleRepository.delete(roleToDelete);
}
}

0 comments on commit 9b26d96

Please sign in to comment.