forked from eatvio/eatvio_app
feat(recipe): Trying to fix restapi method call
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:eatvio_app/features/recipes/models/recipe_model.dart';
|
||||
import 'package:eatvio_app/features/recipes/services/recipe_service.dart';
|
||||
@@ -19,7 +20,37 @@ class RecipeRepository {
|
||||
|
||||
Future<RecipeModel> publishRecipe(RecipeModel recipe) async {
|
||||
try {
|
||||
return await recipeService.publishRecipe(jsonDecode(jsonEncode(recipe)));
|
||||
final File file = File(recipe.image ?? '');
|
||||
|
||||
List<Map<String, Object>> ingredientsMap = recipe.ingredients?.map((ingredient) {
|
||||
return {
|
||||
'name': ingredient!.name,
|
||||
'unit': ingredient.unit,
|
||||
'portion': ingredient.portion,
|
||||
};
|
||||
}).toList() ??
|
||||
[];
|
||||
|
||||
List<Map<String, Object>> stepsMap = recipe.steps?.map((step) {
|
||||
return {
|
||||
'image': File(step?.image ?? ""),
|
||||
'description': step?.description ?? "",
|
||||
};
|
||||
}).toList() ??
|
||||
[];
|
||||
print(ingredientsMap);
|
||||
print(stepsMap);
|
||||
return await recipeService.publishRecipe(
|
||||
recipe.type,
|
||||
recipe.state,
|
||||
recipe.title,
|
||||
recipe.description,
|
||||
recipe.video_url,
|
||||
ingredientsMap,
|
||||
stepsMap,
|
||||
recipe.cooking_time,
|
||||
file,
|
||||
);
|
||||
} catch (e) {
|
||||
rethrow;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:eatvio_app/features/recipes/models/ingredient.dart';
|
||||
import 'package:eatvio_app/features/recipes/models/recipe_model.dart';
|
||||
import 'package:eatvio_app/features/recipes/models/step.dart';
|
||||
import 'package:retrofit/retrofit.dart';
|
||||
import 'package:eatvio_app/features/diary/models/product.dart';
|
||||
|
||||
@@ -16,7 +21,17 @@ abstract class RecipeService {
|
||||
Future<Map<String, List<Product>>> getInputProduct(@Query('name') String name);
|
||||
|
||||
@POST('/api/recipes/create')
|
||||
Future<RecipeModel> publishRecipe(@Part() Map<String, Object?> recipe);
|
||||
Future<RecipeModel> publishRecipe(
|
||||
@Part() String? type,
|
||||
@Part() String? state,
|
||||
@Part() String? title,
|
||||
@Part() String? description,
|
||||
@Part() String? video_url,
|
||||
@Part() List<Map<String, Object>> ingredients,
|
||||
@Part() List<Map<String, Object>> steps,
|
||||
@Part() int? cooking_time,
|
||||
@Part() File image,
|
||||
);
|
||||
|
||||
@POST('/api/recipes/update/{id}')
|
||||
Future<RecipeModel> editRecipe(@Part() Map<String, Object?> recipe, String id);
|
||||
|
||||
@@ -77,11 +77,71 @@ class _RecipeService implements RecipeService {
|
||||
}
|
||||
|
||||
@override
|
||||
Future<RecipeModel> publishRecipe(Map<String, Object?> recipe) async {
|
||||
Future<RecipeModel> publishRecipe(
|
||||
String? type,
|
||||
String? state,
|
||||
String? title,
|
||||
String? description,
|
||||
String? video_url,
|
||||
List<Map<String, Object>> ingredients,
|
||||
List<Map<String, Object>> steps,
|
||||
int? cooking_time,
|
||||
File image,
|
||||
) async {
|
||||
final _extra = <String, dynamic>{};
|
||||
final queryParameters = <String, dynamic>{};
|
||||
queryParameters.removeWhere((k, v) => v == null);
|
||||
final _headers = <String, dynamic>{};
|
||||
final _data = FormData.fromMap(recipe);
|
||||
final _data = FormData();
|
||||
if (type != null) {
|
||||
_data.fields.add(MapEntry(
|
||||
'type',
|
||||
type,
|
||||
));
|
||||
}
|
||||
if (state != null) {
|
||||
_data.fields.add(MapEntry(
|
||||
'state',
|
||||
state,
|
||||
));
|
||||
}
|
||||
if (title != null) {
|
||||
_data.fields.add(MapEntry(
|
||||
'title',
|
||||
title,
|
||||
));
|
||||
}
|
||||
if (description != null) {
|
||||
_data.fields.add(MapEntry(
|
||||
'description',
|
||||
description,
|
||||
));
|
||||
}
|
||||
if (video_url != null) {
|
||||
_data.fields.add(MapEntry(
|
||||
'video_url',
|
||||
video_url,
|
||||
));
|
||||
}
|
||||
ingredients.forEach((i) {
|
||||
_data.fields.add(MapEntry('ingredients', jsonEncode(i)));
|
||||
});
|
||||
steps.forEach((i) {
|
||||
_data.fields.add(MapEntry('steps', jsonEncode(i)));
|
||||
});
|
||||
if (cooking_time != null) {
|
||||
_data.fields.add(MapEntry(
|
||||
'cooking_time',
|
||||
cooking_time.toString(),
|
||||
));
|
||||
}
|
||||
_data.files.add(MapEntry(
|
||||
'image',
|
||||
MultipartFile.fromFileSync(
|
||||
image.path,
|
||||
filename: image.path.split(Platform.pathSeparator).last,
|
||||
),
|
||||
));
|
||||
final _result = await _dio
|
||||
.fetch<Map<String, dynamic>>(_setStreamType<RecipeModel>(Options(
|
||||
method: 'POST',
|
||||
|
||||
@@ -69,6 +69,10 @@ Future<void> init() async {
|
||||
() => Dio(
|
||||
BaseOptions(
|
||||
baseUrl: baseUrl,
|
||||
headers: {
|
||||
'accept': 'application/json',
|
||||
'content-type': 'application/json',
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user