Exemplo n.º 1
0
class ReaderHandler(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)) 

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

		sex=tools.to_int(h.get('sex',None))
		age=tools.to_int(h.get('age',None))
		email=tools.strip_string(h.get('email',''))

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

		reader={}
		reader['reader_name']=reader_name
		reader['sex']=sex
		reader['age']=age
		reader['email']=email

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

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

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

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

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

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

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

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

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

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

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

		if_exist=self.readerdao.if_exist(reader_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.readerdao.update_by_rid(reader_id,h)


#		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 return
		reader=self.readerdao.get_by_rid(reader_id)
		logging.info('update reader object successed! the reader: %s', str(reader))

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

	def getbyrid(self):
		rid=self.get_argument('readerid',None)
		rid=tools.strip_string(rid)

		resp=None
		if rid != None:
			logging.info("reader detail! readerid:'%s'",str(rid))
			ret=self.readerdao.get_by_rid(rid)
			resp=ret
			logging.info('query result: %s', str(resp))
		else:
			logging.error("there is no input!")
			resp=self.Resp.make_response(code=RespCode.NO_PARAMETER,pare= 'input')
			self.write(resp)
			return

		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)

		resp = self.Resp.make_response(code=RespCode.SUCCESS, content=resp)
		self.write(resp)
Exemplo n.º 2
0
class ReaderHandler(tornado.web.RequestHandler):
    def initialize(self):
        self.db = self.application.db
        self.readerdao = ReaderDao(self.db)
        self.Resp = Resp()

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

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

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

        sex = tools.to_int(h.get('sex', None))
        age = tools.to_int(h.get('age', None))
        email = tools.strip_string(h.get('email', None))

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

        reader = {}
        reader['reader_name'] = reader_name
        reader['sex'] = sex
        reader['age'] = age
        reader['email'] = email

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

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

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

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

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

        logging.info("in delete method! delete reader by reader_id:'%s'.",
                     str(reader_id))
        ret = self.readerdao.del_by_rid(reader_id)

        if ret == None:
            err_str = "error oucurred when delete reader by id: %s" % reader_id
            logging.error(err_str)
            resp = self.Resp.make_response(code=RespCode.INVALID_PARAMETER,
                                           err_str=err_str)
            self.write(resp)
            return

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

        resp = self.Resp.make_response(code=RespCode.SUCCESS,
                                       para='delete reader')
        self.write(resp)

    def get(self):
        rid = self.get_argument('reader_id', None)
        rid = tools.to_int(rid)

        if rid == None:
            logging.error("there is no reader_id!")
            resp = self.Resp.make_response(code=RespCode.NO_PARAMETER)
            self.write(resp)
            return

        logging.info("reader detail! readerid:'%s'", str(rid))
        ret = self.readerdao.get_by_rid(rid)
        resp = ret
        logging.info('query result: %s', str(resp))

        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)

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

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

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

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

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

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

        ret = self.readerdao.update_by_rid(reader_id, h)

        reader = self.readerdao.get_by_rid(reader_id)
        logging.info('update reader object successed! the reader: %s',
                     str(reader))

        resp = self.Resp.make_response(code=RespCode.SUCCESS,
                                       content=reader,
                                       pare="update reader")
        self.write(resp)
Exemplo n.º 3
0
class ReaderHandler(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))

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

        sex = tools.to_int(h.get('sex', None))
        age = tools.to_int(h.get('age', None))
        email = tools.strip_string(h.get('email', ''))

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

        reader = {}
        reader['reader_name'] = reader_name
        reader['sex'] = sex
        reader['age'] = age
        reader['email'] = email

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

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

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

    def delbyrid(self):
        reader_id = self.get_argument('reader_id', None)
        reader_id = tools.strip_string(reader_id)

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

        logging.info("in delete method! delete reader by reader_id:'%s'.",
                     str(reader_id))
        ret = self.readerdao.del_by_rid(bid)
        '''
		if ret == None:
		resp= self.Resp.make_response(code = RespCode.

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

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

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

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

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

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

        if_exist = self.readerdao.if_exist(reader_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.readerdao.update_by_rid(reader_id, h)

        #		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 return
        reader = self.readerdao.get_by_rid(reader_id)
        logging.info('update reader object successed! the reader: %s',
                     str(reader))

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

    def getbyrid(self):
        rid = self.get_argument('readerid', None)
        rid = tools.strip_string(rid)

        resp = None
        if rid != None:
            logging.info("reader detail! readerid:'%s'", str(rid))
            ret = self.readerdao.get_by_rid(rid)
            resp = ret
            logging.info('query result: %s', str(resp))
        else:
            logging.error("there is no input!")
            resp = self.Resp.make_response(code=RespCode.NO_PARAMETER,
                                           pare='input')
            self.write(resp)
            return

        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)

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