Skip to content

Commit

Permalink
more coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
fokosun committed Aug 23, 2023
1 parent d216a9b commit 105d14f
Show file tree
Hide file tree
Showing 7 changed files with 353 additions and 39 deletions.
47 changes: 11 additions & 36 deletions app/Http/Controllers/RecipeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,13 @@

namespace App\Http\Controllers;

use App\Exceptions\CookbookModelNotFoundException;
use App\Http\Requests\RecipeStoreRequest;
use App\Models\Recipe;
use App\Services\RecipeService;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Log;
use PHPOpenSourceSaver\JWTAuth\Facades\JWTAuth;
use Tymon\JWTAuth\Exceptions\JWTException;
use Tymon\JWTAuth\JWT;

/**
Expand Down Expand Up @@ -57,12 +52,6 @@ public function show($recipeId)
return $this->service->show($recipeId);
}

/**
* @param Request $request
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
* @throws \App\Exceptions\CookbookModelNotFoundException
* @throws \Illuminate\Validation\ValidationException
*/
public function addClap(Request $request)
{
$this->validate(
Expand All @@ -85,11 +74,6 @@ public function myRecipes(Request $request, JWT $jwtAuth): \Illuminate\Http\Json
], 401);
}

/**
* @param RecipeStoreRequest $request
* @param JWT $jwtAuth
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\JsonResponse|\Illuminate\Http\Response
*/
public function store(RecipeStoreRequest $request, JWT $jwtAuth)
{
try {
Expand All @@ -115,36 +99,23 @@ public function store(RecipeStoreRequest $request, JWT $jwtAuth)
}
}

/**
* @param Request $request
* @param $recipeId
* @param JWT $jwtAuth
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\JsonResponse|\Illuminate\Http\Response
* @throws \App\Exceptions\CookbookModelNotFoundException
* @throws \Tymon\JWTAuth\Exceptions\JWTException
*/
public function update(Request $request, $recipeId, JWT $jwtAuth)
{
if (
$request->user()->ownRecipe($recipeId) &&
$jwtAuth->parseToken()->check()
$request->user()->ownRecipe($recipeId)
) {
return $this->service->update($request, $recipeId);
if (
$jwtAuth->parseToken()->check()
) {
return $this->service->update($request, $recipeId);
}
}

return response()->json([
'error' => 'You are not authorized to access this resource.'
], 401);
}

/**
* @param Request $request
* @param $recipeId
* @param JWT $jwtAuth
* @return Application|ResponseFactory|JsonResponse|Response
* @throws CookbookModelNotFoundException
* @throws JWTException
*/
public function destroy(Request $request, $recipeId, JWT $jwtAuth)
{
if (
Expand All @@ -159,7 +130,7 @@ public function destroy(Request $request, $recipeId, JWT $jwtAuth)
], Response::HTTP_UNAUTHORIZED);
}

public function report(Request $request, JWT $jwtAuth)
public function report(Request $request, JWT $jwtAuth): JsonResponse
{
if ($jwtAuth->parseToken()->check()) {
$recipe = Recipe::find($request->get('recipe_id'));
Expand All @@ -181,5 +152,9 @@ public function report(Request $request, JWT $jwtAuth)
'message' => 'There was an error processing this request. Please try again later.'
]);
}

return response()->json([
'error' => 'You are not authorized to perform this action.'
], Response::HTTP_UNAUTHORIZED);
}
}
12 changes: 12 additions & 0 deletions app/Models/Role.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Role extends Model
{
protected $table = 'user_roles';

public $fillable = ['user_id', 'role_id'];
}
12 changes: 10 additions & 2 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,17 @@ public function isAlreadyFollowing(User $user): bool
return in_array($user->getKey(), $followings);
}

private function hasRole(string $role)
private function hasRole(string $role_id)
{
return false;
$role_id = DB::table('roles')->where(['role_id' => $role_id])->first()?->id;
$roles = $this->roles()->pluck('role_id')->toArray();

return in_array($role_id, $roles);
}

public function roles(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany(Role::class, 'user_id');
}

public function getTikTokUser()
Expand Down
29 changes: 29 additions & 0 deletions database/migrations/2023_08_22_004422_create_user_roles_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('user_roles', function (Blueprint $table) {
$table->id();
$table->integer('user_id');
$table->integer('role_id');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('user_roles');
}
};
28 changes: 28 additions & 0 deletions database/migrations/2023_08_22_004528_create_roles_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('roles', function (Blueprint $table) {
$table->id();
$table->text('role_id');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('roles');
}
};
11 changes: 10 additions & 1 deletion database/seeders/StaticContentsSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class StaticContentsSeeder extends Seeder
{
Expand All @@ -17,7 +18,7 @@ class StaticContentsSeeder extends Seeder
*/
public function run()
{
\Illuminate\Support\Facades\DB::table('static_contents')->insert([
DB::table('static_contents')->insert([
[
'title' => 'cookie-policy',
'content' => file_get_contents(__DIR__.'/policies/cookie-policy.php'),
Expand All @@ -32,5 +33,13 @@ public function run()
'content' => file_get_contents(__DIR__.'/policies/terms-and-conditions.php'),
],
]);

DB::table('roles')->insert([
[
'role_id' => 'super',
], [
'role_id' => 'contributor',
]
]);
}
}
Loading

0 comments on commit 105d14f

Please sign in to comment.