Browse Source

get all; delete

develop
Robin Thoni 8 years ago
parent
commit
0365293c94

+ 31
- 0
app/Http/Business/ImagesBusiness.php View File

3
 namespace App\Http\Business;
3
 namespace App\Http\Business;
4
 
4
 
5
 use App\Http\DBO\ImageUploadDbo;
5
 use App\Http\DBO\ImageUploadDbo;
6
+use Carbon\Carbon;
6
 use Exception;
7
 use Exception;
7
 use Luticate\Auth\DBO\LuticateUsersDbo;
8
 use Luticate\Auth\DBO\LuticateUsersDbo;
8
 use Luticate\Utils\LuBusiness;
9
 use Luticate\Utils\LuBusiness;
9
 use App\Http\DataAccess\ImagesDataAccess;
10
 use App\Http\DataAccess\ImagesDataAccess;
10
 use App\Http\DBO\ImagesDbo;
11
 use App\Http\DBO\ImagesDbo;
12
+use Luticate\Utils\LuLog;
11
 
13
 
12
 class ImagesBusiness extends LuBusiness {
14
 class ImagesBusiness extends LuBusiness {
13
     
15
     
26
         "-" . time() . "." . self::IMAGES_FORMAT;
28
         "-" . time() . "." . self::IMAGES_FORMAT;
27
     }
29
     }
28
 
30
 
31
+    public static function getAllLite($only_mine, LuticateUsersDbo $_user, $page = 0, $perPage = 20000000)
32
+    {
33
+        return ImagesDataAccess::getAllLite($only_mine, $_user, $page, $perPage);
34
+    }
35
+
36
+    public static function getRawById($image_id)
37
+    {
38
+        /**
39
+         * @var $dbo ImagesDbo
40
+         */
41
+        $dbo = self::getById($image_id);
42
+        echo file_get_contents($dbo->getPath());
43
+        exit;
44
+    }
45
+
29
     public static function upload(ImageUploadDbo $image, LuticateUsersDbo $_user)
46
     public static function upload(ImageUploadDbo $image, LuticateUsersDbo $_user)
30
     {
47
     {
31
         $path = self::generateImagePath($image, $_user);
48
         $path = self::generateImagePath($image, $_user);
33
         $dbo = new ImagesDbo();
50
         $dbo = new ImagesDbo();
34
         $dbo->setUserId($_user->getId());
51
         $dbo->setUserId($_user->getId());
35
         $dbo->setPath($path);
52
         $dbo->setPath($path);
53
+        $dbo->setDate(Carbon::now());
36
 
54
 
37
         $image->getImage()->scaleImage(420, 420, true);
55
         $image->getImage()->scaleImage(420, 420, true);
38
         $image->getImage()->setImageFormat(self::IMAGES_FORMAT);
56
         $image->getImage()->setImageFormat(self::IMAGES_FORMAT);
43
         }
61
         }
44
         catch (Exception $e)
62
         catch (Exception $e)
45
         {
63
         {
64
+            LuLog::log($e);
46
             try
65
             try
47
             {
66
             {
48
                 unlink($path);
67
                 unlink($path);
58
 
77
 
59
     public static function del($image_id, LuticateUsersDbo $_user)
78
     public static function del($image_id, LuticateUsersDbo $_user)
60
     {
79
     {
80
+        /**
81
+         * @var $imageDbo ImagesDbo
82
+         */
61
         $imageDbo = self::getById($image_id);
83
         $imageDbo = self::getById($image_id);
62
         if ($imageDbo->getUserId() != $_user->getId()) {
84
         if ($imageDbo->getUserId() != $_user->getId()) {
63
             self::unauthorized("You do not own this image");
85
             self::unauthorized("You do not own this image");
64
         }
86
         }
87
+        self::deleteById($imageDbo->getId());
88
+        try
89
+        {
90
+            unlink($imageDbo->getPath());
91
+        }
92
+        catch (Exception $e)
93
+        {
94
+            LuLog::log($e);
95
+        }
65
         return true;
96
         return true;
66
     }
97
     }
67
 }
98
 }

+ 10
- 0
app/Http/Controller/ImagesController.php View File

15
         return new ImagesBusiness();
15
         return new ImagesBusiness();
16
     }
16
     }
17
 
