messages implemented
This commit is contained in:
@@ -5,6 +5,8 @@ declare(strict_types=1);
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Services\FollowService;
|
||||
use App\Services\UserStatsService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
@@ -14,6 +16,8 @@ final class ArtworkInteractionController extends Controller
|
||||
{
|
||||
public function favorite(Request $request, int $artworkId): JsonResponse
|
||||
{
|
||||
$state = $request->boolean('state', true);
|
||||
|
||||
$this->toggleSimple(
|
||||
request: $request,
|
||||
table: 'user_favorites',
|
||||
@@ -25,6 +29,18 @@ final class ArtworkInteractionController extends Controller
|
||||
|
||||
$this->syncArtworkStats($artworkId);
|
||||
|
||||
// Update creator's favorites_received_count
|
||||
$creatorId = (int) DB::table('artworks')->where('id', $artworkId)->value('user_id');
|
||||
if ($creatorId) {
|
||||
$svc = app(UserStatsService::class);
|
||||
if ($state) {
|
||||
$svc->incrementFavoritesReceived($creatorId);
|
||||
$svc->setLastActiveAt((int) $request->user()->id);
|
||||
} else {
|
||||
$svc->decrementFavoritesReceived($creatorId);
|
||||
}
|
||||
}
|
||||
|
||||
return response()->json($this->statusPayload((int) $request->user()->id, $artworkId));
|
||||
}
|
||||
|
||||
@@ -72,41 +88,25 @@ final class ArtworkInteractionController extends Controller
|
||||
|
||||
public function follow(Request $request, int $userId): JsonResponse
|
||||
{
|
||||
if (! Schema::hasTable('friends_list')) {
|
||||
return response()->json(['message' => 'Follow unavailable'], 422);
|
||||
}
|
||||
|
||||
$actorId = (int) $request->user()->id;
|
||||
|
||||
if ($actorId === $userId) {
|
||||
return response()->json(['message' => 'Cannot follow yourself'], 422);
|
||||
}
|
||||
|
||||
$svc = app(FollowService::class);
|
||||
$state = $request->boolean('state', true);
|
||||
|
||||
$query = DB::table('friends_list')
|
||||
->where('user_id', $actorId)
|
||||
->where('friend_id', $userId);
|
||||
|
||||
if ($state) {
|
||||
if (! $query->exists()) {
|
||||
DB::table('friends_list')->insert([
|
||||
'user_id' => $actorId,
|
||||
'friend_id' => $userId,
|
||||
'date_added' => now(),
|
||||
]);
|
||||
}
|
||||
$svc->follow($actorId, $userId);
|
||||
} else {
|
||||
$query->delete();
|
||||
$svc->unfollow($actorId, $userId);
|
||||
}
|
||||
|
||||
$followersCount = (int) DB::table('friends_list')
|
||||
->where('friend_id', $userId)
|
||||
->count();
|
||||
|
||||
return response()->json([
|
||||
'ok' => true,
|
||||
'is_following' => $state,
|
||||
'followers_count' => $followersCount,
|
||||
'ok' => true,
|
||||
'is_following' => $state,
|
||||
'followers_count' => $svc->followersCount($userId),
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user