-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.py
More file actions
130 lines (110 loc) · 4.09 KB
/
Copy pathServer.py
File metadata and controls
130 lines (110 loc) · 4.09 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
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
from flask import Flask, request, jsonify, send_file
from flask_cors import CORS
from pymongo import MongoClient
import bson.binary
import bson.json_util
from werkzeug.security import generate_password_hash, check_password_hash
from werkzeug.utils import secure_filename
import io
from bson import ObjectId
app = Flask(__name__)
CORS(app)
# app.config['MONGO_URI'] = 'mongodb+srv://sarveshbhosale111:ay5bsR2dL5ZvlRz5@cluster0.b3hgrqo.mongodb.net/?retryWrites=true&w=majority'
# # connecting to the mongoDb client
# cluster = MongoClient(app.config['MONGO_URI'])
# connecting to the mongoDb client
cluster = MongoClient(
host="localhost",
port=27017,
serverSelectionTimeoutMS = 1000)
# giving the cluster name
db = cluster['RestaurantDB']
# giving the collection name
user = db['user']
productImage = db['productImage']
products = db['products']
@app.route("/", methods=['GET'])
def startup():
return "Connected to Restaurant Server"
#Create User
@app.route("/createuser",methods=['POST'])
def createUser():
details = request.json
email = request.json["email"]
phoneNo = request.json["phoneNo"]
password = generate_password_hash(request.json["password"], method='sha256', salt_length=8)
details["password"] = password
details["isAdmin"]= details.get('isAdmin',False)
if(user.count_documents({"$or":[{"email":email},{"phoneNo":phoneNo}]}) == 0):
user.insert_one(details)
return 'Success'
else:
return 'User Already Exists'
#Login User
@app.route('/login', methods=['POST'])
def login():
data = request.json
email = data.get('email')
password = data.get('password')
# Check if email and password are provided in the request
if not email or not password:
return jsonify({'error': 'Email and password are required'}), 400
# Retrieve user from the database
users = user.find_one({'email': email})
users["_id"] = str(users["_id"])
# Check if user exists and password is correct
if not users or not check_password_hash(users['password'], password):
return jsonify({'error': 'Invalid email or password'}), 401
# Return success message with user information
return jsonify({
'message': 'Login successful',
'user':users
})
@app.route('/uploadimage', methods=['POST'])
def upload_image():
file = request.files['image']
filename = secure_filename(file.filename)
with file.stream as f:
image_data = bson.binary.Binary(f.read())
result = productImage.insert_one({'filename': filename, 'image': image_data})
return jsonify({'id': str(result.inserted_id)}), 201
@app.route('/getimage/<id>', methods=["GET"])
def get_image(id):
document = productImage.find_one({'_id': bson.objectid.ObjectId(id)})
if document is None or 'image' not in document:
return 'Image not found', 404
return send_file(io.BytesIO(document['image']), mimetype='image/jpeg')
@app.route('/addproduct',methods=['POST'])
def addProduct():
details = request.json
products.insert_one(details)
return 'Success'
@app.route('/getproducts', methods=['GET'])
def getProducts():
allProducts = list(products.find())
data = []
for product in allProducts:
product['_id'] = str(product['_id'])
data.append(product)
return jsonify(data)
@app.route('/updateproducts/<id>', methods=['PATCH'])
def update_data(id):
print(id)
data = request.json
document = {'_id': ObjectId(id)}
update = {'$set': data}
result = products.update_one(document, update)
if result.modified_count > 0:
return jsonify({'message': 'Product updated successfully'})
else:
return jsonify({'message': 'No Product found with that ID'})
@app.route('/deleteproducts/<id>', methods=['DELETE'])
def delete_data(id):
document = {'_id': ObjectId(id)}
result = products.delete_one(document)
if result.deleted_count > 0:
return jsonify({'message': 'Document deleted successfully'})
else:
return jsonify({'message': 'No document found with that ID'})
if __name__ == "__main__":
app.run(port=5000, debug=True)