Преглед на файлове

commands get/add/edit/del

tags/0.1.0
Robin Thoni преди 9 години
родител
ревизия
70cc5cd55a

+ 14
- 0
app/Http/Business/CommandTypesBusiness.php Целия файл

@@ -0,0 +1,14 @@
1
+<?php
2
+
3
+namespace App\Http\Business;
4
+
5
+use Luticate\Utils\LuBusiness;
6
+use App\Http\DataAccess\CommandTypesDataAccess;
7
+use App\Http\DBO\CommandTypesDbo;
8
+
9
+class CommandTypesBusiness extends LuBusiness {
10
+    protected static function getDataAccess()
11
+    {
12
+        return new CommandTypesDataAccess();
13
+    }
14
+}

+ 51
- 0
app/Http/Business/CommandsBusiness.php Целия файл

@@ -0,0 +1,51 @@
1
+<?php
2
+
3
+namespace App\Http\Business;
4
+
5
+use Luticate\Utils\LuBusiness;
6
+use App\Http\DataAccess\CommandsDataAccess;
7
+use App\Http\DBO\CommandsDbo;
8
+
9
+class CommandsBusiness extends LuBusiness {
10
+    protected static function getDataAccess()
11
+    {
12
+        return new CommandsDataAccess();
13
+    }
14
+
15
+    protected static function checkCommand(CommandsDbo $command, $command_id = null)
16
+    {
17
+        $existingCommand = CommandsDataAccess::getByName($command->getName());
18
+        if (!is_null($existingCommand) && $command_id != $existingCommand->getId()) {
19
+            self::badInput("Command name already exists");
20
+        }
21
+        if (is_null($command->getName()) || strlen($command->getName()) == 0) {
22
+            self::badInput("Missing command name");
23
+        }
24
+        HostsBusiness::getById($command->getHostId());
25
+        CommandTypesBusiness::getById($command->getCommandTypeId());
26
+        if (is_null($command->getDescription())) {
27
+            $command->setDescription("");
28
+        }
29
+        if (is_null($command->getData()) || strlen($command->getData()) == 0) {
30
+            $command->setData("{}");
31
+        }
32
+        $json = json_decode($command->getData());
33
+        if (is_null($json)) {
34
+            self::badInput("Command data could not be converted to json");
35
+        }
36
+    }
37
+
38
+    public static function add(CommandsDbo $command)
39
+    {
40
+        self::checkCommand($command);
41
+        return CommandsDataAccess::addId($command);
42
+    }
43
+
44
+    public static function edit(CommandsDbo $command, $command_id)
45
+    {
46
+        self::getById($command_id);
47
+        self::checkCommand($command, $command_id);
48
+        $command->setId($command_id);
49
+        return CommandsDataAccess::editById($command_id, $command);
50
+    }
51
+}

+ 81
- 0
app/Http/Controller/CommandsController.php Целия файл

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

+ 0
- 14
app/Http/Controller/SensorTypesController.php Целия файл

@@ -1,14 +0,0 @@
1
-<?php
2
-
3
-namespace App\Http\Controller;
4
-
5
-use Luticate\Utils\LuController;
6
-use App\Http\Business\SensorTypesBusiness;
7
-use App\Http\DBO\SensorTypesDbo;
8
-
9
-class SensorTypesController extends LuController {
10
-    protected function getBusiness()
11
-    {
12
-        return new SensorTypesBusiness();
13
-    }
14
-}

