def get(self, id): """GET request that deals with requests that look for a portfolio by id""" # Request to the model portfolio = PortfolioModel.find_by_id(id) # if found if portfolio: return portfolio.json() # if not return {'message': 'Portfolio not found'}, 404
def delete(self, id): """DELETE request that deals with the deletion of book given its id""" # Look for the portfolio with a specific id Portfolio = PortfolioModel.find_by_id(id) # if exist if Portfolio: try: # we delete it Portfolio.delete_from_db() return {'message': 'Portfolio deleted'} except: # if error during deletion return {'message': 'A portfolio is linked to a many portfolio_book relations, so before deleting it you need to delete all these relations.'} else: # if doesn't exist return {'message' : 'Portfolio not found'}, 404
def put(self, id): """PUT request that deals with the edition or a creation of a portfolio with at a certain id""" # Parse the application/json data data = Portfolio.parser.parse_args() # Call the model portfolio = PortfolioModel.find_by_id(id) # if already exists we update if portfolio: portfolio.name = data['name'] else: # if doesn't we create portfolio = PortfolioModel(**data) # save and commit to database portfolio.save_to_db() # return the object as json return portfolio.json()