Completed
Pull Request — master (#218)
by Eli
05:41
created

PublicController::returnAddAsJson()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 18
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 18
loc 18
ccs 0
cts 11
cp 0
rs 9.4286
cc 3
eloc 10
nc 4
nop 5
crap 12
1
<?php
2
3
namespace OCA\Bookmarks\Controller\Rest;
4
5
use \OCP\AppFramework\ApiController;
6
use \OCP\IRequest;
7
use \OCP\IDb;
8
use \OCP\AppFramework\Http\JSONResponse;
9
use \OC\User\Manager;
10
use OCA\Bookmarks\Controller\Lib\Bookmarks;
11
12
class PublicController extends ApiController {
13
14
	private $db;
15
	private $userManager;
16
17 3
	public function __construct($appName, IRequest $request, IDb $db, Manager $userManager) {
18 3
		parent::__construct(
19 3
				$appName, $request);
20
21 3
		$this->db = $db;
22 3
		$this->userManager = $userManager;
23 3
	}
24
25
    /**
26
	 * @CORS
27
	 * @NoAdminRequired
28
	 * @NoCSRFRequired
29
	 */
30 3
	public function returnAsJson($user, $tags = array(), $conjunction = "or", $select = null, $sortby = "") {
31
32 3
		if ($tags != null && $tags[0] == "") {
33
			$tags = array();
34
		}
35
36 3
		$attributesToSelect = array('url', 'title');
37
38 3
		if ($select != null) {
39
			$attributesToSelect = array_merge($attributesToSelect, $select);
40
			$attributesToSelect = array_unique($attributesToSelect);
41
		}
42
43 3
		$output = Bookmarks::findBookmarks($user, $this->db, 0, $sortby, $tags, true, -1, false, $attributesToSelect, $conjunction);
44
45 3
		if (count($output) == 0) {
46 2
			$output["status"] = 'error';
47 2
			$output["message"] = "No results from this query";
48 2
			return new JSONResponse($output);
49
		}
50
51 1
		return new JSONResponse($output);
52
	}
53
54
55
    /**
56
     * @CORS
57
     * @NoAdminRequired
58
     * @NoCSRFRequired
59
     */
60 View Code Duplication
    public function returnAddAsJson($user, $url = "", $tags = array(), $title = "", $description = "") {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
61
62
        if ($tags[0] == "") {
63
            $tags = array();
64
        }
65
66
        $output = Bookmarks::addBookmark($user, $this->db, $url, $title, $tags, $description, false);
67
68
        if (count($output) == 0) {
69
            $output["status"] = 'error';
70
            $output["message"] = "No results from this query";
71
            return new JSONResponse($output);
72
        }
73
74
        $output = Bookmarks::findUniqueBookmark($output,$user,$this->db);
75
76
        return new JSONResponse($output);
77
    }
78
79
    /**
80
     * @CORS
81
     * @NoAdminRequired
82
     * @NoCSRFRequired
83
     */
84 View Code Duplication
    public function returnUpdateAsJson($user, $id, $url = "", $tags = array(), $title = "", $description = "") {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
86
        if ($tags[0] == "") {
87
            $tags = array();
88
        }
89
90
        $output = Bookmarks::editBookmark($user, $this->db, $id, $url, $title, $tags, $description, false);
91
92
        if (count($output) == 0) {
93
            $output["status"] = 'error';
94
            $output["message"] = "No results from this query";
95
            return new JSONResponse($output);
96
        }
97
98
        $output = Bookmarks::findUniqueBookmark($output,$user,$this->db);
99
100
        return new JSONResponse($output);
101
    }
102
103
    /**
104
     * @CORS
105
     * @NoAdminRequired
106
     * @NoCSRFRequired
107
     */
108
    public function returnDeleteAsJson($user, $id) {
109
        $output = Bookmarks::deleteUrl($user, $this->db, $id);
110
111
        if (!$output) {
112
            $output = array();
113
            $output["status"] = 'error';
114
            $output["message"] = "Cannot delete bookmark";
115
            return new JSONResponse($output);
116
        }else{
117
            $output = array();
118
            $output["status"] = 'success';
119
            $output["message"] = "Bookmark deleted";
120
            return new JSONResponse($output);
121
        }
122
    }
123
124
    /**
125
     * @CORS
126
     * @NoAdminRequired
127
     * @NoCSRFRequired
128
     */
129
    public function returnClickBookmarkAsJson($user, $url) {
130
131
        // Check if it is a valid URL
132
        if (filter_var($url, FILTER_VALIDATE_URL) === FALSE) {
133
            return new JSONResponse(array(), Http::STATUS_BAD_REQUEST);
134
        }
135
136
        $query = $this->db->prepareQuery('
137
            UPDATE `*PREFIX*bookmarks`
138
            SET `clickcount` = `clickcount` + 1
139
            WHERE `user_id` = ?
140
                AND `url` LIKE ?
141
            ');
142
143
        $params = array($user, htmlspecialchars_decode($url));
144
        $query->execute($params);
145
146
        $output = array();
147
        $output["status"] = "success";
148
        return new JSONResponse($output);
149
    }
150
151
	public function newJsonErrorMessage($message) {
152
		$output = array();
153
		$output["status"] = 'error';
154
		$output["message"] = $message;
155
		return new JSONResponse($output);
156
	}
157
158
}
159