WeatherStatusController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\WeatherStatus\Controller;
  8. use OCA\WeatherStatus\ResponseDefinitions;
  9. use OCA\WeatherStatus\Service\WeatherStatusService;
  10. use OCP\AppFramework\Http;
  11. use OCP\AppFramework\Http\DataResponse;
  12. use OCP\AppFramework\OCSController;
  13. use OCP\IRequest;
  14. /**
  15. * @psalm-import-type WeatherStatusForecast from ResponseDefinitions
  16. * @psalm-import-type WeatherStatusSuccess from ResponseDefinitions
  17. * @psalm-import-type WeatherStatusLocation from ResponseDefinitions
  18. * @psalm-import-type WeatherStatusLocationWithSuccess from ResponseDefinitions
  19. * @psalm-import-type WeatherStatusLocationWithMode from ResponseDefinitions
  20. */
  21. class WeatherStatusController extends OCSController {
  22. public function __construct(
  23. string $appName,
  24. IRequest $request,
  25. private WeatherStatusService $service,
  26. private ?string $userId,
  27. ) {
  28. parent::__construct($appName, $request);
  29. }
  30. /**
  31. * @NoAdminRequired
  32. *
  33. * Try to use the address set in user personal settings as weather location
  34. *
  35. * @return DataResponse<Http::STATUS_OK, WeatherStatusLocationWithSuccess, array{}>
  36. *
  37. * 200: Address updated
  38. */
  39. public function usePersonalAddress(): DataResponse {
  40. return new DataResponse($this->service->usePersonalAddress());
  41. }
  42. /**
  43. * @NoAdminRequired
  44. *
  45. * Change the weather status mode. There are currently 2 modes:
  46. * - ask the browser
  47. * - use the user defined address
  48. *
  49. * @param int $mode New mode
  50. * @return DataResponse<Http::STATUS_OK, WeatherStatusSuccess, array{}>
  51. *
  52. * 200: Weather status mode updated
  53. */
  54. public function setMode(int $mode): DataResponse {
  55. return new DataResponse($this->service->setMode($mode));
  56. }
  57. /**
  58. * @NoAdminRequired
  59. *
  60. * Set address and resolve it to get coordinates
  61. * or directly set coordinates and get address with reverse geocoding
  62. *
  63. * @param string|null $address Any approximative or exact address
  64. * @param float|null $lat Latitude in decimal degree format
  65. * @param float|null $lon Longitude in decimal degree format
  66. * @return DataResponse<Http::STATUS_OK, WeatherStatusLocationWithSuccess, array{}>
  67. *
  68. * 200: Location updated
  69. */
  70. public function setLocation(?string $address, ?float $lat, ?float $lon): DataResponse {
  71. $currentWeather = $this->service->setLocation($address, $lat, $lon);
  72. return new DataResponse($currentWeather);
  73. }
  74. /**
  75. * @NoAdminRequired
  76. *
  77. * Get stored user location
  78. *
  79. * @return DataResponse<Http::STATUS_OK, WeatherStatusLocationWithMode, array{}>
  80. *
  81. * 200: Location returned
  82. */
  83. public function getLocation(): DataResponse {
  84. $location = $this->service->getLocation();
  85. return new DataResponse($location);
  86. }
  87. /**
  88. * @NoAdminRequired
  89. *
  90. * Get forecast for current location
  91. *
  92. * @return DataResponse<Http::STATUS_OK, WeatherStatusForecast[]|array{error: string}, array{}>|DataResponse<Http::STATUS_NOT_FOUND, WeatherStatusSuccess, array{}>
  93. *
  94. * 200: Forecast returned
  95. * 404: Forecast not found
  96. */
  97. public function getForecast(): DataResponse {
  98. $forecast = $this->service->getForecast();
  99. if (isset($forecast['success']) && $forecast['success'] === false) {
  100. return new DataResponse($forecast, Http::STATUS_NOT_FOUND);
  101. } else {
  102. return new DataResponse($forecast);
  103. }
  104. }
  105. /**
  106. * @NoAdminRequired
  107. *
  108. * Get favorites list
  109. *
  110. * @return DataResponse<Http::STATUS_OK, string[], array{}>
  111. *
  112. * 200: Favorites returned
  113. */
  114. public function getFavorites(): DataResponse {
  115. return new DataResponse($this->service->getFavorites());
  116. }
  117. /**
  118. * @NoAdminRequired
  119. *
  120. * Set favorites list
  121. *
  122. * @param string[] $favorites Favorite addresses
  123. * @return DataResponse<Http::STATUS_OK, WeatherStatusSuccess, array{}>
  124. *
  125. * 200: Favorites updated
  126. */
  127. public function setFavorites(array $favorites): DataResponse {
  128. return new DataResponse($this->service->setFavorites($favorites));
  129. }
  130. }