150 lines
4.7 KiB
PHP
150 lines
4.7 KiB
PHP
<?php namespace AhmadFatoni\ApiGenerator\Controllers\API;
|
|
|
|
use Cms\Classes\Controller;
|
|
use BackendMenu;
|
|
|
|
use Illuminate\Http\Request;
|
|
use AhmadFatoni\ApiGenerator\Helpers\Helpers;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Tps\Tps\Models\Cartoons;
|
|
class CartoonsController extends Controller
|
|
{
|
|
protected $Cartoons;
|
|
|
|
protected $helpers;
|
|
|
|
public function __construct(Cartoons $Cartoons, Helpers $helpers)
|
|
{
|
|
parent::__construct();
|
|
$this->Cartoons = $Cartoons;
|
|
$this->helpers = $helpers;
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$data = $this->Cartoons->all()->toArray();
|
|
|
|
$baseUrl = url('/storage/app/media');
|
|
|
|
foreach ($data as &$project) {
|
|
$project['list_image'] = $baseUrl . $project['list_image'];
|
|
}
|
|
|
|
$filteredData = array_map(function($data) {
|
|
return [
|
|
'id' => $data['id'],
|
|
'name' => $data['name'],
|
|
'list_image' => $data['list_image'],
|
|
'created_at' => $data['created_at'],
|
|
'updated_at' => $data['updated_at'],
|
|
];
|
|
}, $data);
|
|
|
|
return $this->helpers->apiArrayResponseBuilder(200, 'success', $filteredData);
|
|
}
|
|
|
|
public function show($id)
|
|
{
|
|
$data = $this->Cartoons::find($id);
|
|
$baseUrl = url('/storage/app/media');
|
|
|
|
if (!is_null($data)) {
|
|
$dataArray = $data->toArray();
|
|
|
|
// Update the image URLs
|
|
$dataArray['list_image'] = $baseUrl . $dataArray['list_image'];
|
|
$dataArray['banner_image'] = $baseUrl . $dataArray['banner_image'];
|
|
$dataArray['logo'] = $baseUrl . $dataArray['logo'];
|
|
$dataArray['main_characters_image'] = $baseUrl . $dataArray['main_characters_image'];
|
|
$dataArray['shots_image'] = $baseUrl . $dataArray['shots_image'];
|
|
$dataArray['posters_image'] = $baseUrl . $dataArray['posters_image'];
|
|
|
|
if (!is_null($dataArray['list_characters'])) {
|
|
foreach ($dataArray['list_characters'] as &$character) {
|
|
foreach ($character['character_images'] as &$character_image) {
|
|
$character_image['image'] = $baseUrl . $character_image['image'];
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!is_null($dataArray['list_shots'])) {
|
|
foreach ($dataArray['list_shots'] as &$shot) {
|
|
$shot['shot_image'] = $baseUrl . $shot['shot_image'];
|
|
}
|
|
}
|
|
|
|
if (!is_null($dataArray['list_posters'])) {
|
|
foreach ($dataArray['list_posters'] as &$poster) {
|
|
$poster['image'] = $baseUrl . $poster['image'];
|
|
}
|
|
}
|
|
|
|
return $this->helpers->apiArrayResponseBuilder(200, 'success', [$dataArray]);
|
|
} else {
|
|
return $this->helpers->apiArrayResponseBuilder(404, 'error', null);
|
|
}
|
|
}
|
|
|
|
public function store(Request $request){
|
|
|
|
$arr = $request->all();
|
|
|
|
while ( $data = current($arr)) {
|
|
$this->Cartoons->{key($arr)} = $data;
|
|
next($arr);
|
|
}
|
|
|
|
$validation = Validator::make($request->all(), $this->Cartoons->rules);
|
|
|
|
if( $validation->passes() ){
|
|
$this->Cartoons->save();
|
|
return $this->helpers->apiArrayResponseBuilder(201, 'created', ['id' => $this->Cartoons->id]);
|
|
}else{
|
|
return $this->helpers->apiArrayResponseBuilder(400, 'fail', $validation->errors() );
|
|
}
|
|
|
|
}
|
|
|
|
public function update($id, Request $request){
|
|
|
|
$status = $this->Cartoons->where('id',$id)->update($data);
|
|
|
|
if( $status ){
|
|
|
|
return $this->helpers->apiArrayResponseBuilder(200, 'success', 'Data has been updated successfully.');
|
|
|
|
}else{
|
|
|
|
return $this->helpers->apiArrayResponseBuilder(400, 'bad request', 'Error, data failed to update.');
|
|
|
|
}
|
|
}
|
|
|
|
public function delete($id){
|
|
|
|
$this->Cartoons->where('id',$id)->delete();
|
|
|
|
return $this->helpers->apiArrayResponseBuilder(200, 'success', 'Data has been deleted successfully.');
|
|
}
|
|
|
|
public function destroy($id){
|
|
|
|
$this->Cartoons->where('id',$id)->delete();
|
|
|
|
return $this->helpers->apiArrayResponseBuilder(200, 'success', 'Data has been deleted successfully.');
|
|
}
|
|
|
|
|
|
public static function getAfterFilters() {return [];}
|
|
public static function getBeforeFilters() {return [];}
|
|
public static function getMiddleware() {return [];}
|
|
public function callAction($method, $parameters=false) {
|
|
if(isset($parameters['cartoon'])) $parameters = [$parameters['cartoon']];
|
|
return call_user_func_array(array($this, $method), $parameters);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} |