unioil-loyalty-app/app/Http/Controllers/API/InAppNotificationController...

141 lines
5.1 KiB
PHP

<?php
namespace App\Http\Controllers\API;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\InAppNotifications;
use App\Helpers\HttpStatusCode;
use App\Contracts\AdminActionLogsInterface;
use App\Helpers\CurrentUserHelper;
use App\Http\Resources\InAppNotificationResource;
use App\Http\Requests\InAppNotificationFormValidation;
use App\Admin;
use Illuminate\Support\Facades\DB;
class InAppNotificationController extends Controller
{
public $top_up;
public $admin_logs;
protected $format;
const MODULE = 'NOTIFICATION';
public function __construct(HttpStatusCode $httpStatusCode, AdminActionLogsInterface $admin_logs)
{
$this->admin_logs = $admin_logs;
$this->format = $httpStatusCode;
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$otp = DB::select('select * from otp_logs order by otplog_id desc limit 1');
// Get all Notifications from the Notifications Table
$list = InAppNotifications::all();
if(!count($list)) return $this->format->success("No records found",[]); // Check if notifications are present and return the appropriate response
return response()->json(["code"=>200,"message"=>"SUCCESS","data"=>$otp], 200);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(InAppNotificationFormValidation $request)
{
// Check if admin is the one who is issuing the action.
$currentUser = CurrentUserHelper::get_currentUser();
$admin = Admin::where('username', $currentUser->username)->first();
if(!$admin->admin_id) return $this->format->unauthorized(); // Check if it is the admin, otherwise return appropriate response
$notification = new InAppNotifications();
$notification->subject = $request->get('subject');
$notification->description = $request->get('description');
$notification->trigger_schedule = $request->get('schedule') ? $request->get('schedule') : null;
$notification->expiration_date = $request->get('expiration') or null;
$notification->status = 0;
if(!$notification->save()) return $this->format->unprocessableEntity('Failed to create a notification.');
$id = $notification->id;
$this->admin_logs->log($id,self::MODULE,'STORE');
return $this->format->created('Notification has been created', ["id"=>$id]);
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
// Show specific notification
$data = InAppNotifications::where(['id' => $id])->first();
if(!$data) return $this->format->notFound(); // Check if notifications are present and return the appropriate response
return response()->json(["code"=>200,"message"=>"SUCCESS","data"=>$data], 200);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(InAppNotificationFormValidation $request, $id)
{
// Check if ID is present, otherwise return a not found error.
if(!$id) return $this->format->notFound();
// Check if admin is the one who is issuing the action.
// $currentUser = CurrentUserHelper::get_currentUser();
// $admin = Admin::where('username', $currentUser->username)->first();
// if(!$admin->admin_id) return $this->format->unauthorized(); // Check if it is the admin, otherwise return appropriate response
$data = InAppNotifications::where(['id' => $id])->first();
if(!$data) return $this->format->notFound(); // Check if notifications are present and return the appropriate response
$data->subject = $request->get('subject');
$data->description = $request->get('description');
$data->status = $request->get('status');
if(!$data->save()) return $this->format->unprocessableEntity('Failed to update the notification');
$id = $data->id;
$this->admin_logs->log($id,self::MODULE,'STORE');
return $this->format->success('Notification has been updated', ["id"=>$id]);
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
// Check if ID is present, otherwise return a not found error.
if(!$id) return $this->format->notFound();
$currentUser = CurrentUserHelper::get_currentUser();
$admin = Admin::where('username', $currentUser->username)->first();
if(!$admin->admin_id) return $this->format->unauthorized(); // Check if it is the admin, otherwise return appropriate response
$data = InAppNotifications::where(['id' => $id])->first();
if(!$data) return $this->format->notFound(); // Check if notifications are present and return the appropriate response
if(!$data->delete()) return $this->format->unprocessableEntity('Failed to delete the notification');
$id = $data->id;
$this->admin_logs->log($id,self::MODULE,'STORE');
return $this->format->success('Notification has been deleted');
}
}