Skip to content

refactor(data): use ownershipCheckMiddleware to prevent duplicate DB … #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 8, 2025
Merged
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
11 changes: 5 additions & 6 deletions routes/api/v1/data/[id]/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:core/core.dart';
import 'package:dart_frog/dart_frog.dart';
import 'package:data_repository/data_repository.dart';
import 'package:flutter_news_app_api_server_full_source_code/src/helpers/response_helper.dart';
import 'package:flutter_news_app_api_server_full_source_code/src/middlewares/ownership_check_middleware.dart';
import 'package:flutter_news_app_api_server_full_source_code/src/rbac/permission_service.dart';
import 'package:flutter_news_app_api_server_full_source_code/src/registry/model_registry.dart';
import 'package:flutter_news_app_api_server_full_source_code/src/services/dashboard_summary_service.dart';
Expand Down Expand Up @@ -330,13 +331,11 @@ Future<Response> _handlePut(
// simply save the entire request body. Instead, we perform a safe,
// partial update.

// 1. Fetch the existing, trusted user object from the database.
// 1. The existing, trusted user object is already fetched by the
// `ownershipCheckMiddleware` to prevent duplicate database calls.
// This ensures we have the current, authoritative state of the user,
// including their correct roles and ID.
final existingUser = await repo.read(
id: id,
userId: userIdForRepoCall,
);
// including their correct roles and ID, without hitting the DB again.
final existingUser = context.read<FetchedItem<dynamic>>().data as User;

// 2. Create a new User object by merging only the allowed, safe-to-update
// fields from the incoming request (`itemToUpdate`) into the
Expand Down
Loading