17
 
18
+    public function getAll($only_mine, LuticateUsersDbo $_user, $page = 0, $perPage = 20000000)
19
+    {
20
+        return ImagesBusiness::getAllLite($only_mine == "true", $_user, $page, $perPage);
21
+    }
22
+
23
+    public function getRawById($image_id)
24
+    {
25
+        return ImagesBusiness::getRawById($image_id);
26
+    }
27
+
18
     public function upload(ImageUploadDbo $image, LuticateUsersDbo $_user)
28
     public function upload(ImageUploadDbo $image, LuticateUsersDbo $_user)
19
     {
29
     {
20
         return ImagesBusiness::upload($image, $_user);
30
         return ImagesBusiness::upload($image, $_user);

+ 37
- 1
app/Http/DBO/ImagesDbo.php View File

16
         return array(
16
         return array(
17
             "Id" => $this->_id,
17
             "Id" => $this->_id,
18
             "UserId" => $this->_userId,
18
             "UserId" => $this->_userId,
19
-            "Path" => $this->_path
19
+            "Path" => $this->_path,
20
+            "Date" => $this->_date,
21
+            "Name" => $this->_name
20
         );
22
         );
21
     }
23
     }
22
 
24
 
32
         if (isset($json["Path"])) {
34
         if (isset($json["Path"])) {
33
             $dbo->setPath($json["Path"]);
35
             $dbo->setPath($json["Path"]);
34
         }
36
         }
37
+        if (isset($json["Date"])) {
38
+            $dbo->setDate($json["Date"]);
39
+        }
40
+        if (isset($json["Name"])) {
41
+            $dbo->setName($json["Name"]);
42
+        }
35
         return $dbo;
43
         return $dbo;
36
     }
44
     }
37
 
45
 
41
         $dbo->setId(42);
49
         $dbo->setId(42);
42
         $dbo->setUserId(42);
50
         $dbo->setUserId(42);
43
         $dbo->setPath("sample string");
51
         $dbo->setPath("sample string");
52
+        $dbo->setDate("sample string");
53
+        $dbo->setName("sample string");
44
         return $dbo;
54
         return $dbo;
45
     }
55
     }
46
 
56
 
82
     {
92
     {
83
         $this->_path = $value;
93
         $this->_path = $value;
84
     }
94
     }
95
+
96
+    /**
97
+     * @var timestamp without time zone
98
+     */
99
+    protected $_date;
100
+    public function getDate()
101
+    {
102
+        return $this->_date;
103
+    }
104
+    public function setDate($value)
105
+    {
106
+        $this->_date = $value;
107
+    }
108
+
109
+    /**
110
+     * @var text
111
+     */
112
+    protected $_name;
113
+    public function getName()
114
+    {
115
+        return $this->_name;
116
+    }
117
+    public function setName($value)
118
+    {
119
+        $this->_name = $value;
120
+    }
85
 }
121
 }

+ 7
- 0
app/Http/DataAccess/ImagesDataAccess.php View File

2
 
2
 
3
 namespace App\Http\DataAccess;
3
 namespace App\Http\DataAccess;
4
 
4
 
5
+use App\Http\DataAccess\SP\SpGetImages;
6
+use Luticate\Auth\DBO\LuticateUsersDbo;
5
 use Luticate\Utils\LuDataAccess;
7
 use Luticate\Utils\LuDataAccess;
6
 use App\Http\DataAccess\Models\Images;
8
 use App\Http\DataAccess\Models\Images;
7
 use App\Http\DBO\ImagesDbo;
9
 use App\Http\DBO\ImagesDbo;
11
     {
13
     {
12
         return new Images();
14
         return new Images();
13
     }
15
     }
16
+
17
+    public static function getAllLite($only_mine, LuticateUsersDbo $_user, $page = 0, $perPage = 20000000)
18
+    {
19
+        return SpGetImages::getMultipleJson($only_mine, $_user->getId(), $page, $perPage);
20
+    }
14
 }
21
 }

+ 4
- 0
app/Http/DataAccess/Models/ImagesModel.php View File

28
         $dbo->setId($this->id);
28
         $dbo->setId($this->id);
29
         $dbo->setUserId($this->user_id);
29
         $dbo->setUserId($this->user_id);
30
         $dbo->setPath($this->path);
