Code changes by azmat

This commit is contained in:
risnaman
2026-09-30 09:13:45 +05:30
parent c95a3f1581
commit 63ecc3e7e6
316 changed files with 28050 additions and 27216 deletions
+106 -106
View File
@@ -1,106 +1,106 @@
import 'package:dio/dio.dart';
import 'package:http_parser/http_parser.dart';
import 'dart:typed_data';
import 'dart:convert';
import '../../resources/api_constants.dart';
import '../LogoutService/Logoutservice.dart';
class ApiService {
final String baseUrl = ApiConstants.baseUrl;
final Dio dio = Dio();
Future<List<Map<String, dynamic>>> getEntities(String token) async {
try {
dio.options.headers['Authorization'] = 'Bearer $token';
final response = await dio.get('$baseUrl/Bookmarks/Bookmarks');
if (response.statusCode == 401) {
LogoutService.logout();
}
final entities = (response.data as List).cast<Map<String, dynamic>>();
return entities;
} catch (e) {
throw Exception('Failed to get all entities: $e');
}
}
Future<void> createEntity(
String token, Map<String, dynamic> fData, dynamic selectedFile) async {
try {
String apiUrl = "$baseUrl/Bookmarks/Bookmarks";
final Uint8List fileBytes = selectedFile.bytes!;
final mimeType = lookupMimeType(selectedFile.name!);
FormData formData = FormData.fromMap({
'file': MultipartFile.fromBytes(
fileBytes,
filename: selectedFile.name!,
contentType: MediaType.parse(mimeType!),
),
'data': jsonEncode(
fData), // Convert the map to JSON and include it as a parameter
});
Dio dio = Dio(); // Create a new Dio instance
dio.options.headers['Authorization'] = 'Bearer $token';
final response = await dio.post(apiUrl, data: formData);
if (response.statusCode == 401) {
LogoutService.logout();
}
if (response.statusCode == 200) {
// Handle successful response
print('File uploaded successfully');
} else {
print('Failed to upload file with status: ${response.statusCode}');
}
} catch (error) {
print('Error occurred during form submission: $error');
}
}
String lookupMimeType(String filePath) {
final ext = filePath.split('.').last;
switch (ext) {
case 'jpg':
case 'jpeg':
return 'image/jpeg';
case 'png':
return 'image/png';
case 'pdf':
return 'application/pdf';
// Add more cases for other file types as needed
default:
return 'application/octet-stream'; // Default MIME type
}
}
Future<void> updateEntity(
String token, int entityId, Map<String, dynamic> entity) async {
try {
dio.options.headers['Authorization'] = 'Bearer $token';
var response =
await dio.put('$baseUrl/Bookmarks/Bookmarks/$entityId', data: entity);
if (response.statusCode == 401) {
LogoutService.logout();
}
print(entity);
} catch (e) {
throw Exception('Failed to update entity: $e');
}
}
Future<void> deleteEntity(String token, int entityId) async {
try {
dio.options.headers['Authorization'] = 'Bearer $token';
var response = await dio.delete('$baseUrl/Bookmarks/Bookmarks/$entityId');
if (response.statusCode == 401) {
LogoutService.logout();
}
} catch (e) {
throw Exception('Failed to delete entity: $e');
}
}
}
import 'package:dio/dio.dart';
import 'package:http_parser/http_parser.dart';
import 'dart:typed_data';
import 'dart:convert';
import '../../resources/api_constants.dart';
import '../LogoutService/Logoutservice.dart';
class ApiService {
final String baseUrl = ApiConstants.baseUrl;
final Dio dio = Dio();
Future<List<Map<String, dynamic>>> getEntities(String token) async {
try {
dio.options.headers['Authorization'] = 'Bearer $token';
final response = await dio.get('$baseUrl/Bookmarks/Bookmarks');
if (response.statusCode == 401) {
LogoutService.logout();
}
final entities = (response.data as List).cast<Map<String, dynamic>>();
return entities;
} catch (e) {
throw Exception('Failed to get all entities: $e');
}
}
Future<void> createEntity(
String token, Map<String, dynamic> fData, dynamic selectedFile) async {
try {
String apiUrl = "$baseUrl/Bookmarks/Bookmarks";
final Uint8List fileBytes = selectedFile.bytes!;
final mimeType = lookupMimeType(selectedFile.name!);
FormData formData = FormData.fromMap({
'file': MultipartFile.fromBytes(
fileBytes,
filename: selectedFile.name!,
contentType: MediaType.parse(mimeType!),
),
'data': jsonEncode(
fData), // Convert the map to JSON and include it as a parameter
});
Dio dio = Dio(); // Create a new Dio instance
dio.options.headers['Authorization'] = 'Bearer $token';
final response = await dio.post(apiUrl, data: formData);
if (response.statusCode == 401) {
LogoutService.logout();
}
if (response.statusCode == 200) {
// Handle successful response
print('File uploaded successfully');
} else {
print('Failed to upload file with status: ${response.statusCode}');
}
} catch (error) {
print('Error occurred during form submission: $error');
}
}
String lookupMimeType(String filePath) {
final ext = filePath.split('.').last;
switch (ext) {
case 'jpg':
case 'jpeg':
return 'image/jpeg';
case 'png':
return 'image/png';
case 'pdf':
return 'application/pdf';
// Add more cases for other file types as needed
default:
return 'application/octet-stream'; // Default MIME type
}
}
Future<void> updateEntity(
String token, int entityId, Map<String, dynamic> entity) async {
try {
dio.options.headers['Authorization'] = 'Bearer $token';
var response =
await dio.put('$baseUrl/Bookmarks/Bookmarks/$entityId', data: entity);
if (response.statusCode == 401) {
LogoutService.logout();
}
print(entity);
} catch (e) {
throw Exception('Failed to update entity: $e');
}
}
Future<void> deleteEntity(String token, int entityId) async {
try {
dio.options.headers['Authorization'] = 'Bearer $token';
var response = await dio.delete('$baseUrl/Bookmarks/Bookmarks/$entityId');
if (response.statusCode == 401) {
LogoutService.logout();
}
} catch (e) {
throw Exception('Failed to delete entity: $e');
}
}
}
@@ -1,111 +1,111 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../../providers/token_manager.dart';
import 'Bookmarks_api_service.dart';
class CreateEntityScreen extends StatefulWidget {
const CreateEntityScreen({super.key});
@override
_CreateEntityScreenState createState() => _CreateEntityScreenState();
}
class _CreateEntityScreenState extends State<CreateEntityScreen> {
final ApiService apiService = ApiService();
final Map<String, dynamic> formData = {};
final _formKey = GlobalKey<FormState>();
var selectedFileupload_Field;
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Create Bookmarks')),
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(16),
child: Form(
key: _formKey,
child: Column(
children: [
TextFormField(
decoration:
const InputDecoration(labelText: 'bookmark_firstletter'),
onSaved: (value) => formData['bookmark_firstletter'] = value,
),
const SizedBox(height: 16),
TextFormField(
decoration: const InputDecoration(labelText: 'bookmark_link'),
onSaved: (value) => formData['bookmark_link'] = value,
),
const SizedBox(height: 16),
TextFormField(
decoration:
const InputDecoration(labelText: 'fileupload_field'),
onSaved: (value) => formData['fileupload_field'] = value,
),
const SizedBox(height: 16),
Container(
margin: const EdgeInsets.symmetric(vertical: 5), // Add margin
child: ElevatedButton(
onPressed: () async {
if (_formKey.currentState!.validate()) {
_formKey.currentState!.save();
final token = await TokenManager.getToken();
try {
print("token is : $token");
print(formData);
await apiService.createEntity(
token!, formData, selectedFileupload_Field);
Navigator.pop(context);
} catch (e) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Error'),
content: Text('Failed to create entity: $e'),
actions: [
TextButton(
child: const Text('OK'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}
}
},
child: Container(
width: double.infinity,
height: 50,
child: const Center(
child: Text(
'SUBMIT',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600,
),
),
),
),
),
),
],
),
),
),
),
);
}
}
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../../providers/token_manager.dart';
import 'Bookmarks_api_service.dart';
class CreateEntityScreen extends StatefulWidget {
const CreateEntityScreen({super.key});
@override
_CreateEntityScreenState createState() => _CreateEntityScreenState();
}
class _CreateEntityScreenState extends State<CreateEntityScreen> {
final ApiService apiService = ApiService();
final Map<String, dynamic> formData = {};
final _formKey = GlobalKey<FormState>();
var selectedFileupload_Field;
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Create Bookmarks')),
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(16),
child: Form(
key: _formKey,
child: Column(
children: [
TextFormField(
decoration:
const InputDecoration(labelText: 'bookmark_firstletter'),
onSaved: (value) => formData['bookmark_firstletter'] = value,
),
const SizedBox(height: 16),
TextFormField(
decoration: const InputDecoration(labelText: 'bookmark_link'),
onSaved: (value) => formData['bookmark_link'] = value,
),
const SizedBox(height: 16),
TextFormField(
decoration:
const InputDecoration(labelText: 'fileupload_field'),
onSaved: (value) => formData['fileupload_field'] = value,
),
const SizedBox(height: 16),
Container(
margin: const EdgeInsets.symmetric(vertical: 5), // Add margin
child: ElevatedButton(
onPressed: () async {
if (_formKey.currentState!.validate()) {
_formKey.currentState!.save();
final token = await TokenManager.getToken();
try {
print("token is : $token");
print(formData);
await apiService.createEntity(
token!, formData, selectedFileupload_Field);
Navigator.pop(context);
} catch (e) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Error'),
content: Text('Failed to create entity: $e'),
actions: [
TextButton(
child: const Text('OK'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}
}
},
child: Container(
width: double.infinity,
height: 50,
child: const Center(
child: Text(
'SUBMIT',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600,
),
),
),
),
),
),
],
),
),
),
),
);
}
}
File diff suppressed because it is too large Load Diff
@@ -1,140 +1,140 @@
import 'package:flutter/material.dart';
import 'dart:math';
import 'package:flutter/services.dart';
import '../../providers/token_manager.dart';
import 'Bookmarks_api_service.dart';
class UpdateEntityScreen extends StatefulWidget {
final Map<String, dynamic> entity;
UpdateEntityScreen({required this.entity});
@override
_UpdateEntityScreenState createState() => _UpdateEntityScreenState();
}
class _UpdateEntityScreenState extends State<UpdateEntityScreen> {
final ApiService apiService = ApiService();
final _formKey = GlobalKey<FormState>();
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Update Bookmarks')),
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(16),
child: Form(
key: _formKey,
child: Column(
children: [
TextFormField(
initialValue: widget.entity['bookmark_firstletter'],
decoration:
const InputDecoration(labelText: 'bookmark_firstletter'),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter a bookmark_firstletter';
}
return null;
},
onSaved: (value) {
widget.entity['bookmark_firstletter'] = value;
},
),
const SizedBox(height: 16),
TextFormField(
initialValue: widget.entity['bookmark_link'],
decoration: const InputDecoration(labelText: 'bookmark_link'),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter a bookmark_link';
}
return null;
},
onSaved: (value) {
widget.entity['bookmark_link'] = value;
},
),
const SizedBox(height: 16),
TextFormField(
initialValue: widget.entity['fileupload_field'],
decoration:
const InputDecoration(labelText: 'fileupload_field'),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter a fileupload_field';
}
return null;
},
onSaved: (value) {
widget.entity['fileupload_field'] = value;
},
),
const SizedBox(height: 16),
Container(
margin: const EdgeInsets.symmetric(vertical: 5), // Add margin
child: ElevatedButton(
onPressed: () async {
if (_formKey.currentState!.validate()) {
_formKey.currentState!.save();
final token = await TokenManager.getToken();
try {
await apiService.updateEntity(
token!,
widget.entity[
'id'], // Assuming 'id' is the key in your entity map
widget.entity);
Navigator.pop(context);
} catch (e) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Error'),
content: Text('Failed to update entity: $e'),
actions: [
TextButton(
child: const Text('OK'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}
}
},
child: Container(
width: double.infinity,
height: 50,
child: const Center(
child: Text(
'UPDATE',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600,
),
),
),
),
),
),
],
),
),
),
),
);
}
}
import 'package:flutter/material.dart';
import 'dart:math';
import 'package:flutter/services.dart';
import '../../providers/token_manager.dart';
import 'Bookmarks_api_service.dart';
class UpdateEntityScreen extends StatefulWidget {
final Map<String, dynamic> entity;
UpdateEntityScreen({required this.entity});
@override
_UpdateEntityScreenState createState() => _UpdateEntityScreenState();
}
class _UpdateEntityScreenState extends State<UpdateEntityScreen> {
final ApiService apiService = ApiService();
final _formKey = GlobalKey<FormState>();
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Update Bookmarks')),
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(16),
child: Form(
key: _formKey,
child: Column(
children: [
TextFormField(
initialValue: widget.entity['bookmark_firstletter'],
decoration:
const InputDecoration(labelText: 'bookmark_firstletter'),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter a bookmark_firstletter';
}
return null;
},
onSaved: (value) {
widget.entity['bookmark_firstletter'] = value;
},
),
const SizedBox(height: 16),
TextFormField(
initialValue: widget.entity['bookmark_link'],
decoration: const InputDecoration(labelText: 'bookmark_link'),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter a bookmark_link';
}
return null;
},
onSaved: (value) {
widget.entity['bookmark_link'] = value;
},
),
const SizedBox(height: 16),
TextFormField(
initialValue: widget.entity['fileupload_field'],
decoration:
const InputDecoration(labelText: 'fileupload_field'),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter a fileupload_field';
}
return null;
},
onSaved: (value) {
widget.entity['fileupload_field'] = value;
},
),
const SizedBox(height: 16),
Container(
margin: const EdgeInsets.symmetric(vertical: 5), // Add margin
child: ElevatedButton(
onPressed: () async {
if (_formKey.currentState!.validate()) {
_formKey.currentState!.save();
final token = await TokenManager.getToken();
try {
await apiService.updateEntity(
token!,
widget.entity[
'id'], // Assuming 'id' is the key in your entity map
widget.entity);
Navigator.pop(context);
} catch (e) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Error'),
content: Text('Failed to update entity: $e'),
actions: [
TextButton(
child: const Text('OK'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}
}
},
child: Container(
width: double.infinity,
height: 50,
child: const Center(
child: Text(
'UPDATE',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600,
),
),
),
),
),
),
],
),
),
),
),
);
}
}