Files
gsf/app/Http/Controllers/API/ManageNotificationController.php
vedant-chavan 20f55281ef save to codehub
2024-08-09 17:11:41 +05:30

122 lines
3.8 KiB
PHP

<?php
namespace App\Http\Controllers\API;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\User;
use App\Models\NotificationMaster;
use App\Models\NotificationUser;
use Auth;
use Carbon\Carbon;
class ManageNotificationController extends Controller
{
public function get_notification_count(){
try{
$token = readHeaderToken();
if($token){
$user_id = $token['sub'];
$notification_count = NotificationUser::where('user_id',$user_id)
->where('is_read','0')->count();
// print_r($notification_list);
// exit;
$notification_list = NotificationUser::with('notification_master_data','user_data')->where('user_id',$user_id)->get()->toArray();
// echo "<pre>";
// print_r($notification_list);
// exit;
return response()->json([
'success' => true,
'message' => 'Data Fetch Successfully!.',
'notification_count' => $notification_count,
'notification_list' => $notification_list
]);
}else {
return response()->json([
'success' => false,
'message' => 'Authentication failed.',
]);
}
} catch (Exception $ex) {
return response([
'status' => "failed",
], 500);
}
}
public function update_read_status(Request $request){
try {
$token = readHeaderToken();
if($token){
$user_id = $token['sub'];
$nitification_id = $request->notification_id;
NotificationUser::where('notification_id',$nitification_id)
->where('user_id',$user_id)
->update(['is_read' => '1']);
return response()->json([
'success' => true,
'message' => 'Data Updated Successfully!.'
]);
}else {
return response()->json([
'success' => false,
'message' => 'Authentication failed.',
]);
}
} catch (Exception $ex) {
return response([
'status' => "failed",
], 500);
}
}
public function delete_notification(Request $request){
try{
$token = readHeaderToken();
if($token){
$user_id = $token['sub'];
$notification_id = $request->notification_id;
// print_r($notification_id);
// exit;
NotificationUser::where('notification_id',$notification_id)
->where('user_id',$user_id)->delete();
return response()->json([
'success' => true,
'message' => 'Deleted Successfully!.'
]);
}else {
return response()->json([
'success' => false,
'message' => 'Authentication failed.',
]);
}
} catch (Exception $ex) {
}
}
// public function delete(){
// $threeDaysAgo = Carbon::now()->subDays(3);
// NotificationUser::where('created_at','<',$threeDaysAgo)->delete();
// return response()->json([
// 'success' => true,
// 'message' => 'Deleted Successfully!.'
// ]);
// }
}