Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
appFluetter
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Nayeli Monserrat Velasco Lopez
appFluetter
Commits
a1d4ef42
Commit
a1d4ef42
authored
Aug 02, 2024
by
Nayeli Monserrat Velasco Lopez
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'nayeli.velasco' into 'master'
Crear categoria API See merge request
!9
parents
f5b6f189
0fdab128
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
105 additions
and
21 deletions
+105
-21
ArticuloController.dart
lib/src/controllers/ArticuloController.dart
+0
-0
CategoriaController.dart
lib/src/controllers/CategoriaController.dart
+47
-8
CategoryApi.dart
lib/src/http_api/CategoryApi.dart
+32
-5
NewCategoryPage.dart
lib/src/pages/NewCategoryPage.dart
+26
-8
No files found.
lib/src/controllers/ArticuloController.dart
View file @
a1d4ef42
lib/src/controllers/CategoriaController.dart
View file @
a1d4ef42
...
...
@@ -15,28 +15,67 @@ class CategoryController {
'data'
:
null
};
// Verificar la conectividad de la red
List
<
ConnectivityResult
>
connectivityResult
=
await
_connectivity
.
checkConnectivity
();
if
(!
connectivityResult
.
contains
(
ConnectivityResult
.
none
))
{
if
(
connectivityResult
.
contains
(
ConnectivityResult
.
wifi
)
||
connectivityResult
.
contains
(
ConnectivityResult
.
mobile
))
{
try
{
// Realizar la solicitud a la API
Map
<
String
,
dynamic
>
respGet
=
await
_categoryApi
.
getCategories
();
if
(
respGet
[
'statusCode'
]
==
200
)
{
try
{
var
decodeResp
=
json
.
decode
(
respGet
[
'body'
]);
List
<
CategoriaModel
>
listCategories
=
CategoriaModel
.
fromJsonArray
(
decodeResp
[
'data'
]);
List
<
CategoriaModel
>
listCategories
=
CategoriaModel
.
fromJsonArray
(
decodeResp
[
'data'
]);
mapResp
[
'ok'
]
=
true
;
mapResp
[
'message'
]
=
"
${listCategories.length}
categorías encontradas"
;
mapResp
[
'message'
]
=
"
${listCategories.length}
categorías encontradas"
;
mapResp
[
'data'
]
=
listCategories
;
}
catch
(
e
)
{
mapResp
[
'message'
]
=
"Error en procesamiento de datos:
$e
"
;
}
}
else
{
mapResp
[
'message'
]
=
"Error en la respuesta de la API:
${respGet['body']}
"
;
mapResp
[
'message'
]
=
"Error en la respuesta de la API:
${respGet['body']}
"
;
}
}
catch
(
e
)
{
mapResp
[
'message'
]
=
"Error en la solicitud a la API:
$e
"
;
}
}
else
{
mapResp
[
'message'
]
=
'No hay conexión a internet'
;
}
}
else
{
mapResp
[
'message'
]
=
'No hay conexión a internet'
;
}
return
mapResp
;
}
// Método para crear una categoría
Future
<
Map
<
String
,
dynamic
>>
createCategory
(
CategoriaModel
categoria
)
async
{
Map
<
String
,
dynamic
>
mapResp
=
{
'ok'
:
false
,
'message'
:
'No se pudo crear la categoría'
,
'data'
:
null
};
List
<
ConnectivityResult
>
connectivityResult
=
await
_connectivity
.
checkConnectivity
();
if
(!
connectivityResult
.
contains
(
ConnectivityResult
.
none
))
{
if
(
connectivityResult
.
contains
(
ConnectivityResult
.
wifi
)
||
connectivityResult
.
contains
(
ConnectivityResult
.
mobile
))
{
try
{
Map
<
String
,
dynamic
>
respPost
=
await
_categoryApi
.
postCategory
(
categoria
);
if
(
respPost
[
'statusCode'
]
==
200
||
respPost
[
'statusCode'
]
==
201
)
{
print
(
'Regresa datos '
);
try
{
var
decodeResp
=
json
.
decode
(
respPost
[
'body'
]);
CategoriaModel
newCategory
=
CategoriaModel
.
fromJson
(
decodeResp
[
'data'
]);
mapResp
[
'ok'
]
=
true
;
mapResp
[
'message'
]
=
"Categoría creada exitosamente"
;
mapResp
[
'data'
]
=
newCategory
;
}
catch
(
e
)
{
mapResp
[
'message'
]
=
"Error en procesamiento de datos:
$e
"
;
}
}
else
{
mapResp
[
'message'
]
=
"Error en la respuesta de la API:
${respPost['body']}
"
;
}
}
catch
(
e
)
{
mapResp
[
'message'
]
=
"Error en la solicitud a la API:
$e
"
;
...
...
lib/src/http_api/CategoryApi.dart
View file @
a1d4ef42
...
...
@@ -2,22 +2,19 @@ import 'dart:convert';
import
'package:http/http.dart'
as
http
;
import
'package:flutter/foundation.dart'
;
import
'package:miapp_flutter/environments/archivo.dart'
;
import
'package:miapp_flutter/src/models/CategoriaModel.dart'
;
class
CategoryApi
{
final
String
apiUrl
=
'categoria'
;
// Asegúrate de que esta URL sea correcta
final
String
apiUrl
=
'categoria'
;
// Método para obtener las categorías
Future
<
Map
<
String
,
dynamic
>>
getCategories
()
async
{
String
url
=
'
${apiApp}
/
$apiUrl
?offset=0&max=100'
;
if
(
kDebugMode
)
{
print
(
'Url ->
$url
'
);
}
try
{
final
response
=
await
http
.
get
(
Uri
.
parse
(
url
));
// Verifica el estado de la respuesta
if
(
response
.
statusCode
==
200
)
{
return
{
'statusCode'
:
response
.
statusCode
,
...
...
@@ -36,4 +33,34 @@ class CategoryApi {
};
}
}
Future
<
Map
<
String
,
dynamic
>>
postCategory
(
CategoriaModel
categoria
)
async
{
String
url
=
'
$apiApp
/
$apiUrl
'
;
if
(
kDebugMode
)
{
print
(
'Url ->
$url
'
);
}
try
{
final
response
=
await
http
.
post
(
Uri
.
parse
(
url
),
headers:
{
'Content-Type'
:
'application/json'
},
body:
json
.
encode
(
categoria
.
toJson
()),
);
print
(
'Status code:
${response.statusCode}
'
);
print
(
'Response body:
${response.body}
'
);
return
{
"statusCode"
:
response
.
statusCode
,
"body"
:
response
.
body
,
};
}
catch
(
e
)
{
return
{
"statusCode"
:
501
,
"body"
:
'
$e
'
,
};
}
}
}
lib/src/pages/NewCategoryPage.dart
View file @
a1d4ef42
import
'package:flutter/material.dart'
;
import
'package:intl/intl.dart'
;
import
'package:miapp_flutter/src/models/CategoriaModel.dart'
;
import
'dart:convert'
;
import
'../controllers/CategoriaController.dart'
;
class
NewCategoryPage
extends
StatefulWidget
{
const
NewCategoryPage
({
Key
?
key
})
:
super
(
key:
key
);
...
...
@@ -14,7 +17,7 @@ class _NewCategoryPageState extends State<NewCategoryPage> {
final
_claveController
=
TextEditingController
();
final
_nombreController
=
TextEditingController
();
DateTime
_selectedDate
=
DateTime
.
now
();
final
_categoryController
=
CategoryController
();
@override
Widget
build
(
BuildContext
context
)
{
return
Scaffold
(
...
...
@@ -71,16 +74,31 @@ class _NewCategoryPageState extends State<NewCategoryPage> {
),
const
SizedBox
(
height:
16.0
),
ElevatedButton
(
onPressed:
()
{
onPressed:
()
async
{
if
(
_formKey
.
currentState
!.
validate
())
{
int
fechaCreado
=
_selectedDate
.
millisecondsSinceEpoch
;
print
({
"clave"
:
_claveController
.
text
,
"fechaCreado"
:
fechaCreado
,
"nombre"
:
_nombreController
.
text
});
CategoriaModel
newCategory
=
CategoriaModel
(
id:
0
,
key:
_claveController
.
text
,
name:
_nombreController
.
text
,
createdDate:
_selectedDate
.
millisecondsSinceEpoch
,
active:
true
,
);
var
response
=
await
_categoryController
.
createCategory
(
newCategory
);
print
(
'Datos de la categoría a enviar:
${json.encode(newCategory.toJson())}
'
);
print
(
response
);
if
(
response
[
'statusCode'
]
==
200
||
response
[
'statusCode'
]
==
201
)
{
ScaffoldMessenger
.
of
(
context
).
showSnackBar
(
SnackBar
(
content:
Text
(
'Error al crear categoría:
${response['body']}
'
)),
);
}
else
{
ScaffoldMessenger
.
of
(
context
).
showSnackBar
(
SnackBar
(
content:
Text
(
'Categoría creada con éxito'
)),
);
Navigator
.
pop
(
context
);
}
}
},
child:
const
Text
(
'GUARDAR'
),
style:
ElevatedButton
.
styleFrom
(
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment