Skip to content

_🛠️ Refactor suggestion_ #85

@gitnasr

Description

@gitnasr

🛠️ Refactor suggestion

Existence check is good, but the fetched entity is immediately discarded

You correctly guard against updating a non-existent sub-category, but you still update using a freshly-mapped subCategory object, losing any existing server-side values (same issue as above).

Reuse the fetched entity to preserve immutable properties:

-var subCategory = mapper.Map<SubCategory>(updatedSubCategoryDto);
-var existingSubCategory = await subCategoryRepository.GetByIdAsync(subCategory.Id);
+var existingSubCategory = await subCategoryRepository.GetByIdAsync(updatedSubCategoryDto.Id);
 if (existingSubCategory == null)
     throw new NotFoundException($"SubCategory with id {updatedSubCategoryDto.Id} not found");

+mapper.Map(updatedSubCategoryDto, existingSubCategory);
+var subCategory = existingSubCategory;

Also, you can drop the second null-check after Update, since the repository already confirmed existence.

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In Dentizone.Application/Services/CatalogService.cs around lines 107 to 113, the
code fetches the existing sub-category to check existence but then discards it
and updates using the incoming subCategory object, which may overwrite immutable
or server-set properties. To fix this, reuse the fetched existingSubCategory
entity by updating only the mutable fields from the incoming subCategory before
calling Update. Also, remove the redundant null check after Update since
existence is already confirmed.

Originally posted by @coderabbitai[bot] in #84 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions