Compare commits

..

No commits in common. "68c806408312ba47e03c22e137c9c319c9004463" and "c389381b1e72f84b919ce065d080f5d387ac3e11" have entirely different histories.

3 changed files with 59 additions and 90 deletions

View File

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

View File

@ -14,14 +14,10 @@ class ReviewListPage extends StatefulWidget {
} }
class _ReviewListState extends State<ReviewListPage> { class _ReviewListState extends State<ReviewListPage> {
List<models.User> _users = []; List<models.User> users = [];
models.User? _getReviewUser(models.Review review) { models.User? _getReviewUser(models.Review review) {
try { return users.firstWhere((user) => user.id == review.userId);
return _users.firstWhere((user) => user.id == review.userId);
} catch(e) {
return null;
}
} }
@override @override
@ -31,18 +27,14 @@ class _ReviewListState extends State<ReviewListPage> {
final arg = ModalRoute.of(context)!.settings.arguments as models.ReviewList; final arg = ModalRoute.of(context)!.settings.arguments as models.ReviewList;
final reviews = arg.reviews; final reviews = arg.reviews;
if (reviews.isEmpty) {
return;
}
final userIds = reviews.map((review) => review.userId).toSet().toList(); 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); final response = await api.request(context, api.ApiService.auth, 'GET', '/api/Users/UsersByIds?userIds=' + userIds.join(','), null);
if (response == null) return; if (response == null) return;
setState(() { debugPrint('response: ' + response);
_users = (jsonDecode(response) as List<dynamic>).map((user) => models.User.fromJson(user)).toList();
}); users = (jsonDecode(response) as List<dynamic>).map((user) => models.User.fromJson(user)).toList();
} }
@override @override
@ -55,73 +47,56 @@ class _ReviewListState extends State<ReviewListPage> {
selectedIndex: -1, selectedIndex: -1,
body: Scaffold( body: Scaffold(
backgroundColor: const Color(0xFFF9F9F9), backgroundColor: const Color(0xFFF9F9F9),
body: reviews.isEmpty body: SingleChildScrollView(child: Container(
? const Center(child: Text('No reviews yet. Be the first to review this place')) decoration: const BoxDecoration(color: Color(0xFFF9F9F9)),
: SingleChildScrollView(child: Container( width: MediaQuery.of(context).size.width,
decoration: const BoxDecoration(color: Color(0xFFF9F9F9)), padding: const EdgeInsets.all(20.0),
width: MediaQuery.of(context).size.width, child: Column(children:
padding: const EdgeInsets.all(20.0), reviews.map((review) => Container(
child: Column(children: [ width: double.infinity,
for (final review in reviews) padding: const EdgeInsets.all(20),
Container( margin: const EdgeInsets.only(bottom: 10),
width: double.infinity, decoration: const BoxDecoration(
padding: const EdgeInsets.all(20), boxShadow: [
margin: const EdgeInsets.only(bottom: 10), BoxShadow(
decoration: const BoxDecoration( color: Color(0x20000000),
boxShadow: [ offset: Offset(0,1),
BoxShadow( blurRadius: 4,
color: Color(0x20000000),
offset: Offset(0,1),
blurRadius: 4,
),
],
color: Colors.white,
), ),
child: Row( ],
crossAxisAlignment: CrossAxisAlignment.start, color: Colors.white,
children: [ ),
Padding( child: Row(
padding: const EdgeInsets.only(top: 3), crossAxisAlignment: CrossAxisAlignment.start,
child: children: [
_getReviewUser(review)?.profilePicture.isNotEmpty == true const Padding(
? ClipOval( padding: EdgeInsets.only(top: 3),
child: Image( child: Icon(Icons.rate_review, color: Colors.purple, size: 36),
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(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(review.title, style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 24)),
Text(review.content),
const SizedBox(height: 10),
if (review.image != null) Image.network(review.image!.imageUrl, height: 200),
if (review.image != null) const SizedBox(height: 15),
Row(children: [
for (var i = 0; i < review.rating; i++) const Icon(Icons.star, color: Colors.yellow),
for (var i = review.rating; i < 5; i++) const Icon(Icons.star_border),
]),
const SizedBox(height: 10),
Text('Submitted by ' + (_getReviewUser(review)?.username ?? ''), style: const TextStyle(color: Colors.grey, fontSize: 12)),
],
),
),
],
), ),
), const SizedBox(width: 20),
]), Expanded(
)), child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(review.title, style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 24)),
Text(review.content),
const SizedBox(height: 10),
if (review.image != null) Image.network(review.image!.imageUrl, height: 200),
if (review.image != null) const SizedBox(height: 15),
Row(children: [
for (var i = 0; i < review.rating; i++) const Icon(Icons.star, color: Colors.yellow),
for (var i = review.rating; i < 5; i++) const Icon(Icons.star_border),
]),
const SizedBox(height: 10),
Text('Submitted by ' + (_getReviewUser(review)?.username ?? ''), style: const TextStyle(color: Colors.grey, fontSize: 12)),
],
),
),
],
),
)).toList(),
),
)),
floatingActionButton: FloatingActionButton( floatingActionButton: FloatingActionButton(
onPressed: () async { onPressed: () async {
if (!await api.isLoggedIn(context)) { if (!await api.isLoggedIn(context)) {

View File

@ -217,11 +217,9 @@ async fn create_review(auth: AuthorizedUser, data: web::Data<AppData>, input: we
place_description: input.place_description.clone(), place_description: input.place_description.clone(),
title: input.title.clone(), title: input.title.clone(),
content: input.content.clone(), content: input.content.clone(),
rating: input.rating.clone(), rating: input.rating.clone(),
image_id: input.image_id, image_id: input.image_id,
image: input.image_id.and_then(|image_id| { image: None,
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(), Err(_) => HttpResponse::InternalServerError().finish(),
} }