Browse Source

tests; business; data access

develop
Robin Thoni 8 years ago
parent
commit
46f4baa851

+ 4
- 7
src/Utils/Business/LuBusiness.php View File

41
     public static function getById($id)
41
     public static function getById($id)
42
     {
42
     {
43
         $data = static::getDataAccess()->getSingleById($id);
43
         $data = static::getDataAccess()->getSingleById($id);
44
-        if (is_null($data))
44
+        if (is_null($data)) {
45
             self::notFound(static::getResourceName() . " not found");
45
             self::notFound(static::getResourceName() . " not found");
46
+        }
46
         return $data;
47
         return $data;
47
     }
48
     }
48
 
49
 
53
     public static function deleteById($id)
54
     public static function deleteById($id)
54
     {
55
     {
55
         $res = static::getDataAccess()->deleteSingleById($id);
56
         $res = static::getDataAccess()->deleteSingleById($id);
56
-        if (!$res)
57
+        if (!$res) {
57
             self::notFound(static::getResourceName() . " not found");
58
             self::notFound(static::getResourceName() . " not found");
59
+        }
58
         return true;
60
         return true;
59
     }
61
     }
60
 
62
 
66
         return null;
68
         return null;
67
     }
69
     }
68
 
70
 
69
-    public static function getAll($page = 0, $perPage = PHP_INT_MAX, $query = "")
70
-    {
71
-        return static::getDataAccess()->getAll($page, $perPage, $query);
72
-    }
73
-
74
     public static function getResourceName()
71
     public static function getResourceName()
75
     {
72
     {
76
         $match = [];
73
         $match = [];

+ 27
- 12
src/Utils/DataAccess/LuDataAccess.php View File

8
 use Luticate\Utils\Business\LuArrayUtils;
8
 use Luticate\Utils\Business\LuArrayUtils;
9
 use Luticate\Utils\Business\LuStringUtils;
9
 use Luticate\Utils\Business\LuStringUtils;
10
 use Luticate\Utils\Dbo\LuDbo;
10
 use Luticate\Utils\Dbo\LuDbo;
11
+use Luticate\Utils\Dbo\LuPaginatedDbo;
11
 
12
 
12
 abstract class LuDataAccess {
13
 abstract class LuDataAccess {
13
 
14
 
72
         if (is_callable($query)) {
73
         if (is_callable($query)) {
73
             $query = $query(static::getTableAs());
74
             $query = $query(static::getTableAs());
74
         }
75
         }
76
+        else if (is_null($query)) {
77
+            return static::getTableAs();
78
+        }
75
         return $query;
79
         return $query;
76
     }
80
     }
77
 
81
 
89
         $json = LuArrayUtils::camelCaseToSnake($json);
93
         $json = LuArrayUtils::camelCaseToSnake($json);
90
         return $json;
94
         return $json;
91
     }
95
     }
96
+    
97
+    public static function getData($query)
98
+    {
99
+        $query = static::resolveQuery($query);
100
+        $sql = $query->toSql();
101
+        $string = static::getConnection()->select("WITH query AS (${sql}) SELECT COALESCE(json_agg(data), '[]'::json) AS data FROM query data", $query->getBindings())[0]->data;
102
+
103
+        $data = LuStringUtils::convertJsonString($string);
104
+        return $data;
105
+    }
92
 
106
 
93
     /**
107
     /**
94
      * @param $query Builder|callable
108
      * @param $query Builder|callable
96
      */
110
      */
97
     public static function getMultiple($query)
111
     public static function getMultiple($query)
98
     {
112
     {
99
-        $query = static::resolveQuery($query);
100
-        $string = $query->aggregate("json_agg", [static::$_table_as]);
101
-        if (is_null($string)) {
102
-            return [];
103
-        }
104
-        $data = LuStringUtils::convertJsonString($string);
113
+        $data = static::getData($query);
105
         
114
         
106
         return LuDbo::deserializeValue($data, static::$_dboClass . "[]");
115
         return LuDbo::deserializeValue($data, static::$_dboClass . "[]");
107
     }
116
     }
112
      */
121
      */
113
     public static function getSingle($query)
122
     public static function getSingle($query)
