pianello-api/app/Http/Controllers/RouteController.php
Ale Gallo b91cf0cbde Committing laravel + api (part 1)
committing part of the laravel system with templating files and api implementation.
2023-11-10 15:54:41 +01:00

249 lines
7.2 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Category;
use App\Models\Feedback;
use App\Models\Placemark;
use App\Models\Route;
use App\Models\RouteSportDetails;
use Illuminate\Http\Request;
class RouteController extends Controller
{
// Return basic details for all routes
public function getAllRoutes()
{
$routes = Route::with(['category', 'routeSportDetails.sport'])->get();
$result = [];
foreach ($routes as $route) {
$sportsDetails = $route->routeSportDetails;
$sportsData = [];
foreach ($sportsDetails as $detail) {
$sportsData[] = [
'id' => $detail->sport_id,
'name_it' => $detail->sport->name_it,
'name_en' => $detail->sport->name_en,
];
}
$result[] = [
'id' => $route->id,
'title_it' => $route->name_it,
'title_en' => $route->name_en,
'description_it' => $route->description_it,
'description_en' => $route->description_en,
'length' => $sportsDetails->sum('distance'),
'elevation_gain' => $sportsDetails->sum('elevation_gain'),
'sports' => $sportsData,
'category_id' => $route->category->id,
'category_name_it' => $route->category->name_it,
'category_name_en' => $route->category->name_en,
];
}
return response()->json($result);
}
// Return all route categories
public function getRoutesCategories()
{
$categories = Category::all();
return response()->json($categories);
}
// Return routes by category
public function getRoutesByCategory($catId)
{
$routes = Route::with(['category', 'routeSportDetails.sport'])
->where('route_category_id', $catId)
->get();
$result = [];
foreach ($routes as $route) {
$sportsDetails = $route->routeSportDetails;
$sportsData = [];
foreach ($sportsDetails as $detail) {
$sportsData[] = [
'id' => $detail->sport_id,
'name_it' => $detail->sport->name_it,
'name_en' => $detail->sport->name_en,
];
}
$result[] = [
'id' => $route->id,
'title_it' => $route->name_it,
'title_en' => $route->name_en,
'description_it' => $route->description_it,
'description_en' => $route->description_en,
'length' => $sportsDetails->sum('distance'),
'elevation_gain' => $sportsDetails->sum('elevation_gain'),
'sports' => $sportsData,
];
}
return response()->json($result);
}
// Return all sport details for a single route, including pictures
public function getSingleRoute($id)
{
$route = Route::where('id', $id)
->with(['routeSportDetails.sport', 'pictures'])
->first();
return response()->json($route);
}
// Return route details for a single sport, including pictures
public function getSingleRouteDetails($id, $sportId)
{
$route = Route::where('id', $id)
->with(['routeSportDetails' => function($query) use ($sportId) {
$query->where('sport_id', $sportId);
}, 'pictures'])
->first();
return response()->json($route);
}
// Download the GPX file for a specific route and sport
public function downloadGpx($id, $sportId)
{
$routeDetail = RouteSportDetails::where(['route_id' => $id, 'sport_id' => $sportId])
->first();
$file = storage_path("app/{$routeDetail->gpx_path}");
return response()->download($file);
}
// Return placemarks for a specific sport and route
public function getPlacemarks($id, $sportId)
{
$placemarks = Placemark::whereHas('routeSportDetailPlacemark', function($query) use ($id, $sportId) {
$query->where(['route_id' => $id, 'sport_id' => $sportId]);
})
->get();
return response()->json($placemarks);
}
// Get all feedbacks for a specific route and sport
public function getFeedbacks($id, $sportId)
{
$feedbacks = Feedback::whereHas('routeFeedback', function($query) use ($id, $sportId) {
$query->where(['route_id' => $id, 'sport_id' => $sportId]);
})
->get();
return response()->json($feedbacks);
}
// Store feedback for a specific route and sport
public function storeFeedback(Request $request)
{
$data = $request->validate([
'title' => 'required|string|max:255',
'body' => 'required|string',
'rating' => 'required|integer',
'user_nickname' => 'required|string|max:100',
'route_id' => 'required|integer',
'sport_id' => 'required|integer'
]);
$feedback = new Feedback($data);
$feedback->save();
return response()->json(['message' => 'Feedback stored successfully']);
}
/*
public function getAllRoutes(Request $request)
{
// Get items from the database
$items = [
[
'title1' => "titoli eprcorso 1",
'title2' => "titoli eprcorso 2",
'title3' => "titoli eprcorso 3",
],
];
// Return the items as JSON
return response()->json($items);
}
public function getSingleRoute(Request $request, $routeId)
{
// Validate the request
if (!$routeId || $routeId == '')
{
return response()->json([
'error' => 'Scegliere un percorso.',
], 400);
}
// Get the items from the database
$items = [
[
'title' => "titolo eprcorso ".$routeId,
],
];
// Return the items as JSON
return response()->json($items);
}
public function storeFeedback(Request $request, $routeId, $sportId)
{
}
public function getRoutesCategories(Request $request)
{
}
public function getRoutesByCategory(Request $request, $catId)
{
}
public function getItem(Request $request, $routeId)
{
// Get the title and id from the request body
$title = request()->input('title');
$id = request()->input('id');
// Validate the request
if (! $title || ! $id) {
return response()->json([
'error' => 'Invalid request.',
], 400);
}
// Get the items from the database
$items = [
[
'title' => $title."_ext",
'id' => $id."_ext",
],
];
// Return the items as JSON
return response()->json($items);
}
*/
}