def delete_watchlist_by_id(id): if is_not_valid(id): return create_response(is_not_valid(id), 400) current_user_id = get_jwt_identity() watchlist = db['watchlists'].find_one({'_id': ObjectId(id)}) if not watchlist: return create_response(f'Watchlist with id: {id} does not exist', 404) if str(watchlist['userId']) != current_user_id: return create_response(f'User not authorized to delete this watchlist', 403) db['watchlists'].delete_one({'_id': ObjectId(id)}) return create_response()
def get_matching_cars(id): if is_not_valid(id): return create_response(is_not_valid(id), 400) current_user_id = get_jwt_identity() watchlist = db['watchlists'].find_one({'_id': ObjectId(id)}) if not watchlist: return create_response(f'Watchlist with id: {id} does not exist', 404) if str(watchlist['userId']) != current_user_id: return create_response(f'User not authorized to view this watchlist', 403) limit = int(request.args.get('limit', 10)) (cars, count) = fetch_matching_cars(db, watchlist, limit=limit) resp = create_response(json.dumps(cars, default=str)) resp.headers['X-Total-Count'] = str(count) return resp
def add_watchlist(): current_user_id = get_jwt_identity() req = request.get_json() watchlist = { 'userId': ObjectId(current_user_id), 'make': req.get('make'), 'model': req.get('model'), } if req.get('fromYear') and req.get('toYear'): watchlist['year'] = { 'min': req.get('fromYear'), 'max': req.get('toYear') } if req.get('maxMileage'): watchlist['mileage'] = {'max': req.get('maxMileage')} if req.get('maxPrice'): watchlist['price'] = {'max': req.get('maxPrice')} db['watchlists'].insert_one(watchlist) return create_response()
def get_watchlists(): current_user_id = get_jwt_identity() watchlists = list(db['watchlists'].find( {'userId': ObjectId(current_user_id)})) return create_response(json.dumps(watchlists, default=str))