#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ # @Version : Python 3.11.4 # @Software : Sublime Text 4 # @Author : StudentCWZ # @Email : StudentCWZ@outlook.com # @Date : 2023/10/29 00:08 # @File : user.py # @Description : """ from flask import Blueprint, request, jsonify from application.services import UserService from marshmallow import ValidationError from application.schemas import UserSchema user_api = Blueprint('user_api', __name__) @user_api.route('/users', methods=['GET']) def get_users(): users = UserService.get_all_users() return jsonify([user.to_dict() for user in users]) @user_api.route('/users/', methods=['GET']) def get_user(user_id): user = UserService.get_user_by_id(user_id) if user is None: return jsonify({'error': 'User not found'}), 404 return jsonify(user.to_dict()) @user_api.route('/users', methods=['POST']) def create_user(): user_schema = UserSchema() try: data = user_schema.load(request.json) except ValidationError as e: return jsonify(e.messages), 400 username = data.username password = data.password email = data.email user = UserService.create_user(username, password, email) return jsonify(user_schema.dump(user)), 201