update review api.

This commit is contained in:
Phuoc Nguyen
2025-11-17 17:54:32 +07:00
parent 0798b28db5
commit 0841e3bf3d
23 changed files with 4856 additions and 209 deletions

View File

@@ -0,0 +1,61 @@
/// Use Case: Submit Review
///
/// Submits a new product review or updates an existing one.
library;
import 'package:worker/features/reviews/domain/repositories/reviews_repository.dart';
/// Use case for submitting a product review
class SubmitReview {
const SubmitReview(this._repository);
final ReviewsRepository _repository;
/// Execute the use case
///
/// [itemId] - Product item code
/// [rating] - Rating value (0-1 scale for API)
/// [comment] - Review comment text
/// [name] - Optional review ID for updates
///
/// Note: The rating should be in 0-1 scale for the API.
/// If you have a 1-5 star rating, convert it first: `stars / 5.0`
Future<void> call({
required String itemId,
required double rating,
required String comment,
String? name,
}) async {
// Validate rating range (0-1)
if (rating < 0 || rating > 1) {
throw ArgumentError(
'Rating must be between 0 and 1. Got: $rating. '
'If you have a 1-5 star rating, convert it first: stars / 5.0',
);
}
// Validate comment length
if (comment.trim().isEmpty) {
throw ArgumentError('Review comment cannot be empty');
}
if (comment.trim().length < 20) {
throw ArgumentError(
'Review comment must be at least 20 characters. Got: ${comment.trim().length}',
);
}
if (comment.length > 1000) {
throw ArgumentError(
'Review comment must not exceed 1000 characters. Got: ${comment.length}',
);
}
await _repository.submitReview(
itemId: itemId,
rating: rating,
comment: comment.trim(),
name: name,
);
}
}