mirror of
https://github.com/flutter/samples.git
synced 2026-04-25 08:22:16 +00:00
Compass app (#2446)
This commit is contained in:
116
compass_app/server/lib/routes/booking.dart
Normal file
116
compass_app/server/lib/routes/booking.dart
Normal file
@@ -0,0 +1,116 @@
|
||||
// Copyright 2024 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:shelf/shelf.dart';
|
||||
import 'package:shelf_router/shelf_router.dart';
|
||||
|
||||
import '../config/assets.dart';
|
||||
import '../model/booking/booking.dart';
|
||||
|
||||
/// Allows the user to save and restore bookings.
|
||||
///
|
||||
/// Bookings are stored in memory for demo purposes,
|
||||
/// so they are lost when the server stops.
|
||||
///
|
||||
/// For demo purposes, this API includes a default pre-generated booking.
|
||||
///
|
||||
/// The [Booking.id] is also the index in the bookings list.
|
||||
class BookingApi {
|
||||
BookingApi() {
|
||||
// Create a default booking
|
||||
var destination = Assets.destinations.first;
|
||||
final activitiesRef = Assets.activities
|
||||
.where((activity) => activity.destinationRef == destination.ref)
|
||||
.map((activity) => activity.ref)
|
||||
.toList();
|
||||
_bookings.add(
|
||||
Booking(
|
||||
id: _sequentialId++,
|
||||
name: '${destination.name}, ${destination.continent}',
|
||||
startDate: DateTime(2024, 7, 20),
|
||||
endDate: DateTime(2024, 8, 15),
|
||||
destinationRef: destination.ref,
|
||||
activitiesRef: activitiesRef,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// Bookings are kept in memory for demo purposes.
|
||||
// To keep things simple, the id is also the index in the list.
|
||||
final List<Booking> _bookings = List.empty(growable: true);
|
||||
|
||||
// Used to generate IDs for bookings
|
||||
int _sequentialId = 0;
|
||||
|
||||
Router get router {
|
||||
final router = Router();
|
||||
|
||||
// Get User bookings
|
||||
router.get('/', (Request request) {
|
||||
return Response.ok(
|
||||
json.encode(_bookings),
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
);
|
||||
});
|
||||
|
||||
// Get a booking by id
|
||||
router.get('/<id>', (Request request, String id) {
|
||||
final bookingId = int.parse(id);
|
||||
final booking =
|
||||
_bookings.firstWhereOrNull((booking) => booking.id == bookingId);
|
||||
|
||||
if (booking == null) {
|
||||
return Response.notFound('Invalid id');
|
||||
}
|
||||
|
||||
return Response.ok(
|
||||
json.encode(booking),
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
);
|
||||
});
|
||||
|
||||
// Save a new booking
|
||||
router.post('/', (Request request) async {
|
||||
final body = await request.readAsString();
|
||||
final booking = Booking.fromJson(json.decode(body));
|
||||
|
||||
if (booking.id != null) {
|
||||
// POST endpoint only allows newly created bookings
|
||||
return Response.badRequest(
|
||||
body: 'Booking already has id, use PUT instead.');
|
||||
}
|
||||
|
||||
// Add ID to new booking
|
||||
final bookingWithId = booking.copyWith(id: _sequentialId++);
|
||||
|
||||
// Store booking
|
||||
_bookings.add(bookingWithId);
|
||||
|
||||
// Respond with newly created booking
|
||||
return Response(
|
||||
201, // created
|
||||
body: json.encode(bookingWithId),
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
);
|
||||
});
|
||||
|
||||
// Delete booking
|
||||
router.delete('/<id>', (Request request, String id) async {
|
||||
final bookingId = int.parse(id);
|
||||
final booking =
|
||||
_bookings.firstWhereOrNull((booking) => booking.id == bookingId);
|
||||
if (booking == null) {
|
||||
return Response.notFound('Invalid id');
|
||||
}
|
||||
_bookings.remove(booking);
|
||||
// 240: no content
|
||||
return Response(204);
|
||||
});
|
||||
|
||||
return router;
|
||||
}
|
||||
}
|
||||
44
compass_app/server/lib/routes/continent.dart
Normal file
44
compass_app/server/lib/routes/continent.dart
Normal file
@@ -0,0 +1,44 @@
|
||||
// Copyright 2024 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:shelf/shelf.dart';
|
||||
|
||||
import '../model/continent/continent.dart';
|
||||
|
||||
final _continents = [
|
||||
Continent(
|
||||
name: 'Europe',
|
||||
imageUrl: 'https://rstr.in/google/tripedia/TmR12QdlVTT',
|
||||
),
|
||||
Continent(
|
||||
name: 'Asia',
|
||||
imageUrl: 'https://rstr.in/google/tripedia/VJ8BXlQg8O1',
|
||||
),
|
||||
Continent(
|
||||
name: 'South America',
|
||||
imageUrl: 'https://rstr.in/google/tripedia/flm_-o1aI8e',
|
||||
),
|
||||
Continent(
|
||||
name: 'Africa',
|
||||
imageUrl: 'https://rstr.in/google/tripedia/-nzi8yFOBpF',
|
||||
),
|
||||
Continent(
|
||||
name: 'North America',
|
||||
imageUrl: 'https://rstr.in/google/tripedia/jlbgFDrSUVE',
|
||||
),
|
||||
Continent(
|
||||
name: 'Oceania',
|
||||
imageUrl: 'https://rstr.in/google/tripedia/vxyrDE-fZVL',
|
||||
),
|
||||
Continent(
|
||||
name: 'Australia',
|
||||
imageUrl: 'https://rstr.in/google/tripedia/z6vy6HeRyvZ',
|
||||
),
|
||||
];
|
||||
|
||||
Response continentHandler(Request req) {
|
||||
return Response.ok(jsonEncode(_continents));
|
||||
}
|
||||
35
compass_app/server/lib/routes/destination.dart
Normal file
35
compass_app/server/lib/routes/destination.dart
Normal file
@@ -0,0 +1,35 @@
|
||||
// Copyright 2024 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:shelf/shelf.dart';
|
||||
import 'package:shelf_router/shelf_router.dart';
|
||||
|
||||
import '../config/assets.dart';
|
||||
|
||||
class DestinationApi {
|
||||
Router get router {
|
||||
final router = Router();
|
||||
|
||||
router.get('/', (Request request) {
|
||||
return Response.ok(
|
||||
json.encode(Assets.destinations),
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
);
|
||||
});
|
||||
|
||||
router.get('/<id>/activity', (Request request, String id) {
|
||||
final list = Assets.activities
|
||||
.where((activity) => activity.destinationRef == id)
|
||||
.toList();
|
||||
return Response.ok(
|
||||
json.encode(list),
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
);
|
||||
});
|
||||
|
||||
return router;
|
||||
}
|
||||
}
|
||||
50
compass_app/server/lib/routes/login.dart
Normal file
50
compass_app/server/lib/routes/login.dart
Normal file
@@ -0,0 +1,50 @@
|
||||
// Copyright 2024 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:compass_server/config/constants.dart';
|
||||
import 'package:shelf/shelf.dart';
|
||||
import 'package:shelf_router/shelf_router.dart';
|
||||
|
||||
import '../model/login_request/login_request.dart';
|
||||
import '../model/login_response/login_response.dart';
|
||||
|
||||
/// Implements a simple login API.
|
||||
///
|
||||
/// This is provided as an example for Flutter architectural purposes only
|
||||
/// and shouldn't be used as example on how to implement authentication
|
||||
/// in production.
|
||||
///
|
||||
/// This API only accepts a fixed email and password for demonstration purposes,
|
||||
/// then returns a hardcoded token and a user id.
|
||||
///
|
||||
/// This token does not expire and is not secure.
|
||||
class LoginApi {
|
||||
Router get router {
|
||||
final router = Router();
|
||||
|
||||
router.post('/', (Request request) async {
|
||||
final body = await request.readAsString();
|
||||
final loginRequest = LoginRequest.fromJson(json.decode(body));
|
||||
|
||||
if (loginRequest.email == Constants.email &&
|
||||
loginRequest.password == Constants.password) {
|
||||
return Response.ok(
|
||||
json.encode(
|
||||
LoginResponse(
|
||||
token: Constants.token,
|
||||
userId: Constants.userId,
|
||||
),
|
||||
),
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
);
|
||||
}
|
||||
|
||||
return Response.unauthorized('Invalid credentials');
|
||||
});
|
||||
|
||||
return router;
|
||||
}
|
||||
}
|
||||
27
compass_app/server/lib/routes/user.dart
Normal file
27
compass_app/server/lib/routes/user.dart
Normal file
@@ -0,0 +1,27 @@
|
||||
// Copyright 2024 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:compass_server/config/constants.dart';
|
||||
import 'package:shelf/shelf.dart';
|
||||
import 'package:shelf_router/shelf_router.dart';
|
||||
|
||||
/// Implements a simple user API.
|
||||
///
|
||||
/// This API only returns a hardcoded user for demonstration purposes.
|
||||
class UserApi {
|
||||
Router get router {
|
||||
final router = Router();
|
||||
|
||||
router.get('/', (Request request) async {
|
||||
return Response.ok(
|
||||
json.encode(Constants.user),
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
);
|
||||
});
|
||||
|
||||
return router;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user