Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,6 @@ TELEGRAM_BOT_TOKEN=

# Scribe / OpenAPI
# SCRIBE_AUTH_KEY=your-api-key-here
# pengaturan cache untuk API, jika ingin menonaktifkan cache, ubah API_CACHE_ENABLED menjadi false
API_CACHE_ENABLED=true
API_CACHE_DURATION=3600 # dalam detik (1 jam)
8 changes: 5 additions & 3 deletions app/Http/Controllers/Api/Frontend/WebsiteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use App\Repositories\DesaApiRepository;
use App\Repositories\ProfilApiRepository;
use App\Repositories\WebsiteApiRepository;
use App\Services\CacheService;
use App\Transformers\ProfilTransformer;
use App\Transformers\WebsiteTransformer;
use Illuminate\Http\Request;
Expand Down Expand Up @@ -74,9 +75,10 @@ public function __construct(
public function index(Request $request): Fractal|JsonResponse
{
$params = $request->only(['page', 'per_page', 'filter', 'fields', 'search', 'sort', 'order', 'include']);
$cacheKey = $this->getCacheKey('index', $params);
$prefix = config('theme-api.website.cache_prefix', 'website:api');
$cacheKey = $this->getCacheKey($prefix, $params);

return Cache::remember($cacheKey, $this->getCacheDuration(), function () use ($request) {
return app(CacheService::class)->remember($cacheKey, $this->getCacheDuration(), function () use ($request) {
$websiteData = $this->websiteApiRepository->getAllWebsiteData();
return $this->fractal([
['id' => 'profile', (new ProfilTransformer())->transform(Profil::with(['dataUmum'])->first())],
Expand All @@ -92,6 +94,6 @@ public function index(Request $request): Fractal|JsonResponse
['id' => 'counter', $websiteData['counter']],
['id' => 'config', config('profil')],
], new WebsiteTransformer());
});
}, $prefix);
}
}
2 changes: 2 additions & 0 deletions app/Models/DataUmum.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
namespace App\Models;

use App\Observers\DataUmumObserver;
use App\Observers\WebsiteCacheObserver;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

Expand Down Expand Up @@ -120,5 +121,6 @@ public function setPathAttribute($value)
protected static function booted(): void
{
static::observe(DataUmumObserver::class);
static::observe(WebsiteCacheObserver::class);
}
}
11 changes: 8 additions & 3 deletions app/Models/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@

namespace App\Models;

use Illuminate\Support\Carbon;
use App\Observers\WebsiteCacheObserver;
use App\Traits\HandlesResourceDeletion;
use Illuminate\Database\Eloquent\Model;
use Cviebrock\EloquentSluggable\Sluggable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Event extends Model
{
use HandlesResourceDeletion;
use HasFactory;
use Sluggable;
use HandlesResourceDeletion;

protected $table = 'das_events';

Expand Down Expand Up @@ -91,4 +91,9 @@ public static function getOpenEvents()
{
return self::open()->orderBy('start', 'desc')->get();
}

protected static function booted(): void
{
static::observe(WebsiteCacheObserver::class);
}
}
6 changes: 6 additions & 0 deletions app/Models/MediaSosial.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

namespace App\Models;

use App\Observers\WebsiteCacheObserver;
use App\Traits\HandlesResourceDeletion;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
Expand All @@ -56,4 +57,9 @@ class MediaSosial extends Model
protected $resources = [
'logo',
];

protected static function booted(): void
{
static::observe(WebsiteCacheObserver::class);
}
}
24 changes: 12 additions & 12 deletions app/Models/MediaTerkait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,15 @@

namespace App\Models;

use App\Observers\MediaTerkaitObserver;
use App\Observers\WebsiteCacheObserver;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Observers\MediaTerkaitObserver;

class MediaTerkait extends Model
{
use HasFactory;

/**
* Register model lifecycle hooks.
*/
protected static function booted(): void
{
static::observe(MediaTerkaitObserver::class);
}

protected $fillable = [
'logo',
'url',
Expand All @@ -28,8 +21,8 @@ protected static function booted(): void

public function scopeSearch($query, $search)
{
return empty($search)
? $query
return empty($search)
? $query
: $query->where('nama', 'LIKE', "%{$search}%");
}

Expand All @@ -40,5 +33,12 @@ public function scopeStatus($query, $status)
});
}


/**
* Register model lifecycle hooks.
*/
protected static function booted(): void
{
static::observe(MediaTerkaitObserver::class);
static::observe(WebsiteCacheObserver::class);
}
}
11 changes: 8 additions & 3 deletions app/Models/NavMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace App\Models;

use App\Observers\WebsiteCacheObserver;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class NavMenu extends Model
Expand All @@ -27,13 +27,18 @@ class NavMenu extends Model

public function child(): HasMany
{
return $this->hasMany(NavMenu::class, 'parent_id', 'id');
return $this->hasMany(self::class, 'parent_id', 'id');
}

