Commit 78c9d3f4 authored by Ahmad Anvari's avatar Ahmad Anvari

Add new features

parent 3a642411
from db.mongo import Model
class Order(Model):
def __init__(self):
pass
def store(self):
pass
@staticmethod
def get_collection():
pass
@staticmethod
def dict_to_object(dictionary):
pass
def to_dict(self):
pass
from db.mongo import Model
class Product(Model):
def __init__(self, name, price, available, tags):
self.__id = None
self.__name = name
self.__price = price
self.__available = available
self.__tags = tags
@staticmethod
def get_collection():
return Model.get_db().get_collection('products')
@staticmethod
def dict_to_object(dictionary):
pass
def to_dict(self):
dict = {}
if self.__id is not None:
dict["_id"] = self.__id
if self.__name is not None:
dict["name"] = self.__name
if self.__price is not None:
dict["price"] = self.__price
if self.__available is not None:
dict["available"] = self.__available
if self.__tags is not None:
dict["tags"] = self.__tags
return dict
def store(self):
add_document = Product.get_collection().insert_one(self.to_dict())
self.__id = str(add_document.inserted_id)
def search_by_tags(self):
pass
@staticmethod
def search(count, tags):
print(len(tags))
print(tags)
if len(tags) == 0:
objects = Product.get_collection().find().limit(count)
else:
objects = Product.get_collection().find({"tags": {"$all": tags}}).sort([("price", -1)]).limit(count)
result = []
for obj in objects:
obj["_id"] = str(obj["_id"])
result.append(obj)
return result
def get_id(self):
return self.__id
\ No newline at end of file
from app import app
from flask import request, jsonify
from db.product import Product
@app.route('/products', methods=["GET"])
def search_products():
tags = request.args.get('tags')
if tags is None:
tags = []
else:
tags = tags.split(',')
count = request.args.get('count')
if count is None:
count = 5
else:
count = int(count)
r = Product.search(count=count, tags=tags)
return jsonify(r)
@app.route('/products', methods=["POST"])
def add_product():
p1 = Product(name="Lenovo ThinkPad TH230", price=193000000, tags=["laptop", "pc", "computer", "lenovo", "thinkpad"], available=int(50))
p2 = Product(name="MSI 127", price=308000000, tags=["laptop", "pc", "computer", "msi", "gaming"], available=int(20))
p1.store()
p2.store()
return jsonify({"id": [p1.get_id(), p2.get_id()]})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment