Beispiel #1
0
    def put(self, name):
        data = Plant.parser.parse_args()
        plant = PlantModel.find_by_name(name)

        if plant is None:
            plant = PlantModel(name, data['quantity'], data['price'],
                               data['genus_name'])
        else:
            plant.quantity = data['quantity']
            plant.price = data['price']
        plant.save_to_db()

        return plant.json()
Beispiel #2
0
    def post(self, name):
        if PlantModel.find_by_name(name):
            return {
                'message':
                'An plant with name "{}" already exists'.format(name)
            }, 400  # 400 means bad request

        # Makes sure the item json the user sends to us has a price field.
        data = Plant.parser.parse_args()
        plant = PlantModel(name, data['quantity'], data['price'],
                           data['genus_name'])
        # Try and insert the POST data into the database.
        try:
            plant.save_to_db()
        except:
            return {
                "message": "An error occurred inserting the plant."
            }, 500  # Internal Server Error

        return plant.json(), 201
Beispiel #3
0
    def delete(self, name):
        plant = PlantModel.find_by_name(name)
        if plant:
            plant.delete_from_db()

        return {"message": "Plant deleted"}
Beispiel #4
0
 def get(self, name):
     plant = PlantModel.find_by_name(name)
     if plant:
         return plant.json()
     return {"message": "Plant not found"}, 404