Completed
Push — master ( dac55d...ca0743 )
by Jan-Christoph
07:35
created

FavoriteController::getFavorites()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 3
nc 1
nop 0
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 $lat int
36
	 * @param $lng int
37
	 * @param $timestamp string
38
	 * @param $name string
39
	 * @param $userId int
40
	 * @param $id int
41
	 * @return JSONResponse
42
	 */
43
	public function update($lat, $lng, $timestamp, $name, $userId, $id) {
44
45
		$favorite = new Favorite();
46
		$favorite->setLat($lat);
47
		$favorite->setLng($lng);
48
		if((string)(float)$timestamp === $timestamp) {
49
			if(strtotime(date('d-m-Y H:i:s',$timestamp)) === (int)$timestamp) {
50
				$favorite->setTimestamp((int)$timestamp);
51
			} elseif(strtotime(date('d-m-Y H:i:s',$timestamp/1000)) === (int)floor($timestamp/1000)) {
52
				$favorite->setTimestamp((int)floor($timestamp/1000));
53
			}
54
		} else {
55
			$favorite->timestamp = strtotime($timestamp);
56
		}
57
		$favorite->setName($name);
58
		$favorite->setUserId($userId);
59
		$favorite->setId($id);
60
61
		/* Only save favorite if it exists in db */
62
		try {
63
			$this->favoriteMapper->find($id);
64
			return new JSONResponse($this->favoriteMapper->insert($favorite));
65
		} catch(\OCP\AppFramework\Db\DoesNotExistException $e) {
0 ignored issues
show
Bug introduced by
The class OCP\AppFramework\Db\DoesNotExistException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
66
			return new JSONResponse([
67
				'error' => $e->getMessage()
68
			]);
69
		}
70
	}
71
72
	/**
73
	 * @NoAdminRequired
74
	 *
75
	 * @param $lat float
76
	 * @param $lng float
77
	 * @return JSONResponse
78
	 */
79
	public function addFavorite($lat, $lng){
80
		$favorite = new Favorite();
81
		$favorite->setName("empty");
82
		$favorite->setTimestamp(time());
83
		$favorite->setUserId($this->userId);
84
		$favorite->setLat($lat);
85
		$favorite->setLng($lng);
86
87
		/* @var $favorite Favorite */
88
		$favorite = $this->favoriteMapper->insert($favorite);
89
90
		$response = array('id'=> $favorite->getId());
91
		return new JSONResponse($response);
92
	}
93
94
	/**
95
	 * @NoAdminRequired
96
	 *
97
	 * @return JSONResponse
98
	 */
99
	public function getFavorites(){
100
		$favorites = $this->favoriteMapper->findAll();
101
		return new JSONResponse($favorites);
102
	}
103
104
	/**
105
	 * @NoAdminRequired
106
	 *
107
	 * @param $id int
108
	 * @return JSONResponse
109
	 */
110
	public function removeFavorite($id){
111
		$favorite = $this->favoriteMapper->find($id);
112
		if($favorite->userId == $this->userId) {
113
			$this->favoriteMapper->delete($favorite);
114
		}
115
		return new JSONResponse();
116
	}
117
118
}
119