From d21c8109c4034020ef8674b21e4dbca72fbfe8d4 Mon Sep 17 00:00:00 2001 From: Jesse Chung Date: Sat, 3 Nov 2018 14:12:41 -0700 Subject: [PATCH 1/8] database operations backend --- MongoDB_backend/request.py | 96 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 MongoDB_backend/request.py diff --git a/MongoDB_backend/request.py b/MongoDB_backend/request.py new file mode 100644 index 0000000..32e05ce --- /dev/null +++ b/MongoDB_backend/request.py @@ -0,0 +1,96 @@ +from pymongo import MongoClient +from bson.objectid import ObjectId + +client = MongoClient() + +db = client.pretty + +contracts = db.contract +users = db.users + +def createContract(ownerID, sLat, sLong, eLat, eLon, price): + contract = { + "ownerID" : ownerID, + "acceptID" : -1, + "startLocation" :[sLat, sLong], + "endLocation" : [eLat, eLon], + "price" : price, + "valid" : true, + "active" : false, + } + + result = contracts.insert(contract); + return ObjectID.toString(result); + +def createUser(name, password, userID, rating): + user = { + "name" : name, + "password" : password, + "username" : userID, + "rating" : rating + } + result = users.insert(user) + + return ObjectID.toString(result); + + +def acceptContract(userID, contractID): + objID = ObjectID(contractID) + found = contracts.find( {"$and" :[{"_id" : objID}, {"valid": true}]}).count() + + if found > 0: + contracts.update({"_id": objID}, { "$set" : {"valid": false, "active": true, "acceptID": userID } }) + return true + else: + return false + +def completeContract(contractID): + objID = ObjectID(contractID) + found = contracts.find( {"$and" : [{"_id" : objID}, {"active": true}]} ).count() + + if found > 0: + contracts.update( {"_id" : objID}, { "$set" : {"active" : false}}) + return true + else: + return false + + + + +def validLogin(username, password): + userProfile = users.find_one({"screenName" : username}) + for attribute in userProfile: + passw = attribute.get("password") + + if password == passw: + return [true, ObjectID.toString(userProfile)] + else: + return [false, -1] + + + +def getContract(contractID): + contract = contracts.find_one({"_id": ObjectID(contractID)}) + + for data in contract: + toReturn = [data.get("owner"), data.get("startLocation"), data.get("endLocation"), data.get("price")] + + return toReturn + +def getUser(blockID): + contract = contracts.find({"_id": ObjectID(blockID)}) + + for data in contract: + toReturn = [data.get("name"), data.get("password"), data.get("screenName"), data.get("rating")] + + return toReturn + + + +#createContract("Theo" , 20, 20, 60, 40, 100) +#createUser("Arjun", "arjun", "arjunmishra", 0) +#getContract("Theo") + + + + From 5c6e4344530f4839a331af7ba29c3c70d84983c0 Mon Sep 17 00:00:00 2001 From: Jesse Chung Date: Sat, 3 Nov 2018 14:28:47 -0700 Subject: [PATCH 2/8] fixed getSpatial --- MongoDB_backend/request.py | 68 +++++++++++++++++++++++--------------- 1 file changed, 42 insertions(+), 26 deletions(-) diff --git a/MongoDB_backend/request.py b/MongoDB_backend/request.py index 32e05ce..de3e8c7 100644 --- a/MongoDB_backend/request.py +++ b/MongoDB_backend/request.py @@ -1,4 +1,5 @@ -from pymongo import MongoClient + +from pymongo import MongoClient, GEO2D from bson.objectid import ObjectId client = MongoClient() @@ -8,21 +9,30 @@ contracts = db.contract users = db.users -def createContract(ownerID, sLat, sLong, eLat, eLon, price): +contracts.create_index([("startLocation", GEO2D)]) +contracts.create_index([("endLocation", GEO2D)]) + +def createContract(ownerID, sLat, sLong, eLat, eLon, price, title, description): contract = { "ownerID" : ownerID, "acceptID" : -1, "startLocation" :[sLat, sLong], "endLocation" : [eLat, eLon], "price" : price, - "valid" : true, - "active" : false, + "title" : title, + "description" : description, + "valid" : True, + "active" : False, } result = contracts.insert(contract); - return ObjectID.toString(result); + return str(result); def createUser(name, password, userID, rating): + found = users.find({userID}).count() + if found: + return -1 + user = { "name" : name, "password" : password, @@ -30,47 +40,51 @@ def createUser(name, password, userID, rating): "rating" : rating } result = users.insert(user) + print str(result); - return ObjectID.toString(result); + return str(result); def acceptContract(userID, contractID): - objID = ObjectID(contractID) - found = contracts.find( {"$and" :[{"_id" : objID}, {"valid": true}]}).count() + objID = ObjectId(contractID) + found = contracts.find( {"$and" :[{"_id" : objID}, {"valid": True}]}).count() if found > 0: - contracts.update({"_id": objID}, { "$set" : {"valid": false, "active": true, "acceptID": userID } }) - return true + contracts.update({"_id": objID}, { "$set" : {"valid": False, "active": True, "acceptID": userID } }) + return True else: - return false + return False def completeContract(contractID): - objID = ObjectID(contractID) - found = contracts.find( {"$and" : [{"_id" : objID}, {"active": true}]} ).count() + objID = ObjectId(contractID) + found = contracts.find( {"$and" : [{"_id" : objID}, {"active": True}]} ).count() if found > 0: - contracts.update( {"_id" : objID}, { "$set" : {"active" : false}}) - return true + contracts.update( {"_id" : objID}, { "$set" : {"active" : False}}) + return True else: - return false + return False def validLogin(username, password): - userProfile = users.find_one({"screenName" : username}) - for attribute in userProfile: - passw = attribute.get("password") + found = users.find_one({"$and" :[{"username" : username, "password": password}] }) - if password == passw: - return [true, ObjectID.toString(userProfile)] + if found : + return str(found.get('_id')) else: - return [false, -1] + return -1 + + +def getContractSpatial(lat, lon, radius) : + query = contracts.find({"loc": {"$within": {"$center": [[lat, lon], radius]}}}) + return query def getContract(contractID): - contract = contracts.find_one({"_id": ObjectID(contractID)}) + contract = contracts.find_one({"_id": ObjectId(contractID)}) for data in contract: toReturn = [data.get("owner"), data.get("startLocation"), data.get("endLocation"), data.get("price")] @@ -78,7 +92,7 @@ def getContract(contractID): return toReturn def getUser(blockID): - contract = contracts.find({"_id": ObjectID(blockID)}) + contract = contracts.find({"_id": ObjectId(blockID)}) for data in contract: toReturn = [data.get("name"), data.get("password"), data.get("screenName"), data.get("rating")] @@ -87,8 +101,10 @@ def getUser(blockID): -#createContract("Theo" , 20, 20, 60, 40, 100) -#createUser("Arjun", "arjun", "arjunmishra", 0) +#createContract("5bdde74ab7ae36d406d6ea65" , 20, 20, 60, 40, 100) +#createUser("Theo", "pass", "theoluan", 0) +#acceptContract("5bdde74ab7ae36d406d6ea65", "5bdde93fb7ae36d449895550") +#print validLogin("theoluan", "not") #getContract("Theo") From 8739f8cab61bf602e4657aafca6f9ac5cbb6b100 Mon Sep 17 00:00:00 2001 From: Jesse Chung Date: Sat, 3 Nov 2018 15:12:07 -0700 Subject: [PATCH 3/8] fixed bugs and errors- so far functional --- MongoDB_backend/request.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/MongoDB_backend/request.py b/MongoDB_backend/request.py index de3e8c7..17ef641 100644 --- a/MongoDB_backend/request.py +++ b/MongoDB_backend/request.py @@ -29,7 +29,7 @@ def createContract(ownerID, sLat, sLong, eLat, eLon, price, title, description): return str(result); def createUser(name, password, userID, rating): - found = users.find({userID}).count() + found = users.find({"username" :userID}).count() if found: return -1 @@ -78,7 +78,7 @@ def validLogin(username, password): def getContractSpatial(lat, lon, radius) : - query = contracts.find({"loc": {"$within": {"$center": [[lat, lon], radius]}}}) + query = contracts.find({"startLocation": {"$within": {"$center": [[lat, lon], radius]}}}) return query From b3e4c7f3228455205483e15d2f24cab62c344ea6 Mon Sep 17 00:00:00 2001 From: Colhodm Date: Sat, 3 Nov 2018 15:59:17 -0700 Subject: [PATCH 4/8] Did more back end integration --- .../Cal-Hacks-TBD/Base.lproj/Main.storyboard | 38 ++++++++++++++----- .../Cal-Hacks-TBD/LoginController.swift | 20 +++++----- .../Cal-Hacks-TBD/MapViewController.swift | 32 ++++++++++------ .../Cal-Hacks-TBD/ViewController.swift | 5 +++ 4 files changed, 64 insertions(+), 31 deletions(-) diff --git a/Cal-Hacks-TBD/Cal-Hacks-TBD/Base.lproj/Main.storyboard b/Cal-Hacks-TBD/Cal-Hacks-TBD/Base.lproj/Main.storyboard index 8f1c04e..aec9645 100644 --- a/Cal-Hacks-TBD/Cal-Hacks-TBD/Base.lproj/Main.storyboard +++ b/Cal-Hacks-TBD/Cal-Hacks-TBD/Base.lproj/Main.storyboard @@ -32,14 +32,6 @@ -