|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * SPDX-License-Identifier: MIT |
| 5 | + * Copyright (c) 2017-2018 Tobias Reich |
| 6 | + * Copyright (c) 2018-2025 LycheeOrg. |
| 7 | + */ |
| 8 | + |
| 9 | +namespace App\Actions\Photo; |
| 10 | + |
| 11 | +use App\Exceptions\ConflictingPropertyException; |
| 12 | +use App\Models\Photo; |
| 13 | +use App\Models\PhotoRating; |
| 14 | +use App\Models\Statistics; |
| 15 | +use App\Models\User; |
| 16 | +use Illuminate\Support\Facades\DB; |
| 17 | + |
| 18 | +class Rating |
| 19 | +{ |
| 20 | + /** |
| 21 | + * Set or update the rating for the photo. |
| 22 | + * |
| 23 | + * Handles: |
| 24 | + * - Creating new ratings (rating > 0, no existing rating) |
| 25 | + * - Updating existing ratings (rating > 0, existing rating) |
| 26 | + * - Removing ratings (rating == 0) |
| 27 | + * - Atomic statistics updates |
| 28 | + * |
| 29 | + * @param Photo $photo The photo to rate |
| 30 | + * @param User $user The user rating the photo |
| 31 | + * @param int $rating The rating value (0-5, where 0 removes the rating) |
| 32 | + * |
| 33 | + * @return Photo the photo with refreshed statistics |
| 34 | + * |
| 35 | + * @throws ConflictingPropertyException if a database conflict occurs during the transaction |
| 36 | + */ |
| 37 | + public function do(Photo $photo, User $user, int $rating): Photo |
| 38 | + { |
| 39 | + try { |
| 40 | + DB::transaction(function () use ($photo, $user, $rating): void { |
| 41 | + // Ensure statistics record exists atomically (Q001-07) |
| 42 | + $statistics = Statistics::firstOrCreate( |
| 43 | + ['photo_id' => $photo->id], |
| 44 | + [ |
| 45 | + 'album_id' => null, |
| 46 | + 'visit_count' => 0, |
| 47 | + 'download_count' => 0, |
| 48 | + 'favourite_count' => 0, |
| 49 | + 'shared_count' => 0, |
| 50 | + 'rating_sum' => 0, |
| 51 | + 'rating_count' => 0, |
| 52 | + ] |
| 53 | + ); |
| 54 | + |
| 55 | + if ($rating > 0) { |
| 56 | + // Find existing rating by this user for this photo |
| 57 | + $existing_rating = PhotoRating::where('photo_id', $photo->id) |
| 58 | + ->where('user_id', $user->id) |
| 59 | + ->first(); |
| 60 | + |
| 61 | + if ($existing_rating !== null) { |
| 62 | + // Update: adjust statistics delta |
| 63 | + $delta = $rating - $existing_rating->rating; |
| 64 | + $statistics->rating_sum += $delta; |
| 65 | + $existing_rating->rating = $rating; |
| 66 | + $existing_rating->save(); |
| 67 | + } else { |
| 68 | + // Insert: create new rating and increment statistics |
| 69 | + PhotoRating::create([ |
| 70 | + 'photo_id' => $photo->id, |
| 71 | + 'user_id' => $user->id, |
| 72 | + 'rating' => $rating, |
| 73 | + ]); |
| 74 | + $statistics->rating_sum += $rating; |
| 75 | + $statistics->rating_count++; |
| 76 | + } |
| 77 | + |
| 78 | + $statistics->save(); |
| 79 | + } else { |
| 80 | + // Rating == 0: remove rating (idempotent, Q001-06) |
| 81 | + $existing_rating = PhotoRating::where('photo_id', $photo->id) |
| 82 | + ->where('user_id', $user->id) |
| 83 | + ->first(); |
| 84 | + |
| 85 | + if ($existing_rating !== null) { |
| 86 | + $statistics->rating_sum -= $existing_rating->rating; |
| 87 | + $statistics->rating_count--; |
| 88 | + $statistics->save(); |
| 89 | + $existing_rating->delete(); |
| 90 | + } |
| 91 | + // If no existing rating, do nothing (idempotent) |
| 92 | + } |
| 93 | + }); |
| 94 | + |
| 95 | + // Reload photo with fresh statistics |
| 96 | + $photo->refresh(); |
| 97 | + |
| 98 | + return $photo; |
| 99 | + } catch (\Throwable $e) { |
| 100 | + throw new ConflictingPropertyException('Failed to update photo rating due to a conflict. Please try again.', $e); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments