WeatherStatusController.php 3.9 KB

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