-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
244 lines (216 loc) · 8.8 KB
/
db.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import os
import psycopg2
import pymongo
import pymongo.collection
import pymongo.results
import redis
import models.Trips
import models.user
import models.posts
from enum import Enum
from bson import ObjectId
class ResultCode(Enum):
SUCCESS = 0
FAILED_TRANSACTION = 1
REPEATED_ELEMENT = 2
USER_NOT_FOUND = 3
POST_NOT_FOUND = 4
INVALID_PASSWORD = 5
class PostgresDatabase:
def __init__(self):
self.connection = psycopg2.connect(
host=os.environ.get("DB_HOST"),
port=os.environ.get("DB_PORT"),
database=os.environ.get("DB_NAME"),
user=os.environ.get("DB_USER"),
password=os.environ.get("DB_PASSWORD")
)
def authenticate_user(self, mail: str, password: str):
try:
cursor = self.connection.cursor()
cursor.execute("SELECT password FROM Users WHERE mail = %s", (mail,))
stored_password = cursor.fetchone()
if stored_password is None:
print("Usuario no encontrado")
return ResultCode.USER_NOT_FOUND
print(f"Password en la base de datos: {stored_password[0]}")
print(f"Password ingresada: {password}")
if password == stored_password[0]:
print("Contraseña verificada correctamente")
return ResultCode.SUCCESS
else:
print("Contraseña incorrecta")
return ResultCode.INVALID_PASSWORD
except Exception as e:
print(f"Error during authentication: {e}")
return ResultCode.FAILED_TRANSACTION
def register_user(self, user: models.user.NewUser):
try:
cursor = self.connection.cursor()
cursor.execute(
"CALL AddUser(%s, %s, %s, %s)",
(user.name, user.mail, user.password, user.role)
)
self.connection.commit()
cursor.close()
return ResultCode.SUCCESS
except psycopg2.errors.InFailedSqlTransaction:
self.connection.rollback()
return ResultCode.FAILED_TRANSACTION
except psycopg2.errors.UniqueViolation:
self.connection.rollback()
return ResultCode.REPEATED_ELEMENT
def delete_user(self, user: models.user.DelUser):
try:
cursor = self.connection.cursor()
cursor.execute(
"CALL DeleteUser(%s)", (user.mail,)
)
self.connection.commit()
cursor.close()
return ResultCode.SUCCESS
except psycopg2.errors.InFailedSqlTransaction:
self.connection.rollback()
return ResultCode.FAILED_TRANSACTION
except psycopg2.errors.lookup("02000"):
self.connection.rollback()
return ResultCode.USER_NOT_FOUND
def edit_user(self, user: models.user.EditUser):
try:
cursor = self.connection.cursor()
cursor.execute(
"CALL EditUser(%s, %s, %s, %s, %s)",
(user.mail, user.name, user.newMail, user.password, user.role)
)
self.connection.commit()
cursor.close()
return ResultCode.SUCCESS
except psycopg2.errors.InFailedSqlTransaction:
self.connection.rollback()
return ResultCode.FAILED_TRANSACTION
except psycopg2.errors.lookup("02000"):
self.connection.rollback()
return ResultCode.USER_NOT_FOUND
class MongoDatabase:
def __init__(self):
self.connection = pymongo.MongoClient("mongodb://root:root@dbmongo:27017/")
self.db = self.connection["ViajesDB"]
self.posts = self.db["posts"]
self.destinos = self.db["destinos"]
self.bucketLists = self.db["bucketLists"]
self.bucketListsFollow = self.db["bucketListsFollow"]
self.trips = self.db["trips"]
def create_post(self, post: models.posts.NewPost):
result: pymongo.results.InsertOneResult = self.posts.insert_one(dict(post))
if result.acknowledged:
return [ResultCode.SUCCESS, result.inserted_id]
return ResultCode.FAILED_TRANSACTION
def get_post(self, post: models.posts.GetPost):
return self.posts.find_one({"_id": ObjectId(post.PostID)}) or ResultCode.POST_NOT_FOUND
def like_post(self, post: models.posts.LikePost):
result: pymongo.results.UpdateResult = self.posts.update_one(
{"_id": post.PostID},
{"$push": {"Likes": post.LikeAuthorID}}
)
if result.acknowledged:
return ResultCode.SUCCESS
return ResultCode.FAILED_TRANSACTION
def add_comment_post(self, postID: str, comment: models.posts.Comment):
result = self.posts.update_one(
{"_id": postID},
{"$push": {"Comentarios": dict(comment)}}
)
if result.acknowledged:
return ResultCode.SUCCESS
return ResultCode.FAILED_TRANSACTION
def add_like_destino(self, post: models.Trips.LikeDestination):
result: pymongo.results.UpdateResult = self.posts.update_one(
{"_id": post.PostID},
{"$inc": {"Likes": 1}}
)
if result.acknowledged:
return ResultCode.SUCCESS
return ResultCode.FAILED_TRANSACTION
def create_destino(self, destino: models.Trips.NewDestination):
result: pymongo.results.InsertOneResult = self.destinos.insert_one(dict(destino))
if result.acknowledged:
return [ResultCode.SUCCESS, result.inserted_id]
return ResultCode.FAILED_TRANSACTION
def add_comment_destino(self, destinoID: str, comment: models.Trips.Comment):
result = self.posts.update_one(
{"_id": destinoID},
{"$push": {"Comentarios": dict(comment)}}
)
if result.acknowledged:
return ResultCode.SUCCESS
return ResultCode.FAILED_TRANSACTION
def add_like_destino(self, post: models.Trips.LikeDestination):
result: pymongo.results.UpdateResult = self.posts.update_one(
{"_id": post.PostID},
{"$inc": {"Likes": 1}}
)
if result.acknowledged:
return ResultCode.SUCCESS
return ResultCode.FAILED_TRANSACTION
def create_bucket_list(self, bucketList: models.Trips.BucketListCreation):
result: pymongo.results.InsertOneResult = self.bucketLists.insert_one(dict(bucketList))
if result.acknowledged:
return [ResultCode.SUCCESS, result.inserted_id]
return ResultCode.FAILED_TRANSACTION
def follow_bucket_list(self, bucket_list: models.Trips.BucketListFollower):
result: pymongo.results.UpdateResult = self.posts.update_one(
{"_id": bucket_list.FollowerUserID},
{"$push": {"bucketListsFollowing": bucket_list.BucketListID}},
upsert=True
)
if result.acknowledged:
return ResultCode.SUCCESS
return ResultCode.FAILED_TRANSACTION
def create_trip(self, trip: models.Trips.CreateTrip):
result: pymongo.results.InsertOneResult = self.trips.insert_one(dict(trip))
if result.acknowledged:
return ResultCode.SUCCESS
return ResultCode.FAILED_TRANSACTION
def like_comment(self, postID: str, commentID: str, userID: str):
result = self.posts.update_one(
{"_id": ObjectId(postID), "Comentarios._id": ObjectId(commentID)},
{"$addToSet": {"Comentarios.$.Likes": userID}}
)
if result.acknowledged:
return ResultCode.SUCCESS
return ResultCode.FAILED_TRANSACTION
class RedisDatabase:
def __init__(self):
self.connection = redis.Redis(
host="redis",
port=6379,
db=0,
decode_responses=True
)
self.connection.ping()
def set_hash_data(self, key, value):
self.connection.hset(key, mapping=dict(value))
self.connection.expire(key, 100)
return ResultCode.SUCCESS
def delete_hash_data(self, key, llaves):
self.connection.hdel(key, *llaves)
return ResultCode.SUCCESS
def set_user_session(self, mail, user_data):
try:
self.connection.hset(mail, mapping=user_data)
self.connection.expire(mail, 100) # Sesión temporal de 100 segundos
return ResultCode.SUCCESS
except Exception as e:
print(f"Error setting user session in Redis: {e}")
return ResultCode.FAILED_TRANSACTION
def get_user_session(self, mail):
try:
return self.connection.hgetall(mail)
except Exception as e:
print(f"Error getting user session in Redis: {e}")
return None
def refresh_user_session(self, mail):
try:
self.connection.expire(mail, 100)
except Exception as e:
print(f"Error refreshing session in Redis: {e}")