Beispiel #1
0
def create():
    json = request.json
    obj = ToDo(json)
    db.session.add(obj)
    db.session.commit()

    return apiresult.ok()
Beispiel #2
0
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()
Beispiel #3
0
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()
Beispiel #4
0
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()
Beispiel #5
0
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()
Beispiel #6
0
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)
Beispiel #7
0
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)
Beispiel #8
0
def getById(id):
    res = helper.mongoJson((todos.find_one({'_id': id})))
    return apiresult.ok(res)
Beispiel #9
0
def all():
    res = helper.mongoJson(todos.find())
    print(res)
    return apiresult.ok(res)
Beispiel #10
0
def delete(id):
    try:
        res = todos.delete_one({'_id': ObjectId(id)})
        return apiresult.ok()
    except:
        return apiresult.internalServerError()
Beispiel #11
0
def getById(id):
    record = ToDo.query.filter_by(id=id).first()

    return apiresult.ok(record.serialize)
Beispiel #12
0
def all():
    records = ToDo.query.all()
    arr = [r.serialize for r in records]
    #print(arr)
    return apiresult.ok(arr)