1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ownCloud - maps |
4
|
|
|
* |
5
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
6
|
|
|
* later. See the COPYING file. |
7
|
|
|
* |
8
|
|
|
* @author Vinzenz Rosenkranz <[email protected]> |
9
|
|
|
* @copyright Vinzenz Rosenkranz 2015 |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace OCA\Maps\Controller; |
13
|
|
|
|
14
|
|
|
use OCA\Maps\Db\Favorite; |
15
|
|
|
use OCA\Maps\Db\FavoriteMapper; |
16
|
|
|
use \OCP\IRequest; |
17
|
|
|
use \OCP\AppFramework\Http\JSONResponse; |
18
|
|
|
use \OCP\AppFramework\ApiController; |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
class FavoriteController extends ApiController { |
22
|
|
|
|
23
|
|
|
private $userId; |
24
|
|
|
private $favoriteMapper; |
25
|
|
|
|
26
|
|
|
public function __construct($appName, IRequest $request, FavoriteMapper $favoriteMapper, $userId) { |
27
|
|
|
parent::__construct($appName, $request); |
28
|
|
|
$this->favoriteMapper = $favoriteMapper; |
29
|
|
|
$this->userId = $userId; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @NoAdminRequired |
34
|
|
|
* |
35
|
|
|
* @param $name string |
36
|
|
|
* @param $id int |
37
|
|
|
* @return JSONResponse |
38
|
|
|
*/ |
39
|
|
View Code Duplication |
public function updateFavorite($name, $id) { |
|
|
|
|
40
|
|
|
|
41
|
|
|
$favorite = new Favorite(); |
42
|
|
|
$favorite->setId($id); |
43
|
|
|
$favorite->setName($name); |
44
|
|
|
$favorite->setTimestamp(time()); |
45
|
|
|
|
46
|
|
|
/* Only save favorite if it exists in db */ |
47
|
|
|
try { |
48
|
|
|
$this->favoriteMapper->find($id); |
49
|
|
|
return new JSONResponse($this->favoriteMapper->update($favorite)); |
50
|
|
|
} catch(\OCP\AppFramework\Db\DoesNotExistException $e) { |
|
|
|
|
51
|
|
|
return new JSONResponse([ |
52
|
|
|
'error' => $e->getMessage() |
53
|
|
|
]); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @NoAdminRequired |
59
|
|
|
* |
60
|
|
|
* @param $lat float |
61
|
|
|
* @param $lng float |
62
|
|
|
* @return JSONResponse |
63
|
|
|
*/ |
64
|
|
|
public function addFavorite($lat, $lng, $name = null){ |
65
|
|
|
$favorite = new Favorite(); |
66
|
|
|
$favorite->setName($name); |
67
|
|
|
$favorite->setTimestamp(time()); |
68
|
|
|
$favorite->setUserId($this->userId); |
69
|
|
|
$favorite->setLat($lat); |
70
|
|
|
$favorite->setLng($lng); |
71
|
|
|
|
72
|
|
|
/* @var $favorite Favorite */ |
73
|
|
|
$favorite = $this->favoriteMapper->insert($favorite); |
74
|
|
|
|
75
|
|
|
$response = array('id'=> $favorite->getId()); |
76
|
|
|
return new JSONResponse($response); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @NoAdminRequired |
81
|
|
|
* |
82
|
|
|
* @return JSONResponse |
83
|
|
|
*/ |
84
|
|
|
public function getFavorites(){ |
85
|
|
|
$favorites = $this->favoriteMapper->findAll(); |
86
|
|
|
return new JSONResponse($favorites); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @NoAdminRequired |
91
|
|
|
* |
92
|
|
|
* @param $name string |
93
|
|
|
* |
94
|
|
|
* @return JSONResponse |
95
|
|
|
*/ |
96
|
|
|
public function getFavoritesByName($name){ |
97
|
|
|
$favorites = $this->favoriteMapper->findByName($name); |
98
|
|
|
return new JSONResponse($favorites); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @NoAdminRequired |
103
|
|
|
* |
104
|
|
|
* @param $id int |
105
|
|
|
* @return JSONResponse |
106
|
|
|
*/ |
107
|
|
|
public function removeFavorite($id){ |
108
|
|
|
$favorite = $this->favoriteMapper->find($id); |
109
|
|
|
if($favorite->userId == $this->userId) { |
110
|
|
|
$this->favoriteMapper->delete($favorite); |
111
|
|
|
} |
112
|
|
|
return new JSONResponse(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
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.