Преглед изворни кода

tests; business; data access

develop
Robin Thoni пре 8 година
родитељ
комит
46f4baa851

+ 4
- 7
src/Utils/Business/LuBusiness.php Прегледај датотеку

@@ -41,8 +41,9 @@ abstract class LuBusiness {
41 41
     public static function getById($id)
42 42
     {
43 43
         $data = static::getDataAccess()->getSingleById($id);
44
-        if (is_null($data))
44
+        if (is_null($data)) {
45 45
             self::notFound(static::getResourceName() . " not found");
46
+        }
46 47
         return $data;
47 48
     }
48 49
 
@@ -53,8 +54,9 @@ abstract class LuBusiness {
53 54
     public static function deleteById($id)
54 55
     {
55 56
         $res = static::getDataAccess()->deleteSingleById($id);
56
-        if (!$res)
57
+        if (!$res) {
57 58
             self::notFound(static::getResourceName() . " not found");
59
+        }
58 60
         return true;
59 61
     }
60 62
 
@@ -66,11 +68,6 @@ abstract class LuBusiness {
66 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 71
     public static function getResourceName()
75 72
     {
76 73
         $match = [];

+ 27
- 12
src/Utils/DataAccess/LuDataAccess.php Прегледај датотеку

@@ -8,6 +8,7 @@ use Illuminate\Database\Query\Builder;
8 8
 use Luticate\Utils\Business\LuArrayUtils;
9 9
 use Luticate\Utils\Business\LuStringUtils;
10 10
 use Luticate\Utils\Dbo\LuDbo;
11
+use Luticate\Utils\Dbo\LuPaginatedDbo;
11 12
 
12 13
 abstract class LuDataAccess {
13 14
 
@@ -72,6 +73,9 @@ abstract class LuDataAccess {
72 73
         if (is_callable($query)) {
73 74
             $query = $query(static::getTableAs());
74 75
         }
76
+        else if (is_null($query)) {
77
+            return static::getTableAs();
78
+        }
75 79
         return $query;
76 80
     }
77 81
 
@@ -89,6 +93,16 @@ abstract class LuDataAccess {
89 93
         $json = LuArrayUtils::camelCaseToSnake($json);
90 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 108
      * @param $query Builder|callable
@@ -96,12 +110,7 @@ abstract class LuDataAccess {
96 110
      */
97 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 115
         return LuDbo::deserializeValue($data, static::$_dboClass . "[]");
107 116
     }
@@ -112,18 +121,24 @@ abstract class LuDataAccess {
112 121
      */
113 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 126
         if (count($data) < 1) {
122 127
             return null;
123 128
         }
124 129
 
125 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 144
      * @param $id

+ 2
- 2
src/Utils/Dbo/LuPaginatedDbo.php Прегледај датотеку

@@ -34,7 +34,7 @@ class LuPaginatedDbo extends LuDbo {
34 34
     {
35 35
         return array(
36 36
             "Count" => $this->_count,
37
-            "Data" => $this->_data
37
+            "Data" => LuDbo::serializeValue($this->_data)
38 38
         );
39 39
     }
40 40
 
@@ -50,7 +50,7 @@ class LuPaginatedDbo extends LuDbo {
50 50
         foreach ($this->_data as $data) {
51 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 56
     public static function jsonDeserialize($json)

+ 20
- 3
tests/DatabaseTest.php Прегледај датотеку

@@ -215,15 +215,32 @@ class DatabaseTest extends \PHPUnit_Framework_TestCase
215 215
 
216 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 219
             LuDbo::serializeValue(TestTableDataAccess::getMultiple(function(Builder $q)
222 220
             {
223 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 244
     public function testDaGetSingleById()
228 245
     {
229 246
         $this->assertSame(["Id" => 1, "SomeText" => "lol2", "SomeIntegerArray" => [], "CreatedAt" => "2016-06-15 13:45:23"],

Loading…
Откажи
Сачувај