Noureddinesa commited on
Commit
66ec538
1 Parent(s): 919a134

Upload 5 files

Browse files
Files changed (5) hide show
  1. Dockerfile +27 -0
  2. Mydatabase.db +0 -0
  3. database.py +155 -0
  4. main.py +124 -0
  5. requirements.txt +3 -0
Dockerfile ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9-alpine
2
+
3
+ WORKDIR /flask_app
4
+
5
+ COPY requirements.txt .
6
+
7
+ RUN pip install -r requirements.txt
8
+
9
+ # Set up a new user named "user" with user ID 1000
10
+ RUN useradd user
11
+
12
+ # Switch to the "user" user
13
+ USER user
14
+
15
+ # Set home to the user's home directory
16
+ ENV HOME=/home/user \
17
+ PATH=/home/user/.local/bin:$PATH
18
+
19
+ # Set the working directory to the user's home directory
20
+ WORKDIR $HOME/app
21
+
22
+ # Copy the current directory contents into the container at $HOME/app setting the owner to the user
23
+ COPY --chown=user . $HOME/app
24
+
25
+ EXPOSE 8888
26
+
27
+ CMD ["python", "main.py"]
Mydatabase.db ADDED
Binary file (20.5 kB). View file
 
database.py ADDED
@@ -0,0 +1,155 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from colorama import Fore, Back, Style, init
2
+
3
+ init()
4
+
5
+ #conn = sqlite3.connect('Mydatabase.db')
6
+
7
+ def CreateDatabse(conn):
8
+ cursor = conn.cursor()
9
+ try:
10
+
11
+ cursor.execute('''
12
+ CREATE TABLE Friends (
13
+ Userid TEXT PRIMARY KEY NOT NULL,
14
+ Fullname TEXT NOT NULL,
15
+ Role TEXT NOT NULL
16
+ )
17
+ ''')
18
+ cursor.execute('''
19
+ CREATE TABLE Cards (
20
+ Cardid INTEGER PRIMARY KEY AUTOINCREMENT,
21
+ fk_Userid TEXT NOT NULL,
22
+ Description TEXT NOT NULL,
23
+ FOREIGN KEY(fk_Userid) REFERENCES Friends(Userid)
24
+ )
25
+ ''')
26
+ CommitAndClose(conn)
27
+ print(Style.BRIGHT + Fore.GREEN + "Tables created successfully")
28
+
29
+ except Exception as e:
30
+ print(Style.BRIGHT + Fore.RED + "Error creating tables")
31
+ print(Style.BRIGHT+ Fore.RESET +str(e))
32
+
33
+
34
+ def InsertUser(conn,userid,fullname,role):
35
+ cursor = conn.cursor()
36
+ try:
37
+ cursor.execute("""
38
+ INSERT INTO Friends
39
+ (Userid, Fullname, Role)
40
+ VALUES (?,?,?)""",
41
+ (userid,fullname,role))
42
+ print(Style.BRIGHT + Fore.GREEN + "User inserted successfully")
43
+ except Exception as e:
44
+ print(Style.BRIGHT + Fore.RED + "Error inserting data")
45
+ print(Style.BRIGHT+ Fore.RESET +str(e))
46
+ CommitAndClose(conn)
47
+
48
+ def InsertCard(conn,userid,description):
49
+ cursor = conn.cursor()
50
+ try:
51
+ cursor.execute("""
52
+ INSERT INTO cards
53
+ (fk_Userid, Description)
54
+ VALUES (?,?)""",
55
+ (userid,description))
56
+ print(Style.BRIGHT + Fore.GREEN + "Card inserted successfully")
57
+ except Exception as e:
58
+ print(Style.BRIGHT + Fore.RED + "Error inserting data")
59
+ print(Style.BRIGHT+ Fore.RESET +str(e))
60
+ CommitAndClose(conn)
61
+
62
+ def FetchAllUsers(conn):
63
+ cursor = conn.cursor()
64
+ rows = cursor.execute('SELECT * FROM Friends').fetchall()
65
+ print(Style.BRIGHT + Fore.GREEN + "Users fetched successfully")
66
+ FormatedRows = [{"Userid":row[0],"Fullname":row[1],"Role":row[2]} for row in rows]
67
+ CommitAndClose(conn)
68
+ return FormatedRows
69
+
70
+ def FetchAllCards(conn):
71
+ cursor = conn.cursor()
72
+ rows = cursor.execute('SELECT * FROM Cards').fetchall()
73
+ print(Style.BRIGHT + Fore.GREEN + "Cards fetched successfully"+Style.RESET_ALL)
74
+ FormatedRows = [{"Cardid":row[0],"Userid":row[1],"Description":row[2]} for row in rows]
75
+
76
+ for row in FormatedRows:
77
+ #print(row)
78
+ userid = row['Userid']
79
+ userinfo = cursor.execute('SELECT * FROM Friends Where Userid = ?', (userid,)).fetchone()
80
+ #print(userinfo)
81
+ row["Userinfo"] = {"FullName":userinfo[1],"Role":userinfo[2]}
82
+ CommitAndClose(conn)
83
+ return FormatedRows
84
+
85
+ def FetchUser(conn,userid):
86
+ cursor = conn.cursor()
87
+ row = cursor.execute('SELECT * FROM Friends Where Userid = ?', (userid,)).fetchone()
88
+ print(Style.BRIGHT + Fore.GREEN + f"{userid} fetched successfully")
89
+ CommitAndClose(conn)
90
+ return row
91
+
92
+ def FetchCard(conn,cardid):
93
+ cursor = conn.cursor()
94
+ row = cursor.execute('SELECT * FROM Cards Where Cardid = ?', (cardid,)).fetchone()
95
+ print(Style.BRIGHT + Fore.GREEN + f"{cardid} fetched successfully")
96
+ CommitAndClose(conn)
97
+ return row
98
+
99
+ def UpdateCard(conn,cardid,description):
100
+ cursor = conn.cursor()
101
+ try:
102
+ cursor.execute("""
103
+ UPDATE cards
104
+ SET Description = ?
105
+ WHERE Cardid = ?""",
106
+ (description,cardid))
107
+ print(Style.BRIGHT + Fore.GREEN + "Card updated successfully")
108
+ except Exception as e:
109
+ print(Style.BRIGHT + Fore.RED + "Error updating data")
110
+ print(Style.BRIGHT+ Fore.RESET +str(e))
111
+ CommitAndClose(conn)
112
+
113
+
114
+ def CommitAndClose(conn):
115
+ conn.commit()
116
+ conn.close()
117
+ print(Style.BRIGHT + Fore.GREEN + "Database closed successfully" + Style.RESET_ALL)
118
+
119
+ #CreateDatabse()
120
+ """InsertUser(conn,"user01","John Doe","Frontend Developer")
121
+ InsertUser(conn,"user02","Jane Doe","Backend Developer")
122
+ InsertUser(conn,"user03","Jack Doe","Fullstack Developer")
123
+ InsertUser(conn,"user04","Jill Doe","Devops Developer")
124
+ InsertCard(conn,"user01","Noureddine is a Fullstack Developer")
125
+ InsertCard(conn,"user02","Best one")
126
+ InsertCard(conn,"user03","Awesome")
127
+ InsertCard(conn,"user04","Good and rich profile")
128
+ InsertCard(conn,"user04","Good and Strong background")"""
129
+
130
+ #print(Style.RESET_ALL+str(FetchCard(conn,1)))
131
+ #print(Style.RESET_ALL+str(FetchUser(conn,"user01")))
132
+ #CommitAndClose(conn)
133
+
134
+
135
+
136
+
137
+
138
+
139
+
140
+
141
+
142
+
143
+
144
+
145
+
146
+
147
+
148
+
149
+
150
+
151
+
152
+
153
+
154
+
155
+
main.py ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask,request, jsonify
2
+ from flask_cors import CORS
3
+ from database import *
4
+ import sqlite3
5
+ import uuid
6
+
7
+ app = Flask(__name__)
8
+ CORS(app)
9
+
10
+
11
+ # Backend Routes
12
+ @app.route('/', methods=['GET'])
13
+ def index():
14
+ response = jsonify({"message":"Hi in This simple Flask Server"})
15
+ return response,200
16
+
17
+ @app.route('/Cards', methods=['GET'])
18
+ def GetAllCards():
19
+ conn = sqlite3.connect('Mydatabase.db')
20
+ try:
21
+ cards = FetchAllCards(conn)
22
+ response = jsonify(cards)
23
+ return response,200
24
+ except Exception as e:
25
+ response = jsonify({"message":str(e)})
26
+ return response,400
27
+
28
+ @app.route('/Users', methods=['GET'])
29
+ def GetAllUsers():
30
+ conn = sqlite3.connect('Mydatabase.db')
31
+ try:
32
+ users = FetchAllUsers(conn)
33
+ response = jsonify(users)
34
+ return response,200
35
+ except Exception as e:
36
+ response = jsonify({"message":str(e)})
37
+ return response,400
38
+
39
+ @app.route('/create_card', methods=['POST'])
40
+ def CreateCard():
41
+ conn = sqlite3.connect('Mydatabase.db')
42
+ try:
43
+ userid = request.json.get('userid')
44
+ description = request.json.get('description')
45
+ InsertCard(conn,userid,description)
46
+ response = jsonify({"message":"Card created successfully"})
47
+ return response,200
48
+ except Exception as e:
49
+ response = jsonify({"message":str(e)})
50
+ return response,400
51
+
52
+ @app.route('/create_user', methods=['POST'])
53
+ def CreateUser():
54
+ conn = sqlite3.connect('Mydatabase.db')
55
+ try:
56
+ userid = str(uuid.uuid4())
57
+ fullname = request.json.get('fullname')
58
+ role = request.json.get('role')
59
+ InsertUser(conn,userid,fullname,role)
60
+ response = jsonify({"message":"User created successfully","userid":userid})
61
+ return response,200
62
+ except Exception as e:
63
+ response = jsonify({"message":str(e)})
64
+ return response,400
65
+
66
+ @app.route("/Get_user",methods = ['GET'])
67
+ def GetUser():
68
+ conn = sqlite3.connect('Mydatabase.db')
69
+ try:
70
+ userid = request.json.get("userid")
71
+ user = FetchUser(conn,userid)
72
+ response = jsonify({"userid":user[0],"FullName":user[1],"Role":user[2]})
73
+ return response,200
74
+ except Exception as e :
75
+ response= jsonify({"message":f"Failed while retriving the user{userid}"})
76
+ return response,400
77
+
78
+ @app.route("/Get_card",methods=['GET'])
79
+ def GetCard():
80
+ conn = sqlite3.connect('Mydatabase.db')
81
+ try:
82
+ cardid = request.json.get("cardid")
83
+ card = FetchCard(conn,cardid)
84
+ response = jsonify({"cardid":card[0],"userid":card[1],"description":card[2]})
85
+ return response,200
86
+ except Exception as e :
87
+ response= jsonify({"message":f"Failed while retriving the card{cardid}"})
88
+ return response,400
89
+
90
+ @app.route("/delete_card", methods=['DELETE'])
91
+ def DeleteCard():
92
+ conn = sqlite3.connect('Mydatabase.db')
93
+ cursor = conn.cursor()
94
+ cardid = request.json.get("cardid")
95
+ try:
96
+ cursor.execute('DELETE FROM Cards WHERE Cardid = ?', (cardid,))
97
+ conn.commit()
98
+ conn.close()
99
+ response = jsonify({"message":"Card deleted successfully"})
100
+ return response,200
101
+ except Exception as e:
102
+ response = jsonify({"message":str(e)})
103
+ print(str(e))
104
+ return response
105
+
106
+
107
+ if __name__ == '__main__':
108
+ app.run(debug=True,host='0.0.0.0',port=8888)
109
+
110
+
111
+
112
+
113
+
114
+
115
+
116
+
117
+
118
+
119
+
120
+
121
+
122
+
123
+
124
+
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ colorama==0.4.6
2
+ Flask==3.0.3
3
+ Flask_Cors==4.0.1