def deleteTodoList(id): tl = TodoList.query.filter(TodoList.id==id).first() if tl: # add data db.session.delete(tl) db.session.commit() return wrapResponse(tl.toJson()) else: return wrapResponse(f'TodoList with id: {id} not found!',\ 400,"NOT FOUND"), 400
def DeleteTodoItem(id): ti = TodoItem.query.filter(TodoItem.id==id).first() if ti: # add data db.session.delete(ti) db.session.commit() return wrapResponse(ti.toJson()) else: return wrapResponse(f'TodoItem with id: {id} not found!',\ 400,"NOT FOUND"), 400
def updateTodoList(request): id = request.json['id'] # find todolist tl = TodoList.query.filter(TodoList.id==id).first() if tl: data = request.json # add these to class for key in data: setattr(tl,key,data[key]) # add data db.session.add(tl) db.session.commit() return wrapResponse(tl.toJson()) else: return wrapResponse(f'TodoList with id: {id} not found!',400,"NOT FOUND"),\ 400
def getTodoItems(request,id): page=1 per_page=10 ti = TodoItem.query\ .filter(TodoItem.list_id==id)\ .paginate(page=page,per_page=per_page) items=[] for item in ti.items: items.append(item.toJson()) return wrapResponse(items)
def addTodoList(request): # create new instance tl = TodoList() # get data from request data = request.json # add these to class for key in data: setattr(tl,key,data[key]) # add data db.session.add(tl) db.session.commit() # return response return wrapResponse(tl.toJson())
def addTodoItem(request, lid): # create new instance ti = TodoItem() # add list id ti.list_id = lid # get data from request data = request.json # add these to class for key in data: setattr(ti,key,data[key]) # add data db.session.add(ti) db.session.commit() # return response return wrapResponse(ti.toJson())
def getTodoLists(request): # select top 50 items page=1 per_page=50 paginated = TodoList.query\ .paginate(page=page,per_page=per_page) data = { 'page':page, 'per_page':per_page, 'pages': paginated.pages, 'total': paginated.total } if paginated.total==0: data['found']=0 data['items']=[] else: items=[] for item in paginated.items: items.append(item.toJson()) data['items'] = items return wrapResponse(data)
def home(): return wrapResponse({'message': "It works"})