generated from FGA0138-MDS-Ajax/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from FGA0138-MDS-Ajax/21-crud-usuario-funcionario
21 crud usuario funcionario
- Loading branch information
Showing
20 changed files
with
479 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
// import 'dart:io'; | ||
|
||
// import 'package:catavento/bloc/demanda_bloc.dart'; | ||
// import 'package:catavento/constants.dart'; | ||
// import 'package:catavento/bloc/demanda_bloc.dart'; | ||
import 'package:catavento/typedefs.dart'; | ||
// import 'package:flutter/widgets.dart'; | ||
// import 'package:flutter/material.dart'; | ||
// import 'package:flutter/widgets.dart'; | ||
import 'package:flutter_bloc/flutter_bloc.dart'; | ||
// import 'package:http/http.dart'; | ||
// import 'package:intl/intl.dart'; | ||
import 'package:supabase_flutter/supabase_flutter.dart'; | ||
|
||
part 'usuario_event.dart'; | ||
part 'usuario_state.dart'; | ||
|
||
class UsuarioBloc extends Bloc<UsuarioEvent, UsuarioState> { | ||
final supabase = Supabase.instance.client; | ||
DatabaseResponse currentData = []; | ||
UsuarioEvent get initialState => UsuarioLoading(); | ||
|
||
UsuarioBloc() : super(UsuarioLoadingState([], {})) { | ||
on<UsuarioLoading>(_onLoading); | ||
|
||
on<UsuarioCreate>(_onCreate); | ||
|
||
on<UsuarioDelete>(_onDelete); | ||
|
||
on<UsuarioUpdate>(_onUpdate); | ||
} | ||
|
||
void _onCreate(UsuarioCreate event, Emitter<UsuarioState> emit) async { | ||
final usuario = { | ||
'usuario_nome': event.nome, | ||
'email': event.email, | ||
'setor': event.setor, | ||
'tipo': event.tipo | ||
}; | ||
|
||
try { | ||
final response = await supabase.from('usuarios').insert(usuario).select(); | ||
if (response.isNotEmpty) { | ||
currentData.add(response[0]); | ||
} else { | ||
throw Exception("Erro ao adiciona usuario"); | ||
} | ||
} catch (_) { | ||
throw Exception('Erro ao adicionar usuario'); | ||
} | ||
|
||
final metaData = _countUsuarios(); | ||
|
||
emit(UsuarioCreateState(currentData, metaData)); | ||
} | ||
|
||
void _onDelete(UsuarioDelete event, Emitter<UsuarioState> emit) async { | ||
try { | ||
final response = | ||
await supabase.from('usuarios').delete().eq('id', event.id).select(); | ||
if (response.isNotEmpty) { | ||
currentData.removeAt(event.order); | ||
} | ||
} catch (e) { | ||
print('Erro ao buscar dados: $e'); | ||
} | ||
|
||
final metaData = _countUsuarios(); | ||
|
||
emit(UsuarioDeleteState(currentData, metaData)); | ||
} | ||
|
||
void _onUpdate(UsuarioUpdate event, Emitter<UsuarioState> emit) async { | ||
try { | ||
final nome = event.nome; | ||
final setor = event.setor; | ||
final email = event.email; | ||
final tipo = event.tipo; | ||
final id = event.id; | ||
|
||
await supabase.from('usuarios').update({ | ||
'nome': nome, | ||
'setor': setor, | ||
'email': email, | ||
'tipo': tipo, | ||
}).eq('id', event.id); | ||
|
||
// verificar se pode usar o id | ||
currentData[id]['nome'] = nome; | ||
currentData[id]['setor'] = setor; | ||
currentData[id]['email'] = email; | ||
currentData[id]['tipo'] = tipo; | ||
|
||
final metaData = _countUsuarios(); | ||
|
||
emit(UsuarioUpdateState(currentData, metaData)); | ||
} catch (e) { | ||
print("Erro ao atualizar usuario: $e"); | ||
} | ||
} | ||
|
||
void _onLoading(UsuarioLoading event, Emitter<UsuarioState> emit) async { | ||
final response = await supabase.from('usuarios').select(); | ||
currentData = response; | ||
|
||
final metaData = _countUsuarios(); | ||
|
||
emit(UsuarioLoadingState(currentData, metaData)); | ||
} | ||
|
||
Map<String, int> _countUsuarios() { | ||
int numUsuarios = 0; | ||
|
||
for (var usuario in currentData) { | ||
if (usuario["tipo"] == "funcionario") { | ||
numUsuarios++; | ||
} | ||
} | ||
final metaData = {"total": numUsuarios}; | ||
return metaData; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
part of 'usuario_bloc.dart'; | ||
|
||
sealed class UsuarioEvent { | ||
const UsuarioEvent(); | ||
} | ||
|
||
class UsuarioCreate extends UsuarioEvent { | ||
final String nome; | ||
final String setor; | ||
final String email; | ||
final String tipo; | ||
|
||
const UsuarioCreate( | ||
this.nome, | ||
this.setor, | ||
this.email, | ||
this.tipo, | ||
); | ||
} | ||
|
||
class UsuarioDelete extends UsuarioEvent { | ||
final int id; | ||
final int order; | ||
|
||
const UsuarioDelete( | ||
this.id, | ||
this.order, | ||
); | ||
} | ||
|
||
class UsuarioUpdate extends UsuarioEvent { | ||
final int id; | ||
final String nome; | ||
final String setor; | ||
final String email; | ||
final String tipo; | ||
|
||
const UsuarioUpdate( | ||
this.nome, | ||
this.setor, | ||
this.email, | ||
this.tipo, | ||
this.id, | ||
); | ||
} | ||
|
||
class UsuarioLoading extends UsuarioEvent {} |
Oops, something went wrong.