Skip to content

Commit 65f900b

Browse files
committed
refactor(data): use ownershipCheckMiddleware to prevent duplicate DB calls
- Replace direct database call with fetched data from ownershipCheckMiddleware - Remove redundant read operation, improving efficiency - Ensure existing user object is still properly validated and used
1 parent f33667d commit 65f900b

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

routes/api/v1/data/[id]/index.dart

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'package:core/core.dart';
44
import 'package:dart_frog/dart_frog.dart';
55
import 'package:data_repository/data_repository.dart';
66
import 'package:flutter_news_app_api_server_full_source_code/src/helpers/response_helper.dart';
7+
import 'package:flutter_news_app_api_server_full_source_code/src/middlewares/ownership_check_middleware.dart';
78
import 'package:flutter_news_app_api_server_full_source_code/src/rbac/permission_service.dart';
89
import 'package:flutter_news_app_api_server_full_source_code/src/registry/model_registry.dart';
910
import 'package:flutter_news_app_api_server_full_source_code/src/services/dashboard_summary_service.dart';
@@ -330,13 +331,11 @@ Future<Response> _handlePut(
330331
// simply save the entire request body. Instead, we perform a safe,
331332
// partial update.
332333

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

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

0 commit comments

Comments
 (0)