Exemple #1
0
def test_dao(db):
	dao=BookDao(db)
	book1 ={}
	book1['book_titile']= 'introduce to tornado'
	book1['addr']='4-2-7'
	book2 = {'book_title':'learning python', 'author':'liu dw','publisher':'bca','description':'this is a book about basic knowledge of python', 'addr':'4-2-7', 'price':15.21}
	book3 = {'book_title':'Restful Web Service', 'author':'yunjianfei','publisher':'bca','description':'this is a book about restful web service', 'addr':'4-2-34', 'price':34.14}

	book4 = {'author':'aimer yin', 'publisher':'baihualou', 'description':' kanxinqing', 'addr':'7-8-2','price':34.2}

	#ret = dao.insert_by_dict(book3)
	#print 'insert book : %s, ret: %s' % (str(book3),str(ret))

	#h = dao.get_by_bid(1)
	#print 'get by id, ret: %s'% str(h)
	#ret = dao.get_by_title('kim')
	#print 'get by title ,ret: %s' % str(ret)

	#ret = dao.del_by_bid(14)
	ret=dao.update_by_bid(17,book4)
	print ret
Exemple #2
0
class BookHandler(tornado.web.RequestHandler):
    def initialize(self):
        self.db = self.application.db
        self.bookdao = BookDao(self.db)
        self.Resp = Resp()

    def get(self):
        book_id = self.get_argument('book_id', None)
        book_id = tools.to_int(book_id)
        book_title = self.get_argument('book_title', None)
        book_title = tools.strip_string(book_title)
        author = self.get_argument('author', None)
        author = tools.strip_string(author)
        publisher = self.get_argument('publisher', None)
        publisher = tools.strip_string(publisher)

        resp = None
        if book_id != None:
            logging.info("in GET method! Get book by bookid: '%s'",
                         str(book_id))
            book = self.bookdao.get_by_bid(book_id)
            resp = book
        elif book_title != None:
            logging.info("in GET method! Get book by title: '%s'",
                         str(book_title))
            book = self.bookdao.get_by_title(book_title)
            resp = book
        elif author != None:
            logging.info("in GET method! Get book by author: '%s'",
                         str(author))
            book = self.bookdao.get_by_author(author)
            resp = book
        elif publisher != None:
            logging.info("in GET method! Get book by publisher: '%s'",
                         str(publisher))
            book = self.bookdao.get_by_publisher(publisher)
            resp = book
        else:
            logging.info("in GET method! Get All book")
            books = self.bookdao.get_allbook
            resp = books

        logging.info("Query result: %s", str(resp))

        if resp == None:
            logging.error("There is no record!")
            resp = self.Resp.make_response(code=RespCode.NO_RECORD)
            self.write(resp)
            return

        resp = self.Resp.make_response(code=RespCode.SUCCESS, content=resp)
        self.write(resp)

    def post(self):
        data = self.request.body
        h = json.loads(data)

        logging.info("in  insert method. receive data:%s", str(data))
        book_title = h.get('book_title', None)
        book_title = tools.strip_string(book_title)
        if book_title == None:
            logging.error("there is no parameter 'book_title'!")
            resp = self.Resp.make_response(code=RespCode.NO_PARAMETER,
                                           para='book_title')
            self.write(resp)
            return

        addr = tools.strip_string(h.get('addr', None))
        if addr == None:
            logging.error("there is no parameter 'addr'!")
            resp = self.Resp.make_response(code=ReaspCode.NO_PARAMETER,
                                           para='addr')
            self.write(resp)
            return

        author = tools.strip_string(h.get('author', None))
        price = h.get('price', None)
        publisher = tools.strip_string(h.get('publisher', None))
        description = tools.strip_string(h.get('description', None))
        logging.debug("check parameter complete, ready to save in db")
        book = {}
        book['book_title'] = book_title
        book['author'] = author
        book['publisher'] = publisher
        book['description'] = description
        book['addr'] = addr
        book['price'] = price

        ret = self.bookdao.insert_by_dict(book)
        if ret == None:
            err_str = "error oncurred when insert into table 'book'"
            logging.error(err_str)
            resp = self.Resp.make_response(code=RespCode.DB_ERROR,
                                           err_str=err_str)
            self.write(resp)
            return

        logging.info('save book object successed! the book: %s', str(book))
        book['book_id'] = ret
        resp = self.Resp.make_response(code=RespCode.SUCCESS,
                                       para="add book",
                                       content=book)
        self.write(resp)

    def delete(self):
        book_id = self.get_argument('book_id', None)
        book_id = tools.strip_string(book_id)

        if book_id == None:
            logging.error("there is no parameter 'book_id'!")
            resp = self.Resp.make_response(code=RespCode.NO_PARAMETER,
                                           para='book_id')
            self.write(resp)
            return

        logging.info(" in delete method! delete book by book_id:'%s'.",
                     str(book_id))
        ret = self.bookdao.del_by_bid(book_id)
        err_str = "error oucurred when delete book by id: '%s'" % book_id

        if ret == None:
            logging.error(err_str)
            resp = self.Resp.make_response(code=RespCode.INVALID_PARAMETER,
                                           err_str=err_str)
            self.write(resp)
            return

        logging.info("delete book object successed!")

        resp = self.Resp.make_response(code=RespCode.SUCCESS,
                                       para="delete book")
        self.write(resp)

    def put(self):
        data = self.request.body
        h = json.loads(data)

        logging.info("in update method! receive data: %s", str(data))
        book_id = h.get('book_id', None)
        book_id = tools.strip_string(book_id)

        if book_id == None:
            logging.error("there is no parameter 'book_id'!")
            resp = self.Resp.make_response(code=RespCode.NO_PARAMETER,
                                           para='book_id')
            self.write(resp)
            return

        if_exist = self.bookdao.if_exist(book_id)
        if if_exist == False:
            logging.error("the book doesn't existed!")
            resp = self.Resp.make_response(code=RespCode.INVALID_PARAMETER,
                                           para='book_id')
            self.write(resp)
            return

        h1 = {}
        for k, v in h.items():
            if v != None:
                h1[k] = v

        ret = self.bookdao.update_by_bid(book_id, h1)
        resp = self.Resp.make_response(code=RespCode.SUCCESS,
                                       para='update book')
        self.write(resp)