114
     {
123
     {
115
-        $query = static::resolveQuery($query);
116
-        $string = $query->take(1)->aggregate("json_agg", [static::$_table_as]);
117
-        if (is_null($string)) {
118
-            return null;
119
-        }
120
-        $data = LuStringUtils::convertJsonString($string);
124
+        $data = static::getData($query);
125
+        
121
         if (count($data) < 1) {
126
         if (count($data) < 1) {
122
             return null;
127
             return null;
123
         }
128
         }
124
 
129
 
125
         return LuDbo::deserializeValue($data[0], static::$_dboClass);
130
         return LuDbo::deserializeValue($data[0], static::$_dboClass);
126
     }
131
     }
132
+    
133
+    public static function getMultiplePaginated($query, $page, $perPage)
134
+    {
135
+        $query = static::resolveQuery($query);
136
+        $sql = $query->toSql();
137
+        $count = static::getConnection()->select("WITH query AS (${sql}) SELECT count(data) AS data FROM query data", $query->getBindings())[0]->data;
138
+        $query->take($perPage)->offset($page * $perPage);
139
+        $data = self::getMultiple($query);
140
+        return new LuPaginatedDbo($count, $data);
141
+    }
127
 
142
 
128
     /**
143
     /**
129
      * @param $id
144
      * @param $id

+ 2
- 2
src/Utils/Dbo/LuPaginatedDbo.php View File

34
     {
34
     {
35
         return array(
35
         return array(
36
             "Count" => $this->_count,
36
             "Count" => $this->_count,
37
-            "Data" => $this->_data
37
+            "Data" => LuDbo::serializeValue($this->_data)
38
         );
38
         );
39
     }
39
     }
40
 
40
 
50
         foreach ($this->_data as $data) {
50
         foreach ($this->_data as $data) {
51
             $values[] = $callback($data);
51
             $values[] = $callback($data);
52
         }
52
         }
53
-        return new LuPaginatedDbo($this->_count, $values);
53
+        return new static($this->_count, $values);
54
     }
54
     }
55
     
55
     
56
     public static function jsonDeserialize($json)
56
     public static function jsonDeserialize($json)

+ 20
- 3
tests/DatabaseTest.php View File

215
 
215
 
216
     public function testDaGetMultiple()
216
     public function testDaGetMultiple()
217
     {
217
     {
218
-        $this->assertSame([
219
-            ["Id" => 1, "SomeText" => "lol2", "SomeIntegerArray" => [], "CreatedAt" => "2016-06-15 13:45:23"]
220
-        ],
218
+        $this->assertSame([["Id" => 1, "SomeText" => "lol2", "SomeIntegerArray" => [], "CreatedAt" => "2016-06-15 13:45:23"]],
221
             LuDbo::serializeValue(TestTableDataAccess::getMultiple(function(Builder $q)
219
             LuDbo::serializeValue(TestTableDataAccess::getMultiple(function(Builder $q)
222
             {
220
             {
223
                 return $q->where("id", "=", 1);
221
                 return $q->where("id", "=", 1);
224
             })));
222
             })));
225
     }
223
     }
226
 
224
 
225
+    public function testDaGetMultipleOffset()
226
+    {
227
+        $this->assertSame([["Id" => 11, "SomeText" => "lol2", "SomeIntegerArray" => [], "CreatedAt" => "2016-06-15 13:45:23"]],
228
+            LuDbo::serializeValue(TestTableDataAccess::getMultiple(function(Builder $q)
229
+            {
230
+                return $q->take(1)->offset(2);
231
+            })));
232
+    }
233
+
234
+    public function testDaGetPaginated()
235
+    {
236
+        $this->assertSame(["Count" => 3, "Data" =>
237
+            [["Id" => 11, "SomeText" => "lol2", "SomeIntegerArray" => [], "CreatedAt" => "2016-06-15 13:45:23"]]],
238
+            LuDbo::serializeValue(TestTableDataAccess::getMultiplePaginated(function(Builder $q)
239
+            {
240
+                return $q->orderBy("id", "ASC");
241
+            }, 1, 1)));
242
+    }
243
+
227
     public function testDaGetSingleById()
244
     public function testDaGetSingleById()
228
     {
245
     {
229
         $this->assertSame(["Id" => 1, "SomeText" => "lol2", "SomeIntegerArray" => [], "CreatedAt" => "2016-06-15 13:45:23"],
246
         $this->assertSame(["Id" => 1, "SomeText" => "lol2", "SomeIntegerArray" => [], "CreatedAt" => "2016-06-15 13:45:23"],

Loading…
Cancel
Save