yusufgundogdu commited on
Commit
d9a9aa5
·
verified ·
1 Parent(s): b684e26

Create consume_method.py

Browse files
Files changed (1) hide show
  1. consume_method.py +42 -0
consume_method.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import jsonify
2
+ from database import get_db
3
+
4
+ def consume_user(udid):
5
+ """Kullanıcının usage_limit'ini 1 azaltır"""
6
+ db = get_db()
7
+ cursor = db.cursor()
8
+
9
+ # Kullanıcıyı bul
10
+ user = cursor.execute(
11
+ 'SELECT * FROM users WHERE udid = ?',
12
+ (udid,)
13
+ ).fetchone()
14
+
15
+ if not user:
16
+ return jsonify({"error": "Kullanıcı bulunamadı"}), 404
17
+
18
+ user_dict = dict(user)
19
+
20
+ # Kullanım hakkı kontrolü
21
+ if user_dict['usage_limit'] <= 0:
22
+ return jsonify({"error": "Kullanım hakkınız bulunmamaktadır"}), 403
23
+
24
+ # usage_limit'i 1 azalt
25
+ new_limit = user_dict['usage_limit'] - 1
26
+ cursor.execute(
27
+ 'UPDATE users SET usage_limit = ? WHERE udid = ?',
28
+ (new_limit, udid)
29
+ )
30
+ db.commit()
31
+
32
+ # Güncellenmiş kullanıcı bilgisini dön
33
+ updated_user = cursor.execute(
34
+ 'SELECT * FROM users WHERE udid = ?',
35
+ (udid,)
36
+ ).fetchone()
37
+
38
+ return jsonify({
39
+ "message": "Kullanım hakkı başarıyla tüketildi",
40
+ "remaining_usage": new_limit,
41
+ "user": dict(updated_user)
42
+ })