Exemple #3
0
class BookHandler(tornado.web.RequestHandler):
	def initialize(self):
		self.db = self.application.db
		self.bookdao=BookDao(self.db)
		self.readerdao=ReaderDao(self.db)
		self.borrowdao=BorrowDao(self.db)
		self.Resp=Resp()

	def insert(self):
		data = self.request.body
		h = json.loads(data)

		logging.error("in insert method. receive data:%s", str(data)) 

		book_title = h.get('book-title',None)
		book_title = tools.strip_string(book_title)
		if book_title == None:
			logging.error("there is no parameter 'book_title'!")
			resp = self.Resp.make_response(code=RespCode.NO_PARAMETER,para='book_title')
			self.write(resp)
			return

		addr=tools.strip_string(h.get(addr,None))
		if addr == None:
			logging.error("there is no parameter 'addr'!")
			resp = self.Reap.make_response(code = RespCode.NO_PARAMETER,para='addr')
			self.write(resp)
			return

		author=tools.strip_string(h.get('author',''))
		publisher=tools.strip_string(h.get('publisher',''))
		description=tools.strip_string(h.get('description',''))
		price=float(h.get('price',0))

		logging.debug("check parameters complete, ready to save in db")

		book={}
		book['book_title']=book_title
		book['author']=author
		book['publiser']=publisher
		book['description']=description
		book['addr']=addr
		book['price']=price

		ret = self.bookdao.insert_by_dict(book)
		if ret == None:
			err_str="error oucurred when insert into table 'book'"
			logging.error(err_str)
			resp = self.Resp.make_response(code = RespCode.DB_ERROR,err_str=err_str)
			self.write(resp)
			return

		logging.info('save book object successed! the book: %s', str(book))

		book['book_id']=ret
		resp = self.Resp.make_response(code=RespCode.SUCCESS,content=book)
		self.write(resp)

	def update(self):
		data=self.request.body
		h = json.loads(data)

		logging.info("in update method! receive data: %s", str(data))

		book_id=h.get('book_id',None)
		book_id=tools.strip_string(book_id)

		book_title=tools.strip_string(h.get('book_title',None))
		if book_id ==None:
			logging.error("there is no parameter 'book_id'!")
			resp = self.Resp.make_response(code=RespCode.NO_PARAMETER, para='book_id')
			self.write(resp)
			return

		if_exist=self.bookdao.if_exist(book_id)
		if if_exist == False:
			logging.error("the book doesn't existed!")
			resp=self.Resp.make_response(code=RespCode.NO_RECORD, para='book_id')	
			self.write(resp)
			return


		ret=self.bookdao.update_by_bid(book_id,h)
		resp=self.Resp.make_response(code=RespCode.SUCCESS)
		self.write(resp)

 
