mirror of
https://github.com/pacnpal/thrillwiki_laravel.git
synced 2025-12-20 11:51:11 -05:00
101 lines
2.7 KiB
Markdown
101 lines
2.7 KiB
Markdown
# Review System Models
|
|
|
|
## Review Model
|
|
Represents user reviews for rides in the system.
|
|
|
|
### Properties
|
|
- `id` (int) - Primary key
|
|
- `ride_id` (int) - Foreign key to rides table
|
|
- `user_id` (int) - Foreign key to users table
|
|
- `rating` (int) - Rating from 1 to 5
|
|
- `title` (string, nullable) - Optional review title
|
|
- `content` (text) - Review content
|
|
- `status` (enum) - ReviewStatus enum value
|
|
- `moderated_at` (timestamp) - When review was moderated
|
|
- `moderated_by` (int) - Foreign key to users table (moderator)
|
|
- `helpful_votes_count` (int) - Counter cache for helpful votes
|
|
|
|
### Relationships
|
|
- `ride` - BelongsTo relationship to Ride model
|
|
- `user` - BelongsTo relationship to User model
|
|
- `moderator` - BelongsTo relationship to User model
|
|
- `helpfulVotes` - HasMany relationship to HelpfulVote model
|
|
|
|
### Scopes
|
|
- `pending()` - Reviews awaiting moderation
|
|
- `approved()` - Approved reviews
|
|
- `rejected()` - Rejected reviews
|
|
- `byRide()` - Filter by ride
|
|
- `byUser()` - Filter by user
|
|
|
|
### Methods
|
|
- `approve()` - Approve the review
|
|
- `reject()` - Reject the review
|
|
- `moderate()` - General moderation method
|
|
- `toggleHelpfulVote()` - Toggle helpful vote from a user
|
|
|
|
## HelpfulVote Model
|
|
Represents users marking reviews as helpful.
|
|
|
|
### Properties
|
|
- `id` (int) - Primary key
|
|
- `review_id` (int) - Foreign key to reviews table
|
|
- `user_id` (int) - Foreign key to users table
|
|
- `created_at` (timestamp) - When vote was cast
|
|
|
|
### Relationships
|
|
- `review` - BelongsTo relationship to Review model
|
|
- `user` - BelongsTo relationship to User model
|
|
|
|
### Methods
|
|
- `toggle()` - Toggle vote status
|
|
|
|
## Database Considerations
|
|
|
|
### Indexes
|
|
1. Reviews Table:
|
|
- Primary Key: id
|
|
- Foreign Keys: ride_id, user_id, moderated_by
|
|
- Composite: [ride_id, user_id] (unique)
|
|
- Indexes: [ride_id, status], [user_id, created_at], status
|
|
|
|
2. Helpful Votes Table:
|
|
- Primary Key: id
|
|
- Foreign Keys: review_id, user_id
|
|
- Composite: [review_id, user_id] (unique)
|
|
- Indexes: [user_id, created_at]
|
|
|
|
### Constraints
|
|
1. Reviews:
|
|
- One review per ride per user
|
|
- Rating must be between 1 and 5
|
|
- Status must be valid enum value
|
|
- Content is required
|
|
|
|
2. Helpful Votes:
|
|
- One vote per review per user
|
|
- Cascading deletes with review
|
|
|
|
## Usage Examples
|
|
|
|
```php
|
|
// Create a review
|
|
$review = Review::create([
|
|
'ride_id' => $ride->id,
|
|
'user_id' => Auth::id(),
|
|
'rating' => 5,
|
|
'title' => 'Great ride!',
|
|
'content' => 'This was amazing...',
|
|
]);
|
|
|
|
// Toggle helpful vote
|
|
$review->toggleHelpfulVote(Auth::id());
|
|
|
|
// Moderate a review
|
|
$review->moderate(ReviewStatus::APPROVED, Auth::id());
|
|
|
|
// Get ride's approved reviews
|
|
$reviews = $ride->reviews()->approved()->latest()->get();
|
|
|
|
// Get user's helpful votes
|
|
$votes = $user->helpfulVotes()->with('review')->get(); |