def create(): json = request.json obj = ToDo(json) db.session.add(obj) db.session.commit() return apiresult.ok()
def delete(id): json = request.json obj = ToDo.query.filter_by(id=id).first() if obj is not None: db.session.delete(obj) db.session.commit() return apiresult.ok()
def update(id): json = request.json updating = ToDo.query.filter_by(id=id).first() if updating is not None: updating.title = json['title'] updating.desc = json['desc'] updating.bydate = json['bydate'] db.session.commit() return apiresult.ok()
def create(): try: json = request.json t = json['title'] d = json['desc'] if dupplicate(t, d): return apiresult.duplicated() json['createdon'] = helper.now() todos.insert(json) return apiresult.ok() except: return apiresult.internalServerError()
def update(id): try: obj = todos.find({'_id': id}) if obj == None: return apiresult.parameterError({'id': id}) content = request.json todos.update({'_id': ObjectId(id)}, { '$set': { 'title': content['title'], 'desc': content['desc'], 'bydate': content['bydate'] } }) return apiresult.ok() except: return apiresult.internalServerError()
def search(): p = request.json op = p['operation'] title = '%' if 'title' in p: title = f'%{p["title"]}%' desc = '%' if 'desc' in p: desc = f'%{p["desc"]}%' cond = [] cond.append(ToDo.title.like(title)) cond.append(ToDo.desc.like(desc)) records = [] if op == 'and': records = ToDo.query.filter(and_(*cond)) else: #default is or records = ToDo.query.filter(or_(*cond)) arr = [r.serialize for r in records] return apiresult.ok(arr)
def search(): p = request.json op = p['operation'] cond = [] if 'title' in p: title = p['title'] cond.append({'title': {"$regex": ".*{}.*".format(title)}}) if 'desc' in p: desc = p['desc'] cond.append({'desc': {"$regex": ".*{}.*".format(desc)}}) res = helper.mongoJson( todos.find({ f"${op}": cond # "$or": # [ # {'desc':{"$regex": ".*{}.*".format(desc)}}, # {'title':{"$regex": ".*{}.*".format(title)}} # ] })) print(res) return apiresult.ok(res)
def getById(id): res = helper.mongoJson((todos.find_one({'_id': id}))) return apiresult.ok(res)
def all(): res = helper.mongoJson(todos.find()) print(res) return apiresult.ok(res)
def delete(id): try: res = todos.delete_one({'_id': ObjectId(id)}) return apiresult.ok() except: return apiresult.internalServerError()
def getById(id): record = ToDo.query.filter_by(id=id).first() return apiresult.ok(record.serialize)
def all(): records = ToDo.query.all() arr = [r.serialize for r in records] #print(arr) return apiresult.ok(arr)