Mithril as main method

This commit is contained in:
2017-10-25 12:08:41 +07:00
parent 6040809710
commit d1d5ee1b0c
157 changed files with 19593 additions and 716 deletions

View File

@@ -1,16 +1,8 @@
<?php
namespace App\Controllers;
use App\Models\ApiModel;
class Api
{
private $model;
public function __construct()
{
$this->model = new ApiModel();
}
public function index()
{
@@ -27,48 +19,47 @@ class Api
echo json_encode($index, JSON_UNESCAPED_SLASHES);
}
// public function posts($args = ['id' => '1'])
// {
// $get = [];
//
// if ($args['id'] == 3) {
// $get['data'] = $this->model->showJoin();
// } else {
// $get['data'] = $this->model->showJoin([
// ['pengumuman.status', '=', $args['id']]
// ]);
// }
//
// if ($get['data'] == false) {
// $get['data']['content'] = 'Tidak ada pengumuman';
// $get['data']['valid_at'] = '';
// $get['data']['expired_at'] = '';
// $get['data']['status'] = 0;
// $get['data']['background'] = '#333';
// $get['data']['foreground'] = '#888';
// }
//
// if (array_key_exists(0, $get['data']) == false) {
// $temp_data = $get['data'];
// unset($get['data']);
// $get['data'][] = $temp_data;
// $temp_data = [];
// }
//
// header('Content-Type: application/json');
// echo json_encode($get);
// }
public function get($table, $id = "")
public function get($table, $id = "", $args = [])
{
$get = [];
$get['data'] = $this->model->showAll(
($id == "") ? [] : [
['id', '=', $id]
], $table
);
$model = 'App\Models\ApiModel';
if ($table == 'pengumuman') {
$model = 'App\Models\Pengumuman';
}
if ($args == []) {
$get['data'] = $id == "" ? $model::showAll($table) : $model::fetch(
$table,
[
[$table.'.id', '=', $id]
]);
} else {
if ($args['status'] != 3) {
$get['data'] = $model::showAll($table, [
["$table.status", '=', $args['status']]
]);
} else {
$get['data'] = $model::showAll($table);
}
}
$get['count'] = count($get['data']);
if ($table == 'kategori') {
if (isset($get['data'][0])) {
for ($i=0; $i < count($get['data']); $i++) {
$get['data'][$i]['posts'] = count(\App\Models\Pengumuman::showAll('pengumuman', [
['pengumuman.status', '=', 1],
['pengumuman.category', '=', $get['data'][$i]['id']]
]));
}
} else {
$get['data']['posts'] = count(\App\Models\Pengumuman::showAll('pengumuman', [
['pengumuman.status', '=', 1],
['pengumuman.category', '=', $get['data']['id']]
]));
}
}
header('Content-Type: application/json');
echo json_encode($get);
}
@@ -80,8 +71,17 @@ class Api
$args = file_get_contents("php://input");
$args = json_decode($args, true);
$put['data'] = $this->model->update($table, $args);
$put['count'] = count($put['data']);
if (isset($args['posts'])) unset($args['posts']);
$update = \App\Models\ApiModel::update($table, $args);
if (!is_array($update)) {
$put['status'] = false;
$put['message'] = $update;
} else {
$put['status'] = true;
$put['data'] = $update;
$put['count'] = count($put['data']);
}
header('Content-Type: application/json');
echo json_encode($put);
@@ -93,11 +93,18 @@ class Api
$args = file_get_contents("php://input");
$args = json_decode($args, true);
$post['data'] = $this->model->entry($table, $args);
$post['data'] = $this->model->showAll([
['id', '=', $post['data'][0]]
], $table);
$post['count'] = count($post['data']);
$entry = \App\Models\ApiModel::entry($table, $args);
$entry = \App\Models\ApiModel::showAll($table, [
['id', '=', $entry[0]]
]);
if (!is_array($entry)) {
$post['status'] = false;
$post['message'] = $entry;
} else {
$post['status'] = true;
$post['data'] = $entry;
$post['count'] = count($post['data']);
}
header('Content-Type: application/json');
echo json_encode($post);
@@ -109,8 +116,15 @@ class Api
$args = file_get_contents("php://input");
$args = json_decode($args, true);
$delete['data'] = $this->model->remove($table, $args['id']);
$delete['count'] = count($delete['data']);
$remove = \App\Models\ApiModel::remove($table, $args['id']);
if (!is_array($delete['data'])) {
$delete['status'] = false;
$delete['messsage'] = $remove;
} else {
$delete['status'] = true;
$delete['data'] = $remove;
$delete['count'] = count($delete['data']);
}
header('Content-Type: application/json');
echo json_encode($delete);