Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ public ResponseEntity<ApiResponse<Void>> deleteCategory(@PathVariable Long id) {
return ResponseEntity.ok(ApiResponse.success(null, "Category deleted (soft-deleted) successfully"));
}

@PutMapping("/restore/{id}")
public ResponseEntity<ApiResponse<CategoryResponseDTO>> restoreCategory(@PathVariable Long id) {
CategoryResponseDTO restored = categoryService.restoreCategory(id);
return ResponseEntity.ok(ApiResponse.success(restored, "Category restored successfully"));
}

@GetMapping("/activecategories")
public ResponseEntity<ApiResponse<PagedResponse<CategoryResponseDTO>>> getActiveCategories(
@PageableDefault(page = 0, size = 10, sort = "name") Pageable pageable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public interface CategoryService {

public void deleteCategory(Long id);

public CategoryResponseDTO restoreCategory(Long id);

public Page<CategoryResponseDTO> getActiveCategories(Pageable pageable);

}
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,29 @@ public void deleteCategory(Long id) {
categoryRepository.save(existingCategory);
}

/* =======================
RESTORE SOFT-DELETED CATEGORY
======================= */
@Override
@Transactional
public CategoryResponseDTO restoreCategory(Long id) {
log.debug("Restoring category with ID: {}", id);
Category existingCategory = categoryRepository.findById(id)
.orElseThrow(() ->
new ResourceNotFoundException("Category", "id", id));

if (Boolean.FALSE.equals(existingCategory.getIsDelete())) {
log.warn("Category with ID: {} is already active", id);
return CategoryResponseDTO.fromEntity(existingCategory);
}

existingCategory.setIsDelete(false);
existingCategory.setUpdatedDate(LocalDateTime.now());
Category restored = categoryRepository.save(existingCategory);
log.info("Category with ID: {} restored successfully", id);
return CategoryResponseDTO.fromEntity(restored);
}

@Override
@Transactional(readOnly = true)
public Page<CategoryResponseDTO> getActiveCategories(Pageable pageable) {
Expand Down