Skip to content

Commit

Permalink
Add error handling and logging to controller methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Bijay-Shre-stha committed Mar 30, 2024
1 parent 5f35cba commit ce0fab9
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 28 deletions.
7 changes: 7 additions & 0 deletions app/Http/Controllers/AnswerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Http\Request;
use App\Models\Answer;
use App\Models\Question;
use Illuminate\Support\Facades\Log;

class AnswerController extends Controller
{
Expand Down Expand Up @@ -35,10 +36,16 @@ public function create()
*/
public function store(Request $request)
{
try{
$data = $request->all();
$answer = Answer::create($data);
return redirect()->route('question.show', ['question' => $answer->question_id])
->with('success', 'Your Answer has been added!');
}
catch(\Exception $e){
Log::error($e->getMessage());
return redirect()->back()->with('error', 'Error in adding answer');
}
}


Expand Down
3 changes: 3 additions & 0 deletions app/Http/Controllers/AvailableCommunityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;

class AvailableCommunityController extends Controller
{
Expand All @@ -21,6 +22,7 @@ public function index()
return view('availableCommunity.index', compact('communities'));
}
catch (Exception $e) {
Log::error($e->getMessage());
return redirect()->route('availableCommunity.index')->with('error', 'Error while fetching communities.');
}
}
Expand All @@ -40,6 +42,7 @@ public function join(Request $request, string $id)

return redirect()->route('availableCommunity.index')->with('success', 'You have successfully joined the community.');
} catch (Exception $e) {
Log::error($e->getMessage());
return redirect()->route('availableCommunity.index')->with('error', 'Error while joining the community, please login.');
}
}
Expand Down
7 changes: 7 additions & 0 deletions app/Http/Controllers/CommunityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Database\Schema\Blueprint;
use App\Models\User;
use App\Models\UserCommunity;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;

class CommunityController extends Controller
Expand Down Expand Up @@ -37,12 +38,18 @@ public function create()
*/
public function store(Request $request)
{
try{
$user = auth()->user();
$community = new UserCommunity();
$community->created_user_id = $user->id;
$community->communityName = $request->communityName;
$community->save();
return redirect(route('community.index'))->with('success', 'Community created successfully');
}
catch(\Exception $e){
Log::error($e->getMessage());
return redirect()->back()->with('error', 'Error in creating community');
}
}

/**
Expand Down
33 changes: 22 additions & 11 deletions app/Http/Controllers/JoinedCommunityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Models\CommunityQuestion;
use App\Models\JoinedUser;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class JoinedCommunityController extends Controller
{
Expand All @@ -21,9 +22,14 @@ public function index()

public function leaveCommunity($id)
{
$community = JoinedUser::findOrFail($id);
$community->delete();
return redirect()->route('joinedCommunity.index')->with('success', 'You have left the community!');
try {
$community = JoinedUser::findOrFail($id);
$community->delete();
return redirect()->route('joinedCommunity.index')->with('success', 'You have left the community!');
} catch (\Exception $e) {
Log::error($e->getMessage());
return redirect()->route('joinedCommunity.index')->with('error', 'Error while leaving the community.');
}
}


Expand Down Expand Up @@ -52,17 +58,22 @@ public function store(Request $request)
*/
public function show(string $id)
{
$joinedUser = JoinedUser::findOrFail($id);
try {
$joinedUser = JoinedUser::findOrFail($id);

if (!$joinedUser->userCommunity) {
// Handle the case where user community is not found
abort(404);
}
if (!$joinedUser->userCommunity) {
// Handle the case where user community is not found
abort(404);
}

$communityQuestions = CommunityQuestion::where('community_id', $joinedUser->user_community_id)->get();
$community_id = $joinedUser->user_community_id; // Get the community ID
$communityQuestions = CommunityQuestion::where('community_id', $joinedUser->user_community_id)->get();
$community_id = $joinedUser->user_community_id; // Get the community ID

return view('communityQuestion.index', compact('communityQuestions' ,'community_id'));
return view('communityQuestion.index', compact('communityQuestions', 'community_id'));
} catch (\Exception $e) {
Log::error($e->getMessage());
return redirect()->route('joinedCommunity.index')->with('error', 'Error while fetching community questions.');
}
}
/**
* Show the form for editing the specified resource.
Expand Down
43 changes: 26 additions & 17 deletions app/Http/Controllers/QuestionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Http\Request;
use App\Models\Question;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;

class QuestionController extends Controller
Expand All @@ -16,23 +17,21 @@ class QuestionController extends Controller
*/
public function index(Request $request)
{

// $questionTable = $request->session()->get('questionTable');
// $questions = DB::table($questionTable)->get();

$user_id = auth()->user()->id;
$questions = Question::where('user_id', $user_id)->get();
return view('question.index', compact('questions'));
try {
$user_id = auth()->user()->id;
$questions = Question::where('user_id', $user_id)->get();
return view('question.index', compact('questions'));
} catch (\Exception $e) {
Log::error($e->getMessage());
return redirect()->back()->with('error', 'Error in fetching questions');
}
}

/**
* Show the form for creating a new resource.
*/
public function create(Request $request)
{
// $questionTable = $request->session()->get('questionTable');
// return view("question.create", compact('questionTable'));

return view("question.create");
}

Expand All @@ -41,10 +40,15 @@ public function create(Request $request)
*/
public function store(QuestionRequest $request)
{
$question = $request->validated();
$question['user_id'] = auth()->user()->id;
Question::create($question);
return redirect()->route('question.index')->with('success', 'Your question has been added!');
try {
$question = $request->validated();
$question['user_id'] = auth()->user()->id;
Question::create($question);
return redirect()->route('question.index')->with('success', 'Your question has been added!');
} catch (\Exception $e) {
Log::error($e->getMessage());
return redirect()->back()->with('error', 'Error in adding question');
}
}

/**
Expand Down Expand Up @@ -79,8 +83,13 @@ public function update(Request $request, string $id)
public function destroy(string $id)
{
//
$question = Question::findOrFail($id);
$question->delete();
return redirect()->route('question.index')->with('success', 'Your question has been deleted!');
try {
$question = Question::findOrFail($id);
$question->delete();
return redirect()->route('question.index')->with('success', 'Your question has been deleted!');
} catch (\Exception $e) {
Log::error($e->getMessage());
return redirect()->back()->with('error', 'Error in deleting question');
}
}
}

0 comments on commit ce0fab9

Please sign in to comment.