|
8 | 8 |
|
9 | 9 | namespace App\Http\Controllers\AiVision; |
10 | 10 |
|
| 11 | +use App\Factories\PersonFactory; |
| 12 | +use App\Http\Requests\Face\BatchAssignFacesRequest; |
11 | 13 | use App\Http\Requests\Face\BatchDismissFacesRequest; |
12 | 14 | use App\Http\Requests\Face\FaceMaintenanceIndexRequest; |
13 | 15 | use App\Http\Resources\Collections\PaginatedFaceResource; |
| 16 | +use App\Jobs\RecomputePersonStatsJob; |
14 | 17 | use App\Models\Face; |
15 | | -use App\Models\Person; |
16 | 18 | use Illuminate\Routing\Controller; |
17 | 19 |
|
18 | 20 | /** |
@@ -58,23 +60,34 @@ public function batchDismiss(BatchDismissFacesRequest $request): array |
58 | 60 | $count = Face::whereIn('id', $request->face_ids) |
59 | 61 | ->update(['is_dismissed' => true, 'person_id' => null]); |
60 | 62 |
|
61 | | - foreach ($affected_person_ids as $person_id) { |
62 | | - $person = Person::find($person_id); |
63 | | - if ($person === null) { |
64 | | - continue; |
65 | | - } |
| 63 | + RecomputePersonStatsJob::dispatchSync($affected_person_ids); |
66 | 64 |
|
67 | | - $person->face_count = Face::where('person_id', '=', $person_id)->where('is_dismissed', '=', false)->count(); |
68 | | - if ($person->face_count === 0) { |
69 | | - $person->delete(); |
70 | | - continue; |
71 | | - } |
| 65 | + return ['dismissed_count' => $count]; |
| 66 | + } |
72 | 67 |
|
73 | | - $person->photo_count = Face::where('person_id', '=', $person_id)->where('is_dismissed', '=', false)->distinct('photo_id')->count('photo_id'); |
74 | | - $person->save(); |
75 | | - } |
| 68 | + /** |
| 69 | + * Batch-assign multiple faces to an existing person or a newly created one. |
| 70 | + * |
| 71 | + * POST /Face/maintenance/batch-assign |
| 72 | + * |
| 73 | + * @return array{assigned_count: int, person_id: string} |
| 74 | + */ |
| 75 | + public function batchAssign(BatchAssignFacesRequest $request, PersonFactory $person_factory): array |
| 76 | + { |
| 77 | + $person = $person_factory->findOrCreate($request->person_id, $request->new_person_name); |
76 | 78 |
|
77 | | - return ['dismissed_count' => $count]; |
| 79 | + $old_person_ids = Face::whereIn('id', $request->face_ids) |
| 80 | + ->whereNotNull('person_id') |
| 81 | + ->where('person_id', '!=', $person->id) |
| 82 | + ->distinct() |
| 83 | + ->pluck('person_id') |
| 84 | + ->all(); |
| 85 | + |
| 86 | + $count = Face::whereIn('id', $request->face_ids)->update(['person_id' => $person->id]); |
| 87 | + |
| 88 | + RecomputePersonStatsJob::dispatchSync([$person->id, ...$old_person_ids]); |
| 89 | + |
| 90 | + return ['assigned_count' => $count, 'person_id' => $person->id]; |
78 | 91 | } |
79 | 92 | } |
80 | 93 |
|
0 commit comments