-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial announcement system - closes #752
- Loading branch information
1 parent
1251b26
commit 34b7627
Showing
18 changed files
with
349 additions
and
209 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
app/Http/Controllers/Announcement/CheckAnnouncementController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Announcement; | ||
|
||
use App\Http\Controllers\Controller; | ||
use App\Http\Resources\AnnouncementResource; | ||
use App\Models\Announcement; | ||
use App\Traits\LocaleTrait; | ||
use Illuminate\Http\Request; | ||
|
||
class CheckAnnouncementController extends Controller | ||
{ | ||
use LocaleTrait; | ||
|
||
/** | ||
* Display active announcements. | ||
*/ | ||
public function __invoke(Request $request) | ||
{ | ||
|
||
$announcment = Announcement::active()->orderBy('start_at', 'desc')->get(); | ||
|
||
if ($announcment->isEmpty()) { | ||
return response()->json([], 204); | ||
} | ||
|
||
return AnnouncementResource::collection($announcment); | ||
|
||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace App\Http\Resources; | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Resources\Json\JsonResource; | ||
|
||
/** | ||
* @mixin \App\Models\Announcement | ||
*/ | ||
class AnnouncementResource extends JsonResource | ||
{ | ||
/** | ||
* Transform the resource into an array. | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
public function toArray(Request $request): array | ||
{ | ||
return [ | ||
'data' => [ | ||
'id' => $this->id, | ||
'title_en' => $this->title_en, | ||
'title_fr' => $this->title_fr, | ||
'text_en' => $this->text_en, | ||
'text_fr' => $this->text_fr, | ||
'start_at' => $this->start_at, | ||
'end_at' => $this->end_at, | ||
], | ||
'can' => [ | ||
'update' => false, | ||
'delete' => false, | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class Announcement extends Model | ||
{ | ||
/** @use HasFactory<\Database\Factories\AnnouncementFactory> */ | ||
use HasFactory; | ||
|
||
/** | ||
* Get the attributes that should be cast. | ||
* | ||
* @return array<string, string> | ||
*/ | ||
public function casts(): array | ||
{ | ||
return [ | ||
'start_at' => 'datetime', | ||
'end_at' => 'datetime', | ||
]; | ||
} | ||
|
||
/** | ||
* Return active announcements | ||
*/ | ||
public function scopeActive(Builder $query) | ||
{ | ||
return $query->where('start_at', '<=', now()) | ||
->where('end_at', '>=', now()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace App\Policies; | ||
|
||
use App\Enums\Permissions\UserRole; | ||
use App\Models\Announcement; | ||
use App\Models\User; | ||
|
||
class AnnouncementPolicy | ||
{ | ||
/** | ||
* Determine whether the user can view any models. | ||
*/ | ||
public function viewAny(User $user): bool | ||
{ | ||
return $user->hasRole(UserRole::ADMIN); | ||
} | ||
|
||
/** | ||
* Determine whether the user can view the model. | ||
*/ | ||
public function view(User $user, Announcement $announcement): bool | ||
{ | ||
return $user->hasRole(UserRole::ADMIN); | ||
} | ||
|
||
/** | ||
* Determine whether the user can create models. | ||
*/ | ||
public function create(User $user): bool | ||
{ | ||
return $user->hasRole(UserRole::ADMIN); | ||
} | ||
|
||
/** | ||
* Determine whether the user can update the model. | ||
*/ | ||
public function update(User $user, Announcement $announcement): bool | ||
{ | ||
return $user->hasRole(UserRole::ADMIN); | ||
} | ||
|
||
/** | ||
* Determine whether the user can delete the model. | ||
*/ | ||
public function delete(User $user, Announcement $announcement): bool | ||
{ | ||
return $user->hasRole(UserRole::ADMIN); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace Database\Factories; | ||
|
||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
|
||
/** | ||
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Announcement> | ||
*/ | ||
class AnnouncementFactory extends Factory | ||
{ | ||
/** | ||
* Define the model's default state. | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
public function definition(): array | ||
{ | ||
return [ | ||
'title_en' => $this->faker->sentence, | ||
'title_fr' => $this->faker->sentence, | ||
'text_en' => $this->faker->paragraph, | ||
'text_fr' => $this->faker->paragraph, | ||
'start_at' => $this->faker->dateTimeBetween('-2 week', '-1 week'), | ||
'end_at' => $this->faker->dateTimeBetween('+1 week', '+2 week'), | ||
]; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
database/migrations/2025_01_03_170952_create_announcements_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?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('announcements', function (Blueprint $table) { | ||
$table->id(); | ||
$table->timestamps(); | ||
$table->string('title_en', 255); | ||
$table->string('title_fr', 255); | ||
$table->string('text_en', 500); | ||
$table->string('text_fr', 500); | ||
$table->dateTime('start_at'); | ||
$table->dateTime('end_at'); | ||
}); | ||
} | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.