Skip to content

Commit

Permalink
fix: planner 업데이트 기능
Browse files Browse the repository at this point in the history
  • Loading branch information
SangWoon123 committed Sep 1, 2024
1 parent 08521ee commit 5de6915
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.tukorea.planding.domain.planner.entity;

import com.tukorea.planding.domain.planner.PlannerRole;
import com.tukorea.planding.domain.planner.PlannerStatus;
import com.tukorea.planding.domain.schedule.entity.Schedule;
import com.tukorea.planding.domain.user.entity.User;
import com.tukorea.planding.global.audit.BaseEntity;
import jakarta.persistence.*;
import lombok.*;
Expand Down Expand Up @@ -60,7 +62,21 @@ public void update(String title, String content, PlannerStatus status, LocalDate
this.deadline = deadline;
}

public void updateManager() {

public void updateManager(User newManager) {
this.users.stream()
.filter(plannerUser -> plannerUser.getRole() == PlannerRole.MANAGER)
.forEach(plannerUser -> plannerUser.updateRole(PlannerRole.GENERAL));

this.users.stream()
.filter(plannerUser -> plannerUser.getUser().equals(newManager))
.findFirst()
.ifPresentOrElse(
plannerUser -> plannerUser.updateRole(PlannerRole.MANAGER),
() -> this.users.add(PlannerUser.builder()
.planner(this)
.user(newManager)
.role(PlannerRole.MANAGER)
.build())
);
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package com.tukorea.planding.domain.planner.service;

import com.tukorea.planding.domain.group.service.query.UserGroupQueryService;
import com.tukorea.planding.domain.notify.entity.NotificationType;
import com.tukorea.planding.domain.planner.PlannerRole;
import com.tukorea.planding.domain.planner.dto.PlannerRequest;
import com.tukorea.planding.domain.planner.dto.GroupPlannerResponse;
import com.tukorea.planding.domain.planner.dto.PlannerResponse;
import com.tukorea.planding.domain.planner.dto.PlannerUpdateRequest;
import com.tukorea.planding.domain.planner.dto.group.PlannerWeekResponse;
import com.tukorea.planding.domain.planner.dto.SchedulePlannerResponse;
import com.tukorea.planding.domain.planner.entity.Planner;
import com.tukorea.planding.domain.planner.entity.PlannerUser;
import com.tukorea.planding.domain.planner.repository.PlannerRepository;
import com.tukorea.planding.domain.planner.repository.PlannerUserRepository;
import com.tukorea.planding.domain.schedule.dto.response.ScheduleResponse;
import com.tukorea.planding.domain.schedule.entity.Action;
import com.tukorea.planding.domain.schedule.entity.Schedule;
import com.tukorea.planding.domain.schedule.service.ScheduleQueryService;
Expand All @@ -24,8 +26,8 @@
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -113,12 +115,28 @@ private void adjustPlannerNumbers(Schedule schedule, int deletedPlannerNumber) {
}
}

public PlannerResponse getPlannerByGroup(UserInfo userInfo, String groupCode, Long plannerId) {
if (!userGroupQueryService.checkUserAccessToGroupRoom(groupCode, userInfo.getUserCode())) {
throw new BusinessException(ErrorCode.ACCESS_DENIED);
}
Planner planner = plannerRepository.findById(plannerId)
.orElseThrow(() -> new IllegalArgumentException("Planner not found with id: " + plannerId));

return PlannerResponse.fromEntity(planner);
}

public GroupPlannerResponse updateGroupPlanner(String userCode, String groupCode, PlannerUpdateRequest request) {
if (!userGroupQueryService.checkUserAccessToGroupRoom(groupCode, userCode)) {
throw new BusinessException(ErrorCode.ACCESS_DENIED);
}
Planner planner = plannerRepository.findById(request.plannerId())
.orElseThrow(() -> new IllegalArgumentException("Planner not found with id: " + request.plannerId()));

User newManager = userQueryService.getUserByUserCode(request.managerCode());
planner.updateManager(newManager);

updateUsers(planner, request.userCodes());

planner.update(request.title(), request.content(), request.status(), request.deadline());

return GroupPlannerResponse.fromEntity(planner, Action.UPDATE);
Expand Down Expand Up @@ -147,21 +165,31 @@ private void updateUsers(Planner planner, List<String> newUserCodes) {
}
}

public List<GroupPlannerResponse> getPlannersByGroup(UserInfo userInfo, String groupCode, Long scheduleId) {
public SchedulePlannerResponse getPlannersByGroup(UserInfo userInfo, String groupCode, Long scheduleId) {
if (!userGroupQueryService.checkUserAccessToGroupRoom(groupCode, userInfo.getUserCode())) {
throw new BusinessException(ErrorCode.ACCESS_DENIED);
}
Schedule schedule = scheduleQueryService.findScheduleById(scheduleId);
List<Planner> planners = plannerRepository.findBySchedule(schedule);

return planners.stream()
.map(GroupPlannerResponse::fromEntity)
.collect(Collectors.toList());
return SchedulePlannerResponse.fromEntity(planners, schedule);
}

public List<PlannerWeekResponse> getWeekPlannerByGroup(LocalDate startDate, LocalDate endDate, String groupCode, UserInfo userInfo) {
return plannerRepository.findAllByGroupAndDateRange(groupCode, startDate, endDate).stream()
.map(PlannerWeekResponse::fromEntity)
public List<PlannerWeekResponse> findPlannersByGroupCodeAndDateRange(LocalDate startDate, LocalDate endDate, String groupCode, UserInfo userInfo) {
List<Planner> planners = plannerRepository.findAllByGroupAndDateRange(groupCode, startDate, endDate);

Map<Long, List<Planner>> groupedPlanners = planners.stream().collect(Collectors.groupingBy(planner -> planner.getSchedule().getId()));

return groupedPlanners.entrySet().stream()
.map(entry -> PlannerWeekResponse.builder()
.scheduleId(entry.getKey())
.scheduleTitle(entry.getValue().get(0).getSchedule().getTitle())
.scheduleDate(entry.getValue().get(0).getSchedule().getScheduleDate())
.planners(entry.getValue().stream()
.map(PlannerResponse::fromEntity)
.collect(Collectors.toList()))
.type(NotificationType.PLANNER)
.build())
.collect(Collectors.toList());
}
}

0 comments on commit 5de6915

Please sign in to comment.