ソースを参照

hosts get/add/del/edit

tags/0.1.0
Robin Thoni 8年前
コミット
8d52746f1e

+ 51
- 0
app/Http/Business/HostsBusiness.php ファイルの表示

@@ -0,0 +1,51 @@
1
+<?php
2
+
3
+namespace App\Http\Business;
4
+
5
+use Luticate\Utils\LuBusiness;
6
+use App\Http\DataAccess\HostsDataAccess;
7
+use App\Http\DBO\HostsDbo;
8
+
9
+class HostsBusiness extends LuBusiness {
10
+    protected static function getDataAccess()
11
+    {
12
+        return new HostsDataAccess();
13
+    }
14
+
15
+    protected static function checkHost(HostsDbo $host, $host_id = null)
16
+    {
17
+        $existingHost = HostsDataAccess::getByName($host->getName());
18
+        if (!is_null($existingHost) && $host_id != $existingHost->getId()) {
19
+            self::badInput("Host name already exists");
20
+        }
21
+        if (is_null($host->getName()) || strlen($host->getName()) == 0) {
22
+            self::badInput("Missing host name");
23
+        }
24
+        if (is_null($host->getUrl()) || strlen($host->getUrl()) == 0) {
25
+            self::badInput("Missing host url");
26
+        }
27
+        if (is_null($host->getToken())) {
28
+            $host->setToken("");
29
+        }
30
+    }
31
+
32
+    public static function add(HostsDbo $host)
33
+    {
34
+        self::checkHost($host);
35
+        return HostsDataAccess::addId($host);
36
+    }
37
+
38
+    public static function edit(HostsDbo $host, $host_id)
39
+    {
40
+        self::getById($host_id);
41
+        self::checkHost($host, $host_id);
42
+        $host->setId($host_id);
43
+        return HostsDataAccess::editById($host_id, $host);
44
+    }
45
+
46
+    public static function del($host_id)
47
+    {
48
+        self::getById($host_id);
49
+        return HostsDataAccess::deleteById($host_id);
50
+    }
51
+}

+ 67
- 0
app/Http/Controller/HostsController.php ファイルの表示

@@ -0,0 +1,67 @@
1
+<?php
2
+
3
+namespace App\Http\Controller;
4
+
5
+use Luticate\Utils\LuController;
6
+use App\Http\Business\HostsBusiness;
7
+use App\Http\DBO\HostsDbo;
8
+
9
+class HostsController extends LuController {
10
+    protected function getBusiness()
11
+    {
12
+        return new HostsBusiness();
13
+    }
14
+
15
+    /**
16
+     * Get all hosts, sorted by name
17
+     * @param int $page The page number, 0 based
18
+     * @param int $perPage The number of items per page
19
+     * @param string $query The filter query
20
+     * @return \Luticate\Utils\LuMultipleDbo
21
+     */
22
+    public function getAll($page = 0, $perPage = PHP_INT_MAX, $query = "")
23
+    {
24
+        return HostsBusiness::getAll($page, $perPage, $query);
25
+    }
26
+
27
+    /**
28
+     * Get a host
29
+     * @param $host_id int The host id
30
+     * @return HostsDbo
31
+     */
32
+    public function get($host_id)
33
+    {
34
+        return HostsBusiness::getById($host_id);
35
+    }
36
+
37
+    /**
38
+     * Add a new host
39
+     * @param HostsDbo $host The host
40
+     * @return int
41
+     */
42
+    public function add(HostsDbo $host)
43
+    {
44
+        return HostsBusiness::add($host);
45
+    }
46
+
47
+    /**
48
+     * Edit an existing host
49
+     * @param HostsDbo $host The host
50
+     * @param $host_id int The host id
51
+     * @return bool
52
+     */
53
+    public function edit(HostsDbo $host, $host_id)
54
+    {
55
+        return HostsBusiness::edit($host, $host_id);
56
+    }
57
+
58
+    /**
59
+     * Delete an existing host
60
+     * @param $host_id int The host id
61
+     * @return bool
62
+     */
63
+    public function del($host_id)
64
+    {
65
+        return HostsBusiness::del($host_id);
66
+    }
67
+}

+ 35
- 0
app/Http/DBO/CamotionPermissions.php ファイルの表示

