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

cameras get/add/del/edit

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

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

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

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

@@ -0,0 +1,21 @@
1
+<?php
2
+
3
+namespace App\Http\Business;
4
+
5
+use Luticate\Utils\LuBusiness;
6
+use App\Http\DataAccess\EntityTypesDataAccess;
7
+use App\Http\DBO\EntityTypesDbo;
8
+
9
+class EntityTypesBusiness extends LuBusiness {
10
+    const ENTITY_CAMERA = 1;
11
+
12
+    protected static function getDataAccess()
13
+    {
14
+        return new EntityTypesDataAccess();
15
+    }
16
+
17
+    public static function getAllForType($type)
18
+    {
19
+        return EntityTypesDataAccess::getAllForType($type);
20
+    }
21
+}

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

@@ -42,10 +42,4 @@ class HostsBusiness extends LuBusiness {
42 42
         $host->setId($host_id);
43 43
         return HostsDataAccess::editById($host_id, $host);
44 44
     }
45
-
46
-    public static function del($host_id)
47
-    {
48
-        self::getById($host_id);
49
-        return HostsDataAccess::deleteById($host_id);
50
-    }
51 45
 }

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

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

+ 139
- 0
app/Http/DBO/CamerasDbo.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 CamerasDbo 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
+            "EntityTypeId" => $this->_entityTypeId,
22
+            "Data" => $this->_data
23
+        );
24
+    }
25
+
26
+    public static function jsonDeserialize($json)
27
+    {
28
+        $dbo = new CamerasDbo();
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["EntityTypeId"])) {
42
+            $dbo->setEntityTypeId($json["EntityTypeId"]);
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 CamerasDbo();
53
+        $dbo->setId(42);
54
+        $dbo->setName("sample string");
55
+        $dbo->setDescription("sample string");
56
+        $dbo->setHostId(42);
57
+        $dbo->setEntityTypeId(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 $_entityTypeId;
118
+    public function getEntityTypeId()
119
+    {
120
+        return $this->_entityTypeId;
121
+    }
122
+    public function setEntityTypeId($value)
123
+    {
124
+        $this->_entityTypeId = $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
+}

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

@@ -0,0 +1,121 @@
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 EntityTypesDbo 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
+            "Type" => $this->_type
22
+        );
23
+    }
24
+
25
+    public static function jsonDeserialize($json)
26
+    {
27
+        $dbo = new EntityTypesDbo();
28
+        if (isset($json["Id"])) {
29
+            $dbo->setId($json["Id"]);
30
+        }
31
+        if (isset($json["Name"])) {
32
+            $dbo->setName($json["Name"]);
33
+        }
34
+        if (isset($json["Class"])) {
35
+            $dbo->setClass($json["Class"]);
36
+        }
37
+        if (isset($json["DefaultData"])) {
38
+            $dbo->setDefaultData($json["DefaultData"]);
39
+        }
40
+        if (isset($json["Type"])) {
41
+            $dbo->setType($json["Type"]);
42
+        }
43
+        return $dbo;
44
+    }
45
+
46
+    public static function generateSample()
47
+    {
48
+        $dbo = new EntityTypesDbo();
49
+        $dbo->setId(42);
50
+        $dbo->setName("sample string");
51
+        $dbo->setClass("sample string");
52
+        $dbo->setDefaultData("sample string");
53
+        $dbo->setType(42);
54
+        return $dbo;
55
+    }
56
+
57
+    /**
58
+     * @var integer
59
+     */
60
+    protected $_id;
61
+    public function getId()
62
+    {
63
+        return $this->_id;
64
+    }
65
+    public function setId($value)
66
+    {
67
+        $this->_id = $value;
68
+    }
69
+
70
+    /**
71
+     * @var string
72
+     */
73
+    protected $_name;
74
+    public function getName()
75
+    {
76
+        return $this->_name;
77
+    }
78
+    public function setName($value)
79
+    {
80
+        $this->_name = $value;
81
+    }
82
+
83
+    /**
84
+     * @var string
85
+     */
86
+    protected $_class;
87
+    public function getClass()
88
+    {
89
+        return $this->_class;
90
+    }
91
+    public function setClass($value)
92
+    {
93
+        $this->_class = $value;
94
+    }
95
+
96
+    /**
97
+     * @var text
98
+     */
99
+    protected $_defaultData;
100
+    public function getDefaultData()
101
+    {
102
+        return $this->_defaultData;
103
+    }
104
+    public function setDefaultData($value)
105
+    {
106
+        $this->_defaultData = $value;
107
+    }
108
+
109
+    /**
110
+     * @var integer
111
+     */
112
+    protected $_type;
113
+    public function getType()
114
+    {
115
+        return $this->_type;
116
+    }
117
+    public function setType($value)
118
+    {
119
+        $this->_type = $value;
120
+    }
121
+}

