<?php

namespace App\Http\Controllers;

use App\Models\SocialLink;
use Exception;
use Illuminate\Support\Facades\DB;
use function request;

class SocialLinkController extends Controller
{
    public function store()
    {
        try {
            $transaction = DB::transaction(function () {
                SocialLink::create(request()->attributes);
            });
            return is_null($transaction) ? back(303)->with(['success' => 'success']) : $transaction;
        } catch (Exception $exception) {
            return back(303)->with(['error' => $exception->getMessage()]);
        }
    }

    public function update(SocialLink $socialLink)
    {

        $socialLink->update(request()->attributes);

        return back(303)->with(['success' => 'success']);

    }

    public function destroy(SocialLink $socialLink)
    {
        $socialLink->delete();
        return back(303)->with(['success' => 'success']);
    }

    public function index()
    {
        return SocialLink::all();
    }

}