1
0

WeatherStatusController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020, Julien Veyssier
  5. *
  6. * @author Julien Veyssier <eneiluj@posteo.net>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\WeatherStatus\Controller;
  25. use OCA\WeatherStatus\ResponseDefinitions;
  26. use OCA\WeatherStatus\Service\WeatherStatusService;
  27. use OCP\AppFramework\Http;
  28. use OCP\AppFramework\Http\DataResponse;
  29. use OCP\AppFramework\OCSController;
  30. use OCP\IRequest;
  31. /**
  32. * @psalm-import-type WeatherStatusForecast from ResponseDefinitions
  33. */
  34. class WeatherStatusController extends OCSController {
  35. public function __construct(
  36. string $appName,
  37. IRequest $request,
  38. private WeatherStatusService $service,
  39. private ?string $userId,
  40. ) {
  41. parent::__construct($appName, $request);
  42. }
  43. /**
  44. * @NoAdminRequired
  45. *
  46. * Try to use the address set in user personal settings as weather location
  47. *
  48. * @return DataResponse<Http::STATUS_OK, array{success: bool, lat: ?float, lon: ?float, address: ?string}, array{}>
  49. *
  50. * 200: Address updated
  51. */
  52. public function usePersonalAddress(): DataResponse {
  53. return new DataResponse($this->service->usePersonalAddress());
  54. }
  55. /**
  56. * @NoAdminRequired
  57. *
  58. * Change the weather status mode. There are currently 2 modes:
  59. * - ask the browser
  60. * - use the user defined address
  61. *
  62. * @param int $mode New mode
  63. * @return DataResponse<Http::STATUS_OK, array{success: bool}, array{}>
  64. *
  65. * 200: Weather status mode updated
  66. */
  67. public function setMode(int $mode): DataResponse {
  68. return new DataResponse($this->service->setMode($mode));
  69. }
  70. /**
  71. * @NoAdminRequired
  72. *
  73. * Set address and resolve it to get coordinates
  74. * or directly set coordinates and get address with reverse geocoding
  75. *
  76. * @param string|null $address Any approximative or exact address
  77. * @param float|null $lat Latitude in decimal degree format
  78. * @param float|null $lon Longitude in decimal degree format
  79. * @return DataResponse<Http::STATUS_OK, array{success: bool, lat: ?float, lon: ?float, address: ?string}, array{}>
  80. *
  81. * 200: Location updated
  82. */
  83. public function setLocation(?string $address, ?float $lat, ?float $lon): DataResponse {
  84. $currentWeather = $this->service->setLocation($address, $lat, $lon);
  85. return new DataResponse($currentWeather);
  86. }
  87. /**
  88. * @NoAdminRequired
  89. *
  90. * Get stored user location
  91. *
  92. * @return DataResponse<Http::STATUS_OK, array{lat: float, lon: float, address: string, mode: int}, array{}>
  93. *
  94. * 200: Location returned
  95. */
  96. public function getLocation(): DataResponse {
  97. $location = $this->service->getLocation();
  98. return new DataResponse($location);
  99. }
  100. /**
  101. * @NoAdminRequired
  102. *
  103. * Get forecast for current location
  104. *
  105. * @return DataResponse<Http::STATUS_OK, WeatherStatusForecast[], array{}>|DataResponse<Http::STATUS_NOT_FOUND, array{success: bool}, array{}>
  106. *
  107. * 200: Forecast returned
  108. * 404: Forecast not found
  109. */
  110. public function getForecast(): DataResponse {
  111. $forecast = $this->service->getForecast();
  112. if (isset($forecast['success']) && $forecast['success'] === false) {
  113. return new DataResponse($forecast, Http::STATUS_NOT_FOUND);
  114. } else {
  115. return new DataResponse($forecast);
  116. }
  117. }
  118. /**
  119. * @NoAdminRequired
  120. *
  121. * Get favorites list
  122. *
  123. * @return DataResponse<Http::STATUS_OK, string[], array{}>
  124. *
  125. * 200: Favorites returned
  126. */
  127. public function getFavorites(): DataResponse {
  128. return new DataResponse($this->service->getFavorites());
  129. }
  130. /**
  131. * @NoAdminRequired
  132. *
  133. * Set favorites list
  134. *
  135. * @param string[] $favorites Favorite addresses
  136. * @return DataResponse<Http::STATUS_OK, array{success: bool}, array{}>
  137. *
  138. * 200: Favorites updated
  139. */
  140. public function setFavorites(array $favorites): DataResponse {
  141. return new DataResponse($this->service->setFavorites($favorites));
  142. }
  143. }