From 59dc85370e89030ecbcc40384f0b43e023d5aa58 Mon Sep 17 00:00:00 2001 From: Willian <47341070+wjrcode@users.noreply.github.com> Date: Thu, 13 Oct 2022 16:59:34 -0300 Subject: [PATCH] Item, Lista e limpeza --- lib/Controller/ListaController.dart | 146 +++++++++++++++++++++++++++ lib/Model/Generica.dart | 19 ---- lib/Model/Habito/HabitoModel.dart | 3 +- lib/Model/Item/ItemModel.dart | 29 ++++++ lib/Model/Lista/ListaModel.dart | 34 +++++++ lib/Repository/HabitoRepository.dart | 4 +- lib/Repository/ItemRepository.dart | 23 +++++ lib/Repository/ListaRepository.dart | 53 ++++++++++ 8 files changed, 288 insertions(+), 23 deletions(-) create mode 100644 lib/Controller/ListaController.dart delete mode 100644 lib/Model/Generica.dart create mode 100644 lib/Model/Item/ItemModel.dart create mode 100644 lib/Model/Lista/ListaModel.dart create mode 100644 lib/Repository/ItemRepository.dart create mode 100644 lib/Repository/ListaRepository.dart diff --git a/lib/Controller/ListaController.dart b/lib/Controller/ListaController.dart new file mode 100644 index 0000000..86e9a3e --- /dev/null +++ b/lib/Controller/ListaController.dart @@ -0,0 +1,146 @@ +import 'dart:ffi'; + +import 'package:flutter/material.dart'; +import 'package:http/http.dart'; +import 'package:organizei/Model/Item/ItemModel.dart'; +import 'package:organizei/Model/Lista/ListaModel.dart'; +import 'package:organizei/Controller/Base/Base.dart'; +import 'package:organizei/Repository/ListaRepository.dart'; +import 'package:organizei/services/persistencia/login.configuracoes.dart'; + +class ListaController extends Base { + ListaController(this.repository, this.context); + + final BuildContext context; + final ListaRepository repository; + final formKey = GlobalKey(); + var model = ListaModel(); + //var loginConfiguracoes = LoginConfiguracoes(); + + listaId(int? value) => model.id = value; + listaNome(String? value) => model.nome = value.toString(); + listaCor(String? value) => model.cor = value.toString(); + listaItens(List? value) => model.itens = value; + + var controllerNome = TextEditingController(); + var controllerCor = TextEditingController(); + + Future saveLista() async { + if (!formKey.currentState!.validate()) { + return false; + } + + formKey.currentState!.save(); + + try { + if (model.id == null) { + return await repository.addLista(model).then((value) async { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + elevation: 6.0, + behavior: SnackBarBehavior.floating, + shape: RoundedRectangleBorder( + side: const BorderSide(color: Colors.black, width: 3), + borderRadius: BorderRadius.circular(16), + ), + content: Text( + value.msg!, + style: const TextStyle( + color: Colors.black, + fontWeight: FontWeight.w500, + ), + ), + backgroundColor: value.valido! + ? const Color(0xFF74C198) + : const Color(0xFFEF7E69), + ), + ); + + await Future.delayed(const Duration(milliseconds: 500)); + + if (value.valido!) { + return value.valido!; + } else { + return false; + } + }); + } else { + return await repository.updateLista(model).then((value) async { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + elevation: 6.0, + behavior: SnackBarBehavior.floating, + shape: RoundedRectangleBorder( + side: const BorderSide(color: Colors.black, width: 3), + borderRadius: BorderRadius.circular(16), + ), + content: Text( + value.msg!, + style: const TextStyle( + color: Colors.black, + fontWeight: FontWeight.w500, + ), + ), + backgroundColor: value.valido! + ? const Color(0xFF74C198) + : const Color(0xFFEF7E69), + ), + ); + + await Future.delayed(const Duration(milliseconds: 500)); + + if (value.valido!) { + return value.valido!; + } else { + return false; + } + }); + } + } catch (e) { + print(e); + return false; + } + } + + Future?> getListas() async { + return await repository.getListas(); + } + + Future excluirLista() async { + try { + return await repository.excluirLista(model).then((value) async { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + elevation: 6.0, + behavior: SnackBarBehavior.floating, + shape: RoundedRectangleBorder( + side: const BorderSide(color: Colors.black, width: 3), + borderRadius: BorderRadius.circular(16), + ), + content: Text( + value.msg!, + style: const TextStyle( + color: Colors.black, + fontWeight: FontWeight.w500, + ), + ), + backgroundColor: value.valido! + ? const Color(0xFF74C198) + : const Color(0xFFEF7E69), + ), + ); + + await Future.delayed(const Duration(milliseconds: 500)); + + if (value.valido!) { + return value.valido!; + } else { + return false; + } + }); + } catch (e) { + print(e); + return false; + } + } +} diff --git a/lib/Model/Generica.dart b/lib/Model/Generica.dart deleted file mode 100644 index 293b3d5..0000000 --- a/lib/Model/Generica.dart +++ /dev/null @@ -1,19 +0,0 @@ -import 'package:organizei/Model/API/ResponseAPIModel.dart'; - -class GenericaModel { - String? tipo; - - ResponseAPIModel? responseAPIModel; - - GenericaModel({this.tipo}); - - GenericaModel.fromJson(Map json) { - tipo = json['tipo']; - } - - Map toJson() { - final Map data = new Map(); - data['tipo'] = this.tipo; - return data; - } -} diff --git a/lib/Model/Habito/HabitoModel.dart b/lib/Model/Habito/HabitoModel.dart index fcc5836..d6bcaf7 100644 --- a/lib/Model/Habito/HabitoModel.dart +++ b/lib/Model/Habito/HabitoModel.dart @@ -1,7 +1,6 @@ import 'package:organizei/Model/API/ResponseAPIModel.dart'; -import 'package:organizei/Model/Generica.dart'; -class HabitoModel extends GenericaModel { +class HabitoModel { int? id; int? idRotina; String? nome; diff --git a/lib/Model/Item/ItemModel.dart b/lib/Model/Item/ItemModel.dart new file mode 100644 index 0000000..304336d --- /dev/null +++ b/lib/Model/Item/ItemModel.dart @@ -0,0 +1,29 @@ +import 'package:organizei/Model/API/ResponseAPIModel.dart'; + +class ItemModel { + int? id; + String? nome; + bool? concluido; + ResponseAPIModel? responseAPIModel; + + ItemModel({ + this.id, + this.nome, + this.concluido, + }); + + ItemModel.fromJson(Map json) { + id = int.parse(json['id']); + nome = json['nome']; + concluido = json['concluido']; + } + + Map toJson() { + final Map data = new Map(); + data['id'] = id; + data['nome'] = nome; + data['concluido'] = concluido; + + return data; + } +} diff --git a/lib/Model/Lista/ListaModel.dart b/lib/Model/Lista/ListaModel.dart new file mode 100644 index 0000000..9bb5262 --- /dev/null +++ b/lib/Model/Lista/ListaModel.dart @@ -0,0 +1,34 @@ +import 'package:organizei/Model/API/ResponseAPIModel.dart'; +import 'package:organizei/Model/Item/ItemModel.dart'; + +class ListaModel { + int? id; + String? nome; + String? cor; + List? itens; + ResponseAPIModel? responseAPIModel; + + ListaModel({ + this.id, + this.nome, + this.cor, + this.itens, + }); + + ListaModel.fromJson(Map json) { + id = int.parse(json['id']); + nome = json['nome']; + cor = json['cor']; + itens = json['itens']; + } + + Map toJson() { + final Map data = new Map(); + data['id'] = id; + data['nome'] = nome; + data['cor'] = cor; + data['itens'] = itens; + + return data; + } +} diff --git a/lib/Repository/HabitoRepository.dart b/lib/Repository/HabitoRepository.dart index 800a5ed..6d455db 100644 --- a/lib/Repository/HabitoRepository.dart +++ b/lib/Repository/HabitoRepository.dart @@ -71,10 +71,10 @@ class HabitoRepository { final response = await http.get(_url, headers: ApiModel.headers); Map jsonMap = jsonDecode(response.body); - List listaProdutos = (jsonMap['habitos'] as List) + List listaHabitos = (jsonMap['habitos'] as List) .map((item) => HabitoModel.fromJson(item)) .toList(); - return listaProdutos; + return listaHabitos; } } diff --git a/lib/Repository/ItemRepository.dart b/lib/Repository/ItemRepository.dart new file mode 100644 index 0000000..31d78cd --- /dev/null +++ b/lib/Repository/ItemRepository.dart @@ -0,0 +1,23 @@ +import 'dart:convert'; + +import 'package:organizei/Model/API/APIModel.dart'; + +import 'package:http/http.dart' as http; +import 'package:organizei/Model/API/ResponseAPIModel.dart'; +import 'package:organizei/Model/Item/ItemModel.dart'; + +class ItemRepository { + Future concluirItem(ItemModel model, bool concluido) async { + var json = { + "concluido": concluido, + }; + + final response = await http.put( + Uri.parse( + ApiModel.ApiUrl + '/item/' + model.id.toString() + '/concluir'), + headers: ApiModel.headers, + body: jsonEncode(json)); + + return ResponseAPIModel.fromJson(jsonDecode(response.body)); + } +} diff --git a/lib/Repository/ListaRepository.dart b/lib/Repository/ListaRepository.dart new file mode 100644 index 0000000..30e7d7b --- /dev/null +++ b/lib/Repository/ListaRepository.dart @@ -0,0 +1,53 @@ +import 'dart:convert'; + +import 'package:organizei/Model/API/APIModel.dart'; + +import 'package:http/http.dart' as http; +import 'package:organizei/Model/API/ResponseAPIModel.dart'; +import 'package:organizei/Model/Lista/ListaModel.dart'; + +import '../Model/Item/ItemModel.dart'; + +class ListaRepository { + Future addLista(ListaModel model) async { + var json = {"nome": model.nome, "cor": model.cor, "itens": model.itens}; + + final response = await http.post(Uri.parse(ApiModel.ApiUrl + '/listas'), + headers: ApiModel.headers, body: jsonEncode(json)); + + return ResponseAPIModel.fromJson(jsonDecode(response.body)); + } + + Future updateLista(ListaModel model) async { + var json = {"nome": model.nome, "cor": model.cor, "itens": model.itens}; + + final response = await http.put( + Uri.parse(ApiModel.ApiUrl + '/listas/' + model.id.toString()), + headers: ApiModel.headers, + body: jsonEncode(json)); + + return ResponseAPIModel.fromJson(jsonDecode(response.body)); + } + + Future excluirLista(ListaModel model) async { + final response = await http.delete( + Uri.parse(ApiModel.ApiUrl + '/listas/' + model.id.toString()), + headers: ApiModel.headers); + + return ResponseAPIModel.fromJson(jsonDecode(response.body)); + } + + Future> getListas() async { + Uri _uriSearchProduto = Uri.parse(ApiModel.ApiUrl + '/listas'); + + var _url = Uri.parse(_uriSearchProduto.toString()); + final response = await http.get(_url, headers: ApiModel.headers); + + Map jsonMap = jsonDecode(response.body); + List listaProdutos = (jsonMap['listas'] as List) + .map((item) => ListaModel.fromJson(item)) + .toList(); + + return listaProdutos; + } +}