+ 103
- 0
app/Http/DBO/CommandTypesDbo.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 CommandTypesDbo extends LuDbo {
13
+
14
+    public function jsonSerialize()
15
+    {
16
+        return array(
17
+            "Id" => $this->_id,
18
+            "Name" => $this->_name,
19
+            "Class" => $this->_class,
20
+            "DefaultData" => $this->_defaultData
21
+        );
22
+    }
23
+
24
+    public static function jsonDeserialize($json)
25
+    {
26
+        $dbo = new CommandTypesDbo();
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["Class"])) {
34
+            $dbo->setClass($json["Class"]);
35
+        }
36
+        if (isset($json["DefaultData"])) {
37
+            $dbo->setDefaultData($json["DefaultData"]);
38
+        }
39
+        return $dbo;
40
+    }
41
+
42
+    public static function generateSample()
43
+    {
44
+        $dbo = new CommandTypesDbo();
45
+        $dbo->setId(42);
46
+        $dbo->setName("sample string");
47
+        $dbo->setClass("sample string");
48
+        $dbo->setDefaultData("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 $_class;
82
+    public function getClass()
83
+    {
84
+        return $this->_class;
85
+    }
86
+    public function setClass($value)
87
+    {
88
+        $this->_class = $value;
89
+    }
90
+
91
+    /**
92
+     * @var text
93
+     */
94
+    protected $_defaultData;
95
+    public function getDefaultData()
96
+    {
97
+        return $this->_defaultData;
98
+    }
99
+    public function setDefaultData($value)
100
+    {
101
+        $this->_defaultData = $value;
102
+    }
103
+}

+ 139
- 0
app/Http/DBO/CommandsDbo.php Целия файл

@@ -0,0 +1,139 @@
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 CommandsDbo extends LuDbo {
13
+
14
+    public function jsonSerialize()
15
+    {
16
+        return array(
17
+            "Id" => $this->_id,
18
+            "Name" => $this->_name,
19
+            "Description" => $this->_description,
20
+            "HostId" => $this->_hostId,
21
+            "CommandTypeId" => $this->_commandTypeId,
22
+            "Data" => $this->_data
23
+        );
24
+    }
25
+
26
+    public static function jsonDeserialize($json)
27
+    {
28
+        $dbo = new CommandsDbo();
29
+        if (isset($json["Id"])) {
30
+            $dbo->setId($json["Id"]);
31
+        }
32
+        if (isset($json["Name"])) {
33
+            $dbo->setName($json["Name"]);
34
+        }
35
+        if (isset($json["Description"])) {
36
+            $dbo->setDescription($json["Description"]);
37
+        }
38
+        if (isset($json["HostId"])) {
39
+            $dbo->setHostId($json["HostId"]);
40
+        }
41
+        if (isset($json["CommandTypeId"])) {
42
+            $dbo->setCommandTypeId($json["CommandTypeId"]);
43
+        }
44
+        if (isset($json["Data"])) {
45
+            $dbo->setData($json["Data"]);
46
+        }
47
+        return $dbo;
48
+    }
49
+
50
+    public static function generateSample()
51
+    {
52
+        $dbo = new CommandsDbo();
53
+        $dbo->setId(42);
54
+        $dbo->setName("sample string");
55
+        $dbo->setDescription("sample string");
56
+        $dbo->setHostId(42);
57
+        $dbo->setCommandTypeId(42);
58
+        $dbo->setData("sample string");
59
+        return $dbo;
60
+    }
61
+
62
+    /**
63
+     * @var integer
64
+     */
65
+    protected $_id;
66
+    public function getId()
67
+    {
68
+        return $this->_id;
69
+    }
70
+    public function setId($value)
71
+    {
72
+        $this->_id = $value;
73
+    }
74
+
75
+    /**
76
+     * @var string
77
+     */
78
+    protected $_name;
79
+    public function getName()
80
+    {
81
+        return $this->_name;
82
+    }
83
+    public function setName($value)
84
+    {
85
+        $this->_name = $value;
86
+    }
87
+
88
+    /**
89
+     * @var string
90
+     */
91
+    protected $_description;
92
+    public function getDescription()
93
+    {
94
+        return $this->_description;
95
+    }
96
+    public function setDescription($value)
97
+    {
98
+        $this->_description = $value;
99
+    }
100
+
101
+    /**
102
+     * @var integer
103
+     */
104
+    protected $_hostId;
105
+    public function getHostId()
106
+    {
107
+        return $this->_hostId;
108
+    }
109
+    public function setHostId($value)
110
+    {
111
+        $this->_hostId = $value;
112
+    }
113
+
114
+    /**
115
+     * @var integer
116
+     */
117
+    protected $_commandTypeId;
118
+    public function getCommandTypeId()
119
+    {
120
+        return $this->_commandTypeId;
121
+    }
122
+    public function setCommandTypeId($value)
123
+    {
124
+        $this->_commandTypeId = $value;
125
+    }
126
+
127
+    /**
128
+     * @var text
129
+     */
130
+    protected $_data;
131
+    public function getData()
132
+    {
133
+        return $this->_data;
134
+    }
135
+    public function setData($value)
136
+    {
137
+        $this->_data = $value;
138
+    }
139
+}

+ 14
- 0
app/Http/DataAccess/CommandTypesDataAccess.php Целия файл

@@ -0,0 +1,14 @@
1
+<?php
2
+
3
+namespace App\Http\DataAccess;
4
+
5
+use Luticate\Utils\LuDataAccess;
6
+use App\Http\DataAccess\Models\CommandTypes;
7
+use App\Http\DBO\CommandTypesDbo;
8
+
9
+class CommandTypesDataAccess extends LuDataAccess {
10
+    protected static function getModel()
11
+    {
12
+        return new CommandTypes();
13
+    }
14
+}

+ 40
- 0
app/Http/DataAccess/CommandsDataAccess.php Целия файл

@@ -0,0 +1,40 @@
1
+<?php
2
+
3
+namespace App\Http\DataAccess;
4
+
5
+use Luticate\Utils\LuDataAccess;
6
+use App\Http\DataAccess\Models\Commands;
7
+use App\Http\DBO\CommandsDbo;
8
+
9
+class CommandsDataAccess extends LuDataAccess {
10
+    protected static function getModel()
11
+    {
12
+        return new Commands();
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(
23
+            array("name", "ilike", "%" . $query . "%", "or"),
24
+            array("description", "ilike", "%" . $query . "%", "or")
25
+        );
26
+    }
27
+
28
+    /**
29
+     * @param $name
30
+     * @return CommandsDbo|null
31
+     */
32
+    public static function getByName($name)
33
+    {
34
+        $sensor = Commands::where("name", "=", $name)->first();
35
+        if (is_null($sensor)) {
36
+            return null;
37
+        }
38
+        return $sensor->toDbo();
39
+    }
40
+}

+ 9
- 0
app/Http/DataAccess/Models/CommandTypes.php Целия файл

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

+ 53
- 0
app/Http/DataAccess/Models/CommandTypesModel.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 CommandTypes.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\CommandTypesDbo;
15
+
16
+class CommandTypesModel extends LuModel
17
+{
18
+    function __construct()
19
+    {
20
+        parent::__construct();
21
+        $this->timestamps = false;
22
+    }
23
+
24
+    public function toDbo()
25
+    {
26
+        $dbo = new CommandTypesDbo();
27
+
28
+        $dbo->setId($this->id);
29
+        $dbo->setName($this->name);
30
+        $dbo->setClass($this->class);
31
+        $dbo->setDefaultData($this->default_data);
32
+
33
+        return $dbo;
34
+    }
35
+
36
+    /**
37
+     * @param $dbo CommandTypesDbo
38
+     * @param $model LuModel|null
39
+     * @return CommandTypes
40
+     */
41
+    public function fromDbo($dbo, $model = null)
42
+    {
43
+        if (is_null($model))
44
+            $model = new CommandTypes();
45
+
46
+        $model->id = $dbo->getId();
47
+        $model->name = $dbo->getName();
48
+        $model->class = $dbo->getClass();
49
+        $model->default_data = $dbo->getDefaultData();
50
+
51
+        return $model;
52
+    }
53
+}

+ 9
- 0
app/Http/DataAccess/Models/Commands.php Целия файл

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

+ 57
- 0
app/Http/DataAccess/Models/CommandsModel.php Целия файл

@@ -0,0 +1,57 @@
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 Commands.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\CommandsDbo;
15
+
16
+class CommandsModel extends LuModel
17
+{
18
+    function __construct()
19
+    {
20
+        parent::__construct();
21
+        $this->timestamps = false;
22
+    }
23
+
24
+    public function toDbo()
25
+    {
26
+        $dbo = new CommandsDbo();
27
+
28
+        $dbo->setId($this->id);
29
+        $dbo->setName($this->name);
30
+        $dbo->setDescription($this->description);
31
+        $dbo->setHostId($this->host_id);
32
+        $dbo->setCommandTypeId($this->command_type_id);
33
+        $dbo->setData($this->data);
34
+
35
+        return $dbo;
36
+    }
37
+
38
+    /**
39
+     * @param $dbo CommandsDbo
40
+     * @param $model LuModel|null
41
+     * @return Commands
42
+     */
43
+    public function fromDbo($dbo, $model = null)
44
+    {
45
+        if (is_null($model))
46
+            $model = new Commands();
47
+
48
+        $model->id = $dbo->getId();
49
+        $model->name = $dbo->getName();
50
+        $model->description = $dbo->getDescription();
51
+        $model->host_id = $dbo->getHostId();
52
+        $model->command_type_id = $dbo->getCommandTypeId();
53
+        $model->data = $dbo->getData();
54
+
55
+        return $model;
56
+    }
57
+}

+ 9
- 0
app/Http/routes.php Целия файл

@@ -11,6 +11,7 @@ $int = LuRoute::REG_INT;
11 11
 $host_id = "{host_id:$int}";
12 12
 $camera_id = "{camera_id:$int}";
13 13
 $sensor_id = "{sensor_id:$int}";
14
+$command_id = "{command_id:$int}";
14 15
 
15 16
 LuticateBusiness::setupAuth();
16 17
 LuticateBusiness::setupRoutes();
@@ -38,4 +39,12 @@ $route->post("/sensors/add", "Sensors", "add", array(CamotionPermissions::SENSOR
38 39
 $route->post("/sensors/$sensor_id/edit", "Sensors", "edit", array(CamotionPermissions::SENSOR_GET, CamotionPermissions::SENSOR_EDIT));
39 40
 $route->post("/sensors/$sensor_id/del", "Sensors", "del", array(CamotionPermissions::SENSOR_GET, CamotionPermissions::SENSOR_DEL));
40 41
 
42
+
43
+$route->get("/commands/types", "Commands", "getAllTypes", array(CamotionPermissions::COMMAND_GET, CamotionPermissions::COMMAND_ADD));
44
+$route->get("/commands", "Commands", "getAll", CamotionPermissions::COMMAND_GET);
45
+$route->get("/commands/$command_id", "Commands", "get", CamotionPermissions::COMMAND_GET);
46
+$route->post("/commands/add", "Commands", "add", array(CamotionPermissions::COMMAND_GET, CamotionPermissions::COMMAND_ADD));
47
+$route->post("/commands/$command_id/edit", "Commands", "edit", array(CamotionPermissions::COMMAND_GET, CamotionPermissions::COMMAND_EDIT));
48
+$route->post("/commands/$command_id/del", "Commands", "del", array(CamotionPermissions::COMMAND_GET, CamotionPermissions::COMMAND_DEL));
49
+
41 50
 //sleep(1);

+ 1
- 1
generate.php Целия файл

@@ -47,7 +47,7 @@ $gen->setConfig(array("dbo" =>
47 47
             "/^sp_lu_.*/"
48 48
         ),
49 49
         "controllers" => array(
50
-            "/^CameraTypesController$/"
50
+            "/^(Camera|Sensor|Command)TypesController$/"
51 51
         )
52 52
     )
53 53
 ));

Loading…
Отказ
Запис