-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_connection.py
More file actions
91 lines (68 loc) · 2.98 KB
/
db_connection.py
File metadata and controls
91 lines (68 loc) · 2.98 KB
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
import pymongo
class Mongo():
dbname = "rent-a-room"
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient[dbname]
print(myclient.list_database_names())
dblist = myclient.list_database_names()
room = mydb["rooms"]
customer = mydb["persons"]
if dbname in dblist:
print("The database has been found")
def createLogin(name, firstname, email, password, telnr, address):
mydict = {"name": name, "firstname": firstname, "password": password, "address": address, "email": email, "telnr": telnr}
Mongo.customer.insert_one(mydict)
return 1
def getLogin(email):
return Mongo.customer.find_one({"email": email}, {"email": 1, "name": 1, "password": 1})
def createRoom(name, desc, address, room_amount, space, owner, owner_id):
mydict = {"name": name, "beschreibung": desc, "address": address, "room_amount": room_amount, "space": space, "owner": owner, "owner_id": owner_id, "is_booked": False, "booker_id": "none"}
result = Mongo.room.insert_one(mydict)
return result.inserted_id
def getRoom(filtername):
return Mongo.room.find({"name": filtername}, {"_id": 1, "name": 1, "beschreibung": 1, "owner": 1})
def getAllRoom(self):
return Mongo.room.find({}, {"_id": 1, "name": 1, "beschreibung": 1, "address": 1, "owner": 1})
def getRoomByOwner(owner, owner_id):
return Mongo.room.find({"owner": owner, "owner_id": owner_id}, {"_id": 1, "name": 1, "beschreibung": 1, "address": 1})
def getRoomById(id):
return Mongo.room.find_one({"_id": id}, {})
def getAllRooms(self):
for x in Mongo.room.find({}, {}):
print(x)
return x
def getByName(name):
for x in Mongo.room.find({"name": name }, {}):
print (x)
return x
def getByDesc(beschreibung):
for x in Mongo.room.find({"beschreibung": beschreibung}, {}):
print(x)
return x
def getByRoomAmount(room_amount):
for x in Mongo.room.find({"room_amount": room_amount}, {}):
print(x)
return x
def getBySpace(space):
for x in Mongo.room.find({"space": space},{}):
print(x)
return x
def deleteRoom(id):
objToDelete = {"_id": id}
Mongo.room.delete_one(objToDelete)
return True
def updateRoomById(id, newName, newDesc, newAdd):
myquery = {"_id": id}
newvalues = {"$set": {"name": newName, "beschreibung": newDesc,"address": newAdd}}
Mongo.room.update_one(myquery, newvalues)
return id
def book_room(id, booker_id):
myquery = {"_id": id}
newvalues = {"$set": {"is_booked": True, "booker_id": booker_id}}
Mongo.room.update_one(myquery, newvalues)
return id
def unbook_room(id):
myquery = {"_id": id}
newvalues = {"$set": {"is_booked": False, "booker_id": "None"}}
Mongo.room.update_one(myquery, newvalues)
return id