添加位置服务接口

This commit is contained in:
pengtao 2019-07-10 10:28:34 +08:00
parent c682160492
commit 25c02c23eb
2 changed files with 76 additions and 14 deletions

View File

@ -4,7 +4,6 @@ from flask import Flask, jsonify
from flask_restful import reqparse, abort, Api, Resource
import logging
from myredis.myredis import company_redis, expire_time
from mysql.mmysql import MysqlBase
from config import mysql_promotion_config
@ -22,8 +21,6 @@ parser.add_argument('status')
parser.add_argument('tel')
# # 操作put / get / delete单一资源Todo
# shows a single todo item and lets you delete a todo item
class Company(Resource):
def __init__(self):
self.args = parser.parse_args()
@ -38,7 +35,7 @@ class Company(Resource):
elif id:
sql = f"select id,name,contact,tel,appid,appkey,status from company where id={id};"
else:
sql = f"select id,name,contact,tel,appid,appkey,status from company where 1"
sql = f"select id,name,contact,tel,appid,appkey,status from company ;"
data = mydb.query(sql)
log.info(f"get data from db was {data}")
@ -47,7 +44,7 @@ class Company(Resource):
if line:
company = {}
company['id'], company['name'], company['contact'], company['tel'], company['appid'], company['appkey'], \
company['status'] = line
company['status'] = line
all_data.append(company)
del company
else:
@ -106,7 +103,7 @@ class Company(Resource):
company['appkey'] = 'undefined'
mydb.insert("company", company)
except Exception:
log.error("set values to company mysql/redis failed!", exc_info=True)
log.error("set values to company mysql failed!", exc_info=True)
return jsonify({'code': 500})
return jsonify({'code': 200})
@ -131,5 +128,5 @@ class Company(Resource):
else:
return jsonify({'code': 404, 'message': f"{self.args['id']} not found in mysql!"})
except Exception:
log.error("update values to company redis,mysql failed!", exc_info=True)
log.error("update values to company mysql failed!", exc_info=True)
return jsonify({'code': 500})

View File

@ -4,7 +4,6 @@ from flask import Flask, jsonify
from flask_restful import reqparse, abort, Api, Resource
import logging
from myredis.myredis import company_redis, expire_time
from mysql.mmysql import MysqlBase
from config import mysql_promotion_config
@ -14,19 +13,85 @@ log = logging.getLogger(__name__)
mydb = MysqlBase(**mysql_promotion_config)
parser = reqparse.RequestParser()
parser.add_argument('id')
parser.add_argument('name')
parser.add_argument('contact')
parser.add_argument('gameid')
parser.add_argument('area')
parser.add_argument('type')
parser.add_argument('in_used')
class Location():
def get(self):
pass
sql = f"select id,gameid,area,type from localtion where in_used=1"
try:
data = mydb.query(sql)
except Exception:
log.error("get data from location failed!", exc_info=True)
return jsonify({'code': 500})
all_data = []
log.debug(f"get data from localtion was {data}")
if data:
for line in data:
if line:
localtion = {}
localtion['id'], localtion['gameid'], localtion['area'], localtion['type'] = line
all_data.append(localtion)
del localtion
else:
log.error(f"{data} not found in mysql !")
return jsonify({'code': 404})
def post(self):
pass
try:
location = {}
location['gameid'] = self.args['gameid']
location['area'] = self.args['area']
location['type'] = self.args['type']
location['id'] = self.create_id()
mydb.insert("location", location)
except Exception:
log.error("set values to location mysql failed!", exc_info=True)
return jsonify({'code': 500})
return jsonify({'code': 200})
def create_id(self):
max_id = f"select id from location order by id desc limit 1"
data = mydb.query(max_id)
log.info(f"2 {data}")
try:
max = int(data[0][0]) + 1
log.info(f"max id was {max}!")
except Exception:
log.error(f"error with get location id ", exc_info=True)
max = 1001
return max
def put(self):
pass
try:
location = {}
location['id'] = self.args['id']
location['gameid'] = self.args['gameid']
location['area'] = self.args['area']
location['type'] = self.args['type']
location['in_used'] = self.args['in_used'] or 0
sel_sql = f"select area from location where id={self.args['id']};"
data = mydb.query(sel_sql)
if data:
condition = f"id='{self.args['id']}'"
mydb.update("location", location, condition)
return jsonify({'code': 200})
else:
return jsonify({'code': 404, 'message': f"{self.args['id']} not found in mysql!"})
except Exception:
log.error("update values to location mysql failed!", exc_info=True)
return jsonify({'code': 500})
def delete(self):
pass
id = self.args['id']
try:
del_sql = f'delete from location where id={id};'
mydb.query(del_sql)
except Exception:
log.error(f"location remove {id} failed!", exc_info=True)
return jsonify({'code': 500})
return jsonify({'code': 200})