Ejemplo n.º 1
0
    def post(self, ids):
        """Posts a listing to the database.

        Args:
            ids (str): The listing id of the listing being posted.

        Returns:
            message: What happened with the post call.
        """

        # what the user will send to the post request (in good format)
        data = Listing.parser.parse_args()
        # In our case, the user sends the price as JSON, but the item name gets passed into the function
        # if user doesn't exist
        if not UserModel.find_by_google_tok(data['google_tok']):
            return {"message": "invalid google token, user does not exist in database"}
        if not BookModel.find_by_isbn(ids):  # if book doesn't exist
            return {"message": "invalid isbn, book model does not exist in database"}

        isbn = int(ids)

        item = ListingModel(data["price"], data["condition"],
                            isbn, data["google_tok"], data["status"])

        try:
            item.save_to_db()
        except:
            # internal server error
            return{"message": "An error occurred while inserting"}, 500

        return {"message": "post was successful"}, 201  # post was successful