Esempio n. 1
0
 def get(self):
     """
     GET method
     Return: dict (accounts)
     """
     data = parser.parse_args()
     if data['available']:
         available = str_to_bool(data['available'])
         motos = MotosModel.get_available_motos(available=available)
         return {'motos': [moto.json() for moto in motos]}, 200
     if data['license_number']:
         moto = MotosModel.find_by_license_number(
             license_number=data['license_number'])
         if moto is None:
             return {
                 'motos': [],
                 'message':
                 "Moto with license number [{}] Not found".format(
                     data['license_number'])
             }, 404
     motos = MotosModel.all_motos()
     return {'motos': [moto.json() for moto in motos]}, 200
Esempio n. 2
0
    def post(self):
        """
        POST method
        Adds a new account
        Return: dict (account created / message)
        """
        data = parser.parse_args()

        if not data['license_number']:
            return {'message': {"license_number": "Name cant be empty"}}, 400

        if not data['battery']:
            return {'message': {"battery": "Battery cant be empty"}}, 400

        if not data['longitude']:
            return {'message': {"longitude": "Longitude cant be empty"}}, 400

        if not data['latitude']:
            return {'message': {"latitude": "Latitude cant be empty"}}, 400

        if MotosModel.find_by_license_number(
                license_number=data['license_number']):
            return {
                'message':
                "Account with license number [{}] already exists".format(
                    data['license_number'])
            }, 409

        moto = MotosModel(license_number=data['license_number'],
                          battery=data['battery'],
                          latitude=data['latitude'],
                          longitude=data['longitude'])
        try:
            moto.save_to_db()
            return {'moto': MotosModel.find_by_id(moto.id).json()}, 201
        except:
            return {"message": "Something went wrong"}, 500