30
         $dbo->setPath($this->path);
31
+        $dbo->setDate($this->date);
32
+        $dbo->setName($this->name);
31
 
33
 
32
         return $dbo;
34
         return $dbo;
33
     }
35
     }
45
         $model->id = $dbo->getId();
47
         $model->id = $dbo->getId();
46
         $model->user_id = $dbo->getUserId();
48
         $model->user_id = $dbo->getUserId();
47
         $model->path = $dbo->getPath();
49
         $model->path = $dbo->getPath();
50
+        $model->date = $dbo->getDate();
51
+        $model->name = $dbo->getName();
48
 
52
 
49
         return $model;
53
         return $model;
50
     }
54
     }

+ 160
- 0
app/Http/DataAccess/SP/SpGetImages.php View File

1
+<?php
2
+
3
+/**
4
+ * AUTO GENERATED BY LUTICATE GENERATOR
5
+ * ANY CHANGES WILL BE OVERWRITTEN
6
+ */
7
+
8
+namespace App\Http\DataAccess\SP;
9
+
10
+use Luticate\Utils\LuSpDbo;
11
+use Luticate\Utils\LuMultipleDbo;
12
+use Luticate\Utils\LuStringUtils;
13
+use Illuminate\Support\Facades\DB;
14
+
15
+class SpGetImages extends LuSpDbo {
16
+
17
+    /**
18
+    * @param $dam
19
+    * @return \App\Http\DataAccess\SP\SpGetImages|null
20
+    */
21
+    protected static function damToDbo($dam)
22
+    {
23
+        if (is_null($dam))
24
+            return null;
25
+        $dbo = new SpGetImages();
26
+
27
+        $dbo->setId(LuStringUtils::convertJsonString($dam->id));
28
+        $dbo->setName(LuStringUtils::convertJsonString($dam->name));
29
+        $dbo->setUserId(LuStringUtils::convertJsonString($dam->user_id));
30
+        $dbo->setDate(LuStringUtils::convertJsonString($dam->date));
31
+
32
+        return $dbo;
33
+    }
34
+
35
+
36
+    /**
37
+    * @param $_only_mine boolean
38
+    * @param $_user_id_me integer
39
+    * @return \App\Http\DataAccess\SP\SpGetImages[];
40
+    */
41
+    public static function execute($_only_mine, $_user_id_me)
42
+    {
43
+        $values = DB::select('SELECT to_json(data.id) AS id, to_json(data.name) AS name, to_json(data.user_id) AS user_id, to_json(data.date) AS date FROM sp_get_images(:_only_mine, :_user_id_me) data', array(":_only_mine" => $_only_mine, ":_user_id_me" => $_user_id_me));
44
+        $dboValues = array();
45
+        foreach ($values as $value)
46
+            $dboValues[] = self::damToDbo($value);
47
+        return $dboValues;
48
+    }
49
+
50
+    /**
51
+    * @param $_only_mine boolean
52
+    * @param $_user_id_me integer
53
+    * @param $page int The page number, 0 based
54
+    * @param $perPage int The number of items per page
55
+    * @return \Luticate\Utils\LuMultipleDbo;
56
+    */
57
+    public static function getMultipleJson($_only_mine, $_user_id_me, $page, $perPage)
58
+    {
59
+        $values = DB::select('SELECT (SELECT count(*) FROM sp_get_images(:_only_mine, :_user_id_me)) as count, (SELECT json_agg(q) FROM (SELECT * FROM sp_get_images(:_only_mine, :_user_id_me) OFFSET (:page::int * :perPage::int) LIMIT :perPage) q) as data',
60
+            array(":_only_mine" => $_only_mine, ":_user_id_me" => $_user_id_me, ":page" => $page, ":perPage" => $perPage));
61
+        $value = $values[0];
62
+        if (is_null($value->data))
63
+        {
64
+            $value->data = '[]';
65
+        }
66
+        $data = LuStringUtils::convertJsonString($value->data);
67
+        return new LuMultipleDbo($value->count, $data);
68
+    }
69
+
70
+    public function jsonSerialize()
71
+    {
72
+        return array(
73
+            "Id" => $this->_id,
74
+            "Name" => $this->_name,
75
+            "UserId" => $this->_userId,
76
+            "Date" => $this->_date
77
+        );
78
+    }
79
+
80
+    public static function jsonDeserialize($json)
81
+    {
82
+        $dbo = new SpGetImages();
83
+        if (isset($json["Id"])) {
84
+            $dbo->setId($json["Id"]);
85
+        }
86
+        if (isset($json["Name"])) {
87
+            $dbo->setName($json["Name"]);
88
+        }
89
+        if (isset($json["UserId"])) {
90
+            $dbo->setUserId($json["UserId"]);
91
+        }
92
+        if (isset($json["Date"])) {
93
+            $dbo->setDate($json["Date"]);
94
+        }
95
+        return $dbo;
96
+    }
97
+
98
+    public static function generateSample()
99
+    {
100
+        $dbo = new SpGetImages();
101
+        $dbo->setId(42);
102
+        $dbo->setName("sample string");
103
+        $dbo->setUserId(42);
104
+        $dbo->setDate("sample string");
105
+        return $dbo;
106
+    }
107
+
108
+
109
+    /**
110
+    * @var integer
111
+    */
112
+    protected $_id;
113
+    public function getId()
114
+    {
115
+        return $this->_id;
116
+    }
117
+    public function setId($value)
118
+    {
119
+        $this->_id = $value;
120
+    }
121
+
122
+    /**
123
+    * @var text
124
+    */
125
+    protected $_name;
126
+    public function getName()
127
+    {
128
+        return $this->_name;
129
+    }
130
+    public function setName($value)
131
+    {
132
+        $this->_name = $value;
133
+    }
134
+
135
+    /**
136
+    * @var integer
137
+    */
138
+    protected $_userId;
139
+    public function getUserId()
140
+    {
141
+        return $this->_userId;
142
+    }
143
+    public function setUserId($value)
144
+    {
145
+        $this->_userId = $value;
146
+    }
147
+
148
+    /**
149
+    * @var timestamp without time zone
150
+    */
151
+    protected $_date;
152
+    public function getDate()
153
+    {
154
+        return $this->_date;
155
+    }
156
+    public function setDate($value)
157
+    {
158
+        $this->_date = $value;
159
+    }
160
+}

