import 'dart:convert'; import 'package:didvan/models/note.dart'; import 'package:didvan/services/network/request.dart'; import 'package:didvan/services/network/request_helper.dart'; import 'package:http/http.dart' as RequestServices; class NoteService { static Future getNoteByPostId(int postId) async { try { final response = await RequestServices.get( Uri.parse('${RequestHelper.baseUrl2}/api/notes'), headers: { 'Authorization': 'Bearer ${RequestService.token}', }, ); print( 'Get note - PostId: $postId, Status: ${response.statusCode}, Body: ${response.body}'); if (response.statusCode == 200) { final List data = jsonDecode(response.body); print('Notes list: $data'); final note = data.firstWhere( (e) => e['postId'] == postId, orElse: () => null, ); print('Found note for PostId $postId: $note'); return note != null ? Note.fromJson(note) : null; } print('Failed to get note: ${response.statusCode}, ${response.body}'); return null; } catch (e) { print('Error in getNoteByPostId: $e'); return null; } } static Future saveNote(int postId, String content) async { try { final response = await RequestServices.post( Uri.parse('${RequestHelper.baseUrl2}/api/notes'), headers: { 'Authorization': 'Bearer ${RequestService.token}', 'Content-Type': 'application/json' }, body: jsonEncode({ 'postId': postId, 'content': content, }), ); print( 'Save note - PostId: $postId, Status: ${response.statusCode}, Body: ${response.body}'); if (response.statusCode == 200 || response.statusCode == 201) { try { final responseBody = jsonDecode(response.body); if (responseBody['postId'] == postId && responseBody['content'] == content) { return true; } else { print( 'Server returned success status but data mismatch: ${response.body}'); return false; } } catch (e) { print('Error parsing response body: $e'); return response.statusCode == 200 || response.statusCode == 201; } } else { print('Failed to save note: ${response.statusCode}, ${response.body}'); return false; } } catch (e) { print('Error in saveNote: $e'); return false; } } static Future deleteNote(int postId) async { final response = await RequestServices.delete( Uri.parse('${RequestHelper.baseUrl2}/api/notes/post/$postId'), headers: { 'Authorization': 'Bearer ${RequestService.token}', }, ); return response.statusCode == 200; } static Future updateNote(int postId, String content) async { try { final response = await RequestServices.put( Uri.parse('${RequestHelper.baseUrl2}/api/notes/post/$postId'), headers: { 'Authorization': 'Bearer ${RequestService.token}', 'Content-Type': 'application/json', }, body: jsonEncode({ 'postId': postId, 'content': content, }), ); print( 'Update note - PostId: $postId, Status: ${response.statusCode}, Body: ${response.body}'); if (response.statusCode == 200 || response.statusCode == 201) { try { final responseBody = jsonDecode(response.body); if (responseBody['postId'] == postId && responseBody['content'] == content) { return true; } else { print( 'Server returned success status but data mismatch: ${response.body}'); return false; } } catch (e) { print('Error parsing response body: $e'); return response.statusCode == 200 || response.statusCode == 201; } } else { print('Failed to update note: ${response.statusCode}, ${response.body}'); return false; } } catch (e) { print('Error in updateNote: $e'); return false; } } }