Skip to content

Commit

Permalink
pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
szmmon committed May 7, 2024
1 parent 30acd86 commit 6af0405
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 19 deletions.
8 changes: 7 additions & 1 deletion app/Http/Controllers/ListingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 3,21 @@
namespace App\Http\Controllers;

use App\Models\Listing;
use Gate;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class ListingController extends Controller
{


/**
* Display a listing of the resource.
*/
public function index()
{
return inertia('Listing/index',
['listings' => Listing::all()]);
['listings' => Listing::orderByDesc('created_at')->paginate(10)]);
}

/**
Expand Down Expand Up @@ -59,6 63,8 @@ public function show(Listing $listing)
*/
public function edit(Listing $listing)
{
Gate::authorize('update', $listing);

return inertia(
'Listing/edit',
[
Expand Down
44 changes: 30 additions & 14 deletions app/Policies/ListingPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,59 8,75 @@

class ListingPolicy
{
public function before(?User $user, $ability)
{
if ($user->is_admin /*&& $ability === 'update'*/) {
return true;
}
}

/**
* Determine whether the user can view any models.
*/
public function viewAny(User $user): bool
public function viewAny(?User $user)
{
//
return true;
}

/**
* Determine whether the user can view the model.
*/
public function view(User $user, Listing $listing): bool
public function view(User $user, Listing $listing)
{
//
return true;
}

/**
* Determine whether the user can create models.
*/
public function create(User $user): bool
public function create(User $user)
{
//
return true;
}

/**
* Determine whether the user can update the model.
*/
public function update(User $user, Listing $listing): bool
public function update(User $user, Listing $listing)
{
//
return $user->id === $listing->by_user_id
?Response::allow()
: Response::deny('You do not own this post.');
}

/**
* Determine whether the user can delete the model.
*/
public function delete(User $user, Listing $listing): bool
public function delete(User $user, Listing $listing)
{
//
return $user->id === $listing->by_user_id
?Response::allow()
: Response::deny('You do not own this post.');
}

/**
* Determine whether the user can restore the model.
*/
public function restore(User $user, Listing $listing): bool
public function restore(User $user, Listing $listing)
{
//
return $user->id === $listing->by_user_id
?Response::allow()
: Response::deny('You do not own this post.');
}

/**
* Determine whether the user can permanently delete the model.
*/
public function forceDelete(User $user, Listing $listing): bool
public function forceDelete(User $user, Listing $listing)
{
//
return $user->id === $listing->by_user_id
?Response::allow()
: Response::deny('You do not own this post.');

}
}
9 changes: 8 additions & 1 deletion app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 2,11 @@

namespace App\Providers;

use App\Models\Listing;
use App\Models\User;
use App\Policies\ListingPolicy;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Gate;

class AppServiceProvider extends ServiceProvider
{
Expand All @@ -19,6 23,9 @@ public function register(): void
*/
public function boot(): void
{
//
Gate::define('update', [ListingPolicy::class, 'update']);
Gate::define('delete', [ListingPolicy::class, 'delete']);
Gate::define('restore', [ListingPolicy::class, 'restore']);
Gate::define('forceDelete', [ListingPolicy::class, 'forceDelete']);
}
}
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::table('users', function (Blueprint $table) {
$table->boolean('is_admin')->default(false);
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('is_admin');
});
}
};
4 changes: 4 additions & 0 deletions database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 20,7 @@ public function run(): void
User::factory()->create([
'name' => 'Test User',
'email' => '[email protected]',
'is_admin' => true
]);
User::factory()->create([
'name' => 'Test2 User',
Expand All @@ -28,5 29,8 @@ public function run(): void
Listing::factory(20)->create([
'by_user_id' => 1
]);
Listing::factory(20)->create([
'by_user_id' => 2
]);
}
}
11 changes: 11 additions & 0 deletions resources/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 16,15 @@
.button-primary{
@apply col-span-6 font-medium text-lg bg-indigo-500 hover:bg-indigo-400 text-white p-2 rounded-md
}
.input-filter-l {
@apply placeholder:text-gray-400 dark:placeholder:text-gray-400 border border-gray-200 dark:border-gray-700 rounded-t-md rounded-l-md rounded-b-md rounded-r-none text-sm font-medium text-gray-600 dark:text-gray-400 dark:bg-gray-800
}

.input-filter-r {
@apply placeholder:text-gray-400 dark:placeholder:text-gray-400 border border-l-0 border-gray-200 dark:border-gray-700 rounded-t-md rounded-l-none rounded-b-none rounded-r-md text-sm font-medium text-gray-600 dark:text-gray-400 dark:bg-gray-800
}

.btn-normal {
@apply bg-gray-600 hover:bg-gray-500 text-white font-medium p-2 rounded-md
}
}
15 changes: 15 additions & 0 deletions resources/js/Components/UI/pagination.vue
Original file line number Diff line number Diff line change
@@ -0,0 1,15 @@
<template>
<div class="flex gap-1">
<Link v-for="(link, index) in links" :key="index" class="py-2 px-4 rounded-md"
:href="link.url"
:class="{'bg-indigo-500 dark:bg-indigo-800 text-gray-300' : link.active}"
v-html="link.label">