+ 4
- 0
app/Http/DataAccess/SP/src/sp_get_images.sql View File

1
+
2
+        SELECT i.id, i.name, i.user_id, i.date
3
+        FROM images i
4
+        WHERE (CASE WHEN _only_mine THEN i.user_id = _user_id_me ELSE TRUE END)

+ 3
- 2
app/Http/routes.php View File

17
 
17
 
18
 $route->post("/api/effects/{effect}/apply", "Effects", "apply", Permissions::UPLOAD);
18
 $route->post("/api/effects/{effect}/apply", "Effects", "apply", Permissions::UPLOAD);
19
 
19
 
20
+$route->get("/api/images", "Images", "getAll");
21
+$route->get("/api/images/$image_id", "Images", "getRawById");
20
 $route->post("/api/images/upload", "Images", "upload", Permissions::UPLOAD);
22
 $route->post("/api/images/upload", "Images", "upload", Permissions::UPLOAD);
21
-
22
 $route->post("/api/images/$image_id/del", "Images", "del", Permissions::UPLOAD);
23
 $route->post("/api/images/$image_id/del", "Images", "del", Permissions::UPLOAD);
23
 
24
 
24
-//sleep(2);
25
+sleep(2);

+ 12
- 8
public/app/app.js View File

26
                 }]
26
                 }]
27
             });
27
             });
28
 
28
 
29
-            $stateProvider.state('home', {
29
+            $stateProvider.state('images', {
30
                 url:'/',
30
                 url:'/',
31
                 parent: 'root',
31
                 parent: 'root',
32
-                title: "Home",
33
-                reloadOnSearch: false,
34
-                templateUrl:'views/home.html',
35
-                controller:'HomeController'
32
+                title: "Images",
33
+                templateUrl:'views/images.html',
34
+                controller:'ImagesController'
36
             });
35
             });
37
 
36
 
38
             $stateProvider.state('login', {
37
             $stateProvider.state('login', {
39
                 url:'/login',
38
                 url:'/login',
40
                 parent: 'root',
39
                 parent: 'root',
41
                 title: "Login",
40
                 title: "Login",
42
-                reloadOnSearch: false,
43
                 templateUrl:'views/login.html',
41
                 templateUrl:'views/login.html',
44
                 controller:'LoginController'
42
                 controller:'LoginController'
45
             });
43
             });
