def create_user(): """Create a new user record""" data = request.get_json(force=True) form = UserForm.from_json(data) if form.validate(): new_user = User(**form.data) db.session.add(new_user) db.session.commit() return jsonify(new_user.to_dict()), 201 else: return responses.invalid_request()
def create_user(): """List all breakfasts currently in the database""" data = request.get_json(force=True) form = UserForm.from_json(data) if form.validate(): new_user = UserModel() new_user.populate(form.data) new_user.save() return jsonify(new_user.to_dict()), 201 else: return responses.invalid_request()
def update_user(user_id): """Update a breakfast""" user = User.query.with_hint(User, "WITH (NOLOCK)").get(user_id) if user: form = UserForm.from_json(request.get_json(force=True), user) if form.validate(): form.populate_obj(user) db.session.add(user) db.session.commit() return jsonify(user.to_dict()) else: return responses.invalid_request() else: return "Does not exist", 404
def update_user_preference(user_id, ingredient_id): """Update a breakfast""" pref = UserPreferenceModel() try: pref.load_by_user_ingredient(user_id, ingredient_id) except ValueError: return "Does not exist", 404 form = UserPreferenceForm.from_json(request.get_json(force=True), pref) if form.validate(): pref.populate(form.data) pref.save() return jsonify(pref.to_dict()) else: return responses.invalid_request()
def update_user_preference(user_id, ingredient_id): """Update a breakfast""" user_preference = UserPreference.query.get((user_id, ingredient_id)) if user_preference: form = UserPreferenceUpdateForm.from_json(request.get_json(force=True), user_preference) if form.validate(): form.populate_obj(user_preference) db.session.add(user_preference) db.session.commit() return jsonify(user_preference.to_dict()) else: return responses.invalid_request() else: return "Does not exist", 404
def create_user_preferences(user_id): """Create a new user record""" user = User.query.with_hint(User, "WITH (NOLOCK)").get(user_id) if not user: return "Does not exist", 404 data = request.get_json(force=True) # set the user_id to the one being passed in data['user'] = user.id form = UserPreferenceForm.from_json(data) if form.validate(): new_user_preference = UserPreference(**form.data) db.session.add(new_user_preference) try: db.session.commit() except IntegrityError: # Attempted to create a duplicate user/ingredient relationship return responses.invalid_request() else: return jsonify(new_user_preference.to_dict()), 201 return responses.invalid_request()
def update_user(user_id): """Update a breakfast""" user = UserModel() try: user.load_by_id(user_id) except ValueError: return "Does not exist", 404 form = UserForm.from_json(request.get_json(force=True), user) if form.validate(): user.populate(form.data) user.save() return jsonify(user.to_dict()) else: return responses.invalid_request()
def create_user_preferences(user_id): """Create a new user record""" user = UserModel() try: user.load_by_id(user_id) except ValueError: return "Does not exist", 404 data = request.get_json(force=True) data['user_id'] = user.id form = UserPreferenceForm.from_json(data) if form.validate(): new_pref = UserPreferenceModel() new_pref.populate(form.data) new_pref.save() return jsonify(new_pref.to_dict()), 201 else: return responses.invalid_request() return jsonify({})