Skip to content

Commit 3a48885

Browse files
author
MicBun
committed
kumpul
1 parent 2ebee82 commit 3a48885

12 files changed

+3026
-0
lines changed

.idea/.gitignore

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/Final-Project-Michael-Buntarman-PBD-Sanbercode-Batch-40.iml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/dataSources.xml

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/profiles_settings.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from dotenv import dotenv_values
2+
from flask import Flask
3+
from flask_jwt_extended import JWTManager
4+
from flask_restx import Api
5+
# from flask_restplus import Api
6+
from flask_sqlalchemy import SQLAlchemy
7+
8+
9+
env = dotenv_values(".env")
10+
11+
12+
class Config:
13+
MYSQL_USERNAME = env["MYSQL_USERNAME"]
14+
MYSQL_PASSWORD = env["MYSQL_PASSWORD"]
15+
MYSQL_HOST = env["MYSQL_HOST"]
16+
MYSQL_PORT = env["MYSQL_PORT"]
17+
MYSQL_DB = env["MYSQL_DB"]
18+
19+
20+
SQLALCHEMY_DATABASE_URL = f"mysql+mysqlconnector://{Config.MYSQL_USERNAME}:{Config.MYSQL_PASSWORD}@" \
21+
f"{Config.MYSQL_HOST}:{Config.MYSQL_PORT}/{Config.MYSQL_DB}"
22+
app = Flask(__name__)
23+
app.config['SQLALCHEMY_DATABASE_URI'] = SQLALCHEMY_DATABASE_URL
24+
app.config['JWT_SECRET_KEY'] = 'your_secret_key'
25+
rest_app = Api(app=app)
26+
product_space = rest_app.namespace('Product', description="CRUD Product")
27+
order_space = rest_app.namespace('Order', description="CRUD Order")
28+
database_space = rest_app.namespace('Database', description="Database")
29+
user_space = rest_app.namespace('Users', description="CRUD User")
30+
# rest_app.add_namespace(user_space)
31+
# rest_app.add_namespace(product_space)
32+
# rest_app.add_namespace(order_space)
33+
# rest_app.add_namespace(database_space)
34+
# rest_app.add_namespace(main_space)
35+
db = SQLAlchemy(app)
36+
jwt = JWTManager(app)
37+
from router import *
38+
39+
if __name__ == '__main__':
40+
app.run()

models.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
from dotenv import dotenv_values
2+
3+
import app
4+
5+
db = app.db
6+
7+
8+
class User(db.Model):
9+
__tablename__ = 'users'
10+
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
11+
name = db.Column(db.String(255))
12+
city = db.Column(db.String(255))
13+
state = db.Column(db.String(255))
14+
postal = db.Column(db.Integer)
15+
password = db.Column(db.String(255), default="password")
16+
orders = db.relationship('Order', back_populates='user')
17+
18+
def __init__(self, name, city, state, postal, password="password"):
19+
self.name = name
20+
self.city = city
21+
self.state = state
22+
self.postal = postal
23+
self.password = password
24+
25+
def __repr__(self):
26+
return f"User(name={self.name}, city={self.city}, state={self.state}, postal={self.postal})"
27+
28+
def to_dict(self):
29+
return {
30+
"id": self.id,
31+
"name": self.name,
32+
"city": self.city,
33+
"state": self.state,
34+
"postal": self.postal,
35+
"password": self.password
36+
}
37+
38+
class Product(db.Model):
39+
__tablename__ = 'products'
40+
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
41+
name = db.Column(db.String(255))
42+
category = db.Column(db.String(255))
43+
sub_category = db.Column(db.String(255))
44+
45+
def __init__(self, name, category, sub_category):
46+
self.name = name
47+
self.category = category
48+
self.sub_category = sub_category
49+
50+
def __repr__(self):
51+
return f"Product(name={self.name}, category={self.category}, sub_category={self.sub_category})"
52+
53+
def to_dict(self):
54+
return {
55+
"id": self.id,
56+
"name": self.name,
57+
"category": self.category,
58+
"sub_category": self.sub_category
59+
}
60+
61+
62+
class Order(db.Model):
63+
__tablename__ = 'orders'
64+
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
65+
user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
66+
product_id = db.Column(db.Integer, db.ForeignKey('products.id'))
67+
quantity = db.Column(db.Integer)
68+
user = db.relationship('User', back_populates='orders')
69+
product = db.relationship('Product')
70+
71+
def __init__(self, user_id, product_id, quantity):
72+
self.user_id = user_id
73+
self.product_id = product_id
74+
self.quantity = quantity
75+
76+
def __repr__(self):
77+
return f"Order(user_id={self.user_id}, product_id={self.product_id}, quantity={self.quantity})"
78+
79+
def to_dict(self):
80+
return {
81+
"id": self.id,
82+
"user_id": self.user_id,
83+
"product_id": self.product_id,
84+
"quantity": self.quantity
85+
}

0 commit comments

Comments
 (0)