mirror of
https://github.com/sunrin-ana/2025-SSF-dotory-manage.git
synced 2026-03-09 18:10:02 +00:00
fix: buy product
This commit is contained in:
parent
20c99c6e1c
commit
b21fc45c7d
11 changed files with 91 additions and 56 deletions
8
app.py
8
app.py
|
|
@ -5,8 +5,8 @@ from db import db
|
||||||
from flask_migrate import Migrate
|
from flask_migrate import Migrate
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///dotori.db'
|
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///dotori.db"
|
||||||
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
|
||||||
|
|
||||||
db.init_app(app)
|
db.init_app(app)
|
||||||
migrate = Migrate(app, db)
|
migrate = Migrate(app, db)
|
||||||
|
|
@ -14,7 +14,7 @@ migrate = Migrate(app, db)
|
||||||
with app.app_context():
|
with app.app_context():
|
||||||
db.create_all()
|
db.create_all()
|
||||||
|
|
||||||
api = Api(app, doc='/docs')
|
api = Api(app, doc="/docs")
|
||||||
add_namespaces(api)
|
add_namespaces(api)
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
app.run(debug=True)
|
app.run(debug=True)
|
||||||
|
|
|
||||||
Binary file not shown.
Binary file not shown.
|
|
@ -2,8 +2,9 @@ from flask_sqlalchemy import SQLAlchemy
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from db import db
|
from db import db
|
||||||
|
|
||||||
|
|
||||||
class UserDotori(db.Model):
|
class UserDotori(db.Model):
|
||||||
__tablename__ = 'user_dotori'
|
__tablename__ = "user_dotori"
|
||||||
|
|
||||||
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
|
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
|
||||||
user_id = db.Column(db.Integer, nullable=False)
|
user_id = db.Column(db.Integer, nullable=False)
|
||||||
|
|
@ -13,17 +14,14 @@ class UserDotori(db.Model):
|
||||||
|
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
return {
|
return {
|
||||||
'user_id': self.user_id,
|
"user_id": self.user_id,
|
||||||
'dotori_count': self.dotori_count,
|
"dotori_count": self.dotori_count,
|
||||||
'created_at': self.created_at.isoformat(),
|
"created_at": self.created_at.isoformat(),
|
||||||
'updated_at': self.updated_at.isoformat()
|
"updated_at": self.updated_at.isoformat(),
|
||||||
}
|
}
|
||||||
|
|
||||||
def to_response(self):
|
def to_response(self):
|
||||||
return {
|
return {"user_id": self.user_id, "dotory": self.dotori_count}
|
||||||
'userId': self.user_id,
|
|
||||||
'dotory': self.dotori_count
|
|
||||||
}
|
|
||||||
|
|
||||||
def increment(self, amount=1):
|
def increment(self, amount=1):
|
||||||
self.dotori_count += amount
|
self.dotori_count += amount
|
||||||
|
|
@ -36,5 +34,6 @@ class UserDotori(db.Model):
|
||||||
self.updated_at = datetime.now()
|
self.updated_at = datetime.now()
|
||||||
return self.dotori_count
|
return self.dotori_count
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return f"<UserDotori user_id={self.user_id} dotori_count={self.dotori_count}>"
|
return f"<UserDotori user_id={self.user_id} dotori_count={self.dotori_count}>"
|
||||||
|
|
@ -3,6 +3,7 @@ from flask_restx import Api
|
||||||
from .dotori import dotori_ns
|
from .dotori import dotori_ns
|
||||||
from .product import product_ns
|
from .product import product_ns
|
||||||
|
|
||||||
|
|
||||||
def add_namespaces(api):
|
def add_namespaces(api):
|
||||||
api.add_namespace(dotori_ns, path='/api/dotory')
|
api.add_namespace(dotori_ns, path="/api/dotory")
|
||||||
api.add_namespace(product_ns, path='/api/buy')
|
api.add_namespace(product_ns, path="/api/buy")
|
||||||
|
|
|
||||||
Binary file not shown.
|
|
@ -1,37 +1,47 @@
|
||||||
from flask_restx import Namespace, Resource, fields
|
from flask_restx import Namespace, Resource, fields
|
||||||
from services.dotori_service import DotoriService
|
from services.dotori_service import DotoriService
|
||||||
|
|
||||||
dotori_ns = Namespace('dotory', description='도토리 관련 API')
|
dotori_ns = Namespace("dotory", description="도토리 관련 API")
|
||||||
|
|
||||||
user_model = dotori_ns.model('UserDotori', {
|
user_model = dotori_ns.model(
|
||||||
'user_id': fields.Integer,
|
"UserDotori",
|
||||||
'dotory': fields.Integer,
|
{
|
||||||
})
|
"user_id": fields.Integer,
|
||||||
|
"dotory": fields.Integer,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
@dotori_ns.route('/<int:userId>')
|
|
||||||
|
@dotori_ns.route("/<int:userId>")
|
||||||
class UserDotori(Resource):
|
class UserDotori(Resource):
|
||||||
@dotori_ns.marshal_with(user_model)
|
@dotori_ns.marshal_with(user_model)
|
||||||
def get(self, userId):
|
def get(self, userId):
|
||||||
user_dotori = DotoriService.get_user_dotori(userId)
|
user_dotori = DotoriService.get_user_dotori(userId)
|
||||||
if not user_dotori:
|
if not user_dotori:
|
||||||
dotori_ns.abort(404, 'User not found')
|
dotori_ns.abort(404, "User not found")
|
||||||
return user_dotori.to_response()
|
return user_dotori.to_response()
|
||||||
|
|
||||||
@dotori_ns.expect(dotori_ns.model('AddDotori', {'num': fields.Integer(required=True)}))
|
@dotori_ns.expect(
|
||||||
|
dotori_ns.model("AddDotori", {"num": fields.Integer(required=True)})
|
||||||
|
)
|
||||||
def put(self, userId):
|
def put(self, userId):
|
||||||
data = dotori_ns.payload
|
data = dotori_ns.payload
|
||||||
num = data.get('num')
|
num = data.get("num")
|
||||||
return DotoriService.add_dotori(userId, num).to_response()
|
return DotoriService.add_dotori(userId, num).to_response()
|
||||||
|
|
||||||
@dotori_ns.route('')
|
|
||||||
|
@dotori_ns.route("")
|
||||||
class InitializeDotori(Resource):
|
class InitializeDotori(Resource):
|
||||||
@dotori_ns.expect(dotori_ns.model('Initialize', {'user_id': fields.Integer(required=True)}))
|
@dotori_ns.expect(
|
||||||
|
dotori_ns.model("Initialize", {"user_id": fields.Integer(required=True)})
|
||||||
|
)
|
||||||
def post(self):
|
def post(self):
|
||||||
data = dotori_ns.payload
|
data = dotori_ns.payload
|
||||||
user_id = data.get('user_id')
|
user_id = data.get("user_id")
|
||||||
return DotoriService.initialize_user_dotori(user_id)
|
return DotoriService.initialize_user_dotori(user_id)
|
||||||
|
|
||||||
@dotori_ns.route('/all')
|
|
||||||
|
@dotori_ns.route("/all")
|
||||||
class GetAllDotori(Resource):
|
class GetAllDotori(Resource):
|
||||||
def get(self):
|
def get(self):
|
||||||
users = DotoriService.get_all_users_dotori()
|
users = DotoriService.get_all_users_dotori()
|
||||||
|
|
|
||||||
|
|
@ -1,37 +1,46 @@
|
||||||
from flask_restx import Namespace, Resource, fields
|
from flask_restx import Namespace, Resource, fields
|
||||||
from services.dotori_service import DotoriService
|
from services.dotori_service import DotoriService
|
||||||
|
|
||||||
product_ns = Namespace('buy', description='상품 구매 API')
|
product_ns = Namespace("buy", description="상품 구매 API")
|
||||||
|
|
||||||
buy_request_model = product_ns.model('BuyRequest', {
|
buy_request_model = product_ns.model(
|
||||||
'userId': fields.Integer(required=True),
|
"BuyRequest",
|
||||||
})
|
{
|
||||||
|
"user_id": fields.Integer(required=True),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
buy_response_model = product_ns.model('BuyResponse', {
|
buy_response_model = product_ns.model(
|
||||||
'isSuccess': fields.Boolean,
|
"BuyResponse",
|
||||||
'userId': fields.Integer,
|
{
|
||||||
'dotory': fields.Integer,
|
"isSuccess": fields.Boolean,
|
||||||
})
|
"user_id": fields.Integer,
|
||||||
|
"dotory": fields.Integer,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
@product_ns.route('/<int:productId>')
|
|
||||||
|
@product_ns.route("/<int:productId>")
|
||||||
class BuyProduct(Resource):
|
class BuyProduct(Resource):
|
||||||
@product_ns.expect(buy_request_model)
|
@product_ns.expect(buy_request_model)
|
||||||
@product_ns.response(200, 'Success', buy_response_model)
|
@product_ns.response(200, "Success", buy_response_model)
|
||||||
@product_ns.response(400, 'Failed')
|
@product_ns.response(400, "Failed")
|
||||||
def post(self, productId):
|
def post(self, productId):
|
||||||
data = product_ns.payload
|
data = product_ns.payload
|
||||||
user_id = data.get('user_id')
|
user_id = data.get("user_id")
|
||||||
if not user_id:
|
if not user_id:
|
||||||
product_ns.abort(400, 'userId is required')
|
product_ns.abort(400, "userId is required")
|
||||||
|
|
||||||
product_price = 100 # 가격이 얼마에요?
|
product_price = 100 # 가격이 얼마에요?
|
||||||
success = DotoriService.buy_product(user_id, product_price)
|
success = DotoriService.buy_product(user_id, product_price)
|
||||||
if success:
|
if success:
|
||||||
user_dotori = DotoriService.get_user_dotori(user_id)
|
user_dotori = DotoriService.get_user_dotori(user_id)
|
||||||
|
if user_dotori is None:
|
||||||
|
product_ns.abort(400, "Failed to Buy Product")
|
||||||
return {
|
return {
|
||||||
'isSuccess': True,
|
"isSuccess": True,
|
||||||
'user_id': user_id,
|
"user_id": user_id,
|
||||||
'dotory': user_dotori if user_dotori else 0
|
"dotory": user_dotori.to_response(),
|
||||||
}
|
}
|
||||||
else:
|
else:
|
||||||
product_ns.abort(400, 'Insufficient dotory or purchase failed')
|
product_ns.abort(400, "Insufficient dotory or purchase failed")
|
||||||
|
|
|
||||||
Binary file not shown.
|
|
@ -5,6 +5,8 @@ import sqlalchemy
|
||||||
from models.dotori import UserDotori
|
from models.dotori import UserDotori
|
||||||
from flask import current_app
|
from flask import current_app
|
||||||
from db import db
|
from db import db
|
||||||
|
|
||||||
|
|
||||||
class DotoriService:
|
class DotoriService:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_user_dotori(user_id: int):
|
def get_user_dotori(user_id: int):
|
||||||
|
|
@ -20,7 +22,17 @@ class DotoriService:
|
||||||
if not user_dotori:
|
if not user_dotori:
|
||||||
user_dotori = UserDotori(id=user_id, user_id=user_id, dotori_count=1000000)
|
user_dotori = UserDotori(id=user_id, user_id=user_id, dotori_count=1000000)
|
||||||
print(f"초기화 완료, {user_id}님의 도토리: {user_dotori.dotori_count}")
|
print(f"초기화 완료, {user_id}님의 도토리: {user_dotori.dotori_count}")
|
||||||
db.session.execute(sqlalchemy.text("INSERT INTO user_dotori (user_id, dotori_count, created_at, updated_at) VALUES (:user_id, :dotori_count, :created_at, :updated_at)"), {"user_id":user_id, "dotori_count":1000000, "created_at":datetime.now(), "updated_at":datetime.now()})
|
db.session.execute(
|
||||||
|
sqlalchemy.text(
|
||||||
|
"INSERT INTO user_dotori (user_id, dotori_count, created_at, updated_at) VALUES (:user_id, :dotori_count, :created_at, :updated_at)"
|
||||||
|
),
|
||||||
|
{
|
||||||
|
"user_id": user_id,
|
||||||
|
"dotori_count": 1000000,
|
||||||
|
"created_at": datetime.now(),
|
||||||
|
"updated_at": datetime.now(),
|
||||||
|
},
|
||||||
|
)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
return user_dotori.dotori_count
|
return user_dotori.dotori_count
|
||||||
|
|
||||||
|
|
@ -32,7 +44,9 @@ class DotoriService:
|
||||||
|
|
||||||
if user_dotori.dotori_count < product_price:
|
if user_dotori.dotori_count < product_price:
|
||||||
return False
|
return False
|
||||||
print(f"구매 완료, {user_id}님의 도토리: {user_dotori.dotori_count} -> {user_dotori.dotori_count - product_price}")
|
print(
|
||||||
|
f"구매 완료, {user_id}님의 도토리: {user_dotori.dotori_count} -> {user_dotori.dotori_count - product_price}"
|
||||||
|
)
|
||||||
user_dotori.decrement(product_price)
|
user_dotori.decrement(product_price)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
return True
|
return True
|
||||||
|
|
@ -43,7 +57,9 @@ class DotoriService:
|
||||||
if not user_dotori:
|
if not user_dotori:
|
||||||
user_dotori = UserDotori(user_id=user_id)
|
user_dotori = UserDotori(user_id=user_id)
|
||||||
db.session.add(user_dotori)
|
db.session.add(user_dotori)
|
||||||
print(f"도토리 추가, {user_id}님의 도토리: {user_dotori.dotori_count} -> {user_dotori.dotori_count + amount}")
|
print(
|
||||||
|
f"도토리 추가, {user_id}님의 도토리: {user_dotori.dotori_count} -> {user_dotori.dotori_count + amount}"
|
||||||
|
)
|
||||||
user_dotori.increment(amount)
|
user_dotori.increment(amount)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
return user_dotori
|
return user_dotori
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue