Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding the ability to display announcements #5218

Open
wants to merge 6 commits into
base: 1.0-develop
Choose a base branch
from
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Collection;
use App\Http\Controllers\Controller;
use App\Models\Announcement;
use App\Transformers\AnnouncementTransformer;

class AnnouncementController extends ApplicationApiController
{
/**
* Display a listing of announcements.
*
* @return \Illuminate\Http\JsonResponse
*/
public function index()
{
$announcements = Announcement::all();
$data = $this->fractal
->createData($this->getTransformer(AnnouncementTransformer::class)->transformCollection($announcements))
->toArray();
return response()->json($data);
}

}
38 changes: 38 additions & 0 deletions app/Models/Announcment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Announcement extends Model
{
use HasFactory;

/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'announcements';

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'text',
'mode',
];

/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'mode' => 'integer',
'created_at' => 'datetime',
];
}
38 changes: 38 additions & 0 deletions app/Transformers/Api/Application/AnnouncementTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace App\Transformers;

use League\Fractal\TransformerAbstract;
use App\Models\Announcement;

class AnnouncementTransformer extends TransformerAbstract
{
/**
* Transform the given announcement model into an array.
*
* @param \App\Models\Announcement $announcement
* @return array
*/
public function transform(Announcement $announcement)
{
return [
'id' => $announcement->id,
'text' => $announcement->text,
'mode' => $announcement->mode,
'created_at' => $announcement->created_at->toIso8601String(),
];
}

/**
* Transform a collection of announcements.
*
* @param \Illuminate\Support\Collection $announcements
* @return array
*/
public function transformCollection($announcements)
{
return $announcements->map(function ($announcement) {
return $this->transform($announcement);
})->toArray();
}
}
28 changes: 28 additions & 0 deletions database/migrations/2024_09_18_create_announcements_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

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

class CreateAnnouncementsTable extends Migration
{
/**
* Run the migrations.
*/
public function up()
{
Schema::create('announcements', function (Blueprint $table) {
$table->increments('id');
$table->string('text');
$table->integer('mode'); // modes are 0 (success) 1 (info) 2 (danger) 3 (warning)
$table->timestamp('created_at', 0);
});
}

/**
* Reverse the migrations.
*/
public function down()
{
Schema::dropIfExists('announcements');
}
}