@@ -0,0 +1,35 @@
1
+<?php
2
+
3
+namespace App\Http\DBO;
4
+
5
+/**
6
+ * Created by PhpStorm.
7
+ * User: robin
8
+ * Date: 11/21/15
9
+ * Time: 3:52 PM
10
+ */
11
+class CamotionPermissions
12
+{
13
+    const HOST_GET = "CAMOTION_HOST_GET";
14
+    const HOST_ADD = "CAMOTION_HOST_ADD";
15
+    const HOST_DEL = "CAMOTION_HOST_DEL";
16
+    const HOST_EDIT = "CAMOTION_HOST_EDIT";
17
+
18
+    const COMMAND_GET = "CAMOTION_COMMAND_GET";
19
+    const COMMAND_ADD = "CAMOTION_COMMAND_ADD";
20
+    const COMMAND_DEL = "CAMOTION_COMMAND_DEL";
21
+    const COMMAND_EDIT = "CAMOTION_COMMAND_EDIT";
22
+    const COMMAND_EXEC = "CAMOTION_COMMAND_EXEC";
23
+
24
+    const SENSOR_GET = "CAMOTION_SENSOR_GET";
25
+    const SENSOR_ADD = "CAMOTION_SENSOR_ADD";
26
+    const SENSOR_DEL = "CAMOTION_SENSOR_DEL";
27
+    const SENSOR_EDIT = "CAMOTION_SENSOR_EDIT";
28
+    const SENSOR_EXEC = "CAMOTION_SENSOR_EXEC";
29
+
30
+    const CAMERA_GET = "CAMOTION_CAMERA_GET";
31
+    const CAMERA_ADD = "CAMOTION_CAMERA_ADD";
32
+    const CAMERA_DEL = "CAMOTION_CAMERA_DEL";
33
+    const CAMERA_EDIT = "CAMOTION_CAMERA_EDIT";
34
+    const CAMERA_EXEC = "CAMOTION_CAMERA_EXEC";
35
+}

+ 103
- 0
app/Http/DBO/HostsDbo.php ファイルの表示

@@ -0,0 +1,103 @@
1
+<?php
2
+
3
+/**
4
+ * AUTO GENERATED BY LUTICATE GENERATOR
5
+ * ANY CHANGES WILL BE OVERWRITTEN
6
+ */
7
+
8
+namespace App\Http\DBO;
9
+
10
+use Luticate\Utils\LuDbo;
11
+
12
+class HostsDbo extends LuDbo {
13
+
14
+    public function jsonSerialize()
15
+    {
16
+        return array(
17
+            "Id" => $this->_id,
18
+            "Name" => $this->_name,
19
+            "Url" => $this->_url,
20
+            "Token" => $this->_token
21
+        );
22
+    }
23
+
24
+    public static function jsonDeserialize($json)
25
+    {
26
+        $dbo = new HostsDbo();
27
+        if (isset($json["Id"])) {
28
+            $dbo->setId($json["Id"]);
29
+        }
30
+        if (isset($json["Name"])) {
31
+            $dbo->setName($json["Name"]);
32
+        }
33
+        if (isset($json["Url"])) {
34
+            $dbo->setUrl($json["Url"]);
35
+        }
36
+        if (isset($json["Token"])) {
37
+            $dbo->setToken($json["Token"]);
38
+        }
39
+        return $dbo;
40
+    }
41
+
42
+    public static function generateSample()
43
+    {
44
+        $dbo = new HostsDbo();
45
+        $dbo->setId(42);
46
+        $dbo->setName("sample string");
47
+        $dbo->setUrl("sample string");
48
+        $dbo->setToken("sample string");
49
+        return $dbo;
50
+    }
51
+
52
+    /**
53
+     * @var integer
54
+     */
55
+    protected $_id;
56
+    public function getId()
57
+    {
58
+        return $this->_id;
59
+    }
60
+    public function setId($value)
61
+    {
62
+        $this->_id = $value;
63
+    }
64
+
65
+    /**
66
+     * @var string
67
+     */
68
+    protected $_name;
69
+    public function getName()
70
+    {
71
+        return $this->_name;
72
+    }
73
+    public function setName($value)
74
+    {
75
+        $this->_name = $value;
76
+    }
77
+
78
+    /**
79
+     * @var string
80
+     */
81
+    protected $_url;
82
+    public function getUrl()
83
+    {
84
+        return $this->_url;
85
+    }
86
+    public function setUrl($value)
87
+    {
88
+        $this->_url = $value;
89
+    }
90
+
91
+    /**
92
+     * @var string
93
+     */
94
+    protected $_token;
95
+    public function getToken()
96
+    {
97
+        return $this->_token;
98
+    }
99
+    public function setToken($value)
100
+    {
101
+        $this->_token = $value;
102
+    }
103
+}

+ 37
- 0
app/Http/DataAccess/HostsDataAccess.php ファイルの表示

@@ -0,0 +1,37 @@
1
+<?php
2
+
3
+namespace App\Http\DataAccess;
4
+
5
+use Luticate\Utils\LuDataAccess;
6
+use App\Http\DataAccess\Models\Hosts;
7
+use App\Http\DBO\HostsDbo;
8
+
9
+class HostsDataAccess extends LuDataAccess {
10
+    protected static function getModel()
11
+    {
12
+        return new Hosts();
13
+    }
14
+
15
+    protected static function getOrderBy()
16
+    {
17
+        return array(array("name", "ASC"));
18
+    }
19
+
20
+    protected static function getQueryPredicate($query)
21
+    {
22
+        return array(array("name", "ilike", "%" . $query . "%"));
23
+    }
24
+
25
+    /**
26
+     * @param $name
27
+     * @return HostsDbo|null
28
+     */
29
+    public static function getByName($name)
30
+    {
31
+        $host = Hosts::where("name", "=", $name)->first();
32
+        if (is_null($host)) {
33
+            return null;
34
+        }
35
+        return $host->toDbo();
36
+    }
37
+}

