def show(work_id): schema = WorkSchema() work = Work.get(id=work_id) if not work: abort(404) return schema.dumps(work)
def show(work_id): # This will serialize our data schema = WorkSchema() # This gets a work by ID work = Work.get(id=work_id) # If we can't find a work, send a 404 response if not work: abort(404) # otherwise, send back the work data as JSON return schema.dumps(work)
def update(work_id): schema = WorkSchema() work = Work.get(id=work_id) if not work: abort(404) try: data = schema.load(request.get_json()) work.set(**data) db.commit() except ValidationError as err: return jsonify({ 'message': 'Validation failed', 'errors': err.messages }), 422 return schema.dumps(work)
def create(): schema = WorkSchema() try: data = schema.load(request.get_json()) work = Work(**data) db.commit() except ValidationError as err: return jsonify({ 'message': 'Validation failed', 'errors': err.messages }), 422 return schema.dumps(work), 201
def create(): # This will deserialize the JSON from insomnia schema = WorkSchema() try: # attempt to convert the JSON into a dict data = schema.load(request.get_json()) # Use that to create a work object work = Work(**data, createdBy=g.current_user) # store it in the database db.commit() except ValidationError as err: # if the validation fails, send back a 422 response return jsonify({ 'message': 'Validation failed', 'errors': err.messages }), 422 # otherwise, send back the work data as JSON return schema.dumps(work), 201
def index(): # This will serialize our data # `many=True` because there are many works, ie we expect a list schema = WorkSchema(many=True) works = Work.select() # get all the works return schema.dumps(works) # `schema.dumps` converts the list to JSON
def index(): schema = WorkSchema(many=True) works = Work.select() return schema.dumps(works)