Skip to content

Commit

Permalink
Feat [#117] 카테고리 삭제 API 구현 (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
geniusYoo authored Jul 16, 2024
2 parents 6732005 + 305349d commit 68a21aa
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.sopt.jaksim.category.dto.CategoryCheckResponse;
import org.sopt.jaksim.category.dto.CategoryCreateRequest;
import org.sopt.jaksim.category.dto.FilteredResourceResponse;
import org.sopt.jaksim.category.facade.CategoryMsetFacade;
import org.sopt.jaksim.category.service.CategoryService;
import org.sopt.jaksim.category.facade.CategoryTaskFacade;
import org.sopt.jaksim.global.common.ApiResponseUtil;
Expand All @@ -25,6 +26,7 @@
public class CategoryApiController implements CategoryApi {
private final CategoryService categoryService;
private final CategoryTaskFacade categoryTaskFacade;
private final CategoryMsetFacade categoryMsetFacade;

@PostMapping("/categories")
@Override
Expand All @@ -47,4 +49,12 @@ public ResponseEntity<BaseResponse<?>> getCategoriesByUserId() {
List<CategoryCheckResponse> categories = categoryService.getCategoriesByUserId(); // categoryRepository를 통해 데이터베이스에서 특정 사용자의 카테고리를 조회
return ApiResponseUtil.success(SuccessMessage.SUCCESS, categories); // 성공 응답 반환
}

@DeleteMapping("/categories/{categoryId}")
public ResponseEntity<BaseResponse<?>> delete(@PathVariable("categoryId") Long categoryId) {
categoryTaskFacade.deleteCategoryTaskAndTasks(categoryId);
categoryMsetFacade.deleteCategoryMsetAndMsets(categoryId);
categoryTaskFacade.delete(categoryId);
return ApiResponseUtil.success(SuccessMessage.SUCCESS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,13 @@ public CategoryMsetLinkResponse getFromOtherCategory(Long categoryId) {
return CategoryMsetLinkResponse.of(category, msetList);
}

public void deleteCategoryMsetAndMsets(Long categoryId) {
List<Long> msetIdList = categoryMsetService.deleteAndGetMsetIdList(categoryId);
deleteMsetById(msetIdList);
}

public void deleteMsetById(List<Long> msetIdList) {
msetService.delete(msetIdList);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.sopt.jaksim.category.dto.FilteredResourceResponse;
import org.sopt.jaksim.category.dto.TaskWithTaskTimer;
import org.sopt.jaksim.category.service.CategoryService;
import org.sopt.jaksim.category.service.CategoryTaskService;
import org.sopt.jaksim.task.domain.Task;
import org.sopt.jaksim.task.domain.TaskTimer;
import org.sopt.jaksim.task.service.TaskService;
Expand All @@ -28,6 +29,7 @@ public class CategoryTaskFacade {
private final TaskService taskService;
private final CategoryService categoryService;
private final TaskTimerService taskTimerService;
private final CategoryTaskService categoryTaskService;

public List<FilteredResourceResponse> getAllResources(LocalDate startDate, LocalDate endDate) {
// User user = userFacade.getUserByPrincipal();
Expand Down Expand Up @@ -65,4 +67,13 @@ public List<FilteredResourceResponse> getAllResources(LocalDate startDate, Local
return res;
}

public void delete(Long categoryId) {
categoryService.delete(categoryId);
}
public void deleteCategoryTaskAndTasks(Long categoryId) {
// CategoryTask -> Task 순으로 삭제
List<Long> taskIdList = categoryTaskService.deleteAndGetTaskIdList(categoryId);
taskService.delete(taskIdList);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ public interface CategoryRepository extends JpaRepository<Category, Long> {
List<Category> findByUserIdWithRange(Long userId, LocalDate idxStartDate, LocalDate idxEndDate);

Optional<List<Category>> findByUserId(Long userId);


}
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ else return (categoryCheckResponse.startDate().isBefore(idxDate) &&
categoryCheckResponse.endDate().equals(idxDate);
}


public List<CategoryCheckResponse> getCategoriesByUserId() {
// userId -> user pk -> Long -> SecurityContextHolder Authentication 객체
// principal handler
Expand All @@ -108,5 +107,13 @@ public List<CategoryCheckResponse> getCategoriesByUserId() {
.map(category -> CategoryCheckResponse.of(category.getId(), category.getName(), category.getStartDate(), category.getEndDate()))
.collect(Collectors.toList()); //변환된 스트림을 리스트로 수집
}

public void delete(Long categoryId) {
categoryRepository.findById(categoryId).orElseThrow(
() -> new NotFoundException(ErrorMessage.NOT_FOUND)
);
categoryRepository.deleteById(categoryId);
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.sopt.jaksim.category.service;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.sopt.jaksim.category.domain.CategoryTask;
import org.sopt.jaksim.category.repository.CategoryTaskRepository;
import org.sopt.jaksim.global.exception.NotFoundException;
import org.sopt.jaksim.global.message.ErrorMessage;
import org.springframework.stereotype.Service;

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

@Slf4j
@Service
@RequiredArgsConstructor
public class CategoryTaskService {
private final CategoryTaskRepository categoryTaskRepository;

public List<Long> deleteAndGetTaskIdList(Long categoryId) {
List<CategoryTask> categoryTaskList = categoryTaskRepository.findByCategoryId(categoryId);
if (categoryTaskList.isEmpty()) {
throw new NotFoundException(ErrorMessage.NOT_FOUND);
}
List<Long> taskIdList = categoryTaskList.stream()
.map(CategoryTask::getTaskId).
collect(Collectors.toList());
categoryTaskRepository.deleteAll(categoryTaskList);
return taskIdList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,33 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.sopt.jaksim.category.domain.CategoryTask;
import org.sopt.jaksim.global.exception.NotFoundException;
import org.sopt.jaksim.global.message.ErrorMessage;
import org.sopt.jaksim.mset.domain.CategoryMset;
import org.sopt.jaksim.mset.repository.CategoryMsetRepository;
import org.springframework.stereotype.Service;

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

@Slf4j
@Service
@RequiredArgsConstructor
public class CategoryMsetService {
private final CategoryMsetRepository categoryMsetRepository;

public List<CategoryMset> getByCategoryId(Long categoryId) {
return categoryMsetRepository.findByCategoryId(categoryId);
}

public List<Long> deleteAndGetMsetIdList(Long categoryId) {
List<CategoryMset> categoryMsetList = getByCategoryId(categoryId);
List<Long> msetIdList = categoryMsetList.stream().map(CategoryMset::getMsetId).collect(Collectors.toList());
if (categoryMsetList.isEmpty()) {
throw new NotFoundException(ErrorMessage.NOT_FOUND);
}
categoryMsetRepository.deleteAll(categoryMsetList);
return msetIdList;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,14 @@ public void createByCategory(CategoryCreateRequest categoryCreateRequest, Long c
public Mset getMsetById(Long msetId) {
return msetRepository.findById(msetId).orElseThrow(
() -> new NotFoundException(ErrorMessage.NOT_FOUND)
); }
);
}

public void delete(List<Long> msetIdList) {
List<Mset> msetList = msetRepository.findAllById(msetIdList);
if (msetIdList.isEmpty()) {
throw new NotFoundException(ErrorMessage.NOT_FOUND);
}
msetRepository.deleteAll(msetList);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,12 @@ public List<Task> getTasksByTodoTask(List<TodoTask> todoTaskList) {
return taskRepository.findAllById(taskIdList);
}

public void delete(List<Long> taskIdList) {
List<Task> taskList = taskRepository.findAllById(taskIdList);
if (taskList.isEmpty()) {
throw new NotFoundException(ErrorMessage.NOT_FOUND);
}
taskRepository.deleteAll(taskList);
}

}

0 comments on commit 68a21aa

Please sign in to comment.