public function children(): HasMany
{
return $this->hasMany(NavMenu::class, 'parent_id', 'id')->with(['children' => function ($q) {
return $this->hasMany(self::class, 'parent_id', 'id')->with(['children' => function ($q) {
return $q->selectRaw("id, parent_id , name as text, url as href, target, type, is_show,'fa fa-list' as icon");
}]);
}

protected static function booted(): void
{
static::observe(WebsiteCacheObserver::class);
}
}
14 changes: 8 additions & 6 deletions app/Models/Navigation.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,11 @@
namespace App\Models;

use App\Enums\MenuTipe;
use Illuminate\Support\Facades\Log;
use App\Observers\WebsiteCacheObserver;
use Illuminate\Database\Eloquent\Model;
use Cviebrock\EloquentSluggable\Sluggable;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Navigation extends Model
{

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -71,12 +68,12 @@ public static function lastOrder($parent_id = 0)

public function parent()
{
return $this->belongsTo(Navigation::class, 'parent_id');
return $this->belongsTo(self::class, 'parent_id');
}

public function childrens()
{
return $this->hasMany(Navigation::class, 'parent_id');
return $this->hasMany(self::class, 'parent_id');
}

public function setParentIdAttribute($value)
Expand Down Expand Up @@ -116,4 +113,9 @@ public function getFullUrlAttribute()
break;
}
}

protected static function booted(): void
{
static::observe(WebsiteCacheObserver::class);
}
}
10 changes: 8 additions & 2 deletions app/Models/Pengurus.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

use App\Enums\JenisJabatan;
use App\Enums\Status;
use App\Observers\WebsiteCacheObserver;
use App\Traits\HandlesResourceDeletion;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
Expand Down Expand Up @@ -167,12 +168,12 @@ public function cekPengurus(): array
$kecuali = [];

// Cek apakah kades
if (Pengurus::whereHas('jabatan', fn ($q) => $q->where('jenis', JenisJabatan::Camat))->where('status', Status::Aktif)->exists()) {
if (self::whereHas('jabatan', fn ($q) => $q->where('jenis', JenisJabatan::Camat))->where('status', Status::Aktif)->exists()) {
$kecuali[] = JenisJabatan::Camat;
}

// Cek apakah sekdes
if (Pengurus::whereHas('jabatan', fn ($q) => $q->where('jenis', JenisJabatan::Sekretaris))->where('status', Status::Aktif)->exists()) {
if (self::whereHas('jabatan', fn ($q) => $q->where('jenis', JenisJabatan::Sekretaris))->where('status', Status::Aktif)->exists()) {
$kecuali[] = JenisJabatan::Sekretaris;
}

Expand All @@ -193,4 +194,9 @@ public function scopeListAtasan(\Illuminate\Database\Eloquent\Builder $query, ?i
'das_pengurus.nik',
])->join('ref_jabatan', 'das_pengurus.jabatan_id', '=', 'ref_jabatan.id');
}

protected static function booted(): void
{
static::observe(WebsiteCacheObserver::class);
}
}
2 changes: 2 additions & 0 deletions app/Models/Profil.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
namespace App\Models;

use App\Observers\ProfilObserver;
use App\Observers\WebsiteCacheObserver;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
Expand Down Expand Up @@ -97,6 +98,7 @@ public function strukturOrganisasi()
protected static function booted(): void
{
static::observe(ProfilObserver::class);
static::observe(WebsiteCacheObserver::class);

static::saved(function () {
Cache::forget('profil');
Expand Down
2 changes: 2 additions & 0 deletions app/Models/SettingAplikasi.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
namespace App\Models;

use App\Observers\SettingAplikasiObserver;
use App\Observers\WebsiteCacheObserver;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
Expand All @@ -56,6 +57,7 @@ class SettingAplikasi extends Model
protected static function booted(): void
{
static::observe(SettingAplikasiObserver::class);
static::observe(WebsiteCacheObserver::class);

static::saved(function () {
Cache::forget('setting');
Expand Down
8 changes: 7 additions & 1 deletion app/Models/SinergiProgram.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

namespace App\Models;

use App\Observers\WebsiteCacheObserver;
use App\Traits\HandlesResourceDeletion;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
Expand Down Expand Up @@ -58,11 +59,16 @@ class SinergiProgram extends Model
'gambar',
];

protected static function booted(): void
{
static::observe(WebsiteCacheObserver::class);
}

protected static function boot()
{
parent::boot();

SinergiProgram::creating(function ($model) {
self::creating(function ($model) {
$model->urutan = SinergiProgram::max('urutan') + 1;
});
}
Expand Down
7 changes: 6 additions & 1 deletion app/Models/Slide.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

namespace App\Models;

use Illuminate\Support\Facades\Log;
use App\Observers\WebsiteCacheObserver;
use App\Traits\HandlesResourceDeletion;
use Illuminate\Database\Eloquent\Model;

Expand All @@ -55,4 +55,9 @@ class Slide extends Model
protected $resources = [
'gambar',
];

protected static function booted(): void
{
static::observe(WebsiteCacheObserver::class);
}
}
36 changes: 36 additions & 0 deletions app/Observers/WebsiteCacheObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace App\Observers;

use App\Services\CacheService;
use Illuminate\Support\Facades\Cache;

class WebsiteCacheObserver
{
public function created($model): void
{
$this->clearWebsiteCache();
}

public function updated($model): void
{
$this->clearWebsiteCache();
}

public function deleted($model): void
{
$this->clearWebsiteCache();
}

protected function clearWebsiteCache(): void
{
try {
$prefix = config('theme-api.website.cache_prefix', 'website:api');

$cacheService = app(CacheService::class);
$cacheService->removeCachePrefix($prefix);
} catch (\Exception $e) {
//
}
}
}
9 changes: 9 additions & 0 deletions config/theme-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,15 @@
'cache_prefix' => env('KOMPLAIN_API_CACHE_PREFIX', 'komplain:api'),
],

/*
|--------------------------------------------------------------------------
| Website API Settings
|--------------------------------------------------------------------------
*/
'website' => [
'cache_prefix' => env('WEBSITE_API_CACHE_PREFIX', 'website:api'),
],

/*
|--------------------------------------------------------------------------
| Theme and Endpoint Settings
Expand Down
Loading