Skip to content

Commit

Permalink
Merge pull request #4433 from ushahidi/V5/improve-performance
Browse files Browse the repository at this point in the history
apply only parameter on model of post and survey
  • Loading branch information
tuxpiper authored Jun 28, 2022
2 parents 872c918 + 97d5455 commit 0514331
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 89 deletions.
8 changes: 4 additions & 4 deletions v5/Http/Controllers/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ protected function ignoreInput()
* @return mixed
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function show(int $id)
public function show(Request $request, int $id)
{
$post = Post::withPostValues()->where('id', $id)->first();
$post = Post::withPostValues()->where('id', $id)->first(POST::selectModelFields($request));

if (!$post) {
return self::make404();
Expand All @@ -55,9 +55,9 @@ public function show(int $id)
* @return PostCollection
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function index()
public function index(Request $request)
{
return new PostCollection(Post::withPostValues()->paginate(20));
return new PostCollection(Post::withPostValues()->paginate(20, POST::selectModelFields($request)));
} //end index()

private function getUser()
Expand Down
32 changes: 16 additions & 16 deletions v5/Http/Controllers/SurveyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ class SurveyController extends V5Controller
* @return mixed
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function show(int $id)
public function show(Request $request, int $id)
{
$survey = Survey::find($id);
$survey = Survey::find($id, Survey::selectModelFields($request));
if (!$survey) {
return self::make404();
}
return new SurveyResource($survey);
}//end show()
} //end show()


/**
Expand All @@ -37,10 +37,10 @@ public function show(int $id)
* @return SurveyCollection
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function index()
public function index(Request $request)
{
return new SurveyCollection(Survey::all());
}//end index()
return new SurveyCollection(Survey::all(Survey::selectModelFields($request)));
} //end index()

/**
* Display the specified resource.
Expand Down Expand Up @@ -123,11 +123,11 @@ public function store(Request $request)
'field'
);
}
}//end foreach
}//end if
} //end foreach
} //end if

return new SurveyResource($survey);
}//end store()
} //end store()

/**
* Display the specified resource.
Expand Down Expand Up @@ -169,7 +169,7 @@ public function update(int $id, Request $request)
$survey->load('tasks');

return new SurveyResource($survey);
}//end update()
} //end update()

/**
* @param array $input_tasks
Expand Down Expand Up @@ -204,7 +204,7 @@ private function updateTasks(array $input_tasks, Survey $survey)
'task'
);
$this->updateFields(($stage['fields'] ?? []), $stage_model);
}//end foreach
} //end foreach

$input_tasks_collection = new Collection($input_tasks);
$survey->load('tasks');
Expand All @@ -216,7 +216,7 @@ private function updateTasks(array $input_tasks, Survey $survey)
foreach ($tasks_to_delete as $task_to_delete) {
Stage::where('id', $task_to_delete->id)->delete();
}
}//end updateTasks()
} //end updateTasks()

private function isArrayOfNumbers(array $arr)
{
Expand Down Expand Up @@ -264,7 +264,7 @@ private function updateFields(array $input_fields, Stage $stage)
)
);
$added_fields[] = $field_model->id;
}//end if
} //end if

$this->updateTranslations(
$field_model,
Expand All @@ -273,7 +273,7 @@ private function updateFields(array $input_fields, Stage $stage)
$field_model->id,
'field'
);
}//end foreach
} //end foreach

$input_fields_collection = new Collection($input_fields);
$stage->load('fields');
Expand All @@ -285,7 +285,7 @@ private function updateFields(array $input_fields, Stage $stage)
foreach ($fields_to_delete as $field_to_delete) {
Attribute::where('id', $field_to_delete->id)->delete();
}
}//end updateFields()
} //end updateFields()


/**
Expand Down Expand Up @@ -313,5 +313,5 @@ public function delete(int $id, Request $request)
$survey->delete();

return response()->json(['result' => ['deleted' => $id]]);
}//end delete()
} //end delete()
}//end class
52 changes: 6 additions & 46 deletions v5/Http/Resources/BaseResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,57 +16,17 @@

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\Resource;
use v5\Traits\HasHydrate;
use v5\Traits\HasOnlyParameters;

class BaseResource extends Resource
{
use HasHydrate;
use HasOnlyParameters;

public static $wrap = 'result';

public function getHydrate($relationships, Request $request)
{
$only_original = self::toHydrate($request, $relationships);
return array_filter($only_original, function ($o) use ($relationships) {
return in_array($o, $relationships);
});
}

public static function toHydrate(Request $request, $relationships)
{
$to_hydrate = $relationships;
if ($request->has('hydrate') && !$request->get('hydrate')) {
$to_hydrate = [];
}
if ($request->get('hydrate')) {
$to_hydrate = explode(',', $request->get('hydrate'));
}
return $to_hydrate;
}

public static function onlyOriginal($request, $approved_fields)
{
$only_original = $approved_fields;
if ($request->query('format') === 'minimal') {
$only_original = ['id', 'name', 'description', 'translations'];
} elseif ($request->get('only')) {
$only_original = explode(',', $request->get('only'));
}
return $only_original;
}

public static function includeFields($request, $approved_fields = [])
{
$fields = $approved_fields;
if ($request->has('only') && !$request->get('only')) {
return [];
}
$only_original = self::onlyOriginal($request, $approved_fields);
if (count($only_original) > 0) {
$fields = array_filter($only_original, function ($f) use ($approved_fields) {
return in_array($f, $approved_fields);
});
}
return $fields;
}


protected function setResourceFields($fields)
{
$result = [];
Expand Down
4 changes: 3 additions & 1 deletion v5/Models/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;
use v5\Traits\HasOnlyParameters;

/**
* Class ResourceModel
Expand All @@ -14,7 +15,8 @@
*/
class BaseModel extends Model
{

use HasOnlyParameters;

protected $validationRules = [];

/**
Expand Down
42 changes: 30 additions & 12 deletions v5/Models/Post/Post.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* *
* * Ushahidi Acl
Expand All @@ -22,6 +23,7 @@
use Illuminate\Support\Facades\Input;
use Ushahidi\App\Validator\LegacyValidator;
use Ushahidi\Core\Tools\Permissions\InteractsWithPostPermissions;
use Illuminate\Http\Request;

class Post extends BaseModel
{
Expand Down Expand Up @@ -75,12 +77,11 @@ class Post extends BaseModel
*
* @var array
*/
protected $hidden = [
];
protected $hidden = [];

/**
* @var array
*/
*/
protected $fillable = [
'form_id',
'user_id',
Expand All @@ -103,7 +104,7 @@ class Post extends BaseModel
* The model's default values for attributes.
*
* @var array
*/
*/
protected $attributes = [
'type' => 'report',
'locale' => 'en_US',
Expand Down Expand Up @@ -220,7 +221,7 @@ public function validationMessages()
]
)
];
}//end validationMessages()
} //end validationMessages()

