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

Update page model #38

Open
wants to merge 10 commits into
base: 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
3 changes: 1 addition & 2 deletions app/Api/V1/Controllers/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function store(Request $request)
$rules = [
'title' => 'required|max:200',
'status' => 'in:Active,Inactive',
'description' => 'required|max:500',
'content' => 'required',
];
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
Expand Down Expand Up @@ -217,7 +217,6 @@ public function update(Request $request, int $id)
$rules = [
'title' => 'max:200',
'status' => 'in:Active,Inactive',
'description' => 'max:500',
];
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
Expand Down
4 changes: 3 additions & 1 deletion app/Api/V1/Transformers/PageTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ public function transform($item)
'id' => $item['id'],
'title' => $item['title'],
'status' => $item['status'],
'description' => $item['description'],
'content' => $item['content'],
'logo' => $item['logo'],
mimarcel marked this conversation as resolved.
Show resolved Hide resolved
'parent_id' => $item['parent_id'],
'lastUpdate' => $item['updated_at']->toDateTimeString(),
];
}
Expand Down
18 changes: 11 additions & 7 deletions app/Helpers/CsvHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@

class CsvHandler
{
protected $path = '';
protected $delimiter = ',';

public static function convertToArray($path)
public static function convertToArray($path, $associative = false)
{
if (!file_exists($path) || !is_readable($path)) {
return FALSE;
Expand All @@ -18,19 +15,26 @@ public static function convertToArray($path)

$data = [];
if (($handle = new SplFileObject($path, 'r')) !== FALSE) {
$data = self::convertFileToArray($handle);
$data = self::convertFileToArray($handle, $associative);
$handle = null;
}

return $data;
}

public static function convertFileToArray(SplFileObject $file)
public static function convertFileToArray(SplFileObject $file, $associative = false)
{
$data = [];
$file->setFlags(SplFileObject::READ_CSV | SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY | SplFileObject::DROP_NEW_LINE);
if (!$file->eof() && $associative) {
$columns = $file->fgetcsv();
}
while (!$file->eof()) {
$data[] = $file->fgetcsv();
$row = $file->fgetcsv();
if ($associative) {
$row = array_combine($columns, $row);
}
$data[] = $row;
}

return $data;
Expand Down
2 changes: 1 addition & 1 deletion app/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Page extends Model
const ACTIVE = 'Active';
const INACTIVE = 'Inactive';

protected $fillable = ['title', 'status', 'description', 'user_id'];
protected $fillable = ['title', 'status', 'content', 'logo', 'user_id', 'parent_id'];

public function user()
{
Expand Down
39 changes: 39 additions & 0 deletions database/migrations/2019_07_27_120740_update_pages_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

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

class UpdatePagesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
DB::statement('ALTER TABLE pages CHANGE description content TEXT');

Schema::table('pages', function (Blueprint $table) {
$table->string('logo');
$table->unsignedInteger('parent_id');
$table->foreign('parent_id')->references('id')->on('pages');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
DB::statement('ALTER TABLE pages CHANGE content description VARCHAR(500)');

Schema::table('pages', function (Blueprint $table) {
$table->dropColumn('logo');
$table->dropForeign('pages_parent_id_foreign');
$table->dropColumn('parent_id');
});
}
}
2 changes: 2 additions & 0 deletions database/seeds/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\User;
use App\City;
use App\County;
use App\Page;

class DatabaseSeeder extends Seeder
{
Expand All @@ -25,6 +26,7 @@ public function run()
Incident::truncate();
City::truncate();
County::truncate();
Page::truncate();
Precinct::truncate();
User::getQuery()->delete();
Eloquent::unguard();
Expand Down
17 changes: 5 additions & 12 deletions database/seeds/PagesTableSeeder.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

use App\Page;
use App\Helpers\CsvHandler;
use Illuminate\Database\Seeder;
use Faker\Factory as Faker;
use App\Page;

class PagesTableSeeder extends Seeder
{
Expand All @@ -13,16 +13,9 @@ class PagesTableSeeder extends Seeder
*/
public function run()
{
$faker = Faker::create();

foreach (range(1, 50) as $index)
{
Page::create([
'title' => $faker->text(255),
'status' => $faker->randomElement(['Active', 'Inactive']),
'description' => $faker->realText(500),
'user_id' => 1
]);
$data = CsvHandler::convertToArray('resources/files/pages/pages.csv', true);
foreach ($data as $row) {
Page::create($row);
}
}
}
Loading