0.1.0
This commit is contained in:
@@ -3,16 +3,16 @@
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||
use Database\Factories\UserFactory;
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Query\JoinClause;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class User extends Authenticatable implements MustVerifyEmail
|
||||
{
|
||||
/** @use HasFactory<UserFactory> */
|
||||
|
||||
use HasFactory, Notifiable;
|
||||
|
||||
/**
|
||||
@@ -24,6 +24,7 @@ class User extends Authenticatable implements MustVerifyEmail
|
||||
'name',
|
||||
'email',
|
||||
'password',
|
||||
'settings',
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -36,6 +37,10 @@ class User extends Authenticatable implements MustVerifyEmail
|
||||
'remember_token',
|
||||
];
|
||||
|
||||
protected $attributes = [
|
||||
'settings' => "{timezone:'UTC'}",
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the attributes that should be cast.
|
||||
*
|
||||
@@ -46,6 +51,7 @@ class User extends Authenticatable implements MustVerifyEmail
|
||||
return [
|
||||
'email_verified_at' => 'datetime',
|
||||
'password' => 'hashed',
|
||||
'settings' => 'array',
|
||||
];
|
||||
}
|
||||
|
||||
@@ -59,4 +65,41 @@ class User extends Authenticatable implements MustVerifyEmail
|
||||
return $this->belongsToMany(Chapter::class, 'reading_histories')->withTimestamps();
|
||||
}
|
||||
|
||||
public function cleanUpReadingHistories(): array
|
||||
{
|
||||
// Get the user's ID
|
||||
$userId = $this->id;
|
||||
|
||||
// Step 1: Identify records to keep
|
||||
$idsToKeep = ReadingHistory::query()
|
||||
->select('reading_histories.id') // Specify table name explicitly
|
||||
->joinSub(
|
||||
ReadingHistory::query()
|
||||
->select('comic_id', 'user_id', 'chapter_id', DB::raw('MIN(created_at) as earliest_created_at'))
|
||||
->where('user_id', $userId) // Specify user_id with table name
|
||||
->groupBy('comic_id', 'user_id', 'chapter_id'),
|
||||
'b',
|
||||
function (JoinClause $join) {
|
||||
$join->on('reading_histories.comic_id', '=', 'b.comic_id')
|
||||
->on('reading_histories.user_id', '=', 'b.user_id') // Disambiguate user_id
|
||||
->on('reading_histories.chapter_id', '=', 'b.chapter_id')
|
||||
->on('reading_histories.created_at', '=', 'b.earliest_created_at');
|
||||
}
|
||||
)
|
||||
->where('reading_histories.user_id', $userId) // Specify table name explicitly
|
||||
->pluck('id');
|
||||
|
||||
// Step 2: Delete duplicates for the user
|
||||
$deletedCount = ReadingHistory::where('user_id', $userId)
|
||||
->whereNotIn('id', $idsToKeep)
|
||||
->delete();
|
||||
|
||||
// Return the result as an array
|
||||
return [
|
||||
'user_id' => $userId,
|
||||
'kept_ids' => $idsToKeep,
|
||||
'deleted_count' => $deletedCount,
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user