#		get=self.bookdao.get_by_bid(book_id)
#		book={}
#		book['addr']=tools.strip_string(h.get(addr,get['addr']))
#		book['author']=tools.strip_string(h.get('author',get['author']))
#		book['publisher']=tools.strip_string(h.get('publisher',get['pubulisher']))
#		book['description']=tools.strip_string(h.get('description',get['description']))
#		book['price']=float(h.get('price',get['price']))

#		ret=self.bookdao.update_by_bid(book_id,book)

#		ret always be 0,so can not classify it, it is problem

		

	def getbybid(self):

		book_id=self.get_argument('book_id',None)
		book_id=tools.strip_string(book_id)
		if book_id ==None:
			logging.error("there is no parameter 'book_id'!")
			resp = self.Resp.make_response(code=RespCode.NO_PARAMETER, para='book_id')
			self.write(resp)
			return

		logging.info("in getbybid method. get book by id: '%s'",str(book_id))
		ret=self.bookdao.get_by_bid(book_id)
		resp =ret

		if resp == None or len(resp)== 0:
			logging.error("there is no record!")
			resp = self.Resp.make_response(code=RespCode.NO_RECORD)
			self.write(resp)
			return

		resp = self.Resp.make_response(code=RespCode.SUCCESS,content=resp)
		self.write(resp)

	def getall(self):


		logging.info("in get all book method :")
		ret=self.bookdao.get_allbook()
		resp =ret

		if resp == None or len(resp)== 0:
			logging.error("there is something wrong!")
			resp = self.Resp.make_response(code=RespCode.DB_ERROR)
			self.write(resp)
			return

		resp = self.Resp.make_response(code=RespCode.SUCCESS,content=resp)
		self.write(resp)

	def delbybid(self):
		book_id=self.get_argument('book_id',None)
		book_id=tools.strip_string(book_id)
		
		if book_id == None:
			logging.error("there is no parameter 'book_id'!")
			resp = self.Resp.make_response(code=RespCode.NO_PARAMETER, para='book_id')
			self.write(resp)
			return
		
		logging.info("in delete method! delete book by book_id:'%s'.",str(book_id))
		ret=self.bookdao.del_by_bid(bid)

####same problem as before , db.excute() return 0 whether is succuss or fail###

		'''
		if ret == None:
		resp= self.Resp.make_response(code = RespCode.

		'''
		logging.info("delete book object successed!")

		resp = self.Resp.make_response(code=RespCode.SUCCESS)
		self.write(resp)