</Link>
</div>
</template>

<script setup>
import {Link} from "@inertiajs/vue3";
defineProps({links: Array})
</script>
32 changes: 32 additions & 0 deletions resources/js/Pages/Index/Components/filters.vue
Original file line number Diff line number Diff line change
@@ -0,0 1,32 @@
<template>
<form action="">
<div class="flex flex-wrap gap-2 mt-2 mb-8">
<div class="flex flex-nowrap items-center">
<input type="text" name="" id="" placeholder="Price from" class="input-filter-l w-28">
<input type="text" name="" id="" placeholder="Price to" class="input-filter-r w-28">
</div>

<div class="flex flex-nowrap items-center">
<select name="" id="" class="input-filter-l w-28">
<option :value="null">Beds</option>
<option v-for="n in 5 " :key="n" :value="n">{{ n }}</option>
<option :value="null">6 </option>
</select>
<select name="" id="" class="input-filter-r w-28">
<option :value="null">Baths</option>
<option v-for="n in 5 " :key="n" :value="n">{{ n }}</option>
<option :value="null">6 </option>
</select>
</div>

<div class="flex flex-nowrap items-center">
<input type="text" name="" id="" placeholder="Area from" class="input-filter-l w-28">
<input type="text" name="" id="" placeholder="Area to" class="input-filter-r w-28">
</div>
<button type="submit" class="btn-normal">Filter</button>
<button type="reset" class="btn-normal">Clear</button>
</div>
</form>
</template>

<script setup></script>
14 changes: 11 additions & 3 deletions resources/js/Pages/Listing/index.vue
Original file line number Diff line number Diff line change
@@ -1,12 1,20 @@
<template>

<Filters/>

<div class="grid grid-cols-1 md:grid-cols-2 xl:grid-cols-3 gap-4">
<Listing v-for="listing in listings" :key="listing.id" :listing="listing" />
<Listing v-for="listing in listings.data" :key="listing.id" :listing="listing" />
</div>

<div v-if="listings.data.length" class="w-full flex justify-center mt-4 mb-4">
<Pagination :links="listings.links"></Pagination>
</div>
</template>

<script setup>
import { useMonthlyPayment } from "@/Composables/useMonthlyPayment.js";
import Listing from "@/Pages/Listing/Index/Components/listing.vue";
defineProps({ listings: Array });
import Pagination from "@/Components/UI/pagination.vue";
import Filters from "@/Pages/Index/Components/filters.vue"
defineProps({ listings: Object });
</script>

0 comments on commit 6af0405

Please sign in to comment.