Fix userIds request not working, show username on reviews

This commit is contained in:
Reimar 2024-09-13 08:40:32 +02:00
parent 4480080496
commit c389381b1e
Signed by: Reimar
GPG Key ID: 93549FA07F0AE268
4 changed files with 27 additions and 20 deletions

View File

@ -73,9 +73,10 @@ namespace API.Controllers
} }
[HttpGet("UsersByIds")] [HttpGet("UsersByIds")]
public async Task<ActionResult<List<UserDTO>>> GetUsersByIds(List<string> userIds) public async Task<ActionResult<List<UserDTO>>> GetUsersByIds(string userIds)
{ {
return await _queryUsersByIds.Handle(userIds); List<string> ids = userIds.Split(",").ToList();
return await _queryUsersByIds.Handle(ids);
} }
[Authorize] [Authorize]

View File

@ -13,6 +13,8 @@ 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 {
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();
@ -22,7 +24,7 @@ Future<String?> request(BuildContext? context, ApiService service, String method
}; };
final token = prefs.getString('token'); final token = prefs.getString('token');
final Map<String, String> headers = {'Accept': 'application/json'}; final Map<String, String> headers = {};
if (token != null) headers.addAll({'Authorization': 'Bearer $token'}); if (token != null) headers.addAll({'Authorization': 'Bearer $token'});
final http.Response response; final http.Response response;
@ -58,7 +60,7 @@ Future<String?> request(BuildContext? context, ApiService service, String method
messenger?.showSnackBar(SnackBar(content: Text(json['message'] ?? json['title']))); messenger?.showSnackBar(SnackBar(content: Text(json['message'] ?? json['title'])));
debugPrint('API error: ' + json['message']); debugPrint('API error: ' + json['message']);
} catch (e) { } catch (e) {
debugPrint('Can\'t parse response: ' + e.toString()); 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

@ -16,6 +16,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) {
return users.firstWhere((user) => user.id == review.userId);
}
@override @override
void didChangeDependencies() async { void didChangeDependencies() async {
super.didChangeDependencies(); super.didChangeDependencies();
@ -24,10 +28,8 @@ class _ReviewListState extends State<ReviewListPage> {
final reviews = arg.reviews; final reviews = arg.reviews;
final userIds = reviews.map((review) => review.userId).toSet().toList(); final userIds = reviews.map((review) => review.userId).toSet().toList();
final queryParams = userIds.map((id) => "userIds=$id");
final queryString = "?${queryParams.join("&")}";
final response = await api.request(context, api.ApiService.auth, 'GET', '/api/Users/UsersByIds$queryString', 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;
debugPrint('response: ' + response); debugPrint('response: ' + response);
@ -79,12 +81,14 @@ class _ReviewListState extends State<ReviewListPage> {
Text(review.title, style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 24)), Text(review.title, style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 24)),
Text(review.content), Text(review.content),
const SizedBox(height: 10), const SizedBox(height: 10),
if (review.image != null) Image.network(review.image!.imageUrl, height: 200,), if (review.image != null) Image.network(review.image!.imageUrl, height: 200),
if (review.image != null) const SizedBox(height: 15), if (review.image != null) const SizedBox(height: 15),
Row(children: [ Row(children: [
for (var i = 0; i < review.rating; i++) const Icon(Icons.star, color: Colors.yellow), 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), 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)),
], ],
), ),
), ),

View File

@ -388,18 +388,18 @@ packages:
dependency: transitive dependency: transitive
description: description:
name: leak_tracker name: leak_tracker
sha256: "7f0df31977cb2c0b88585095d168e689669a2cc9b97c309665e3386f3e9d341a" sha256: "3f87a60e8c63aecc975dda1ceedbc8f24de75f09e4856ea27daf8958f2f0ce05"
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "10.0.4" version: "10.0.5"
leak_tracker_flutter_testing: leak_tracker_flutter_testing:
dependency: transitive dependency: transitive
description: description:
name: leak_tracker_flutter_testing name: leak_tracker_flutter_testing
sha256: "06e98f569d004c1315b991ded39924b21af84cf14cc94791b8aea337d25b57f8" sha256: "932549fb305594d82d7183ecd9fa93463e9914e1b67cacc34bc40906594a1806"
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "3.0.3" version: "3.0.5"
leak_tracker_testing: leak_tracker_testing:
dependency: transitive dependency: transitive
description: description:
@ -444,18 +444,18 @@ packages:
dependency: transitive dependency: transitive
description: description:
name: material_color_utilities name: material_color_utilities
sha256: "0e0a020085b65b6083975e499759762399b4475f766c21668c4ecca34ea74e5a" sha256: f7142bb1154231d7ea5f96bc7bde4bda2a0945d2806bb11670e30b850d56bdec
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "0.8.0" version: "0.11.1"
meta: meta:
dependency: transitive dependency: transitive
description: description:
name: meta name: meta
sha256: "7687075e408b093f36e6bbf6c91878cc0d4cd10f409506f7bc996f68220b9136" sha256: bdb68674043280c3428e9ec998512fb681678676b3c54e773629ffe74419f8c7
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "1.12.0" version: "1.15.0"
mgrs_dart: mgrs_dart:
dependency: transitive dependency: transitive
description: description:
@ -673,10 +673,10 @@ packages:
dependency: transitive dependency: transitive
description: description:
name: test_api name: test_api
sha256: "9955ae474176f7ac8ee4e989dadfb411a58c30415bcfb648fa04b2b8a03afa7f" sha256: "5b8a98dafc4d5c4c9c72d8b31ab2b23fc13422348d2997120294d3bac86b4ddb"
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "0.7.0" version: "0.7.2"
typed_data: typed_data:
dependency: transitive dependency: transitive
description: description:
@ -713,10 +713,10 @@ packages:
dependency: transitive dependency: transitive
description: description:
name: vm_service name: vm_service
sha256: "3923c89304b715fb1eb6423f017651664a03bf5f4b29983627c4da791f74a4ec" sha256: f652077d0bdf60abe4c1f6377448e8655008eef28f128bc023f7b5e8dfeb48fc
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "14.2.1" version: "14.2.4"
web: web:
dependency: transitive dependency: transitive
description: description: