diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/__pycache__/crud.cpython-310.pyc b/__pycache__/crud.cpython-310.pyc new file mode 100644 index 0000000..b3a648a Binary files /dev/null and b/__pycache__/crud.cpython-310.pyc differ diff --git a/__pycache__/database.cpython-310.pyc b/__pycache__/database.cpython-310.pyc new file mode 100644 index 0000000..4ae746d Binary files /dev/null and b/__pycache__/database.cpython-310.pyc differ diff --git a/__pycache__/main.cpython-310.pyc b/__pycache__/main.cpython-310.pyc new file mode 100644 index 0000000..bdb839d Binary files /dev/null and b/__pycache__/main.cpython-310.pyc differ diff --git a/__pycache__/models.cpython-310.pyc b/__pycache__/models.cpython-310.pyc new file mode 100644 index 0000000..b0a9892 Binary files /dev/null and b/__pycache__/models.cpython-310.pyc differ diff --git a/__pycache__/schemas.cpython-310.pyc b/__pycache__/schemas.cpython-310.pyc new file mode 100644 index 0000000..4a36cc1 Binary files /dev/null and b/__pycache__/schemas.cpython-310.pyc differ diff --git a/crud.py b/crud.py new file mode 100644 index 0000000..b7b460d --- /dev/null +++ b/crud.py @@ -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 diff --git a/database.py b/database.py new file mode 100644 index 0000000..0f7553b --- /dev/null +++ b/database.py @@ -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() diff --git a/main.py b/main.py new file mode 100644 index 0000000..46124d0 --- /dev/null +++ b/main.py @@ -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 diff --git a/models.py b/models.py new file mode 100644 index 0000000..d5b531b --- /dev/null +++ b/models.py @@ -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)) diff --git a/schemas.py b/schemas.py new file mode 100644 index 0000000..7af61d6 --- /dev/null +++ b/schemas.py @@ -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