Get Area Information API (Questionable Implementation)
The implementation of the area information retrieval requires careful review of the json.dumps usage.
Save House Basic Information API
Several issues remain unclear:
- Filtering facility IDs: The logic for filtering out invalid facility IDs.
- Adding facility list to house: How the facility list is associated with the house.
API Documentation
- URL:
/houses/info - Method:
POST - Accepts: All facility parameters
- Return Format:
{
"house_id": 1
}
Business Logic:
- Accept all basic house settings and facility parameters.
- Save basic house information to the
housetable. - Add facility IDs to the house-facility association.
Code Implementation:
# Validate incoming parameters
if not all((title, price, area_id, address, room_count, acreage, unit, capacity, beds, deposit, min_days, max_days)):
return jsonify(errno=RET.PARAMERR, errmsg="Incomplete parameters")
# Create house object
user_id = g.user_id
house = House(
user_id=user_id,
area_id=area_id,
title=title,
price=price,
address=address,
room_count=room_count,
acreage=acreage,
unit=unit,
capacity=capacity,
beds=beds,
deposit=deposit,
min_days=min_days,
max_days=max_days
)
# Process facility information: filter out invalid facility IDs
facility_id_list = house_data.get("facility") # Example: [1, 4]
if facility_id_list:
# User selected some facilities
# Filter invalid IDs by querying database
facility_list = Facility.query.filter(Facility.id.in_(facility_id_list)).all()
# Add facility information to the house
if facility_list:
house.facilities = facility_list
# Save all information to database
db.session.add(house)
db.session.commit()
# Return house ID for the subsequent image upload request
return jsonify(errno=RET.OK, errmsg="Save successful", data={"house_id": house.id})
Save House Image API
API Documentation
- URL:
/houses/image - Method:
POST - Accepted Parameters:
house_id,image_file - Return Format:
{
"image_url": "image_url"
}
Business Logic:
- Check if the house exists.
- Upload the image to Qiniu Cloud Storage.
- Save image information to the database.
- Handle the main image for the house.
- Commit all changes.
Code Implementation:
# Accept parameters
house_id = request.form.get("house_id")
image_file = request.files.get("house_image")
# Validate parameters
if not all([house_id, image_file]):
return jsonify(errno=RET.PARAMERR, errmsg="Incomplete parameters")
# Check if house exists
try:
house = House.query.get(house_id)
except Exception as e:
logging.error(e)
return jsonify(errno=RET.DBERR, errmsg="Database error")
if house is None:
return jsonify(errno=RET.NODATA, errmsg="House does not exist")
# Upload image to Qiniu
image_data = image_file.read()
file_name = storage(image_data)
# Save image record to database (not yet committed)
house_image = HouseImage(
house_id=house_id,
url=file_name
)
db.session.add(house_image)
# Set main image for the house if not already set
if not house.index_image_url:
house.index_image_url = file_name
db.session.add(house)
# Commit and return image URL
db.session.commit()
image_url = constants.QINIU_URL_DOMAIN + file_name
return jsonify(errno=RET.OK, errmsg="Image saved successfully", data={"image_url": image_url})
Get Landlord's Published House Listings
API Documentation
- URL:
/users/houses - Method:
GET - Return Format:
{
"errno": 0,
"errmsg": "OK",
"data": {
"houses": houses_list
}
}
Business Logic:
- Get
user_idfrom the globalgvariable. - Query all houses belonging to the user.
- Return the list to the frontend.
Code Implementation:
user_id = g.user_id
user = User.query.get(user_id)
houses = user.houses # one-to-many relationship
houses_list = []
if houses:
for house in houses:
houses_list.append(house.to_basic_dict())
return jsonify(errno=RET.OK, errmsg="OK", data={"houses": houses_list})
Get House Information for Homepgae Slideshow
API Documentation
- URL:
/houses/index - Method:
GET - Parameters: None
- Return Format:
{
"errno": 0,
"errmsg": "OK",
"data": json_houses
}
Business Logic:
- Query the top 5 houses with the highest order count.
- Convert the house objects to JSON and store in a list.
- Return the list to the frontend.
Code Implementation:
houses = House.query.order_by(House.order_count.desc()).limit(constants.HOME_PAGE_MAX_HOUSES)
if not houses:
return jsonify(errno=RET.NODATA, errmsg="No data found")
houses_list = []
for house in houses:
# Skip houses without a main image
if not house.index_image_url:
continue
houses_list.append(house.to_basic_dict())
# Convert to JSON and optionally cache in Redis
json_houses = json.dumps(houses_list)
return '{"errno":0, "errmsg":"OK", "data":%s}' % json_houses, 200, {"Content-Type": "application/json"}
Get House Detail API
API Documentation
- URL:
/houses/<int:house_id> - Method:
GET - Accepted Parameters:
house_id(in URL) - Return Format:
{
"errno": "0",
"errmsg": "OK",
"data": {
"user_id": user_id,
"house": json_house
}
}
Note: If the current user is not the landlord, they can book the room. This requires returning the current user's ID, or -1 if not logged in.
Business Logic:
- Get user ID from session; default to
-1if not logged in. - Validate
house_id. - Try to retrieve house info from Redis cache first; if not found, query the database and cache it.
- Convert the house object to JSON and return.
Code Implementation:
# Get user ID from session, default to -1 if not logged in
user_id = session.get("user_id", "-1")
# Try to get house info from Redis cache
ret = redis_store.get("house_info_%s" % house_id)
if ret:
logging.info("hit house info redis")
return '{"errno":"0", "errmsg":"OK", "data":{"user_id":%s, "house":%s}}' % (user_id, ret), 200, {"Content-Type": "application/json"}
# If not in cache, query database
house = House.query.get(house_id)
house_data = house.to_full_dict()
json_house = json.dumps(house_data)
# Store in Redis cache
redis_store.setex("house_info_%s" % house_id, constants.HOUSE_DETAIL_REDIS_EXPIRE_SECOND, json_house)
# Return response
resp = '{"errno":"0", "errmsg":"OK", "data":{"user_id":%s, "house":%s}}' % (user_id, json_house), 200, {"Content-Type": "application/json"}
return resp
Get House List API
Note: There is a question regarding the use of append with == to add an empty string to a list.
API Documentation
- URL:
/houses/list(inferred) - Method:
GET - Parameters: To be determined
- Return Format: To be determined
Business Logic:
- To be implemented.
Code Implementation:
(No code provided in original article)
The above sections detail the API endpoints for a house rental platform, including saving house information, uploading images, retrieving user listings, homepage slideshow data, house details, and house lists.