def user_info(): data = get_token_data() if not data: return utils.response_bad_request('Erro desconhecido.') user,error = user_service.get_user(data.get('id',0)) if error: return utils.response_bad_request(error) return utils.response_ok(info_schema.dump(user))
def delete_establishment(id): establishment, error = establishment_service.get_establishment(id) if error: return utils.response_bad_request(error) elif establishment is None: return utils.response_not_found('Estabelecimento não encontrado.') success, error = establishment_service.delete_establishment(establishment) if error: return utils.response_bad_request(error) return utils.response_ok('Estabelecimento excluído com sucesso.')
def home_data_chart(): data = request.get_json() errors = schema.validate(data) if errors: return utils.response_bad_request(errors) params = json.dumps(data) result = db.engine.execute( 'SELECT * FROM f_view_chart_vendas(\':json\')'.replace( ':json', params)) vendas = ChartVendas(many=True).dump(list(result)) result = db.engine.execute( 'SELECT * FROM f_view_chart_servicos_cancelados(\':json\')'.replace( ':json', params)) cancelados = ChartCancelados(many=True).dump(list(result)) result = db.engine.execute( 'SELECT * FROM f_view_chart_ranking_clientes(\':json\')'.replace( ':json', params)) clientes = ChartRankingClientes(many=True).dump(list(result)) result = db.engine.execute( 'SELECT * FROM f_view_chart_tipos_servicos(\':json\')'.replace( ':json', params)) tipos_servicos = ChartTiposServicos(many=True).dump(list(result)) db.session.remove() return utils.response_ok({ 'vendas': vendas, 'cancelados': cancelados, 'clientes': clientes, 'tiposServicos': tipos_servicos })
def get_establishments(): establishments, error = establishment_service.get_establishments() if error: return utils.response_bad_request(error) return utils.response_ok( EstablishmentSchema(many=True).dump(establishments))
def user_login(): data = request.get_json() errors = login_schema.validate(data) if errors: return utils.response_bad_request(errors) user = user_service.get_user_by_email(data.get('email','').lower()) if not user: return utils.response_not_found('Usuário não encontrado.') valid_password = user_service.validate_password(data.get('password',''),user.password) if not valid_password: return utils.response_bad_request('E-mail e/ou senha inválidos') token = generate_token({'id':user.id}) return utils.response_ok(token)
def get_by_filters(): data = request.get_json() errors = schema_filters.validate(data) if errors: return utils.response_bad_request(errors) result = db.engine.execute('SELECT * FROM f_view_fluxo_caixa(\':json\')'.replace(':json', json.dumps(data))) return utils.response_ok(FluxoCaixaSchema(many=True).dump(list(result)))
def user_register(): data = request.get_json() errors = register_schema.validate(data) if errors: return utils.response_bad_request(errors) if user_service.get_user_by_email(data.get('email','').lower()): return utils.response_bad_request('E-mail já possui cadastro.') user = User(**data) user.email = user.email.lower() user,error = user_service.save_user(user) if error: return utils.response_server_error(error) return utils.response_created('Usuário criado com sucesso.')
def get_by_id_establishment(id): establishment, error = establishment_service.get_establishment(id) if error: return utils.response_bad_request(error) if establishment is None: return utils.response_not_found('Estabelecimento não encontrado.') return utils.response_ok(schema.dump(establishment))
def post(): json = request.get_json() error = cliente_schema.validate(json) if error: return utils.response_bad_request(error) success, error = cliente_service.saveCliente(json) if error: return utils.response_server_error(error) return utils.response_ok('Cliente cadastrado com sucesso.')
def cancelar(id): data = request.get_json() error = ServicoCancelarSchema().validate(data) if error: return response_bad_request(error) id, error = servico_service.cancel(id, data.get('motivo')) if error: return response_server_error(error) return response_ok('Serviço cancelado com sucesso.')
def save(): data = request.get_json() error = servico_schema.validate(data) if error: return response_bad_request(error) id, error = servico_service.save(servico_schema.load(data)) if error: return response_server_error(error) return response_ok({ 'message': 'Serviço cadastrado com sucesso.', 'id': id })
def put_establishment(id): establishment, error = establishment_service.get_establishment(id) if error: return utils.response_bad_request(error) elif establishment is None: return utils.response_not_found('Estabelecimento não encontrado.') data = request.get_json() errors = schema.validate(data) if errors: return utils.response_bad_request(errors) establishment.update(data, ['id']) establishment, error = establishment_service.save_establishment( establishment) if error: return utils.response_bad_request(error) return utils.response_ok(schema.dump(establishment))
def update(id): data = request.get_json() error = servico_schema.validate(data) if error: return response_bad_request(error) id, error = servico_service.update(servico_schema.load(data), id) if error: return response_server_error(error) print(type(data)) return response_ok('Serviço atualizado com sucesso.')
def updateCliente(id): json = request.get_json() error = cliente_schema.validate(json) if error: return utils.response_bad_request(error) cliente = cliente_service.getById(id) if not cliente: return utils.response_not_found('Cliente não encontrado.') success, error = cliente_service.updateCliente(cliente, json) if success: return utils.response_ok('Cliente atualizado com sucesso.') return utils.response_ok(error)
def save_images(id): data = request.get_json() error = ServicoImageSchema(many=True).validate(data) if error: return response_bad_request(error) servico = servico_service.get_by_id(id) if not servico: return response_server_error('Serviço não encontrado.') response, error = servico_service.save_images(id, data) if error: return response_server_error(error) return response_ok(response)
def post_establishment(): data = request.get_json() errors = schema.validate(data) if errors: return utils.response_bad_request(errors) establishment = Establishment(**data) establishment, error = establishment_service.save_establishment( establishment) if error: return utils.response_server_error(error) return utils.response_created(schema.dump(establishment))
def post(): data = request.get_json() print(data) errors = schema.validate(data) if errors: return utils.response_bad_request(errors) fluxo_caixa = FluxoCaixa(**schema.load(data)) try: db.session.add(fluxo_caixa) db.session.commit() db.session.remove() return utils.response_created('Registro salvo com sucesso.') except Exception as e: return utils.response_server_error(e.__str__())