class UsersController(Resource): def __init__(self): self.parser = reqparse.RequestParser() self.interface = FirebaseInterface() def get(self): dic = {"data": self.interface.getData("users")} data = json.dumps(dic) data_json = json.loads(data) return data_json def post(self): http_return_code = 201 result = request.get_json() try: User(result) self.interface.addData(result, "users", result["email"]) except Exception as e: http_return_code = 400 result = str(e) return result, http_return_code def put(self): http_return_code = 200 result = request.get_json() cpf = result["cpf"] status = result["status"] try: user = self.interface.getDataByField("users", "cpf", cpf) if user: user[0]["status"] = status self.interface.updateData(user[0], "users", user[0]["id"]) result = "Status atualizado com sucesso" else: result = "Usuario não encontrado" http_return_code = 400 except Exception as e: http_return_code = 400 result = str(e) return result, http_return_code
class SchedulesController(Resource): def __init__(self): self.parser = reqparse.RequestParser() self.interface = FirebaseInterface() self.schedule = Schedule() def post(self): req = request.get_json() try: office_id = req["id_oficina"] office = self.interface.getData("offices", office_id) if office is None: raise Exception("Oficina não encontrada") self.schedule.buildObject(req) self.schedule.validateFields(office["agenda"]) self.schedule.setId() office["agenda"].append(self.schedule.__dict__) self.interface.updateData(office, "offices", office["id"]) result = "Evento criado com sucesso" http_return_code = 201 except Exception as e: http_return_code = 400 result = str(e) return result, http_return_code def delete(self, office_id, event_id): try: office = self.interface.getData("offices", office_id) if office is None: raise Exception("Oficina não encontrada") index = self.schedule.findIdIndex(int(event_id), office["agenda"]) office["agenda"].pop(index) self.interface.updateData(office, "offices", office_id) result = "Evento removido com sucesso" http_return_code = 200 except Exception as e: http_return_code = 400 result = str(e) return result, http_return_code def get(self, office_id): try: office = self.interface.getData("offices", office_id) if office is None: raise Exception("Oficina não encontrada") schedule = {"data": office["agenda"]} data = json.dumps(schedule) result = json.loads(data) http_return_code = 200 except Exception as e: result = str(e) http_return_code = 400 return result, http_return_code def put(self): req = request.get_json() try: office_id = req["id_oficina"] office = self.interface.getData("offices", office_id) if office is None: raise Exception("Oficina não encontrada") self.schedule.buildObject(req) self.schedule.validateFields(office["agenda"]) id = req["id_evento"] index = self.schedule.findIdIndex(id, office["agenda"]) self.schedule.id = id office["agenda"][index] = self.schedule.__dict__ self.interface.updateData(office, "offices", office_id) result = "Evento alterado com sucesso" http_return_code = 200 except Exception as e: result = str(e) http_return_code = 400 return result, http_return_code
class ServicesController(Resource): def __init__(self): self.parser = reqparse.RequestParser() self.interface = FirebaseInterface() def get(self): try: dic = {"data": self.interface.getData("services")} data = json.dumps(dic) result = json.loads(data) http_return_code = 200 except Exception as e: result = str(e) http_return_code = 400 return result, http_return_code def post(self): req = request.get_json() try: name = req["nome"] price = req["preco"] service = Service(name, price) service.validateFields() self.interface.setData(service.__dict__, "services", name) result = "Serviço cadastrado com sucesso" http_return_code = 200 except Exception as e: result = str(e) http_return_code = 400 return result, http_return_code def delete(self, service): try: self.interface.deleteData("services", service) result = "Serviço removido com sucesso" http_return_code = 200 except Exception as e: result = str(e) http_return_code = 400 return result, http_return_code def put(self): req = request.get_json() try: name = req["nome"] price = req["preco"] service = Service(name, price) service.validateFields() self.interface.updateData(service.__dict__, "services", name) result = "Serviço alterado com sucesso" http_return_code = 200 except Exception as e: result = str(e) http_return_code = 400 return result, http_return_code
def setId(self): interface = FirebaseInterface() event_id = interface.getData("const_data", "office_id") self.id = event_id["id"] + 1 interface.updateData({"id": event_id["id"] + 1}, "const_data", "office_id")