Compare commits

...

4 Commits

3 changed files with 90 additions and 59 deletions

View File

@ -13,7 +13,7 @@ enum ApiService {
}
Future<String?> request(BuildContext? context, ApiService service, String method, String path, dynamic body) async {
debugPrint('$method $path');
var debug = '$method $path\n $body\n';
final messenger = context != null ? ScaffoldMessenger.of(context) : null;
final prefs = await SharedPreferences.getInstance();
@ -49,18 +49,22 @@ Future<String?> request(BuildContext? context, ApiService service, String method
);
}
} catch (e) {
debugPrint('Can\'t send requst: ' + e.toString());
messenger?.showSnackBar(const SnackBar(content: Text('Unable to connect to server')));
debug += 'FAILED\n $e';
debugPrint(debug);
return null;
}
debug += 'HTTP ${response.statusCode}\n ${response.body}';
debugPrint(debug);
if (response.statusCode < 200 || response.statusCode >= 300) {
try {
final json = jsonDecode(response.body);
messenger?.showSnackBar(SnackBar(content: Text(json['message'] ?? json['title'])));
debugPrint('API error: ' + json['message']);
} catch (e) {
debugPrint('Can\'t parse response: ' + response.body);
messenger?.showSnackBar(SnackBar(content: Text('Something went wrong (HTTP ${response.statusCode})')));
}
return null;

View File

@ -14,10 +14,14 @@ class ReviewListPage extends StatefulWidget {
}
class _ReviewListState extends State<ReviewListPage> {
List<models.User> users = [];
List<models.User> _users = [];
models.User? _getReviewUser(models.Review review) {
return users.firstWhere((user) => user.id == review.userId);
try {
return _users.firstWhere((user) => user.id == review.userId);
} catch(e) {
return null;
}
}
@override
@ -27,14 +31,18 @@ class _ReviewListState extends State<ReviewListPage> {
final arg = ModalRoute.of(context)!.settings.arguments as models.ReviewList;
final reviews = arg.reviews;
if (reviews.isEmpty) {
return;
}
final userIds = reviews.map((review) => review.userId).toSet().toList();
final response = await api.request(context, api.ApiService.auth, 'GET', '/api/Users/UsersByIds?userIds=' + userIds.join(','), null);
if (response == null) return;
debugPrint('response: ' + response);
users = (jsonDecode(response) as List<dynamic>).map((user) => models.User.fromJson(user)).toList();
setState(() {
_users = (jsonDecode(response) as List<dynamic>).map((user) => models.User.fromJson(user)).toList();
});
}
@override
@ -47,12 +55,15 @@ class _ReviewListState extends State<ReviewListPage> {
selectedIndex: -1,
body: Scaffold(
backgroundColor: const Color(0xFFF9F9F9),
body: SingleChildScrollView(child: Container(
body: reviews.isEmpty
? const Center(child: Text('No reviews yet. Be the first to review this place'))
: SingleChildScrollView(child: Container(
decoration: const BoxDecoration(color: Color(0xFFF9F9F9)),
width: MediaQuery.of(context).size.width,
padding: const EdgeInsets.all(20.0),
child: Column(children:
reviews.map((review) => Container(
child: Column(children: [
for (final review in reviews)
Container(
width: double.infinity,
padding: const EdgeInsets.all(20),
margin: const EdgeInsets.only(bottom: 10),
@ -69,9 +80,23 @@ class _ReviewListState extends State<ReviewListPage> {
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Padding(
padding: EdgeInsets.only(top: 3),
child: Icon(Icons.rate_review, color: Colors.purple, size: 36),
Padding(
padding: const EdgeInsets.only(top: 3),
child:
_getReviewUser(review)?.profilePicture.isNotEmpty == true
? ClipOval(
child: Image(
image: NetworkImage(_getReviewUser(review)!.profilePicture),
height: 36,
width: 36,
fit: BoxFit.cover,
),
)
: const Icon(
Icons.account_circle,
size: 36,
color: Colors.grey,
)
),
const SizedBox(width: 20),
Expanded(
@ -94,8 +119,8 @@ class _ReviewListState extends State<ReviewListPage> {
),
],
),
)).toList(),
),
]),
)),
floatingActionButton: FloatingActionButton(
onPressed: () async {

View File

@ -219,7 +219,9 @@ async fn create_review(auth: AuthorizedUser, data: web::Data<AppData>, input: we
content: input.content.clone(),
rating: input.rating.clone(),
image_id: input.image_id,
image: None,
image: input.image_id.and_then(|image_id| {
db.query_row("SELECT * FROM images WHERE id = :id LIMIT 1", &[(":id", &image_id.to_string())], |row| Image::from_row(row)).ok()
}),
}),
Err(_) => HttpResponse::InternalServerError().finish(),
}