|
1
|
|
|
<?php |
|
2
|
|
|
namespace OCA\FaceRecognition\Controller; |
|
3
|
|
|
|
|
4
|
|
|
use OCP\IRequest; |
|
5
|
|
|
use OCP\Files\IRootFolder; |
|
6
|
|
|
use OCP\AppFramework\Http; |
|
7
|
|
|
use OCP\AppFramework\Http\DataResponse; |
|
8
|
|
|
use OCP\AppFramework\Http\JSONResponse; |
|
9
|
|
|
use OCP\AppFramework\Http\DataDisplayResponse; |
|
10
|
|
|
use OCP\AppFramework\Controller; |
|
11
|
|
|
|
|
12
|
|
|
use OCA\FaceRecognition\Db\Face; |
|
13
|
|
|
use OCA\FaceRecognition\Db\FaceMapper; |
|
14
|
|
|
|
|
15
|
|
|
use OCA\FaceRecognition\Db\Person; |
|
16
|
|
|
use OCA\FaceRecognition\Db\PersonMapper; |
|
17
|
|
|
|
|
18
|
|
|
class PersonController extends Controller { |
|
19
|
|
|
|
|
20
|
|
|
private $rootFolder; |
|
21
|
|
|
private $faceMapper; |
|
22
|
|
|
private $personMapper; |
|
23
|
|
|
private $userId; |
|
24
|
|
|
|
|
25
|
|
View Code Duplication |
public function __construct($AppName, IRequest $request, IRootFolder $rootFolder, FaceMapper $facemapper, PersonMapper $personmapper, $UserId) { |
|
|
|
|
|
|
26
|
|
|
parent::__construct($AppName, $request); |
|
27
|
|
|
$this->rootFolder = $rootFolder; |
|
28
|
|
|
$this->faceMapper = $facemapper; |
|
29
|
|
|
$this->personMapper = $personmapper; |
|
30
|
|
|
$this->userId = $UserId; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @NoAdminRequired |
|
35
|
|
|
*/ |
|
36
|
|
|
public function index() { |
|
37
|
|
|
$resp = array(); |
|
38
|
|
|
$groups = $this->faceMapper->getGroups($this->userId); |
|
39
|
|
|
foreach ($groups as $group) { |
|
40
|
|
|
$resp[$group->getName()] = $this->faceMapper->findAllNamed($this->userId, $group->getName(), 12); |
|
41
|
|
|
} |
|
42
|
|
|
return new DataResponse($resp); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @NoAdminRequired |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $name |
|
49
|
|
|
*/ |
|
50
|
|
|
public function getFaces($name) { |
|
51
|
|
|
$faces = $this->faceMapper->findAllNamed($this->userId, $name); |
|
52
|
|
|
return new DataResponse($faces); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @NoAdminRequired |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $oldName |
|
|
|
|
|
|
59
|
|
|
* @param string $newName |
|
60
|
|
|
*/ |
|
61
|
|
|
public function updateName($name, $newName) { |
|
62
|
|
|
$faces = $this->faceMapper->findAllNamed($this->userId, $name); |
|
63
|
|
|
foreach ($faces as $face) { |
|
64
|
|
|
$face->setName($newName); |
|
65
|
|
|
$this->faceMapper->update($face); |
|
66
|
|
|
} |
|
67
|
|
|
$newFaces = $this->faceMapper->findAllNamed($this->userId, $newName); |
|
68
|
|
|
return new DataResponse($newFaces); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @NoAdminRequired |
|
73
|
|
|
* |
|
74
|
|
|
* @param int $id |
|
75
|
|
|
* @param string $name |
|
76
|
|
|
*/ |
|
77
|
|
|
public function updateNameV2($id, $name) { |
|
78
|
|
|
$person = $this->personMapper->find ($this->userId, $id); |
|
79
|
|
|
$person->setName($name); |
|
80
|
|
|
$this->personMapper->update($person); |
|
81
|
|
|
$newPerson = $this->personMapper->find($this->userId, $id); |
|
82
|
|
|
return new DataResponse($newPerson); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
|
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.