'update
This commit is contained in:
0
__init__.py
Normal file
0
__init__.py
Normal file
BIN
__pycache__/crud.cpython-310.pyc
Normal file
BIN
__pycache__/crud.cpython-310.pyc
Normal file
Binary file not shown.
BIN
__pycache__/database.cpython-310.pyc
Normal file
BIN
__pycache__/database.cpython-310.pyc
Normal file
Binary file not shown.
BIN
__pycache__/main.cpython-310.pyc
Normal file
BIN
__pycache__/main.cpython-310.pyc
Normal file
Binary file not shown.
BIN
__pycache__/models.cpython-310.pyc
Normal file
BIN
__pycache__/models.cpython-310.pyc
Normal file
Binary file not shown.
BIN
__pycache__/schemas.cpython-310.pyc
Normal file
BIN
__pycache__/schemas.cpython-310.pyc
Normal file
Binary file not shown.
35
crud.py
Normal file
35
crud.py
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from models import CityWeather
|
||||||
|
from schemas import CityWeatherCreate
|
||||||
|
|
||||||
|
def get_city_weather(db: Session, city_id: int):
|
||||||
|
return db.query(CityWeather).filter(CityWeather.id == city_id).first()
|
||||||
|
|
||||||
|
def get_city_weather_by_name(db: Session, city_name: str):
|
||||||
|
return db.query(CityWeather).filter(CityWeather.city_name == city_name).first()
|
||||||
|
|
||||||
|
def get_city_weathers(db: Session, skip: int = 0, limit: int = 10):
|
||||||
|
return db.query(CityWeather).offset(skip).limit(limit).all()
|
||||||
|
|
||||||
|
def create_city_weather(db: Session, city_weather: CityWeatherCreate):
|
||||||
|
db_city_weather = CityWeather(**city_weather.dict())
|
||||||
|
db.add(db_city_weather)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(db_city_weather)
|
||||||
|
return db_city_weather
|
||||||
|
|
||||||
|
def update_city_weather(db: Session, city_id: int, city_weather: CityWeatherCreate):
|
||||||
|
db_city = db.query(CityWeather).filter(CityWeather.id == city_id).first()
|
||||||
|
if db_city:
|
||||||
|
for key, value in city_weather.dict(exclude_unset=True).items():
|
||||||
|
setattr(db_city, key, value)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(db_city)
|
||||||
|
return db_city
|
||||||
|
|
||||||
|
def delete_city_weather(db: Session, city_id: int):
|
||||||
|
db_city = db.query(CityWeather).filter(CityWeather.id == city_id).first()
|
||||||
|
if db_city:
|
||||||
|
db.delete(db_city)
|
||||||
|
db.commit()
|
||||||
|
return db_city
|
||||||
14
database.py
Normal file
14
database.py
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
from sqlalchemy import create_engine
|
||||||
|
from sqlalchemy.ext.declarative import declarative_base
|
||||||
|
from sqlalchemy.orm import sessionmaker
|
||||||
|
|
||||||
|
# 使用 mysqlclient
|
||||||
|
# SQLALCHEMY_DATABASE_URL = "mysql://username:password@localhost/weather_db"
|
||||||
|
|
||||||
|
# 使用 PyMySQL
|
||||||
|
SQLALCHEMY_DATABASE_URL = "mysql+pymysql://hackrobot:Xiao4669805@101.42.43.245:3400/hackrobot"
|
||||||
|
|
||||||
|
engine = create_engine(SQLALCHEMY_DATABASE_URL, pool_pre_ping=True)
|
||||||
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||||
|
|
||||||
|
Base = declarative_base()
|
||||||
61
main.py
Normal file
61
main.py
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
from fastapi import FastAPI, HTTPException, Depends
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
# from . import models, schemas, crud
|
||||||
|
import models
|
||||||
|
import schemas
|
||||||
|
import crud
|
||||||
|
|
||||||
|
# from .database import SessionLocal, engine
|
||||||
|
from database import SessionLocal, engine
|
||||||
|
|
||||||
|
|
||||||
|
# 创建数据库表
|
||||||
|
models.Base.metadata.create_all(bind=engine)
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
|
|
||||||
|
# 依赖项,用于获取数据库会话
|
||||||
|
def get_db():
|
||||||
|
db = SessionLocal()
|
||||||
|
try:
|
||||||
|
yield db
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
# 创建新的城市天气记录
|
||||||
|
@app.post("/city_weather/", response_model=schemas.CityWeather)
|
||||||
|
def create_city_weather(city_weather: schemas.CityWeatherCreate, db: Session = Depends(get_db)):
|
||||||
|
db_city = crud.get_city_weather_by_name(db, city_name=city_weather.city_name)
|
||||||
|
if db_city:
|
||||||
|
raise HTTPException(status_code=400, detail="City already registered")
|
||||||
|
return crud.create_city_weather(db=db, city_weather=city_weather)
|
||||||
|
|
||||||
|
# 获取城市天气记录
|
||||||
|
@app.get("/city_weather/{city_id}", response_model=schemas.CityWeather)
|
||||||
|
def read_city_weather(city_id: int, db: Session = Depends(get_db)):
|
||||||
|
db_city_weather = crud.get_city_weather(db, city_id=city_id)
|
||||||
|
if db_city_weather is None:
|
||||||
|
raise HTTPException(status_code=404, detail="City not found")
|
||||||
|
return db_city_weather
|
||||||
|
|
||||||
|
# 获取多个城市天气记录
|
||||||
|
@app.get("/city_weather/", response_model=list[schemas.CityWeather])
|
||||||
|
def read_city_weathers(skip: int = 0, limit: int = 10, db: Session = Depends(get_db)):
|
||||||
|
cities = crud.get_city_weathers(db, skip=skip, limit=limit)
|
||||||
|
return cities
|
||||||
|
|
||||||
|
# 更新城市天气记录
|
||||||
|
@app.put("/city_weather/{city_id}", response_model=schemas.CityWeather)
|
||||||
|
def update_city_weather(city_id: int, city_weather: schemas.CityWeatherCreate, db: Session = Depends(get_db)):
|
||||||
|
db_city = crud.update_city_weather(db=db, city_id=city_id, city_weather=city_weather)
|
||||||
|
if db_city is None:
|
||||||
|
raise HTTPException(status_code=404, detail="City not found")
|
||||||
|
return db_city
|
||||||
|
|
||||||
|
# 删除城市天气记录
|
||||||
|
@app.delete("/city_weather/{city_id}", response_model=schemas.CityWeather)
|
||||||
|
def delete_city_weather(city_id: int, db: Session = Depends(get_db)):
|
||||||
|
db_city = crud.delete_city_weather(db=db, city_id=city_id)
|
||||||
|
if db_city is None:
|
||||||
|
raise HTTPException(status_code=404, detail="City not found")
|
||||||
|
return db_city
|
||||||
13
models.py
Normal file
13
models.py
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
from sqlalchemy import Column, Integer, String, Float
|
||||||
|
from database import Base
|
||||||
|
# from .database import Base
|
||||||
|
|
||||||
|
|
||||||
|
class CityWeather(Base):
|
||||||
|
__tablename__ = "citys"
|
||||||
|
|
||||||
|
id = Column(Integer, primary_key=True, index=True)
|
||||||
|
city_name = Column(String(50), unique=True, index=True, nullable=False)
|
||||||
|
temperature = Column(Float, nullable=False)
|
||||||
|
weather = Column(String(100))
|
||||||
|
other_info = Column(String(200))
|
||||||
16
schemas.py
Normal file
16
schemas.py
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
class CityWeatherBase(BaseModel):
|
||||||
|
city_name: str
|
||||||
|
temperature: float
|
||||||
|
weather: str | None = None
|
||||||
|
other_info: str | None = None
|
||||||
|
|
||||||
|
class CityWeatherCreate(CityWeatherBase):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class CityWeather(CityWeatherBase):
|
||||||
|
id: int
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
orm_mode = True
|
||||||
Reference in New Issue
Block a user