Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…php_laravel_web into feat/fetch-a-role-in-an-organisation-endpoint
  • Loading branch information
sparkybug committed Aug 9, 2024
2 parents 7e6c24b + b1b3bda commit 7c37feb
Show file tree
Hide file tree
Showing 20 changed files with 772 additions and 137 deletions.
4 changes: 1 addition & 3 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:vC0yZGIN9SZps1rwMBcl/jeiBfnpApiJYeJZuj1B5hU=
APP_KEY=
APP_DEBUG=true
APP_URL=http://localhost

Expand Down Expand Up @@ -70,5 +70,3 @@ RESET_PASSWORD_LINK=

# For token test in registrationtest file
TOKEN_AUTH=''

JWT_SECRET=HSPiEuyVveI9JKUu1z2N8TRt5rHGqiGgxPQ0nHloikZtk91fC9DyqCp6fAigZ5Ii
5 changes: 2 additions & 3 deletions app/Http/Controllers/Api/V1/Auth/SocialAuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function saveGoogleRequest(Request $request)

public function saveGoogleRequestPost(Request $request)
{
// Validate the incoming request
// Validate the incoming request working
$validator = Validator::make($request->all(), [
'id_token' => 'required|string',
]);
Expand All @@ -127,8 +127,7 @@ public function saveGoogleRequestPost(Request $request)
}

// Extract Google user data from the request
$google_token = $request->input('id_token');

$google_token = $request->id_token;

