-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…github.com/AleksandraKozubal/KULA-Mobile into #6-logged-user-can-add-kebab-to-favourites
- Loading branch information
Showing
7 changed files
with
416 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import 'package:http/http.dart' as http; | ||
import 'dart:convert'; | ||
import 'package:kula_mobile/Data/Models/suggestion_model.dart'; | ||
import 'package:flutter_dotenv/flutter_dotenv.dart'; | ||
import 'package:kula_mobile/Services/token_storage.dart'; | ||
|
||
class SuggestionDataSource { | ||
final String baseUrl = dotenv.env['API_URL']!; | ||
|
||
Future<List<SuggestionModel>> fetchSuggestions() async { | ||
final token = await TokenStorage.getToken(); | ||
final headers = token != null | ||
? { | ||
'Authorization': 'Bearer $token', | ||
} | ||
: {}; | ||
final response = await http.get( | ||
Uri.parse('$baseUrl/mysuggestions'), | ||
headers: headers.cast<String, String>(), | ||
); | ||
if (response.statusCode == 200) { | ||
final List<dynamic> suggestionsJson = json.decode(response.body); | ||
return suggestionsJson | ||
.map((json) => SuggestionModel.fromJson(json)) | ||
.toList(); | ||
} else { | ||
throw Exception('Failed to load suggestions'); | ||
} | ||
} | ||
|
||
Future<void> addSuggestion( | ||
int kebabPlaceId, | ||
String name, | ||
String description, | ||
) async { | ||
final token = await TokenStorage.getToken(); | ||
final response = await http.post( | ||
Uri.parse( | ||
'$baseUrl/kebab-places/$kebabPlaceId/suggest?name=$name&description=$description&kebabPlace=$kebabPlaceId', | ||
), | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'Authorization': 'Bearer $token', | ||
}, | ||
); | ||
if (response.statusCode != 201) { | ||
throw Exception('Failed to add suggestion'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
class SuggestionModel { | ||
final int id; | ||
final String name; | ||
final String description; | ||
final String status; | ||
final int userId; | ||
final int kebabPlaceId; | ||
final String? comment; | ||
final DateTime createdAt; | ||
final DateTime updatedAt; | ||
|
||
SuggestionModel({ | ||
required this.id, | ||
required this.name, | ||
required this.description, | ||
required this.status, | ||
required this.userId, | ||
required this.kebabPlaceId, | ||
required this.createdAt, | ||
required this.updatedAt, | ||
this.comment, | ||
}); | ||
|
||
factory SuggestionModel.fromJson(Map<String, dynamic> json) { | ||
return SuggestionModel( | ||
id: json['id'], | ||
name: json['name'], | ||
description: json['description'], | ||
status: json['status'], | ||
userId: json['user_id'], | ||
kebabPlaceId: json['kebab_place_id'], | ||
comment: json['comment'], | ||
createdAt: DateTime.parse(json['created_at']), | ||
updatedAt: DateTime.parse(json['updated_at']), | ||
); | ||
} | ||
|
||
Map<String, dynamic> toJson() { | ||
return { | ||
'id': id, | ||
'name': name, | ||
'description': description, | ||
'status': status, | ||
'user_id': userId, | ||
'kebab_place_id': kebabPlaceId, | ||
'comment': comment, | ||
'created_at': createdAt.toIso8601String(), | ||
'updated_at': updatedAt.toIso8601String(), | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import 'package:kula_mobile/Data/Data_sources/suggestion_data_source.dart'; | ||
import 'package:kula_mobile/Data/Models/suggestion_model.dart'; | ||
|
||
class SuggestionRepositoryImpl { | ||
final SuggestionDataSource suggestionDataSource; | ||
|
||
SuggestionRepositoryImpl({required this.suggestionDataSource}); | ||
|
||
Future<List<SuggestionModel>> fetchSuggestions() { | ||
return suggestionDataSource.fetchSuggestions(); | ||
} | ||
|
||
Future<void> addSuggestion( | ||
int kebabPlaceId, | ||
String name, | ||
String description, | ||
) { | ||
return suggestionDataSource.addSuggestion(kebabPlaceId, name, description); | ||
} | ||
} |
Oops, something went wrong.