This commit is contained in:
foobar
2022-08-21 21:39:06 +02:00
commit 27c1969aaa
7354 changed files with 897064 additions and 0 deletions

View File

@@ -0,0 +1,93 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\City;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
class CityController extends Controller
{
public function show()
{
$DatabaseController = new DatabaseController();
return view('cities', [
'cities' => $DatabaseController->getNewestWeatherDataForAllCities()
]);
}
public function create(Request $request) {
$request->validate([
'name' => 'required|max:85|regex:/^[a-zA-Z\säöüÄÖÜß]+$/',
]);
$city = $request->name;
$response = WeatherstackController::requestByCity($city);
$WeatherstackController = new WeatherstackController();
$isValidResponse = $WeatherstackController->checkResponse($city, $response);
($isValidResponse[0]) ? DatabaseController::store($response) : redirect('/')->withErrors(['name' => $isValidResponse[1]]);
return redirect('/');
}
public function delete($id) {
City::findOrFail($id)->delete();
return redirect('/');
}
public function refresh() {
$cities = DB::table('cities')
->select('id', 'location_name', 'location_country')
->get();
foreach ($cities as $city) {
$city = "$city->location_name,$city->location_country";
$response = WeatherstackController::requestByCity($city);
($response->successful() === false) ? redirect('/')->withErrors(['name' => "Aktualisierung fehlgeschlagen"])
: DatabaseController::store($response);
}
return redirect('/');
}
public function setup() {
$credentials = [
'email' => 'admin@admin.com',
'password' => 'password'
];
if (!Auth::attempt($credentials)) {
$user = new \App\Models\User();
$user->name = 'Admin';
$user->email = $credentials['email'];
$user->password = Hash::make($credentials['password']);
$user->save();
if (Auth::attempt($credentials)) {
$user = Auth::user();
$adminToken = $user->createToken('admin-token', ['create','update','delete']);
$updateToken = $user->createToken('update-token', ['create','update']);
$basicToken = $user->createToken('basic-token', ['none']);
return [
'admin' => $adminToken->plainTextToken,
'update' => $updateToken->plainTextToken,
'basic' => $basicToken->plainTextToken,
];
}
}
}
}

View File

@@ -0,0 +1,13 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}

View File

@@ -0,0 +1,97 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use App\Models\City;
use App\Models\WeatherData;
use Illuminate\Http\Client\Response as ClientResponse;
use App\Http\Requests\StoreCityRequest;
use App\Http\Requests\UpdateCityRequest;
class DatabaseController extends Controller
{
public static function getNewestWeatherDataByCityID($city_id) {
return DB::table('cities')
->join('weather_data', 'cities.id', '=', 'weather_data.city_id')
->select('cities.id', 'cities.location_name', 'cities.location_country', 'weather_data.current_temperature', 'weather_data.weather_icon', 'weather_data.weather_description', 'weather_data.wind_speed', 'weather_data.wind_dir', 'weather_data.localtime_epoch')
->where('cities.id', '=', $city_id)
->orderBy('localtime_epoch', 'desc')
->first();
}
public function getNewestWeatherDataForAllCities () {
$city_ids = DB::table('cities')->pluck('id');
$responses = collect();
foreach ($city_ids as $city_id) {
$response = $this->getNewestWeatherDataByCityID($city_id);
$responses->push($response);
}
return $responses;
}
public static function store(ClientResponse $response) {
$city = City::firstOrNew([
'location_name' => $response->json('location.name'),
'location_country' => $response->json('location.country')
]);
$city->save();
$weatherData = WeatherData::firstOrNew([
'city_id' => $city->id,
'current_temperature' => $response->json('current.temperature'),
'weather_icon' => $response->json("current.weather_icons")[0],
'weather_description' => $response->json('current.weather_descriptions')[0],
'wind_speed' => $response->json('current.wind_speed'),
'wind_dir' => $response->json('current.wind_dir'),
'localtime_epoch' => $response->json('location.localtime_epoch')
]);
$weatherData->save();
}
public static function storeApiRequest(StoreCityRequest $request) {
$city = City::firstOrNew([
'location_name' => $request->location_name,
'location_country' => $request->location_country
]);
$city->save();
$weatherData = WeatherData::firstOrNew([
'city_id' => $city->id,
'current_temperature' => $request->currentTemperature,
'weather_icon' => $request->weatherIcon,
'weather_description' => $request->weatherDescription,
'wind_speed' => $request->windSpeed,
'wind_dir' => $request->windDir,
'localtime_epoch' => $request->localtimeEpoch
]);
$weatherData->save();
return $city->id;
}
public function updateApiRequest($id, UpdateCityRequest $request) {
$city = $this->getNewestWeatherDataByCityID($id);
DB::table('cities')
->join('weather_data', 'cities.id', '=', 'weather_data.city_id')
->select('cities.location_name', 'cities.location_country', 'weather_data.current_temperature', 'weather_data.weather_icon', 'weather_data.weather_description', 'weather_data.wind_speed', 'weather_data.wind_dir', 'weather_data.localtime_epoch')
->where('cities.id', '=', $id)
->where('weather_data.localtime_epoch', '=', $city->localtime_epoch)
->update([
'cities.location_name' => $request->location_name,
'cities.location_country' => $request->location_country,
'weather_data.current_temperature' => $request->currentTemperature,
'weather_data.weather_icon' => $request->weatherIcon,
'weather_data.weather_description' => $request->weatherDescription,
'weather_data.wind_speed' => $request->windSpeed,
'weather_data.wind_dir' => $request->windDir,
'weather_data.localtime_epoch' => $request->localtimeEpoch
]);
}
}

View File

@@ -0,0 +1,54 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Http;
use Illuminate\Http\Client\Response as ClientResponse;
use App\Models\City;
class WeatherstackController extends Controller
{
public static function requestByCity($city) {
$access_key = config('app.weatherstack_api_key');
$units = 'm'; /* m for Metric, s for Scientific, f for Fahrenheit */
$url = 'http://api.weatherstack.com/current';
return Http::acceptJson()->get($url, [
'access_key' => $access_key,
'units' => $units,
'query' => $city
]);
}
public function checkResponse($city, ClientResponse $response) {
if ($response->successful()) {
[$isValidResponse, $error] = $this->checkErrorMessage($city, $response);
}
elseif ($response->failed()) {
$isValidResponse = false;
$error = 'Request failed.';
}
return [$isValidResponse, $error];
}
public static function checkErrorMessage($city, ClientResponse $response) {
$isValidResponse = true;
$error = '';
if ($response->json('success') === false) {
$isValidResponse = false;
($response->json('error.type') === 'usage_limit_reached') ? $error = 'The usage limit of your API key has been reached.'
: $error = "No city named '{$city}' could be found.";
}
elseif (City::where('location_name', $response->json('location.name'))->exists()) {
$isValidResponse = false;
$error = "'{$response->json('location.name')}, {$response->json('location.country')}' has been already added.";
}
return [$isValidResponse, $error];
}
}