Skip to content
Merged
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 @@ -29,10 +29,6 @@ public class CategoryRequestDTO {
@Schema(description = "Whether the category is marked for deletion (soft delete)")
private Boolean isDelete; // Mirrors the field from the Category entity

@NotNull(message = "Branch ID is required")
private Long branchId; // Added branchId to link category to a branch


public static CategoryRequestDTO fromEntity(Category category) {
if (category == null) return null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,4 @@ public interface CategoryRepository extends JpaRepository<Category, Long> {

Page<Category> findByIsDeleteFalse(Pageable pageable);

boolean existsByNameIgnoreCaseAndBranch_BranchId(String name, Long branchId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
import com.restroly.qrmenu.category.dto.CategoryRequestDTO;
import com.restroly.qrmenu.category.dto.CategoryResponseDTO;
import com.restroly.qrmenu.common.exception.ResourceNotFoundException;
import com.restroly.qrmenu.user.exception.DuplicateResourceException;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import com.restroly.qrmenu.branch.entity.Branch;
import com.restroly.qrmenu.branch.repository.BranchRepository;
import com.restroly.qrmenu.category.dto.CategoryDTO;
import com.restroly.qrmenu.category.entity.Category;
import com.restroly.qrmenu.category.repository.CategoryRepository;
Expand All @@ -26,7 +23,6 @@
public class CategoryServiceImpl implements CategoryService {

private final CategoryRepository categoryRepository;
private final BranchRepository branchRepository;

/* =======================
CREATE CATEGORY
Expand All @@ -35,20 +31,7 @@ public class CategoryServiceImpl implements CategoryService {
public CategoryResponseDTO createCategory(CategoryRequestDTO requestDTO) {
log.debug("Creating category with name: {}", requestDTO.getName());

// Check for duplicate category name within the same branch
if(categoryRepository.existsByNameIgnoreCaseAndBranch_BranchId(
requestDTO.getName(), requestDTO.getBranchId())) {
log.warn("Category with name '{}' already exists in branch '{}'", requestDTO.getName(), requestDTO.getBranchId());
throw new DuplicateResourceException("Category with name '" + requestDTO.getName() + "' already exists");
}

// Validate branch existence
Branch branch = branchRepository.findById(requestDTO.getBranchId())
.orElseThrow(() ->{
log.warn("Branch with ID '{}' not found for category creation", requestDTO.getBranchId());
return new ResourceNotFoundException("Branch with ID '" + requestDTO.getBranchId() + "' not found");
});

// Check for duplicate category name within the same branch //add mapping before logic
Category category = CategoryDTO.toEntity(
CategoryDTO.builder()
.name(requestDTO.getName())
Expand All @@ -58,9 +41,6 @@ public CategoryResponseDTO createCategory(CategoryRequestDTO requestDTO) {
.build()
);

// Attach branch
category.setBranch(branch);

Category savedCategory = categoryRepository.save(category);
log.info("Category created with ID: {}", savedCategory.getCategoryId());
return CategoryResponseDTO.fromEntity(savedCategory);
Expand Down
Loading