first commit
This commit is contained in:
commit
a10790da60
BIN
__pycache__/config.cpython-311.pyc
Normal file
BIN
__pycache__/config.cpython-311.pyc
Normal file
Binary file not shown.
16
app/__init__.py
Normal file
16
app/__init__.py
Normal file
@ -0,0 +1,16 @@
|
||||
from flask import Flask
|
||||
from app.database import db
|
||||
from app.routes.user_routes import user_bp
|
||||
from app.routes.invoice_routes import invoice_bp
|
||||
|
||||
def create_app():
|
||||
app = Flask(__name__)
|
||||
app.config.from_object("config")
|
||||
|
||||
db.init_app(app)
|
||||
|
||||
# Registrar blueprints
|
||||
app.register_blueprint(user_bp, url_prefix="/users")
|
||||
app.register_blueprint(invoice_bp, url_prefix="/invoices")
|
||||
|
||||
return app
|
||||
BIN
app/__pycache__/__init__.cpython-311.pyc
Normal file
BIN
app/__pycache__/__init__.cpython-311.pyc
Normal file
Binary file not shown.
BIN
app/__pycache__/database.cpython-311.pyc
Normal file
BIN
app/__pycache__/database.cpython-311.pyc
Normal file
Binary file not shown.
3
app/database.py
Normal file
3
app/database.py
Normal file
@ -0,0 +1,3 @@
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
|
||||
db = SQLAlchemy()
|
||||
0
app/models/__init__.py
Normal file
0
app/models/__init__.py
Normal file
BIN
app/models/__pycache__/__init__.cpython-311.pyc
Normal file
BIN
app/models/__pycache__/__init__.cpython-311.pyc
Normal file
Binary file not shown.
BIN
app/models/__pycache__/invoice.cpython-311.pyc
Normal file
BIN
app/models/__pycache__/invoice.cpython-311.pyc
Normal file
Binary file not shown.
BIN
app/models/__pycache__/user.cpython-311.pyc
Normal file
BIN
app/models/__pycache__/user.cpython-311.pyc
Normal file
Binary file not shown.
6
app/models/invoice.py
Normal file
6
app/models/invoice.py
Normal file
@ -0,0 +1,6 @@
|
||||
from app.database import db
|
||||
|
||||
class Invoice(db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
amount = db.Column(db.Float, nullable=False)
|
||||
user_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)
|
||||
7
app/models/user.py
Normal file
7
app/models/user.py
Normal file
@ -0,0 +1,7 @@
|
||||
from app.database import db
|
||||
|
||||
class User(db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
name = db.Column(db.String(100), nullable=False)
|
||||
email = db.Column(db.String(100), unique=True, nullable=False)
|
||||
invoices = db.relationship("Invoice", backref="user", lazy=True)
|
||||
0
app/routes/__init__.py
Normal file
0
app/routes/__init__.py
Normal file
BIN
app/routes/__pycache__/__init__.cpython-311.pyc
Normal file
BIN
app/routes/__pycache__/__init__.cpython-311.pyc
Normal file
Binary file not shown.
BIN
app/routes/__pycache__/invoice_routes.cpython-311.pyc
Normal file
BIN
app/routes/__pycache__/invoice_routes.cpython-311.pyc
Normal file
Binary file not shown.
BIN
app/routes/__pycache__/user_routes.cpython-311.pyc
Normal file
BIN
app/routes/__pycache__/user_routes.cpython-311.pyc
Normal file
Binary file not shown.
10
app/routes/invoice_routes.py
Normal file
10
app/routes/invoice_routes.py
Normal file
@ -0,0 +1,10 @@
|
||||
from flask import Blueprint, jsonify
|
||||
from app.models.invoice import Invoice
|
||||
from app.database import db
|
||||
|
||||
invoice_bp = Blueprint("invoices", __name__)
|
||||
|
||||
@invoice_bp.route("/", methods=["GET"])
|
||||
def get_invoices():
|
||||
invoices = Invoice.query.all()
|
||||
return jsonify([{"id": i.id, "amount": i.amount, "user_id": i.user_id} for i in invoices])
|
||||
23
app/routes/user_routes.py
Normal file
23
app/routes/user_routes.py
Normal file
@ -0,0 +1,23 @@
|
||||
from flask import Blueprint, jsonify, request
|
||||
from app.models.user import User
|
||||
from app.database import db
|
||||
|
||||
user_bp = Blueprint("users", __name__)
|
||||
|
||||
@user_bp.route("/", methods=["GET"])
|
||||
def get_users():
|
||||
users = User.query.all()
|
||||
return jsonify([{"id": u.id, "name": u.name, "email": u.email} for u in users])
|
||||
|
||||
@user_bp.route("/", methods=["POST"])
|
||||
def create_user():
|
||||
data = request.get_json()
|
||||
|
||||
if not data or not data.get("name") or not data.get("email"):
|
||||
return jsonify({"error": "Nome e email são obrigatórios"}), 400
|
||||
|
||||
new_user = User(name=data["name"], email=data["email"])
|
||||
db.session.add(new_user)
|
||||
db.session.commit()
|
||||
|
||||
return jsonify({"message": "Usuário criado com sucesso!", "id": new_user.id}), 201
|
||||
4
config.py
Normal file
4
config.py
Normal file
@ -0,0 +1,4 @@
|
||||
import os
|
||||
|
||||
SQLALCHEMY_DATABASE_URI = "sqlite:///database.db"
|
||||
SQLALCHEMY_TRACK_MODIFICATIONS = False
|
||||
BIN
instance/database.db
Normal file
BIN
instance/database.db
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user