Skip to content

Commit

Permalink
Merge pull request #35 from FGA0138-MDS-Ajax/21-crud-usuario-funcionario
Browse files Browse the repository at this point in the history
21 crud usuario funcionario
  • Loading branch information
JAugustoM authored Dec 15, 2024
2 parents ba96dd9 + 96bf602 commit f6c59ed
Show file tree
Hide file tree
Showing 20 changed files with 479 additions and 136 deletions.
46 changes: 26 additions & 20 deletions catavento/lib/bloc/demanda_bloc.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import 'dart:io';

import 'package:catavento/constants.dart';
import 'package:catavento/typedefs.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
// import 'package:image_picker/image_picker.dart';
import 'package:intl/intl.dart';
import 'package:supabase_flutter/supabase_flutter.dart';

part 'demanda_event.dart';
Expand All @@ -15,7 +16,7 @@ class DemandaBloc extends Bloc<DemandaEvent, DemandaState> {

DemandaEvent get initialState => DemandaLoading();

DemandaBloc() : super(LoadingState([], {})) {
DemandaBloc() : super(DemandaLoadingState([], {})) {
on<DemandaFilter>(_onFilter);

on<DemandaLoading>(_onLoading);
Expand Down Expand Up @@ -48,9 +49,9 @@ class DemandaBloc extends Bloc<DemandaEvent, DemandaState> {
final metaData = _countDemandas();

if (newData.isEmpty) {
emit(FilterState(currentData, metaData));
emit(DemandaFilterState(currentData, metaData));
} else {
emit(FilterState(newData, metaData));
emit(DemandaFilterState(newData, metaData));
}
}

Expand All @@ -60,7 +61,7 @@ class DemandaBloc extends Bloc<DemandaEvent, DemandaState> {

final metaData = _countDemandas();

emit(LoadingState(currentData, metaData));
emit(DemandaLoadingState(currentData, metaData));
}

void _onCreate(DemandaCreate event, Emitter<DemandaState> emit) async {
Expand All @@ -75,15 +76,23 @@ class DemandaBloc extends Bloc<DemandaEvent, DemandaState> {
}
}

final dataAdicao = DateFormat(timeFormat).format(DateTime.now());

print('Teste');
print(event.descricao);

final demanda = {
'nomeDemanda': event.nomeDemanda,
'codigo': event.codigo,
'descricao': event.descricao,
'status': event.status,
'nome_demanda': event.nomeDemanda,
'descricao': event.descricao == '' ? 'Bolo normal' : event.descricao,
'status': 'Pendente',
'status_cobertura': 0,
'status_aplique': 0,
'data_adicao': dataAdicao,
'prioridade': 'media',
};

if (fotoUrl != null) {
demanda['imagemUrl'] = fotoUrl;
demanda['image_url'] = fotoUrl;
}

try {
Expand All @@ -99,7 +108,7 @@ class DemandaBloc extends Bloc<DemandaEvent, DemandaState> {

final metaData = _countDemandas();

emit(CreateState(currentData, metaData));
emit(DemandaCreateState(currentData, metaData));
}

void _onDelete(DemandaDelete event, Emitter<DemandaState> emit) async {
Expand All @@ -115,29 +124,26 @@ class DemandaBloc extends Bloc<DemandaEvent, DemandaState> {

final metaData = _countDemandas();

emit(DeleteState(currentData, metaData));
emit(DemandaDeleteState(currentData, metaData));
}

void _onUpdate(DemandaUpdate event, Emitter<DemandaState> emit) async {
try {
final nomeDemanda = event.nomeDemanda;
final codigo = event.codigo;
final descricao = event.descricao;
final order = event.order;

await supabase.from('demandas').update({
'nomeDemanda': nomeDemanda,
'codigo': codigo,
'nome_demanda': nomeDemanda,
'descricao': descricao,
}).eq('id', event.id);

currentData[order]['nomeDemanda'] = nomeDemanda;
currentData[order]['codigo'] = codigo;
currentData[order]['nome_demanda'] = nomeDemanda;
currentData[order]['descricao'] = descricao;

final metaData = _countDemandas();

emit(UpdateState(currentData, metaData));
emit(DemandaUpdateState(currentData, metaData));
} catch (e) {
print("Erro ao atualizar demanda: $e");
}
Expand All @@ -152,9 +158,9 @@ class DemandaBloc extends Bloc<DemandaEvent, DemandaState> {
switch (data['status']) {
case '0' || 'Pendente':
espera++;
case '1':
case '1' || 'Em fabricação':
fabricacao++;
case '2':
case '2' || 'Finalizado':
completo++;
}
}
Expand Down
4 changes: 2 additions & 2 deletions catavento/lib/bloc/demanda_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class DemandaDelete extends DemandaEvent {
final int id;
final int order;

DemandaDelete(this.id, this.order);
const DemandaDelete(this.id, this.order);
}

class DemandaUpdate extends DemandaEvent {
Expand All @@ -41,7 +41,7 @@ class DemandaUpdate extends DemandaEvent {
final String codigo;
final String descricao;

DemandaUpdate(
const DemandaUpdate(
this.id,
this.order,
this.nomeDemanda,
Expand Down
70 changes: 10 additions & 60 deletions catavento/lib/bloc/demanda_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,72 +7,22 @@ sealed class DemandaState {
const DemandaState(this.databaseResponse, this.metaData);
}

class CreateState extends DemandaState {
CreateState(super.databaseResponse, super.metaData);
class DemandaCreateState extends DemandaState {
DemandaCreateState(super.databaseResponse, super.metaData);
}

class DeleteState extends DemandaState {
DeleteState(super.databaseResponse, super.metaData);
class DemandaDeleteState extends DemandaState {
DemandaDeleteState(super.databaseResponse, super.metaData);
}

class UpdateState extends DemandaState {
UpdateState(super.databaseResponse, super.metaData);
class DemandaUpdateState extends DemandaState {
DemandaUpdateState(super.databaseResponse, super.metaData);
}

class LoadingState extends DemandaState {
LoadingState(super.databaseResponse, super.metaData);
class DemandaLoadingState extends DemandaState {
DemandaLoadingState(super.databaseResponse, super.metaData);
}

class FilterState extends DemandaState {
FilterState(super.databaseResponse, super.metaData);
}

class PhotoSelectedState extends DemandaState {
final File? fotoSelecionada;

PhotoSelectedState(
super.databaseResponse,
super.metaData,
this.fotoSelecionada,
);
}

class PhotoUploadedState extends DemandaState {
final String fotoUrl;

PhotoUploadedState(
super.databaseResponse,
super.metaData,
this.fotoUrl,
);
}

class PhotoSelectionErrorState extends DemandaState {
final String message;

PhotoSelectionErrorState(
super.databaseResponse,
super.metaData,
this.message,
);
}

class PhotoUploadErrorState extends DemandaState {
final String error;

PhotoUploadErrorState(
super.databaseResponse,
super.metaData,
this.error,
);
}

class PhotoUploadSuccessState extends DemandaState {
final String publicUrl;

PhotoUploadSuccessState(
super.databaseResponse,
super.metaData,
this.publicUrl,
);
class DemandaFilterState extends DemandaState {
DemandaFilterState(super.databaseResponse, super.metaData);
}
122 changes: 122 additions & 0 deletions catavento/lib/bloc/usuario_bloc.dart
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;
}
}
47 changes: 47 additions & 0 deletions catavento/lib/bloc/usuario_event.dart
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 {}
Loading

0 comments on commit f6c59ed

Please sign in to comment.