/**
* Get the error messages for the defined *bulk* validation rules.
Expand All @@ -247,7 +248,7 @@ private function bulkValidationMessages()
['field' => 'id']
),
];
}//end bulkValidationMessages()
} //end bulkValidationMessages()

/**
* Get the error messages for the defined *bulk* validation rules.
Expand All @@ -273,7 +274,7 @@ public function bulkPatchValidationMessages()
)
]
);
}//end bulkValidationMessages()
} //end bulkValidationMessages()

/**
* Get the error messages for the defined *bulk* validation rules.
Expand Down Expand Up @@ -308,7 +309,7 @@ public function getRules()
'title' => [
'required',
'max:150',
'regex:'.LegacyValidator::REGEX_STANDARD_TEXT,
'regex:' . LegacyValidator::REGEX_STANDARD_TEXT,
],
'slug' => [
'required',
Expand Down Expand Up @@ -362,15 +363,15 @@ function ($attribute, $value, $fail) {
'locale',
'post_date'
];
}//end getRules()
} //end getRules()

/**
* Get the post's translation.
*/
public function translations()
{
return $this->morphMany('v5\Models\Translation', 'translatable');
}//end translations()
} //end translations()


public function getUserIdAttribute($value)
Expand Down Expand Up @@ -558,7 +559,7 @@ protected static function valueTypesRelationships()
'Point',
'Relation',
'PostsMedia',
// 'PostsSet',
// 'PostsSet',
'PostTag'
];
return array_map(function ($t) {
Expand Down Expand Up @@ -642,7 +643,6 @@ public function valuesMedia()
public function valuesPoint()
{
return $this->hasMany('v5\Models\PostValues\PostPoint', 'post_id', 'id');
;
}

public function valuesRelation()
Expand Down Expand Up @@ -673,4 +673,22 @@ public function postStages()
{
return $this->hasMany('v5\Models\PostStages', 'post_id', 'id');
}

/**
* get the required cloumns .
*
* @param Request $request
* @return array
*/
public static function selectModelFields(Request $request): array
{
return self::includeFields($request, (new Post())->fillable, [
'id',
'parent_id',
'base_language',
'form_id',
'status',

]);
}
}//end class
Loading

0 comments on commit 0514331

Please sign in to comment.