Created
January 11, 2024 17:19
-
-
Save CodeBySwati/b5ee2d5a149cc00c14597ed5f61ee857 to your computer and use it in GitHub Desktop.
Create a Laravel application to manage code snippets, based on GitHub Gist's.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // views/gist_comments/edit.blade.php | |
| @extends('layouts.app') | |
| @section('content') | |
| <div class="row"> | |
| @include('flash-message') | |
| <div class="col-md-12 mb-4 py-3 border-bottom border-secondary border-opacity-25"> | |
| <h1 class="h5 float-start"> | |
| Edit Comment | |
| </h1> | |
| <a href="{{ route('gists.comments.index', $gistId) }}" class="btn btn-sm btn-outline-secondary float-end"><i class="bi bi-chat-text"></i> All Comments</a> | |
| </div> | |
| <form method="post" action="{{ route('gists.comments.update', ['gistId' => $gistId, 'commentId' => $comment['id']]) }}"> | |
| @csrf | |
| @method('PUT') | |
| <div class="mb-3"> | |
| <label for="body" class="form-label">Comment :</label> | |
| <textarea id="body" name="body" class="form-control" required>{{ $comment['body'] }}</textarea> | |
| </div> | |
| <button type="submit" class="btn btn-sm btn-primary">Update Comment</button> | |
| <form method="post" action="{{ route('gists.comments.destroy', ['gistId' => $gistId, 'commentId' => $comment['id']]) }}" class="float-end"> | |
| @csrf | |
| @method('DELETE') | |
| <button type="submit" class="btn btn-sm btn-outline-danger"><i class="bi bi-trash"></i> Delete</button> | |
| </form> | |
| </form> | |
| </div> | |
| @endsection |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // views/gist_comments/index.blade.php | |
| @extends('layouts.app') | |
| @section('content') | |
| <div class="row"> | |
| @include('flash-message') | |
| <div class="col-md-12 mb-4 py-3 border-bottom border-secondary border-opacity-25"> | |
| <h1 class="h5 col float-start"> | |
| Comments on Gist | |
| </h1> | |
| </div> | |
| <div class="col-md-10"> | |
| @foreach($comments as $comment) | |
| <div class="card mb-3"> | |
| <div class="card-header"> | |
| <img src="{{ $comment['user']['avatar_url'] ?? '' }}" alt="" class="user-avatar float-start"> | |
| {{ $comment['user']['login'] }} | <small class="">Commented on {{ App\Http\Controllers\GistController::calculate_time(time(), $comment['created_at']) }} </small> | |
| <form method="post" action="{{ route('gists.comments.destroy', ['gistId' => $gistId, 'commentId' => $comment['id']]) }}" class="float-end"> | |
| @csrf | |
| @method('DELETE') | |
| <button type="submit" class="btn btn-sm btn-outline-danger"><i class="bi bi-trash"></i> Delete</button> | |
| </form> | |
| <a href="{{ route('gists.comments.edit', ['gistId' => $gistId, 'commentId' => $comment['id']]) }}" class="btn btn-sm btn-outline-secondary mr-1 float-end"> | |
| <i class="bi bi-pencil"></i> Edit | |
| </a> | |
| <br class="clearfix"> | |
| </div> | |
| <div class="card-body small">{{ $comment['body'] }}</div> | |
| </div> | |
| @endforeach | |
| </div> | |
| <div class="clearfix"></div> | |
| <div class="col-md-10 mb-4 py-3"> | |
| <form method="post" action="{{ route('gists.comments.store', $gistId) }}" class="mt-2"> | |
| @csrf | |
| <label for="body" class="form-label h6">Add Comment :</label> | |
| <textarea id="body" name="body" placeholder="Leave a comment" class="form-control" rows="3"></textarea> | |
| <button type="submit" class="btn btn-sm btn-primary mt-2">Add Comment</button> | |
| </form> | |
| <div class="clearfix"></div> | |
| </div> | |
| </div> | |
| @endsection |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Controller/GistCommentController.php | |
| <?php | |
| namespace App\Http\Controllers; | |
| use Illuminate\Http\Request; | |
| use Illuminate\Support\Facades\Http; | |
| use Illuminate\Support\Facades\Config; | |
| class GistCommentController extends Controller | |
| { | |
| public function showComments(Request $request, $gistId) | |
| { | |
| // Fetch comments for a specific Gist using GitHub API | |
| $response = Http::withToken($request->user()->auth_token)->get("https://api.github.com/gists/{$gistId}/comments"); | |
| // Check if the request was successful | |
| if ($response->successful()) { | |
| $comments = $response->json(); | |
| return view('gist_comments.index', compact('comments', 'gistId')); | |
| } else { | |
| // Handle error response | |
| return redirect()->back()->withErrors(['error' => 'Failed to fetch comments for the Gist.']); | |
| } | |
| } | |
| public function storeComment(Request $request, $gistId) | |
| { | |
| $token = Config::get('services.github.personal_access_token'); | |
| $url = "https://api.github.com/gists/{$gistId}/comments"; | |
| // Fetch the current Gist content | |
| $currentGist = Http::withToken($token)->get($url)->json(); | |
| // Update the Gist content | |
| $updatedGist = [ | |
| 'body' => $request->input('body'), | |
| ]; | |
| // Merge the current Gist content with the updated content | |
| $mergedGist = array_merge($currentGist, $updatedGist); | |
| // Perform the PATCH request to update a comment on the Gist using Github API | |
| $response = Http::withToken($token)->post($url, $mergedGist); | |
| // Check if the request was successful | |
| if ($response->successful()) { | |
| return redirect()->route('gists.comments.index', $gistId)->with('success', 'Comment added successfully.'); | |
| } else { | |
| // Handle error response | |
| return redirect()->back()->withErrors(['error' => 'Failed to add comment to the Gist.']); | |
| } | |
| } | |
| public function editComment(Request $request, $gistId, $commentId) | |
| { | |
| // Fetch a specific comment for the Gist using GitHub API | |
| $response = Http::withToken($request->user()->auth_token)->get("https://api.github.com/gists/{$gistId}/comments/{$commentId}"); | |
| // Check if the request was successful | |
| if ($response->successful()) { | |
| $comment = $response->json(); | |
| return view('gist_comments.edit', compact('comment', 'gistId')); | |
| } else { | |
| // Handle error response | |
| return redirect()->back()->withErrors(['error' => 'Failed to fetch comment for editing.']); | |
| } | |
| } | |
| public function updateComment(Request $request, $gistId, $commentId) | |
| { | |
| $token = Config::get('services.github.personal_access_token'); | |
| $url = "https://api.github.com/gists/{$gistId}/comments/{$commentId}"; | |
| // Fetch the current Gist content | |
| $currentGist = Http::withToken($token)->get($url)->json(); | |
| // Update the Gist content | |
| $updatedGist = [ | |
| 'body' => $request->input('body'), | |
| ]; | |
| // Merge the current Gist content with the updated content | |
| $mergedGist = array_merge($currentGist, $updatedGist); | |
| // Perform the PATCH request to update a comment on the Gist using Github API | |
| $response = Http::withToken($token)->patch($url, $mergedGist); | |
| // Check if the request was successful | |
| if ($response->successful()) { | |
| return redirect()->route('gists.comments.index', $gistId)->with('success', 'Comment updated successfully.'); | |
| } else { | |
| // Handle error response | |
| return redirect()->back()->withErrors(['error' => 'Failed to update comment on the Gist.']); | |
| } | |
| } | |
| public function destroyComment(Request $request, $gistId, $commentId) | |
| { | |
| $token = Config::get('services.github.personal_access_token'); | |
| $url = "https://api.github.com/gists/{$gistId}/comments/{$commentId}"; | |
| // Fetch the current Gist content | |
| $currentGist = Http::withToken($token)->get($url)->json(); | |
| // Delete a comment on the Gist using GitHub API | |
| $response = Http::withToken($token)->delete($url, $currentGist); | |
| // Check if the request was successful | |
| if ($response->successful()) { | |
| return redirect()->route('gists.comments.index', $gistId)->with('success', 'Comment deleted successfully.'); | |
| } else { | |
| // Handle error response | |
| return redirect()->back()->withErrors(['error' => 'Failed to delete comment on the Gist.']); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Controller/GistController.php | |
| <?php | |
| namespace App\Http\Controllers; | |
| use Illuminate\Http\Request; | |
| use Illuminate\Support\Facades\Http; | |
| use Illuminate\Support\Facades\Config; | |
| class GistController extends Controller | |
| { | |
| public function index(Request $request) | |
| { | |
| // Fetch user's Gists using GitHub API | |
| $response = Http::withToken($request->user()->auth_token)->get('https://api.github.com/gists'); | |
| // Check if the request was successful | |
| if ($response->successful()) { | |
| $gists = array(); | |
| $gists_arr = $response->json(); | |
| foreach($gists_arr as $gist): | |
| // Check starred gist | |
| $check_if_gist_starred = $this->checkStarredGist($gist['id']); | |
| if($check_if_gist_starred == '204'): | |
| $check_star = 1; | |
| elseif($check_if_gist_starred == '404'): | |
| $check_star = 0; | |
| endif; | |
| $gist['starred_flag'] = $check_star; | |
| $gists[] = $gist; | |
| endforeach; | |
| return view('gists.index', compact('gists')); | |
| } else { | |
| // Handle error response | |
| return redirect()->back()->withErrors(['error' => 'Failed to fetch Gists.']); | |
| } | |
| } | |
| public function show(Request $request, $gistId) | |
| { | |
| // Fetch a specific Gist using GitHub API | |
| $response = Http::withToken($request->user()->auth_token)->get("https://api.github.com/gists/{$gistId}"); | |
| // Check if the request was successful | |
| if ($response->successful()) { | |
| // Fetch comments for a specific Gist using GitHub API | |
| $comments_response = Http::withToken($request->user()->auth_token)->get("https://api.github.com/gists/{$gistId}/comments"); | |
| // Check if the request was successful | |
| if ($comments_response->successful()) { | |
| $comments = $comments_response->json(); | |
| }else { | |
| $comments = 0; | |
| } | |
| // Check starred gist | |
| $check_if_gist_starred = $this->checkStarredGist($gistId); | |
| if($check_if_gist_starred == '204'): | |
| $check_star = 1; | |
| elseif($check_if_gist_starred == '404'): | |
| $check_star = 0; | |
| endif; | |
| $gist_starred_flag = $check_star; | |
| $gist = $response->json(); | |
| return view('gists.show', compact('gist', 'comments', 'gist_starred_flag')); | |
| } else { | |
| // Handle error response | |
| return redirect()->back()->withErrors(['error' => 'Failed to fetch Gist.']); | |
| } | |
| } | |
| public function create() | |
| { | |
| return view('gists.create'); | |
| } | |
| public function store(Request $request) | |
| { | |
| $token = Config::get('services.github.personal_access_token'); | |
| $url = "https://api.github.com/gists"; | |
| // Fetch the Gist connection | |
| $currentGist = Http::withToken($token)->get($url)->json(); | |
| // Create the Gist content | |
| $createdGist = [ | |
| 'description' => $request->input('description'), | |
| 'public' => $request->input('public', false), | |
| 'files' => [ | |
| $request->input('filename') => [ | |
| 'content' => $request->input('content'), | |
| ], | |
| ], | |
| ]; | |
| // Merge the current Gist content with the created content | |
| $mergedGist = array_merge($currentGist, $createdGist); | |
| // Perform the POST request to create the Gist using Github API | |
| $response = Http::withToken($token)->post($url, $mergedGist); | |
| // Check if the request was successful | |
| if ($response->successful()) { | |
| return redirect()->route('gists.index')->with('success', 'Gist created successfully.'); | |
| } else { | |
| // Handle error response | |
| return redirect()->back()->withErrors(['error' => 'Failed to create Gist.']); | |
| } | |
| } | |
| public function edit(Request $request, $gistId) | |
| { | |
| // Fetch the Gist to edit using GitHub API | |
| $response = Http::withToken($request->user()->auth_token)->get("https://api.github.com/gists/{$gistId}"); | |
| // Check if the request was successful | |
| if ($response->successful()) { | |
| $gist = $response->json(); | |
| return view('gists.edit', compact('gist')); | |
| } else { | |
| // Handle error response | |
| return redirect()->back()->withErrors(['error' => 'Failed to fetch Gist for editing.']); | |
| } | |
| } | |
| public function update(Request $request, $gistId) | |
| { | |
| $token = Config::get('services.github.personal_access_token'); | |
| $url = "https://api.github.com/gists/{$gistId}"; | |
| // Fetch the current Gist content | |
| $currentGist = Http::withToken($token)->get($url)->json(); | |
| // Update the Gist content | |
| $updatedGist = [ | |
| 'description' => $request->input('description'), | |
| 'files' => json_decode($request->input('files')), | |
| ]; | |
| // Merge the current Gist content with the updated content | |
| $mergedGist = array_merge($currentGist, $updatedGist); | |
| // Perform the PATCH request to update the Gist using Github API | |
| $response = Http::withToken($token)->patch($url, $mergedGist); | |
| // Check if the request was successful | |
| if ($response->successful()) { | |
| return redirect()->route('gists.show', $gistId)->with('success', 'Gist updated successfully.'); | |
| } else { | |
| // Handle error response | |
| return redirect()->back()->withErrors(['error' => 'Failed to update Gist.']); | |
| } | |
| } | |
| public function destroy(Request $request, $gistId) | |
| { | |
| $token = Config::get('services.github.personal_access_token'); | |
| $url = "https://api.github.com/gists/{$gistId}"; | |
| // Fetch the current Gist content | |
| $currentGist = Http::withToken($token)->get($url)->json(); | |
| // Delete the Gist using GitHub API | |
| $response = Http::withToken($token)->delete($url, $currentGist); | |
| // Check if the request was successful | |
| if ($response->successful()) { | |
| return redirect()->route('gists.index')->with('success', 'Gist deleted successfully.'); | |
| } else { | |
| // Handle error response | |
| return redirect()->back()->withErrors(['error' => 'Failed to delete Gist.']); | |
| } | |
| } | |
| public function checkStarredGist($gistId) { | |
| $token = Config::get('services.github.personal_access_token'); | |
| $url = "https://api.github.com/gists/{$gistId}/star"; | |
| // Fetch the current Gist content | |
| $currentGist = Http::withToken($token)->get($url)->json(); | |
| // Check If Gist is Starred using GitHub API | |
| $response = Http::withToken($token)->get($url, $currentGist); | |
| if ($response->successful()) { | |
| return $response->status(); | |
| }else { | |
| return $response->status(); | |
| } | |
| } | |
| public static function calculate_time($start_time, $end_time) { | |
| $start_time = time(); // Current Date | |
| $end_time = strtotime($end_time); | |
| $date_diff = $start_time - $end_time; | |
| $days = round($date_diff / (60 * 60 * 24)); | |
| $hours = round($date_diff / 3600); | |
| return ($days > 0) ? $days.' days ago' : $hours .' hour ago'; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // views/gists/create.blade.php | |
| @extends('layouts.app') | |
| @section('content') | |
| <div class="row"> | |
| <div class="mb-4 py-3 border-bottom border-secondary border-opacity-25"> | |
| <h1 class="h5 col float-start"> | |
| Create a Gist | |
| </h1> | |
| <div class="col float-end"> | |
| <a href="{{ route('gists.index') }}" class="btn btn-sm btn-outline-secondary mr-1"><i class="bi bi-collection"></i> All Gists</a> | |
| </div> | |
| </div> | |
| <form method="post" action="{{ route('gists.store') }}"> | |
| @csrf | |
| <div class="mb-3"> | |
| <label for="description" class="form-label">Description: </label> | |
| <input type="text" id="description" name="description" required class="form-control"> | |
| </div> | |
| <div class="mb-3"> | |
| <label for="filename" class="form-label">Filename: </label> | |
| <input type="text" id="filename" name="filename" required class="form-control"> | |
| </div> | |
| <div class="mb-3"> | |
| <label for="content" class="form-label">Content: </label> | |
| <textarea id="content" name="content" required class="form-control" rows="10"></textarea> | |
| </div> | |
| <div class="mb-3"> | |
| <label for="public" class="form-label">Public: </label> | |
| <input type="checkbox" id="public" name="public" value="true"> | |
| </div> | |
| <button type="submit" class="btn btn-sm btn-outline-primary">Create Gist</button> | |
| </form> | |
| </div> | |
| @endsection |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // // views/gists/edit.blade.php | |
| @extends('layouts.app') | |
| @section('content') | |
| <div class="row"> | |
| @include('flash-message') | |
| <div class="col-md-12 mb-4 py-3 border-bottom border-secondary border-opacity-25"> | |
| <h1 class="h5 col"> | |
| Edit Gist | |
| </h1> | |
| </div> | |
| <div class="col-md-12"> | |
| <form method="post" action="{{ route('gists.update', $gist['id']) }}"> | |
| @csrf | |
| @method('PUT') | |
| <div class="mb-3"> | |
| <label for="description" class="form-label">Description:</label> | |
| <input type="text" id="description" name="description" value="{{ $gist['description'] ?? '' }}" required class="form-control"> | |
| </div> | |
| <div class="mb-3"> | |
| <label for="files" class="form-label">Files (JSON format):</label> | |
| <textarea id="files" name="files" required class="form-control" rows="15">{{ json_encode($gist['files'], JSON_PRETTY_PRINT) }}</textarea> | |
| </div> | |
| {{-- Additional fields for editing files, etc. --}} | |
| <button type="submit" class="btn btn-primary">Update Gist</button> | |
| </form> | |
| </div> | |
| </div> | |
| @endsection |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // views/gists/index.blade.php | |
| @extends('layouts.app') | |
| @section('content') | |
| <div class="row"> | |
| @include('flash-message') | |
| <div class="col mb-4 py-3 border-bottom border-secondary border-opacity-25"> | |
| <h1 class="h5 float-start">Your Gists <small class="badge rounded-pill text-bg-warning"> {{ count($gists) }}</small></h1> | |
| <a href="{{ route('gists.create') }}" class="btn btn-sm btn-dark float-end"> | |
| <i class="bi bi-plus"></i> Create New Gist | |
| </a> | |
| <div class="clearfix"></div> | |
| </div> | |
| <div class="col-md-12"> | |
| @foreach($gists as $gist) | |
| <div class="card mb-3"> | |
| <div class="card-header"> | |
| <a class="float-start" href="{{ route('gists.show', $gist['id']) }}"> | |
| <h6 class="card-title"> | |
| {{ $gist['description'] ?? 'No description' }} | |
| </h6> | |
| </a> | |
| <div class="float-end small"> | |
| <a href="{{ route('gists.show', $gist['id']) }}" class="btn btn-sm btn-light"> | |
| <i class="bi bi-code-square"></i> {{ (count($gist['files']) > 0) ? count($gist['files']).' Files' : '0 File' }} | |
| </a> | |
| <a href="{{ route('gists.comments.index', $gist['id']) }}" class="btn btn-sm btn-light"> | |
| <i class="bi bi-comment"></i> {{ ($gist['comments'] > 0) ? $gist['comments'].' Comments' : '0 Comment' }} | |
| </a> | |
| <a href="{{ route('gists.show', $gist['id']) }}" class="btn btn-sm btn-light"> | |
| <i class="bi bi-star"></i> {{ ($gist['starred_flag'] > 0) ? '1' : '0' }} Star | |
| </a> | |
| </form> | |
| </div> | |
| <div class="clearfix"></div> | |
| </div> | |
| <div class="card-body"> | |
| <div class="card-text"> | |
| <div class="col"> | |
| <form method="post" action="{{ route('gists.destroy', $gist['id']) }}"> | |
| @csrf | |
| @method('DELETE') | |
| <button type="submit" class="btn btn-sm btn-outline-danger float-end"><i class="bi bi-trash"></i> Delete</button> | |
| </form> | |
| <a href="{{ route('gists.edit', $gist['id']) }}" class="btn btn-sm btn-outline-secondary mr-1 float-end"><i class="bi bi-pencil"></i> Edit</a> | |
| </div> | |
| <p> | |
| <img src="{{ $gist['owner']['avatar_url'] ?? '' }}" alt="" class="user-avatar float-start"> | |
| <dd class="float-start"> {{ $gist['owner']['login'] ?? '' }} </dd> | |
| <br class="clearfix"> | |
| </p> | |
| <small>Created <b>{{ App\Http\Controllers\GistController::calculate_time(time(), $gist['created_at']) }}</b> | Last updated <b>{{ App\Http\Controllers\GistController::calculate_time(time(), $gist['updated_at']) }}</b></small> | |
| <div class="gist-preview mt-3"> | |
| <script src="{{ $gist['html_url'] ?? '' }}.js"></script> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| @endforeach | |
| </div> | |
| </div> | |
| @endsection |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // views/gists/show.blade.php | |
| @extends('layouts.app') | |
| @section('content') | |
| <div class="row"> | |
| @include('flash-message') | |
| <div class="col-md-12 mb-4 py-3 border-bottom border-secondary border-opacity-25"> | |
| <h1 class="h5 col float-start"> | |
| {{ $gist['description'] ?? 'No description' }} | |
| </h1> | |
| <div class="col float-end"> | |
| @php if($gist_starred_flag > 0): @endphp | |
| <form method="post" action="{{ route('gists.unstar', $gist['id']) }}" class="float-end"> | |
| @csrf | |
| @method('DELETE') | |
| <button type="submit" class="btn btn-sm btn-warning"><i class="bi bi-star-fill"></i> Unstar</button> | |
| </form> | |
| @php else: @endphp | |
| <form method="post" action="{{ route('gists.star', $gist['id']) }}" class="float-end"> | |
| @csrf | |
| @method('PUT') | |
| <button type="submit" class="btn btn-sm btn-warning"><i class="bi bi-star"></i> Star It</button> | |
| </form> | |
| @php endif; @endphp | |
| <form method="post" action="{{ route('gists.destroy', $gist['id']) }}" class="float-end mr-1"> | |
| @csrf | |
| @method('DELETE') | |
| <button type="submit" class="btn btn-sm btn-danger"><i class="bi bi-trash"></i> Delete</button> | |
| </form> | |
| <a href="{{ route('gists.edit', $gist['id']) }}" class="btn btn-sm btn-outline-secondary mr-1"><i class="bi bi-pencil"></i> Edit</a> | |
| </div> | |
| <div class="clearfix"></div> | |
| </div> | |
| <div class="col-md-12 mb-3"> | |
| <script src="{{ $gist['html_url'] ?? '' }}.js"></script> | |
| </div> | |
| <div class="col-md-12"> | |
| <div class="offset-md-1 col-md-11"> | |
| @foreach($comments as $comment) | |
| <div class="card mb-3"> | |
| <div class="card-header"> | |
| <img src="{{ $comment['user']['avatar_url'] ?? '' }}" alt="" class="user-avatar float-start"> | |
| {{ $comment['user']['login'] }} | <small class="">Commented on {{ App\Http\Controllers\GistController::calculate_time(time(), $comment['created_at']) }} </small> | |
| <a href="{{ route('gists.comments.edit', ['gistId' => $gist['id'], 'commentId' => $comment['id']]) }}" class="btn btn-sm btn-outline-secondary float-end"> | |
| Edit | |
| </a> | |
| <br class="clearfix"> | |
| </div> | |
| <div class="card-body small">{{ $comment['body'] }}</div> | |
| </div> | |
| @endforeach | |
| </div> | |
| <div class="offset-md-1 col-md-11 pt-1 py-3"> | |
| <div class="border-bottom border-secondary border-opacity-25 m-0 mb-4 "></div> | |
| <form method="post" action="{{ route('gists.comments.store', $gist['id']) }}" class="mt-2"> | |
| @csrf | |
| <label for="body" class="form-label h6">Add Comment :</label> | |
| <textarea id="body" name="body" placeholder="Leave a comment" class="form-control" rows="3"></textarea> | |
| <button type="submit" class="btn btn-sm btn-primary mt-2">Add Comment</button> | |
| </form> | |
| <div class="clearfix"></div> | |
| </div> | |
| </div> | |
| {{-- <pre>{{ json_encode($gist, JSON_PRETTY_PRINT) }}</pre> --}} | |
| </div> | |
| @endsection |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Controller/GistStarController.php | |
| <?php | |
| namespace App\Http\Controllers; | |
| use Illuminate\Http\Request; | |
| use Illuminate\Support\Facades\Http; | |
| use Illuminate\Support\Facades\Config; | |
| class GistStarController extends Controller | |
| { | |
| public function starGist(Request $request, $gistId) | |
| { | |
| $token = Config::get('services.github.personal_access_token'); | |
| $url = "https://api.github.com/gists/{$gistId}/star"; | |
| // Fetch the current Gist content | |
| $currentGist = Http::withToken($token)->get($url)->json(); | |
| // Star the Gist using GitHub API | |
| $response = Http::withToken($token)->put($url, $currentGist); | |
| // Check if the request was successful | |
| if ($response->successful()) { | |
| return redirect()->route('gists.index')->with('success', 'Gist starred successfully.'); | |
| } else { | |
| // Handle error response | |
| return redirect()->back()->withErrors(['error' => 'Failed to star the Gist.']); | |
| } | |
| } | |
| public function unstarGist(Request $request, $gistId) | |
| { | |
| $token = Config::get('services.github.personal_access_token'); | |
| $url = "https://api.github.com/gists/{$gistId}/star"; | |
| // Fetch the current Gist content | |
| $currentGist = Http::withToken($token)->get($url)->json(); | |
| // Un-star the Gist using GitHub API | |
| $response = Http::withToken($token)->delete($url, $currentGist); | |
| // Check if the request was successful | |
| if ($response->successful()) { | |
| return redirect()->route('gists.index')->with('success', 'Gist unstarred successfully.'); | |
| } else { | |
| // Handle error response | |
| return redirect()->back()->withErrors(['error' => 'Failed to unstar the Gist.']); | |
| } | |
| } | |
| public function starredGists(Request $request) | |
| { | |
| // Fetch starred Gists using GitHub API | |
| $response = Http::withToken($request->user()->auth_token)->get("https://api.github.com/gists/starred"); | |
| // Check if the request was successful | |
| if ($response->successful()) { | |
| $starredGists = $response->json(); | |
| return view('gist_star.starred', compact('starredGists')); | |
| } else { | |
| // Handle error response | |
| return redirect()->back()->withErrors(['error' => 'Failed to fetch starred Gists.']); | |
| } | |
| } | |
| public function checkStarredGist(Request $request, $gistId) | |
| { | |
| $token = Config::get('services.github.personal_access_token'); | |
| $url = "https://api.github.com/gists/{$gistId}/star"; | |
| // Fetch the current Gist content | |
| $currentGist = Http::withToken($token)->get($url)->json(); | |
| // Star the Gist using GitHub API | |
| $response = Http::withToken($token)->get($url, $currentGist); | |
| // Check if the request was successful | |
| if ($response->successful()) { | |
| return $response->status(); | |
| } else { | |
| // Handle error response | |
| return $response->status(); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Controller/GithubController.php | |
| <?php | |
| namespace App\Http\Controllers; | |
| use Laravel\Socialite\Facades\Socialite; | |
| use App\Models\User; | |
| use Illuminate\Support\Facades\Auth; | |
| use Illuminate\Support\Facades\Hash; | |
| use Illuminate\Support\Str; | |
| class GithubController extends Controller | |
| { | |
| public function redirectToGithub() { | |
| return Socialite::driver('github')->redirect(); | |
| } | |
| public function handleGithubCallback() | |
| { | |
| try { | |
| $user = Socialite::driver('github')->user(); | |
| $find_user = User::where('github_id', $user->id)->first(); | |
| if($find_user){ | |
| Auth::login($find_user); | |
| return redirect('/dashboard'); | |
| }else{ | |
| $checkUser = User::where('email', $user->email)->first(); | |
| if($checkUser) { | |
| $checkUser->auth_type = 'github'; | |
| $checkUser->github_id = $user->id; | |
| $checkUser->auth_token = $user->token; | |
| $checkUser->github_refresh_token = $user->refreshToken; | |
| $checkUser->save(); | |
| Auth::login($checkUser); | |
| } else { | |
| $newUser = User::create([ | |
| 'name' => $user->name, | |
| 'nickname' => $user->nickname, | |
| 'email' => $user->email, | |
| 'github_id'=> $user->id, | |
| 'auth_type'=> 'github', | |
| 'auth_token' => $user->token, | |
| 'github_refresh_token' => $user->refreshToken, | |
| 'password' => Hash::make(Str::random(10)), | |
| ]); | |
| Auth::login($newUser, true); | |
| } | |
| return redirect('/dashboard'); | |
| } | |
| } catch (\Exception $e) { | |
| return redirect()->back()->withErrors(['error' => 'Something Went wrong!! Try later']); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // views/gist_star/starred.blade.php | |
| @extends('layouts.app') | |
| @section('content') | |
| <div class="row"> | |
| @include('flash-message') | |
| <div class="col-md-12 mb-4 py-3 border-bottom border-secondary border-opacity-25"> | |
| <h1 class="h5 col"> | |
| Your Starred Gists <small class="badge rounded-pill text-bg-warning"> {{ count($starredGists) }}</small> | |
| </h1> | |
| </div> | |
| <div class="col-md-12"> | |
| @foreach($starredGists as $starredGist) | |
| <div class="card mb-3"> | |
| <div class="card-header"> | |
| <a class="float-start" href="{{ route('gists.show', $starredGist['id']) }}"> | |
| <h6 class="card-title"> | |
| {{ $starredGist['description'] ?? 'No description' }} | |
| </h6> | |
| </a> | |
| <div class="float-end small"> | |
| <a href="{{ route('gists.show', $starredGist['id']) }}" class="btn btn-sm btn-light"> | |
| <i class="bi bi-code-square"></i> {{ (count($starredGist['files']) > 0) ? count($starredGist['files']).' Files' : '0 File' }} | |
| </a> | |
| <a href="{{ route('gists.show', $starredGist['id']) }}" class="btn btn-sm btn-light"> | |
| <i class="bi bi-comment"></i> {{ ($starredGist['comments'] > 0) ? $starredGist['comments'].' Comments' : '0 Comment' }} | |
| </a> | |
| <a href="{{ route('gists.show', $starredGist['id']) }}" class="btn btn-sm btn-light"> | |
| <i class="bi bi-star"></i> 1 Star | |
| </a> | |
| </div> | |
| <div class="clearfix"></div> | |
| </div> | |
| <div class="card-body"> | |
| <div class="card-text"> | |
| <div class="col"> | |
| <form method="post" action="{{ route('gists.unstar', $starredGist['id']) }}"> | |
| @csrf | |
| @method('DELETE') | |
| <button type="submit" class="btn btn-sm btn-warning float-end"><i class="bi bi-star-fill"></i> Unstar</button> | |
| </form> | |
| </div> | |
| <p> | |
| <img src="{{ $starredGist['owner']['avatar_url'] ?? '' }}" alt="" class="user-avatar float-start"> | |
| <dd class="float-start"> {{ $starredGist['owner']['login'] ?? '0' }} </dd> | |
| <br class="clearfix"> | |
| </p> | |
| <small>Created <b>{{ App\Http\Controllers\GistController::calculate_time(time(), $starredGist['created_at']) }}</b> | Last updated <b>{{ App\Http\Controllers\GistController::calculate_time(time(), $starredGist['updated_at']) }}</b></small> | |
| <div class="gist-preview mt-3"> | |
| <script src="{{ $starredGist['html_url'] ?? '' }}.js"></script> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| @endforeach | |
| </div> | |
| </div> | |
| @endsection |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // routes/web.php | |
| <?php | |
| use App\Http\Controllers\ProfileController; | |
| use App\Http\Controllers\GithubController; | |
| use App\Http\Controllers\GistController; | |
| use App\Http\Controllers\GistCommentController; | |
| use App\Http\Controllers\GistStarController; | |
| use Illuminate\Support\Facades\Route; | |
| /* | |
| |-------------------------------------------------------------------------- | |
| | Web Routes | |
| |-------------------------------------------------------------------------- | |
| | | |
| | Here is where you can register web routes for your application. These | |
| | routes are loaded by the RouteServiceProvider and all of them will | |
| | be assigned to the "web" middleware group. Make something great! | |
| | | |
| */ | |
| Route::get('/', function () { | |
| return view('welcome'); | |
| }); | |
| Route::get('/dashboard', function () { | |
| return view('dashboard'); | |
| })->middleware(['auth', 'verified'])->name('dashboard'); | |
| Route::middleware('auth')->group(function () { | |
| Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit'); | |
| Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update'); | |
| Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy'); | |
| }); | |
| require __DIR__.'/auth.php'; | |
| Route::get('auth/github', [GithubController::class, 'redirectToGithub'])->name('github.login'); | |
| Route::get('auth/github/callback', [GithubController::class, 'handleGithubCallback']); | |
| Route::middleware(['auth'])->group(function () { | |
| Route::get('/gists', [GistController::class, 'index'])->name('gists.index'); | |
| Route::get('/gists/{gistId}', [GistController::class, 'show'])->name('gists.show'); | |
| Route::get('/gist/create', [GistController::class, 'create'])->name('gists.create'); | |
| Route::post('/gists', [GistController::class, 'store'])->name('gists.store'); | |
| Route::get('/gists/{gistId}/edit', [GistController::class, 'edit'])->name('gists.edit'); | |
| Route::put('/gists/{gistId}', [GistController::class, 'update'])->name('gists.update'); | |
| Route::delete('/gists/{gistId}', [GistController::class, 'destroy'])->name('gists.destroy'); | |
| Route::get('/gists/{gistId}/comments', [GistCommentController::class, 'showComments'])->name('gists.comments.index'); | |
| Route::post('/gists/{gistId}/comments', [GistCommentController::class, 'storeComment'])->name('gists.comments.store'); | |
| Route::get('/gists/{gistId}/comments/{commentId}/edit', [GistCommentController::class, 'editComment'])->name('gists.comments.edit'); | |
| Route::put('/gists/{gistId}/comments/{commentId}', [GistCommentController::class, 'updateComment'])->name('gists.comments.update'); | |
| Route::delete('/gists/{gistId}/comments/{commentId}', [GistCommentController::class, 'destroyComment'])->name('gists.comments.destroy'); | |
| Route::put('/gists/{gistId}/star', [GistStarController::class, 'starGist'])->name('gists.star'); | |
| Route::delete('/gists/{gistId}/star', [GistStarController::class, 'unstarGist'])->name('gists.unstar'); | |
| Route::get('/starred_gists', [GistStarController::class, 'starredGists'])->name('gist.starred'); | |
| Route::get('/gists/{gistId}/star', [GistStarController::class, 'checkStarredGist'])->name('gist.check_star'); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment