<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\NewsPodcasts;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Validator;
use Yajra\DataTables\Facades\DataTables;
class NewsPodcastsController extends Controller
{
public function allApi($type): JsonResponse
{
$data = NewsPodcasts::where('type', $type)->latest()->take(5)->get();
return success_api_processor($data, 'Fetched successfully!');
}
public function index()
{
return view('admin.news_podcasts.all');
}
public function add()
{
return view('admin.news_podcasts.add');
}
public function listApi(Request $request)
{
if ($request->ajax()) {
$data = NewsPodcasts::all();
return Datatables::of($data)
->addIndexColumn()
->editColumn('type', function ($row) {
return $row->type == 1 ? 'News' : 'Podcast';
})
->addColumn('action', function ($row) {
return '
<div class="">
<a href="' . route('admin.news_podcasts.edit', $row->id) . '" class="align-items-center btn btn-primary">
<span class="icon icofont-pencil-alt-2"></span>
Edit
</a>
<input class="delete_url" value="' . route("admin.news_podcasts.delete", $row->id) . '" hidden>
<a href="#" class="align-items-center btn btn-danger delete_btn">
<span class="icon icofont-pencil-alt-2"></span>
Delete
</a>
</div>';
})
->rawColumns(['action'])
->make(true);
} else {
return 'lost';
}
}
public function store(Request $request)
{
$validateArray = [
'type' => 'required|string',
'title' => 'required|string',
'audio_file' => 'required_if:type,==,2|string',
'description' => 'required|string',
];
//validate
$validator = Validator::make($request->all(), $validateArray, [
'audio_file' => 'Audio file is required when posting a podcast'
]);
if ($validator->fails()) {
return error_api_processor('Some fields seems not correct!', 200, validation_error_processor($validator));
}
try {
$audioName = '';
if ($request->filled('audio_file')) {
$audioName = get_base64_file($request->audio_file)['fileName'];
}
DB::beginTransaction();
$news_podcasts = NewsPodcasts::create([
'type' => $request->type,
'title' => $request->title,
'audio_file' => $audioName,
'description' => $request->description,
]);
if ($request->filled('audio_file')) {
store_base64_file($request->audio_file, $audioName, 'news_podcasts');
$news_podcasts->update([
'audio_file' => $audioName
]);
}
DB::commit();
$type = $request->type == 1 ? 'News ' : 'Podcast ';
$message = $type . 'created successfully.';
return success_api_processor($news_podcasts, $message);
} catch (Exception $e) {
DB::rollBack();
return error_api_processor('Something went wrong! Please try again later.', 200, []);
}
}
public function edit($id)
{
$thisNews = NewsPodcasts::find($id);
if ($thisNews)
return view('admin.news_podcasts.edit', compact('thisNews'));
else
return redirect()->route('admin.news_podcasts.all')->with('status', 'No news or podcast found');
}
public function update(Request $request, $id): JsonResponse
{
$validateArray = [
'title' => 'required|string',
// 'audio_file' => 'required_if:type,==,2|string',
'description' => 'required|string',
];
//validate
$validator = Validator::make($request->all(), $validateArray, [
'audio_file' => 'Audio file is required when posting a podcast'
]);
if ($validator->fails()) {
return error_api_processor('Some fields seems not correct!', 200, validation_error_processor($validator));
}
try {
DB::beginTransaction();
$news = NewsPodcasts::find($id);
$audioName = $news->audio_file;
if ($request->filled('audio_file')) {
$audioName = get_base64_file($request->audio_file)['fileName'];
}
if ($news) {
$news->update([
'title' => $request->title,
'audio_file' => $audioName,
'description' => $request->description,
]);
if ($request->filled('audio_file')) {
if ($news->audio_file != '') {
$path = storage_path('app/public/news_podcasts/' . $news->audio_file);
//if so, then unlink from storage
if (File::exists($path)) {
try {
unlink($path);
} catch (\Exception $e) {
}
}
}
store_base64_file($request->audio_file, $audioName, 'news_podcasts');
$news->update([
'audio_file' => $audioName
]);
}
}
$type = $news->type == 1 ? 'News ' : 'Podcast ';
$message = $type . 'updated successfully.';
DB::commit();
return success_api_processor($news, $message);
} catch (Exception $e) {
DB::rollBack();
return error_api_processor('Something went wrong! Please try again later.', 200, [$e]);
}
}
public function delete($id)
{
$news_podcast = NewsPodcasts::find($id);
if ($news_podcast) {
if ($news_podcast->type == 2) {
$fileName = $news_podcast->audio_file;
$news_podcast->delete();
try {
delete_file('news_podcasts', $fileName);
return success_api_processor([], "Podcast deleted successfully.");
} catch (Exception $e) {
return success_api_processor([$e], "Podcast deleted successfully.");
}
} else {
$news_podcast->delete();
return success_api_processor([], 'News deleted successfully.');
}
}
return error_api_processor('Something went wrong! Please try again later.', 200, []);
}
}