refactor(all): refactor

This commit is contained in:
alex
2024-07-09 17:50:32 +03:00
parent f79b9f03ae
commit e180ff16b6
37 changed files with 1706 additions and 1123 deletions

View File

@@ -37,7 +37,7 @@ class RecipeBloc extends Bloc<RecipeEvent, RecipeState> {
}
final String? recipeID = state.recipeModelObj?.recipeID;
final response = await http.get(
Uri.parse(dotenv.env['SERVER_ROOT']! + '/recipe/show/$recipeID'),
Uri.parse(dotenv.env['SERVER_ROOT']! + '/recipes/show/$recipeID'),
headers: {
'Authorization': 'Bearer $token',
},
@@ -47,23 +47,29 @@ class RecipeBloc extends Bloc<RecipeEvent, RecipeState> {
if (data is Map<String, dynamic>) {
if (state is RecipeLoadedState) {
var currentState = state as RecipeLoadedState;
emit(currentState.copyWith(
emit(
currentState.copyWith(
recipeModelObj: currentState.recipeModelObj,
recipe: currentState.recipe,
token: currentState.token,
commentController: currentState.commentController,
comments: currentState.comments,
steps: currentState.steps,
author: currentState.author,),);
author: currentState.author,
),
);
} else {
emit(RecipeLoadedState(
emit(
RecipeLoadedState(
token: token,
recipe: data,
steps: data['steps'],
author: await _fetchAuthor(data['id']),
comments: fillCommentList(await _fetchComments(data['id'])),
tags: await _fetchTags(data['id']),
commentController: TextEditingController(),),);
commentController: TextEditingController(),
),
);
}
} else {
// Один или несколько ключевых полей отсутствуют, обрабатываем ошибку
@@ -87,7 +93,7 @@ class RecipeBloc extends Bloc<RecipeEvent, RecipeState> {
}
final int id = event.id;
final response = await http.post(
Uri.parse(dotenv.env['SERVER_ROOT']! + '/recipe/delete/$id'),
Uri.parse(dotenv.env['SERVER_ROOT']! + '/recipes/delete/$id'),
headers: {
'Authorization': 'Bearer $token',
},
@@ -125,7 +131,7 @@ class RecipeBloc extends Bloc<RecipeEvent, RecipeState> {
}
final response = await http.get(
Uri.parse(dotenv.env['SERVER_ROOT']! + '/recipe/getAuthor/$id'),
Uri.parse(dotenv.env['SERVER_ROOT']! + '/recipes/getAuthor/$id'),
headers: {
'Authorization': 'Bearer $token',
},
@@ -141,7 +147,7 @@ class RecipeBloc extends Bloc<RecipeEvent, RecipeState> {
AppRouter.navigatorKey.currentContext?.goNamed('authScreen');
}
final response = await http.get(
Uri.parse(dotenv.env['SERVER_ROOT']! + '/recipe/getComments/$id'),
Uri.parse(dotenv.env['SERVER_ROOT']! + '/recipes/getComments/$id'),
headers: {
'Authorization': 'Bearer $token',
},
@@ -159,7 +165,7 @@ class RecipeBloc extends Bloc<RecipeEvent, RecipeState> {
}
final response = await http.get(
Uri.parse(dotenv.env['SERVER_ROOT']! + '/recipe/getTags/$id'),
Uri.parse(dotenv.env['SERVER_ROOT']! + '/recipes/getTags/$id'),
headers: {
'Authorization': 'Bearer $token',
},
@@ -171,30 +177,35 @@ class RecipeBloc extends Bloc<RecipeEvent, RecipeState> {
List<CommentModel> fillCommentList(List<dynamic> comments) {
List<CommentModel> list = [];
for (int i = 0; i < comments.length; i++) {
list.add(CommentModel(
list.add(
CommentModel(
id: comments[i]['id'].toString(),
userImage: comments[i]['profile_photo_path'],
username: comments[i]['name'],
dateText: DateFormat('dd.MM').format(DateTime.parse(comments[i]['updated_at'])),
greetingText: comments[i]['body'],),);
greetingText: comments[i]['body'],
),
);
}
return list;
}
Future<http.Response> _sendCommentData(String? token, String body, int id) async {
String url = dotenv.env['SERVER_ROOT']! + '/recipe/createComment/$id';
String url = dotenv.env['SERVER_ROOT']! + '/recipes/createComment/$id';
final Map<String, dynamic> credentials = {
'id': id,
'body': body,
};
var response = await http.post(Uri.parse(url),
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer $token',
},
body: json.encode(credentials),
encoding: utf8,);
var response = await http.post(
Uri.parse(url),
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer $token',
},
body: json.encode(credentials),
encoding: utf8,
);
return response;
}