48
                 url:'/signUp',
46
                 url:'/signUp',
49
                 parent: 'root',
47
                 parent: 'root',
50
                 title: "Sign Up",
48
                 title: "Sign Up",
51
-                reloadOnSearch: false,
52
                 templateUrl:'views/signup.html',
49
                 templateUrl:'views/signup.html',
53
                 controller:'SignUpController'
50
                 controller:'SignUpController'
54
             });
51
             });
57
                 url:'/upload',
54
                 url:'/upload',
58
                 parent: 'root',
55
                 parent: 'root',
59
                 title: "Upload",
56
                 title: "Upload",
60
-                reloadOnSearch: false,
61
                 templateUrl:'views/upload.html',
57
                 templateUrl:'views/upload.html',
62
                 controller:'UploadController'
58
                 controller:'UploadController'
63
             });
59
             });
64
 
60
 
61
+            /*$stateProvider.state('images', {
62
+                url:'/images',
63
+                parent: 'root',
64
+                title: "Images",
65
+                templateUrl:'views/images.html',
66
+                controller:'ImagesController'
67
+            });*/
68
+
65
             $urlRouterProvider.otherwise('/');
69
             $urlRouterProvider.otherwise('/');
66
 
70
 
67
             $httpProvider.interceptors.push(['luticateAuthCache', '$injector', '$q',
71
             $httpProvider.interceptors.push(['luticateAuthCache', '$injector', '$q',

+ 0
- 4
public/app/controllers/home.controller.js View File

1
-angular.module('app')
2
-    .controller('HomeController', ['$scope', '$state', '$mdDialog',
3
-        function($scope, $state, $mdDialog) {
4
-    }]);

+ 36
- 0
public/app/controllers/images.controller.js View File

1
+angular.module('app')
2
+    .controller('ImagesController', ['$scope', '$state', '$mdDialog', 'ImagesBusiness', 'luticateAuthCache', 'errorDialogMd',
3
+        function($scope, $state, $mdDialog, ImagesBusiness, luticateAuthCache, errorDialogMd) {
4
+
5
+            $scope.luticateAuthCache = luticateAuthCache;
6
+
7
+            var promiseLoad = {
8
+                id: "promiseLoad",
9
+                loaderGroups: ["images"]
10
+            };
11
+            
12
+            $scope.options = {
13
+                page: 0,
14
+                perPage: 30,
15
+                only_mine: luticateAuthCache.getUser() != null
16
+            };
17
+
18
+            $scope.images = [];
19
+
20
+            $scope.reload = function()
21
+            {
22
+                ImagesBusiness.getAll($scope.options, promiseLoad).then(function(data)
23
+                {
24
+                    $scope.images = data;
25
+                }, errorDialogMd.errorDialog);
26
+            };
27
+
28
+            $scope.delete = function(image_id)
29
+            {
30
+                ImagesBusiness.delete({image_id: image_id}, promiseLoad).then(function (data) {
31
+                    $scope.reload();
32
+                }, errorDialogMd.errorDialog);
33
+            };
34
+
35
+            $scope.reload();
36
+    }]);

+ 1
- 1
public/app/controllers/login.controller.js View File

20
                     password: $scope.user.Password
20
                     password: $scope.user.Password
21
                 }).then(function(data)
21
                 }).then(function(data)
22
                 {
22
                 {
23
-                    $state.go("home");
23
+                    $state.go("images");
24
                 }, function (error) {
24
                 }, function (error) {
25
                     $scope.error = error;
25
                     $scope.error = error;
26
                 });
26
                 });

+ 1
- 1
public/app/controllers/toolbar.controller.js View File

22
                 $scope.logout = function () {
22
                 $scope.logout = function () {
23
                         luticateAuthUsers.logout().then(function(data)
23
                         luticateAuthUsers.logout().then(function(data)
24
                         {
24
                         {
25
-                                $state.go("home");
25
+                                $state.go("images");
26
                         });
26
                         });
27
                 }
27
                 }
28
         }]);
28
         }]);

