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
|
|
@ -3,6 +3,7 @@ from flask_restx import Api
|
|||
from .dotori import dotori_ns
|
||||
from .product import product_ns
|
||||
|
||||
|
||||
def add_namespaces(api):
|
||||
api.add_namespace(dotori_ns, path='/api/dotory')
|
||||
api.add_namespace(product_ns, path='/api/buy')
|
||||
api.add_namespace(dotori_ns, path="/api/dotory")
|
||||
api.add_namespace(product_ns, path="/api/buy")
|
||||
|
|
|
|||
Binary file not shown.
|
|
@ -1,38 +1,48 @@
|
|||
from flask_restx import Namespace, Resource, fields
|
||||
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_id': fields.Integer,
|
||||
'dotory': fields.Integer,
|
||||
})
|
||||
user_model = dotori_ns.model(
|
||||
"UserDotori",
|
||||
{
|
||||
"user_id": fields.Integer,
|
||||
"dotory": fields.Integer,
|
||||
},
|
||||
)
|
||||
|
||||
@dotori_ns.route('/<int:userId>')
|
||||
|
||||
@dotori_ns.route("/<int:userId>")
|
||||
class UserDotori(Resource):
|
||||
@dotori_ns.marshal_with(user_model)
|
||||
def get(self, userId):
|
||||
user_dotori = DotoriService.get_user_dotori(userId)
|
||||
if not user_dotori:
|
||||
dotori_ns.abort(404, 'User not found')
|
||||
dotori_ns.abort(404, "User not found")
|
||||
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):
|
||||
data = dotori_ns.payload
|
||||
num = data.get('num')
|
||||
num = data.get("num")
|
||||
return DotoriService.add_dotori(userId, num).to_response()
|
||||
|
||||
@dotori_ns.route('')
|
||||
|
||||
@dotori_ns.route("")
|
||||
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):
|
||||
data = dotori_ns.payload
|
||||
user_id = data.get('user_id')
|
||||
user_id = data.get("user_id")
|
||||
return DotoriService.initialize_user_dotori(user_id)
|
||||
|
||||
@dotori_ns.route('/all')
|
||||
|
||||
@dotori_ns.route("/all")
|
||||
class GetAllDotori(Resource):
|
||||
def get(self):
|
||||
users = DotoriService.get_all_users_dotori()
|
||||
return users
|
||||
return users
|
||||
|
|
|
|||
|
|
@ -1,37 +1,46 @@
|
|||
from flask_restx import Namespace, Resource, fields
|
||||
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', {
|
||||
'userId': fields.Integer(required=True),
|
||||
})
|
||||
buy_request_model = product_ns.model(
|
||||
"BuyRequest",
|
||||
{
|
||||
"user_id": fields.Integer(required=True),
|
||||
},
|
||||
)
|
||||
|
||||
buy_response_model = product_ns.model('BuyResponse', {
|
||||
'isSuccess': fields.Boolean,
|
||||
'userId': fields.Integer,
|
||||
'dotory': fields.Integer,
|
||||
})
|
||||
buy_response_model = product_ns.model(
|
||||
"BuyResponse",
|
||||
{
|
||||
"isSuccess": fields.Boolean,
|
||||
"user_id": fields.Integer,
|
||||
"dotory": fields.Integer,
|
||||
},
|
||||
)
|
||||
|
||||
@product_ns.route('/<int:productId>')
|
||||
|
||||
@product_ns.route("/<int:productId>")
|
||||
class BuyProduct(Resource):
|
||||
@product_ns.expect(buy_request_model)
|
||||
@product_ns.response(200, 'Success', buy_response_model)
|
||||
@product_ns.response(400, 'Failed')
|
||||
@product_ns.response(200, "Success", buy_response_model)
|
||||
@product_ns.response(400, "Failed")
|
||||
def post(self, productId):
|
||||
data = product_ns.payload
|
||||
user_id = data.get('user_id')
|
||||
user_id = data.get("user_id")
|
||||
if not user_id:
|
||||
product_ns.abort(400, 'userId is required')
|
||||
|
||||
product_price = 100 # 가격이 얼마에요?
|
||||
product_ns.abort(400, "userId is required")
|
||||
|
||||
product_price = 100 # 가격이 얼마에요?
|
||||
success = DotoriService.buy_product(user_id, product_price)
|
||||
if success:
|
||||
user_dotori = DotoriService.get_user_dotori(user_id)
|
||||
if user_dotori is None:
|
||||
product_ns.abort(400, "Failed to Buy Product")
|
||||
return {
|
||||
'isSuccess': True,
|
||||
'user_id': user_id,
|
||||
'dotory': user_dotori if user_dotori else 0
|
||||
"isSuccess": True,
|
||||
"user_id": user_id,
|
||||
"dotory": user_dotori.to_response(),
|
||||
}
|
||||
else:
|
||||
product_ns.abort(400, 'Insufficient dotory or purchase failed')
|
||||
product_ns.abort(400, "Insufficient dotory or purchase failed")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue