<?php
 namespace VnChocola\Module\Api\Districts;
 use TCMS\APIController;
 use Model;
 use VNChocola\Model\DistrictsModel;
 class IndexController extends APIController {
 public function getList() {
 $model = Model::load('DistrictsModel');
 $limit = \Request::get('limit', 0);
 if($limit > 0){
 $model->limit($limit);
 }
 $data = $model->toList();
 return $this->ok($data);
 }
 public function find($id) {
 $model = Model::load('DistrictsModel');
 $data = $model->find($id);
 if(!$data) return $this->notFound();
 return $this->ok($data);
 }
 public function create(DistrictsModel $model) {
 if($model->isValid()) {
 $saveRes = $model->save();
 if($saveRes) return $this->ok(['success' => true, 'message' => $saveRes]);
 return $this->badRequest('Lưu dữ liệu thất bại');
 }
 return $this->error($model->getErrors());
 }
 public function update(DistrictsModel $model) {
 if($model->isValid()) {
 if($model->save()) return $this->ok(['success' => true]);
 return $this->badRequest('Lưu dữ liệu thất bại');
 }
 return $this->error($model->getErrors());
 }
 public function delete() {
 $id = \Request::get('data');
 $model = Model::load('DistrictsModel');
 if($model->delete($id)) return $this->ok(['success' => true]);
 return $this->error('Xóa thất bại');
 }
 }
