Skip to content

Commit

Permalink
Merge branch '#6-logged-user-can-add-kebab-to-favourites' of https://…
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
zmigrOO committed Jan 19, 2025
2 parents 4cf357a + 80d860d commit bb71749
Show file tree
Hide file tree
Showing 7 changed files with 416 additions and 15 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,11 @@ flutter run
```shell
flutter pub get
```
- To analyze project for futher testing errors use:
```shell
flutter analyze
```
- To lint whole project use:
```shell
dart format .
```
50 changes: 50 additions & 0 deletions lib/Data/Data_sources/suggestion_data_source.dart
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');
}
}
}
51 changes: 51 additions & 0 deletions lib/Data/Models/suggestion_model.dart
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(),
};
}
}
20 changes: 20 additions & 0 deletions lib/Data/Repositories/suggestion_repository_impl.dart
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);
}
}
Loading

0 comments on commit bb71749

Please sign in to comment.