Update post_methods.py
Browse files- post_methods.py +11 -5
post_methods.py
CHANGED
|
@@ -1,22 +1,28 @@
|
|
| 1 |
-
from
|
|
|
|
| 2 |
|
| 3 |
def add_user():
|
|
|
|
| 4 |
try:
|
| 5 |
if not request.json or "name" not in request.json:
|
| 6 |
-
return jsonify({"error": "
|
| 7 |
|
| 8 |
db = get_db()
|
| 9 |
cursor = db.cursor()
|
| 10 |
-
cursor.execute(
|
| 11 |
-
|
|
|
|
|
|
|
| 12 |
db.commit()
|
|
|
|
| 13 |
return jsonify({
|
| 14 |
-
"message": "
|
| 15 |
"user": {
|
| 16 |
"id": cursor.lastrowid,
|
| 17 |
"name": request.json["name"],
|
| 18 |
"score": request.json.get("score", 0)
|
| 19 |
}
|
| 20 |
}), 201
|
|
|
|
| 21 |
except Exception as e:
|
| 22 |
return jsonify({"error": str(e)}), 500
|
|
|
|
| 1 |
+
from flask import jsonify, request
|
| 2 |
+
from database import get_db
|
| 3 |
|
| 4 |
def add_user():
|
| 5 |
+
"""Yeni kullanıcı ekler"""
|
| 6 |
try:
|
| 7 |
if not request.json or "name" not in request.json:
|
| 8 |
+
return jsonify({"error": "İsim zorunlu alan"}), 400
|
| 9 |
|
| 10 |
db = get_db()
|
| 11 |
cursor = db.cursor()
|
| 12 |
+
cursor.execute(
|
| 13 |
+
'INSERT INTO users (name, score) VALUES (?, ?)',
|
| 14 |
+
(request.json["name"], request.json.get("score", 0))
|
| 15 |
+
)
|
| 16 |
db.commit()
|
| 17 |
+
|
| 18 |
return jsonify({
|
| 19 |
+
"message": "Kullanıcı eklendi",
|
| 20 |
"user": {
|
| 21 |
"id": cursor.lastrowid,
|
| 22 |
"name": request.json["name"],
|
| 23 |
"score": request.json.get("score", 0)
|
| 24 |
}
|
| 25 |
}), 201
|
| 26 |
+
|
| 27 |
except Exception as e:
|
| 28 |
return jsonify({"error": str(e)}), 500
|