WeatherStatusController.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. * @psalm-import-type WeatherStatusSuccess from ResponseDefinitions
  34. * @psalm-import-type WeatherStatusLocation from ResponseDefinitions
  35. * @psalm-import-type WeatherStatusLocationWithSuccess from ResponseDefinitions
  36. * @psalm-import-type WeatherStatusLocationWithMode from ResponseDefinitions
  37. */
  38. class WeatherStatusController extends OCSController {
  39. public function __construct(
  40. string $appName,
  41. IRequest $request,
  42. private WeatherStatusService $service,
  43. private ?string $userId,
  44. ) {
  45. parent::__construct($appName, $request);
  46. }
  47. /**
  48. * @NoAdminRequired
  49. *
  50. * Try to use the address set in user personal settings as weather location
  51. *
  52. * @return DataResponse<Http::STATUS_OK, WeatherStatusLocationWithSuccess, array{}>
  53. *
  54. * 200: Address updated
  55. */
  56. public function usePersonalAddress(): DataResponse {
  57. return new DataResponse($this->service->usePersonalAddress());
  58. }
  59. /**
  60. * @NoAdminRequired
  61. *
  62. * Change the weather status mode. There are currently 2 modes:
  63. * - ask the browser
  64. * - use the user defined address
  65. *
  66. * @param int $mode New mode
  67. * @return DataResponse<Http::STATUS_OK, WeatherStatusSuccess, array{}>
  68. *
  69. * 200: Weather status mode updated
  70. */
  71. public function setMode(int $mode): DataResponse {
  72. return new DataResponse($this->service->setMode($mode));
  73. }
  74. /**
  75. * @NoAdminRequired
  76. *
  77. * Set address and resolve it to get coordinates
  78. * or directly set coordinates and get address with reverse geocoding
  79. *
  80. * @param string|null $address Any approximative or exact address
  81. * @param float|null $lat Latitude in decimal degree format
  82. * @param float|null $lon Longitude in decimal degree format
  83. * @return DataResponse<Http::STATUS_OK, WeatherStatusLocationWithSuccess, array{}>
  84. *
  85. * 200: Location updated
  86. */
  87. public function setLocation(?string $address, ?float $lat, ?float $lon): DataResponse {
  88. $currentWeather = $this->service->setLocation($address, $lat, $lon);
  89. return new DataResponse($currentWeather);
  90. }
  91. /**
  92. * @NoAdminRequired
  93. *
  94. * Get stored user location
  95. *
  96. * @return DataResponse<Http::STATUS_OK, WeatherStatusLocationWithMode, array{}>
  97. *
  98. * 200: Location returned
  99. */
  100. public function getLocation(): DataResponse {
  101. $location = $this->service->getLocation();
  102. return new DataResponse($location);
  103. }
  104. /**
  105. * @NoAdminRequired
  106. *
  107. * Get forecast for current location
  108. *
  109. * @return DataResponse<Http::STATUS_OK, WeatherStatusForecast[]|array{error: string}, array{}>|DataResponse<Http::STATUS_NOT_FOUND, WeatherStatusSuccess, array{}>
  110. *
  111. * 200: Forecast returned
  112. * 404: Forecast not found
  113. */
  114. public function getForecast(): DataResponse {
  115. $forecast = $this->service->getForecast();
  116. if (isset($forecast['success']) && $forecast['success'] === false) {
  117. return new DataResponse($forecast, Http::STATUS_NOT_FOUND);
  118. } else {
  119. return new DataResponse($forecast);
  120. }
  121. }
  122. /**
  123. * @NoAdminRequired
  124. *
  125. * Get favorites list
  126. *
  127. * @return DataResponse<Http::STATUS_OK, string[], array{}>
  128. *
  129. * 200: Favorites returned
  130. */
  131. public function getFavorites(): DataResponse {
  132. return new DataResponse($this->service->getFavorites());
  133. }
  134. /**
  135. * @NoAdminRequired
  136. *
  137. * Set favorites list
  138. *
  139. * @param string[] $favorites Favorite addresses
  140. * @return DataResponse<Http::STATUS_OK, WeatherStatusSuccess, array{}>
  141. *
  142. * 200: Favorites updated
  143. */
  144. public function setFavorites(array $favorites): DataResponse {
  145. return new DataResponse($this->service->setFavorites($favorites));
  146. }
  147. }