#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# Before committing any changes, run the following (making sure that you have
# everything you need to do so):
#        flake8 --max-complexity 12 --ignore E303,W391 $PATH_TO_THIS_FILE

from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
from flask_restful import Resource, Api


webapp = Flask(__name__)
api = Api(webapp)
webapp.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///users.db'
webapp.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(webapp)
ma = Marshmallow(webapp)


class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(32), unique=True)
    password = db.Column(db.String(32))
    first_name = db.Column(db.String(32))
    last_name = db.Column(db.String(32))
    age = db.Column(db.Integer)

    def __init__(self, username, password, first_name, last_name, age):
        self.username = username
        self.password = password
        self.first_name = first_name
        self.last_name = last_name
        self.age = age


class UserSchema(ma.Schema):
    class Meta:
        fields = ('id', 'username', 'password', 'first_name', 'last_name',
                  'age')


user_schema = UserSchema()
users_schema = UserSchema(many=True)


class UserManager(Resource):
    @staticmethod
    def get():
        try:
            id = request.args['id']
        except Exception:
            id = None

        if not id:
            users = User.query.all()
            return jsonify(users_schema.dump(users))
        user = User.query.get(id)
        return jsonify(user_schema.dump(user))

    @staticmethod
    def post():
        username = request.json['username']
        password = request.json['password']
        first_name = request.json['first_name']
        last_name = request.json['last_name']
        age = request.json['age']

        user = User(username, password, first_name, last_name, age)
        db.session.add(user)
        db.session.commit()
        value = "User '%s %s' inserted." % (first_name, last_name)
        return jsonify({'Message': value})

    @staticmethod
    def put():
        try:
            id = request.args['id']
        except Exception:
            id = None
        if not id:
            return jsonify({'Message': 'Missing the user ID'})
        user = User.query.get(id)

        username = request.json['username']
        password = request.json['password']
        first_name = request.json['first_name']
        last_name = request.json['last_name']
        age = request.json['age']

        user.username = username
        user.password = password
        user.first_name = first_name
        user.last_name = last_name
        user.age = age

        db.session.commit()
        value = "User '%s %s' altered." % (first_name, last_name)
        return jsonify({'Message': value})

    @staticmethod
    def delete():
        try:
            id = request.args['id']
        except Exception:
            id = None
        if not id:
            return jsonify({'Message': 'Missing the user ID'})
        user = User.query.get(id)

        db.session.delete(user)
        db.session.commit()

        value = "User '%s' deleted." % str(id)
        return jsonify({'Message': value})


api.add_resource(UserManager, '/api/users')

if __name__ == '__main__':
    webapp.run(debug=True)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# Before committing any changes, run the following (making sure that you have
# everything you need to do so):
#        flake8 --max-complexity 12 --ignore E303,W391 $PATH_TO_THIS_FILE

from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
from flask_restful import Resource, Api


webapp = Flask(__name__)
api = Api(webapp)
webapp.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///users.db'
webapp.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(webapp)
ma = Marshmallow(webapp)


class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(32), unique=True)
    password = db.Column(db.String(32))
    first_name = db.Column(db.String(32))
    last_name = db.Column(db.String(32))
    age = db.Column(db.Integer)

    def __init__(self, username, password, first_name, last_name, age):
        self.username = username
        self.password = password
        self.first_name = first_name
        self.last_name = last_name
        self.age = age


class UserSchema(ma.Schema):
    class Meta:
        fields = ('id', 'username', 'password', 'first_name', 'last_name',
                  'age')


user_schema = UserSchema()
users_schema = UserSchema(many=True)


class UserManager(Resource):
    @staticmethod
    def get():
        try:
            id = request.args['id']
        except Exception:
            id = None

        if not id:
            users = User.query.all()
            return jsonify(users_schema.dump(users))
        user = User.query.get(id)
        return jsonify(user_schema.dump(user))

    @staticmethod
    def post():
        username = request.json['username']
        password = request.json['password']
        first_name = request.json['first_name']
        last_name = request.json['last_name']
        age = request.json['age']

        user = User(username, password, first_name, last_name, age)
        db.session.add(user)
        db.session.commit()
        value = "User '%s %s' inserted." % (first_name, last_name)
        return jsonify({'Message': value})

    @staticmethod
    def put():
        try:
            id = request.args['id']
        except Exception:
            id = None
        if not id:
            return jsonify({'Message': 'Missing the user ID'})
        user = User.query.get(id)

        username = request.json['username']
        password = request.json['password']
        first_name = request.json['first_name']
        last_name = request.json['last_name']
        age = request.json['age']

        user.username = username
        user.password = password
        user.first_name = first_name
        user.last_name = last_name
        user.age = age

        db.session.commit()
        value = "User '%s %s' altered." % (first_name, last_name)
        return jsonify({'Message': value})

    @staticmethod
    def delete():
        try:
            id = request.args['id']
        except Exception:
            id = None
        if not id:
            return jsonify({'Message': 'Missing the user ID'})
        user = User.query.get(id)

        db.session.delete(user)
        db.session.commit()

        value = "User '%s' deleted." % str(id)
        return jsonify({'Message': value})


api.add_resource(UserManager, '/api/users')

if __name__ == '__main__':
    webapp.run(debug=True)


#!/usr/bin/env python3 # -*- coding: utf-8 -*- # Before committing any changes, run the following (making sure that you have # everything you need to do so): #        flake8 --max-complexity 12 --ignore E303,W391 $PATH_TO_THIS_FILE