+ 40
- 0
app/Http/DataAccess/CamerasDataAccess.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\Cameras;
7
+use App\Http\DBO\CamerasDbo;
8
+
9
+class CamerasDataAccess extends LuDataAccess {
10
+    protected static function getModel()
11
+    {
12
+        return new Cameras();
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 CamerasDbo|null
31
+     */
32
+    public static function getByName($name)
33
+    {
34
+        $host = Cameras::where("name", "=", $name)->first();
35
+        if (is_null($host)) {
36
+            return null;
37
+        }
38
+        return $host->toDbo();
39
+    }
40
+}

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

@@ -0,0 +1,24 @@
1
+<?php
2
+
3
+namespace App\Http\DataAccess;
4
+
5
+use Luticate\Utils\LuDataAccess;
6
+use App\Http\DataAccess\Models\EntityTypes;
7
+use App\Http\DBO\EntityTypesDbo;
8
+
9
+class EntityTypesDataAccess extends LuDataAccess {
10
+    protected static function getModel()
11
+    {
12
+        return new EntityTypes();
13
+    }
14
+
15
+    protected static function getOrderBy()
16
+    {
17
+        return array(array("name", "ASC"));
18
+    }
19
+
20
+    public static function getAllForType($type)
21
+    {
22
+        return self::getMultiple(array(array("type", "=", $type)), self::getOrderBy());
23
+    }
24
+}

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

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

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

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

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

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

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

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

@@ -9,6 +9,7 @@ $route = LuRoute::getInstance();
9 9
 
10 10
 $int = LuRoute::REG_INT;
11 11
 $host_id = "{host_id:$int}";
12
+$camera_id = "{camera_id:$int}";
12 13
 
13 14
 LuticateBusiness::setupAuth();
14 15
 LuticateBusiness::setupRoutes();
@@ -18,4 +19,12 @@ $route->get("/hosts", "Hosts", "getAll", CamotionPermissions::HOST_GET);
18 19
 $route->get("/hosts/$host_id", "Hosts", "get", CamotionPermissions::HOST_GET);
19 20
 $route->post("/hosts/add", "Hosts", "add", array(CamotionPermissions::HOST_GET, CamotionPermissions::HOST_ADD));
20 21
 $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));
22
+$route->post("/hosts/$host_id/del", "Hosts", "del", array(CamotionPermissions::HOST_GET, CamotionPermissions::HOST_DEL));
23
+
24
+
25
+$route->get("/cameras/types", "Cameras", "getAllTypes", array(CamotionPermissions::CAMERA_GET, CamotionPermissions::CAMERA_ADD));
26
+$route->get("/cameras", "Cameras", "getAll", CamotionPermissions::CAMERA_GET);
27
+$route->get("/cameras/$camera_id", "Cameras", "get", CamotionPermissions::CAMERA_GET);
28
+$route->post("/cameras/add", "Cameras", "add", array(CamotionPermissions::CAMERA_GET, CamotionPermissions::CAMERA_ADD));
29
+$route->post("/cameras/$camera_id/edit", "Cameras", "edit", array(CamotionPermissions::CAMERA_GET, CamotionPermissions::CAMERA_EDIT));
30
+$route->post("/cameras/$camera_id/del", "Cameras", "del", array(CamotionPermissions::CAMERA_GET, CamotionPermissions::CAMERA_DEL));

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

@@ -46,7 +46,9 @@ $gen->setConfig(array("dbo" =>
46 46
         "sp" => array(
47 47
             "/^sp_lu_.*/"
48 48
         ),
49
-        "controllers" => array()
49
+        "controllers" => array(
50
+            "/^EntityTypesController$/"
51
+        )
50 52
     )
51 53
 ));
52 54
 

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