Skip to content

Commit

Permalink
Build UI for REST API
Browse files Browse the repository at this point in the history
  • Loading branch information
Justinianus2001 committed Oct 16, 2023
1 parent 56eeed6 commit b6c7f0e
Show file tree
Hide file tree
Showing 58 changed files with 1,155 additions and 362 deletions.
9 changes: 8 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
.docker/nginx.conf
/coverage
.env
.gitattributes
.gitignore
.phpunit.result.cache
/coverage
/vendor
LICENSE
README.md
2 changes: 1 addition & 1 deletion .env.docker
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ LOG_LEVEL=debug

DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_PORT=3307
DB_DATABASE=web_blog
DB_USERNAME=laraveluser
DB_PASSWORD=laraveluserpass
Expand Down
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ RUN docker-php-ext-enable pdo pdo_mysql mysqli

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

COPY . .
COPY . .
COPY .env.docker .env
13 changes: 12 additions & 1 deletion app/Helpers/AppHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace App\Helpers;

use Image;

class AppHelper
{
public static function randStr($length = 10): string
public static function randStr($length = 15): string
{
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
Expand All @@ -20,4 +22,13 @@ public static function randEmail(): string
{
return self::randStr() . '@gmail.com';
}

public static function uploadImage($image, $folder): string
{
$imageName = $folder . '/' . self::randStr() . '.' . $image->getClientOriginalExtension();
$imageDir = public_path('storage/' . $imageName);
Image::make($image)->resize(980, 980)->save($imageDir);

return $imageName;
}
}
58 changes: 29 additions & 29 deletions app/Http/Controllers/Api/V1/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,69 +2,69 @@

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Controller;
use App\Http\Controllers\BaseController;
use App\Http\Requests\LoginAuthRequest;
use App\Http\Requests\RegisterAuthRequest;
use App\Http\Resources\V1\UserResource;
use App\Http\Resources\V1\AuthResource;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class AuthController extends Controller
class AuthController extends BaseController
{
/**
* Register a new user.
*/
public function register(RegisterAuthRequest $request): UserResource
public function register(RegisterAuthRequest $request): JsonResponse
{
$user = User::create([
'name' => $request->name,
'email' => $request->email,
'password'=> Hash::make($request->password),
]);

Auth::login($user);
auth()->login($user);

return new UserResource([
return $this->sendResponse(new AuthResource([
'user' => $user,
'token' => $user->createToken('user_token', [
'blog:store',
'blog:update',
'blog:delete',
'category:store',
'category:update',
'category:delete',
'blog:store', 'blog:update', 'blog:delete',
'category:store', 'category:update', 'category:delete',
])->plainTextToken,
]);
]), 'Registered successfully');
}

/**
* Login a user.
*/
public function login(LoginAuthRequest $request)
public function login(LoginAuthRequest $request): JsonResponse
{
$user = User::where('email', $request->email)->first();

if ($user && Hash::check($request->password, $user->password)) {
return new UserResource([
'user' => $user,
'token' => $user->createToken('user_token', [
'blog:store',
'blog:update',
'blog:delete',
'category:store',
'category:update',
'category:delete',
])->plainTextToken,
]);
if (!$user || !Hash::check($request->password, $user->password)) {
return $this->sendError('The provided email or password are incorrect.', 401);
}

auth()->login($user);

return $this->sendResponse(new AuthResource([
'user' => $user,
'token' => $user->createToken('user_token', [
'blog:store', 'blog:update', 'blog:delete',
'category:store', 'category:update', 'category:delete',
])->plainTextToken,
]), 'Logged in successfully');
}

/**
* Logout a user.
*/
public function logout(): void
public function logout(Request $request): JsonResponse
{
auth()->user()->tokens()->delete();
auth('sanctum')->user()->tokens()->delete();
$request->session()->invalidate();

return $this->sendResponse([], 'Logged out successfully');
}
}
76 changes: 43 additions & 33 deletions app/Http/Controllers/Api/V1/BlogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace App\Http\Controllers\Api\V1;

use App\Filters\V1\BlogFilter;
use App\Http\Controllers\Controller;
use App\Http\Controllers\BaseController;
use App\Http\Requests\BulkStoreBlogRequest;
use App\Http\Requests\DeleteBlogRequest;
use App\Http\Requests\StoreBlogRequest;
Expand All @@ -12,31 +12,35 @@
use App\Http\Resources\V1\BlogResource;
use App\Models\Blog;
use Elastic\Elasticsearch\ClientBuilder;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class BlogController extends Controller
class BlogController extends BaseController
{
/**
* Display a listing of the resource.
*/
public function index(Request $request): BlogCollection
public function index(Request $request): JsonResponse
{
$query = (new BlogFilter())->transform($request);
$blogs = Blog::where($query)->with('category');
$blogs = Blog::where($query)->with(['user', 'category'])
->paginate(5)->appends($request->query());

return new BlogCollection($blogs->paginate(5)->appends($request->query()));
return $this->sendResponse(
new BlogCollection($blogs),
'Blogs retrieved successfully');
}

/**
* Search a listing of the resource.
*/
public function search(Request $request, string $keyword)
public function search(Request $request)
{
$elastic = $request->test ?? true;
$keyword = $request->keyword;
$ids = [];

if ($elastic) {
if ($elastic && isset($keyword)) {
$client = ClientBuilder::create()
->setHosts([env('ELASTICSEARCH_HOST')])
->build();
Expand All @@ -53,22 +57,26 @@ public function search(Request $request, string $keyword)
];
$response = $client->search($params);

return $response->asArray();
foreach ($response['hits']['hits'] as $hit) {
$ids[] = $hit['_id'];
}
}
}

/**
* Show the form for creating a new resource.
*/
public function create(): void
{
//
$blogs = Blog::whereIn('id', $ids)->with(['user', 'category'])
->paginate(5)->appends($request->query())
->sortBy(function($model) use ($ids) {
return array_search($model->getKey(), $ids);
});

return $this->sendResponse(
new BlogCollection($blogs),
'Blogs retrieved successfully');
}

/**
* Store a newly created resource in storage.
*/
public function store(StoreBlogRequest $request): BlogResource
public function store(StoreBlogRequest $request): JsonResponse
{
$blog = Blog::create($request->all());

Expand All @@ -90,13 +98,15 @@ public function store(StoreBlogRequest $request): BlogResource
$client->index($params);
}

return new BlogResource($blog);
return $this->sendResponse(
new BlogResource($blog),
'Blog created successfully', 201);
}

/**
* Store some newly created resource in storage.
*/
public function bulkStore(BulkStoreBlogRequest $request): void
public function bulkStore(BulkStoreBlogRequest $request): JsonResponse
{
$bulk = collect($request->all())->map(function ($arr, $key) {
return [
Expand All @@ -108,28 +118,24 @@ public function bulkStore(BulkStoreBlogRequest $request): void
});

Blog::insert($bulk->toArray());
}

/**
* Display the specified resource.
*/
public function show(Blog $blog): BlogResource
{
return new BlogResource($blog->loadMissing('category'));
return $this->sendResponse([], 'Blog created successfully', 201);
}

/**
* Show the form for editing the specified resource.
* Display the specified resource.
*/
public function edit(Blog $blog): void
public function show(Blog $blog): JsonResponse
{
//
return $this->sendResponse(
new BlogResource($blog->loadMissing('category')),
'Blog retrieved successfully');
}

/**
* Update the specified resource in storage.
*/
public function update(UpdateBlogRequest $request, Blog $blog): BlogResource
public function update(UpdateBlogRequest $request, Blog $blog): JsonResponse
{
$blog->update($request->all());

Expand All @@ -153,13 +159,15 @@ public function update(UpdateBlogRequest $request, Blog $blog): BlogResource
$client->update($params);
}

return new BlogResource($blog->loadMissing('category'));
return $this->sendResponse(
new BlogResource($blog->loadMissing('category')),
'Blog updated successfully');
}

/**
* Remove the specified resource from storage.
*/
public function destroy(DeleteBlogRequest $request, Blog $blog): void
public function destroy(DeleteBlogRequest $request, Blog $blog): JsonResponse
{
$elastic = $request->test ?? true;

Expand All @@ -177,5 +185,7 @@ public function destroy(DeleteBlogRequest $request, Blog $blog): void
}

$blog->delete();

return $this->sendResponse([], 'Blog deleted successfully');
}
}
Loading

0 comments on commit b6c7f0e

Please sign in to comment.