Exemple #4
0
class BookHandler(tornado.web.RequestHandler):
    def initialize(self):
        self.db = self.application.db
        self.bookdao = BookDao(self.db)
        self.readerdao = ReaderDao(self.db)
        self.borrowdao = BorrowDao(self.db)
        self.Resp = Resp()

    def insert(self):
        data = self.request.body
        h = json.loads(data)

        logging.error("in insert method. receive data:%s", str(data))

        book_title = h.get('book-title', None)
        book_title = tools.strip_string(book_title)
        if book_title == None:
            logging.error("there is no parameter 'book_title'!")
            resp = self.Resp.make_response(code=RespCode.NO_PARAMETER,
                                           para='book_title')
            self.write(resp)
            return

        addr = tools.strip_string(h.get(addr, None))
        if addr == None:
            logging.error("there is no parameter 'addr'!")
            resp = self.Reap.make_response(code=RespCode.NO_PARAMETER,
                                           para='addr')
            self.write(resp)
            return

        author = tools.strip_string(h.get('author', ''))
        publisher = tools.strip_string(h.get('publisher', ''))
        description = tools.strip_string(h.get('description', ''))
        price = float(h.get('price', 0))

        logging.debug("check parameters complete, ready to save in db")

        book = {}
        book['book_title'] = book_title
        book['author'] = author
        book['publiser'] = publisher
        book['description'] = description
        book['addr'] = addr
        book['price'] = price

        ret = self.bookdao.insert_by_dict(book)
        if ret == None:
            err_str = "error oucurred when insert into table 'book'"
            logging.error(err_str)
            resp = self.Resp.make_response(code=RespCode.DB_ERROR,
                                           err_str=err_str)
            self.write(resp)
            return

        logging.info('save book object successed! the book: %s', str(book))

        book['book_id'] = ret
        resp = self.Resp.make_response(code=RespCode.SUCCESS, content=book)
        self.write(resp)

    def update(self):
        data = self.request.body
        h = json.loads(data)

        logging.info("in update method! receive data: %s", str(data))

        book_id = h.get('book_id', None)
        book_id = tools.strip_string(book_id)

        book_title = tools.strip_string(h.get('book_title', None))
        if book_id == None:
            logging.error("there is no parameter 'book_id'!")
            resp = self.Resp.make_response(code=RespCode.NO_PARAMETER,
                                           para='book_id')
            self.write(resp)
            return

        if_exist = self.bookdao.if_exist(book_id)
        if if_exist == False:
            logging.error("the book doesn't existed!")
            resp = self.Resp.make_response(code=RespCode.NO_RECORD,
                                           para='book_id')
            self.write(resp)
            return

        ret = self.bookdao.update_by_bid(book_id, h)
        resp = self.Resp.make_response(code=RespCode.SUCCESS)
        self.write(resp)


#		get=self.bookdao.get_by_bid(book_id)
#		book={}
#		book['addr']=tools.strip_string(h.get(addr,get['addr']))
#		book['author']=tools.strip_string(h.get('author',get['author']))
#		book['publisher']=tools.strip_string(h.get('publisher',get['pubulisher']))
#		book['description']=tools.strip_string(h.get('description',get['description']))
#		book['price']=float(h.get('price',get['price']))

#		ret=self.bookdao.update_by_bid(book_id,book)

#		ret always be 0,so can not classify it, it is problem

    def getbybid(self):

        book_id = self.get_argument('book_id', None)
        book_id = tools.strip_string(book_id)
        if book_id == None:
            logging.error("there is no parameter 'book_id'!")
            resp = self.Resp.make_response(code=RespCode.NO_PARAMETER,
                                           para='book_id')
            self.write(resp)
            return

        logging.info("in getbybid method. get book by id: '%s'", str(book_id))
        ret = self.bookdao.get_by_bid(book_id)
        resp = ret

        if resp == None or len(resp) == 0:
            logging.error("there is no record!")
            resp = self.Resp.make_response(code=RespCode.NO_RECORD)
            self.write(resp)
            return

        resp = self.Resp.make_response(code=RespCode.SUCCESS, content=resp)
        self.write(resp)

    def getall(self):

        logging.info("in get all book method :")
        ret = self.bookdao.get_allbook()
        resp = ret

        if resp == None or len(resp) == 0:
            logging.error("there is something wrong!")
            resp = self.Resp.make_response(code=RespCode.DB_ERROR)
            self.write(resp)
            return

        resp = self.Resp.make_response(code=RespCode.SUCCESS, content=resp)
        self.write(resp)

    def delbybid(self):
        book_id = self.get_argument('book_id', None)
        book_id = tools.strip_string(book_id)

        if book_id == None:
            logging.error("there is no parameter 'book_id'!")
            resp = self.Resp.make_response(code=RespCode.NO_PARAMETER,
                                           para='book_id')
            self.write(resp)
            return

        logging.info("in delete method! delete book by book_id:'%s'.",
                     str(book_id))
        ret = self.bookdao.del_by_bid(bid)

        ####same problem as before , db.excute() return 0 whether is succuss or fail###
        '''
		if ret == None:
		resp= self.Resp.make_response(code = RespCode.

		'''
        logging.info("delete book object successed!")

        resp = self.Resp.make_response(code=RespCode.SUCCESS)
        self.write(resp)