+ 1
- 1
public/app/index.html View File

49
     <!-- Controller -->
49
     <!-- Controller -->
50
     <script src="controllers/sidebar.controller.js"></script>
50
     <script src="controllers/sidebar.controller.js"></script>
51
     <script src="controllers/toolbar.controller.js"></script>
51
     <script src="controllers/toolbar.controller.js"></script>
52
-    <script src="controllers/home.controller.js"></script>
53
     <script src="controllers/login.controller.js"></script>
52
     <script src="controllers/login.controller.js"></script>
54
     <script src="controllers/signup.controller.js"></script>
53
     <script src="controllers/signup.controller.js"></script>
55
     <script src="controllers/upload.controller.js"></script>
54
     <script src="controllers/upload.controller.js"></script>
55
+    <script src="controllers/images.controller.js"></script>
56
 
56
 
57
     <!-- Modal Controller -->
57
     <!-- Modal Controller -->
58
 
58
 

+ 0
- 5
public/app/views/home.html View File

1
-<div layout="column" layout-fill layout-align="top center">
2
-    <p>
3
-        Welcome to <span class="strike">500px</span> 420px!
4
-    </p>
5
-</div>

+ 14
- 0
public/app/views/images.html View File

1
+<div layout="column" layout-fill layout-align="top center">
2
+    <div class="col-xs-12" lu-busy="images">
3
+        <div class="col-xs-12">
4
+            <md-checkbox ng-model="options.only_mine" ng-show="luticateAuthCache.getUser() != null" ng-change="reload()">
5
+                Only my images
6
+            </md-checkbox>
7
+        </div>
8
+        <div ng-repeat="image in images.Data" class="col-xs-3">
9
+            <img ng-src="/api/images/{{image.Id}}" />
10
+            <p>{{image.Name}}</p>
11
+            <button class="btn btn-danger" ng-click="delete(image.Id)" ng-show="image.UserId == luticateAuthCache.getUser().Id">Delete</button>
12
+        </div>
13
+    </div>
14
+</div>

+ 1
- 1
public/app/views/sidebar.html View File

1
 <md-sidenav layout="column" class="md-sidenav-left md-whiteframe-z2" md-component-id="left" md-is-locked-open="$mdMedia('gt-md')">
1
 <md-sidenav layout="column" class="md-sidenav-left md-whiteframe-z2" md-component-id="left" md-is-locked-open="$mdMedia('gt-md')">
2
     <md-toolbar class="md-theme-indigo">
2
     <md-toolbar class="md-theme-indigo">
3
-        <h1 class="md-toolbar-tools"><a ui-sref="home">420px</a></h1>
3
+        <h1 class="md-toolbar-tools"><a ui-sref="images">420px</a></h1>
4
     </md-toolbar>
4
     </md-toolbar>
5
     <md-content layout-padding ng-controller="SideBarController">
5
     <md-content layout-padding ng-controller="SideBarController">
6
         <div lu-busy="sidebar">
6
         <div lu-busy="sidebar">

+ 2
- 0
public/sdk/Business/images.js View File

9
 
9
 
10
         Business.getAll = ImagesDataAccess.getAll;
10
         Business.getAll = ImagesDataAccess.getAll;
11
 
11
 
12
+        Business.delete = ImagesDataAccess.delete;
13
+
12
         Business.upload = function(data, promise)
14
         Business.upload = function(data, promise)
13
         {
15
         {
14
             data.image = btoa(data.image);
16
             data.image = btoa(data.image);

+ 5
- 0
public/sdk/DataAccess/images.js View File

14
             return luticateRequest.get(entry_point, data, promise);
14
             return luticateRequest.get(entry_point, data, promise);
15
         };
15
         };
16
 
16
 
17
+        DataAccess.delete = function(data, promise)
18
+        {
19
+            return luticateRequest.post(entry_point + data.image_id + "/del", null, null, promise);
20
+        };
21
+
17
         DataAccess.upload = function(data, promise)
22
         DataAccess.upload = function(data, promise)
18
         {
23
         {
19
             return luticateRequest.post(entry_point + "upload", {image: JSON.stringify(data)}, null, promise);
24
             return luticateRequest.post(entry_point + "upload", {image: JSON.stringify(data)}, null, promise);

Loading…
Cancel
Save