+ 9
- 0
app/Http/DataAccess/Models/Hosts.php ファイルの表示

@@ -0,0 +1,9 @@
1
+<?php
2
+
3
+namespace App\Http\DataAccess\Models;
4
+
5
+use App\Http\DBO\HostsDbo;
6
+
7
+class Hosts extends HostsModel
8
+{
9
+}

+ 53
- 0
app/Http/DataAccess/Models/HostsModel.php ファイルの表示

@@ -0,0 +1,53 @@
1
+<?php
2
+
3
+/**
4
+ * AUTO GENERATED BY LUTICATE GENERATOR
5
+ * ANY CHANGES WILL BE OVERWRITTEN
6
+ * DO NOT DIRECTLY USE THIS FILE
7
+ * USE Hosts.php
8
+ * TO MAKE YOUR CHANGES AND DATABASE ACCESS
9
+*/
10
+
11
+namespace App\Http\DataAccess\Models;
12
+
13
+use Luticate\Utils\LuModel;
14
+use App\Http\DBO\HostsDbo;
15
+
16
+class HostsModel extends LuModel
17
+{
18
+    function __construct()
19
+    {
20
+        parent::__construct();
21
+        $this->timestamps = false;
22
+    }
23
+
24
+    public function toDbo()
25
+    {
26
+        $dbo = new HostsDbo();
27
+
28
+        $dbo->setId($this->id);
29
+        $dbo->setName($this->name);
30
+        $dbo->setUrl($this->url);
31
+        $dbo->setToken($this->token);
32
+
33
+        return $dbo;
34
+    }
35
+
36
+    /**
37
+     * @param $dbo HostsDbo
38
+     * @param $model LuModel|null
39
+     * @return Hosts
40
+     */
41
+    public function fromDbo($dbo, $model = null)
42
+    {
43
+        if (is_null($model))
44
+            $model = new Hosts();
45
+
46
+        $model->id = $dbo->getId();
47
+        $model->name = $dbo->getName();
48
+        $model->url = $dbo->getUrl();
49
+        $model->token = $dbo->getToken();
50
+
51
+        return $model;
52
+    }
53
+}

+ 15
- 12
app/Http/routes.php ファイルの表示

@@ -1,18 +1,21 @@
1 1
 <?php
2 2
 
3
-/*
4
-|--------------------------------------------------------------------------
5
-| Application Routes
6
-|--------------------------------------------------------------------------
7
-|
8
-| Here is where you can register all of the routes for an application.
9
-| It is a breeze. Simply tell Lumen the URIs it should respond to
10
-| and give it the Closure to call when that URI is requested.
11
-|
12
-*/
13
-
3
+use App\Http\DBO\CamotionPermissions;
14 4
 use Luticate\Auth\Business\LuticateBusiness;
15 5
 use Luticate\Doc\Business\LuDocBusiness;
6
+use Luticate\Utils\LuRoute;
7
+
8
+$route = LuRoute::getInstance();
16 9
 
10
+$int = LuRoute::REG_INT;
11
+$host_id = "{host_id:$int}";
12
+
13
+LuticateBusiness::setupAuth();
17 14
 LuticateBusiness::setupRoutes();
18
-LuDocBusiness::setupRoutes();
15
+LuDocBusiness::setupRoutes("/camotion");
16
+
17
+$route->get("/hosts", "Hosts", "getAll", CamotionPermissions::HOST_GET);
18
+$route->get("/hosts/$host_id", "Hosts", "get", CamotionPermissions::HOST_GET);
19
+$route->post("/hosts/add", "Hosts", "add", array(CamotionPermissions::HOST_GET, CamotionPermissions::HOST_ADD));
20
+$route->post("/hosts/$host_id/edit", "Hosts", "edit", array(CamotionPermissions::HOST_GET, CamotionPermissions::HOST_EDIT));
21
+$route->post("/hosts/$host_id/del", "Hosts", "del", array(CamotionPermissions::HOST_GET, CamotionPermissions::HOST_DEL));

+ 0
- 0
storage/app/.gitignore ファイルの表示


+ 0
- 0
storage/framework/cache/.gitignore ファイルの表示


+ 0
- 0
storage/framework/sessions/.gitignore ファイルの表示


+ 0
- 0
storage/framework/views/.gitignore ファイルの表示


+ 0
- 0
storage/logs/.gitignore ファイルの表示


読み込み中…
キャンセル
保存