try {
// Retrieve user information from Google
Expand Down
22 changes: 15 additions & 7 deletions app/Http/Controllers/Api/V1/CommentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Http\Controllers\Controller;
use App\Models\Blog;
use App\Models\Comment;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -37,7 +38,7 @@ public function createComment(Request $request, $blogId)
], 201);
} catch (\Exception $e) {
Log::error('Error creating reply comment:', ['exception' => $e->getMessage()]);

return response()->json([
'status' => 500,
'message' => 'Failed to create comment',
Expand All @@ -52,7 +53,7 @@ public function replyComment(Request $request, $commentId)
try {
Log::info('Reached the replyComment method');
Log::info('Request data:', $request->all());

$user = auth('api')->user();
$request->validate([
'content' => 'required|string'
Expand All @@ -78,7 +79,7 @@ public function replyComment(Request $request, $commentId)
\Exception $e
) {
Log::error('Error creating reply comment:', ['exception' => $e->getMessage()]);

return response()->json([
'status' => 500,
'message' => 'Failed to create reply',
Expand Down Expand Up @@ -183,10 +184,17 @@ public function deleteComment($commentId)
'message' => 'Comment deleted successfully',
], 200);
} catch (\Exception $e) {
return response()->json([
'status' => 500,
'message' => 'Failed to delete comment',
], 500);

if($e instanceof ModelNotFoundException) {
return response()->json([
'status' => 404,
'message' => 'Comment not found',
], 404);
}
return response()->json([
'status' => 500,
'message' => 'Failed to delete comment',
], 500);
}
}

Expand Down
53 changes: 53 additions & 0 deletions app/Http/Controllers/Api/V1/NewsletterSubscriptionController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\NewsletterSubscription;
use Illuminate\Contracts\Validation\Factory as ValidationFactory;
use Illuminate\Http\Response;
use Exception;

class NewsletterSubscriptionController extends Controller
{
protected $validator;

public function __construct(ValidationFactory $validator)
{
$this->validator = $validator;
}

public function store(Request $request)
{
$validator = $this->validator->make($request->all(), [
'email' => 'required|email|unique:newsletter_subscriptions,email'
]);

if ($validator->fails()) {
return response()->json([
'success' => false,
'message' => 'Validation failed.',
'errors' => $validator->errors()
], Response::HTTP_BAD_REQUEST);
}

try {
$newsletter_subscription = NewsletterSubscription::create([
'email' => $request->email
]);

return response()->json([
'data' => $newsletter_subscription,
'message' => 'Newsletter Subscribed Successfully',
'status_code' => 201
], Response::HTTP_CREATED);
} catch (Exception $exception) {
return response()->json([
'message' => 'Internal Server Error',
'status_code' => 500
], 500);
}
}
}

49 changes: 36 additions & 13 deletions app/Http/Controllers/Api/V1/PreferenceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ public function index()
'message' => 'Languages fetched successfully',
'preferences' => $preferences
], 200);

}

//


public function store(StorePreferenceRequest $request){

public function store(StorePreferenceRequest $request)
{
if (!auth()->check()) {
return response()->json([
'status_code' => 401,
Expand All @@ -48,20 +48,20 @@ public function store(StorePreferenceRequest $request){
}

$validated = $request->validated();

if (!$validated) {
return response()->json([
'status_code' => 400,
'message' => 'Validation Error',
'errors' => $validator->errors()
], 400);
}

try {
$preference = Auth::user()->preferences()->create($request->all());

Log::info('Preference created', ['user_id' => Auth::id(), 'preference' => $preference]);

return response()->json([
'status_code' => 201,
'message' => 'Preference created successfully',
Expand All @@ -71,10 +71,9 @@ public function store(StorePreferenceRequest $request){
'value' => $preference->value,
]
], 201);

} catch (\Exception $e) {
Log::error("Error creating preference: {$e->getMessage()}");

return response()->json([
'status_code' => 500,
'message' => 'Internal Server Error',
Expand All @@ -93,7 +92,7 @@ public function update(UpdatePreferenceRequest $request, $id)
}

$validated = $request->validated();

if (!$validated) {
return response()->json([
'status_code' => 400,
Expand All @@ -105,7 +104,7 @@ public function update(UpdatePreferenceRequest $request, $id)
try {
$preference = Auth::user()->preferences()->findOrFail($id);

if(!$preference) {
if (!$preference) {
return response()->json([
'status_code' => 404,
'message' => 'Preference not found',
Expand Down Expand Up @@ -134,7 +133,7 @@ public function update(UpdatePreferenceRequest $request, $id)
], 500);
}
}

public function delete(DeletePreferenceRequest $request, $id)
{
$preference = Auth::user()->preferences()->find($id);
Expand All @@ -155,5 +154,29 @@ public function delete(DeletePreferenceRequest $request, $id)
'message' => 'Preference deleted successfully.',
], 200);
}

//show regions
public function showRegion($user_id)
{
$preference = Preference::where('user_id', $user_id)->first();
if ($preference) {
if ($preference->region) {
return response()->json([
'status' => 200,
'message' => "Region retrieved successfully",
'data' => $preference->region,
], 200);
} else {
return response()->json([
'status' => 200,
'message' => "Region has not been set",
'data' => [],
], 200);
}
} else {
return response()->json([
'status' => 404,
'message' => 'Preference not found for user',
], 404);
}
}
}
53 changes: 53 additions & 0 deletions app/Http/Controllers/Api/V1/SqueezePageCoontroller.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Controller;
use App\Http\Requests\FilterSqueezeRequest;
use App\Models\SqueezePage;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Validation\ValidationException;

class SqueezePageCoontroller extends Controller
{
Expand Down Expand Up @@ -76,4 +78,55 @@ public function destroy(string $id)
{
//
}

public function search(Request $request)
{
$request->validate([
'q' => 'required|string|max:255',
]);

$query = SqueezePage::query();

if ($searchTerm = $request->input('q')) {
$query->where(function ($q) use ($searchTerm) {
$q->where('headline', 'LIKE', "%{$searchTerm}%")
->orWhere('sub_headline', 'LIKE', "%{$searchTerm}%")
->orWhere('content', 'LIKE', "%{$searchTerm}%")
->orWhere('title', 'LIKE', "%{$searchTerm}%")
->orWhere('slug', 'LIKE', "%{$searchTerm}%");
});
}

$results = $query->get();

return response()->json([
'status' => Response::HTTP_OK,
'message' => 'Search result retrieved',
'data' => $results,
]);
}

public function filter(FilterSqueezeRequest $request)
{
$query = SqueezePage::query();

// Filter by status if provided
if ($status = $request->input('status')) {
$query->where('status', $status);
}

// Filter by slug if provided
if ($slug = $request->input('slug')) {
$query->where('slug', $slug);
}

// Get the results
$results = $query->get();

return response()->json([
'status' => Response::HTTP_OK,
'message' => 'Filtered results',
'data' => $results,
]);
}
}
35 changes: 20 additions & 15 deletions app/Http/Controllers/Api/V1/User/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,38 @@

class UserController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
$users = User::latest()->paginate();

public function stats()
{
$totalUsers = User::count();
$totalDeletedUsers = User::onlyTrashed()->count();
$totalActiveUsers = User::where('is_active', 1)->count() - $totalDeletedUsers;
$totalInActiveUsers = User::where('is_active', 0)->count();

dd($users);

// $users = [
// 'name' =>
// ];
return response()->json(
[
"status_code" => 200,
"message" => "User statistics retrieved successfully",
"total_users" => $totalUsers,
"deleted_users" => $totalDeletedUsers,
"active_users" => $totalActiveUsers,
"in_active_users" => $totalInActiveUsers,
],
200
);
}
/**
* Display a listing of the resource.
*/
public function index()
{
$users = User::latest()->paginate();


return response()->json(
[
"status_code" => 200,
"message" => "Users returned successfully",
"total_users" => $totalUsers,
"total_deleted_users" => $totalDeletedUsers,
"total_active_users" => $totalActiveUsers,
"total_inActive_users" => $totalInActiveUsers,
"data" =>$users
],
200
Expand Down
Loading

0 comments on commit 7c37feb

Please sign in to comment.