From 91a606a893ff195aa59098008b434fdf5dbf70d7 Mon Sep 17 00:00:00 2001 From: hackrobot Date: Sat, 5 Oct 2024 22:58:38 +0800 Subject: [PATCH] 'update --- __init__.py | 0 __pycache__/crud.cpython-310.pyc | Bin 0 -> 1661 bytes __pycache__/database.cpython-310.pyc | Bin 0 -> 516 bytes __pycache__/main.cpython-310.pyc | Bin 0 -> 2060 bytes __pycache__/models.cpython-310.pyc | Bin 0 -> 670 bytes __pycache__/schemas.cpython-310.pyc | Bin 0 -> 958 bytes crud.py | 35 +++++++++++++++ database.py | 14 ++++++ main.py | 61 +++++++++++++++++++++++++++ models.py | 13 ++++++ schemas.py | 16 +++++++ 11 files changed, 139 insertions(+) create mode 100644 __init__.py create mode 100644 __pycache__/crud.cpython-310.pyc create mode 100644 __pycache__/database.cpython-310.pyc create mode 100644 __pycache__/main.cpython-310.pyc create mode 100644 __pycache__/models.cpython-310.pyc create mode 100644 __pycache__/schemas.cpython-310.pyc create mode 100644 crud.py create mode 100644 database.py create mode 100644 main.py create mode 100644 models.py create mode 100644 schemas.py 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 0000000000000000000000000000000000000000..b3a648ad7d66816971afa53b2ff37241791e6a41 GIT binary patch literal 1661 zcmZ`(O>f&q5Z&eSLy{%gmJ>%Y`f(@_R49-`&{I(q1?002b%O$e3PI+sV#cH-&r(eV z(5L3w{)EB?ddXkfYfm}n)>GfCEXPt{0y~@?4u@~vyydLbY6x5}f0D0`BO(6f&HXFE z=0`01H;fiq=Aw`TDcKM5pil$leU*nrG>G7bM(W7KlTgPxc^eKAAE}udB6ZzBq^`R@ z)-whY0-&*w|G_sWzbkMYf_6|VGw0Uk-(#m&|I z)9JV{5B5MjORB;2BC4~`NQvi9`)rypYm`2~7(GyG!*ktj6r;1{h(F0SX^%|@<~>(* zp}olFF3PhaD^vc^`eL6~AIomiS~0n*x})_Iur8iWCf1bn7%w|D&hwsHvHA!BA6?<~ z$UW%&ZP0dyQ$5-)S83kob%Q)c$wYgkFW{8We@-KN6KoSAtl`IZ*Bm6zQ zjReqSaN+@hajxb-a_-i(ebV;3~X|%A`_NSVe!} z^j|(fjoNMR^-6g*6na-DKjX-?%x4RiJ1k}aT=uQlv3u1r-Rw7OwY}>`!5LMG-XqEf{yB{jyg;Z>;D6KJYEL? literal 0 HcmV?d00001 diff --git a/__pycache__/database.cpython-310.pyc b/__pycache__/database.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..4ae746dfdbee320643824398af9a4bdf31cbd2e5 GIT binary patch literal 516 zcmYjNO=}x55S8}J_OdpG9t!yfZeimjo6wk2V&{V%wuQzCw9CRGuco%JwAx7PQsex<(}{}-(oKd07F218az5#2YlM09q7>RqnmZXeLz`{_GV}p{uVx#Z)yLz*|$NT$BQm&Mo%VzwI z5t*EvoW36&pB;=2;^Z(+j^e}Qczl$MMlZyhc5^KyFKPuv&8o%K&@~}hH?ovMu*URS z{DZIc(s*-L(J#Z$gmx|ArR}=3%UAdNE20H|)gz{R|o7vZNlAeFM z{QSMRnA}XJli6i)d!3r#*#r~Kp1+&Ql5pcqtqH%88d&W%{d;>p&Tlm+or7X!egS&b q$U3j2Ae7WBr=%gRb8)>f-48<2HHVMb%5^&V_`tS*aI#=I@6-{OxoptA(|!9@Z%l z?w~Pf4gyi<&J~3ce(ePge|c=Xmek9gAQN1V!{%wOL{_i4R%&|T{tH;Z}8@8 z=aPo2pu5I{JG#qaeWKaoZKJtiG<~s&x?2P-1FFu%y2V%E#_Hsj%h&k2xwQqiwinpH z!Q=7`zG?LDf&R_{{W0<_zHKz`bL)t7?_DF;T}yjkN2>bb@LS#d=IPVJuYQh2StUiT zy~m;yIagh(y(6I%>^jst;iLja1~4 zT#)nHh&&^dJSQXa7yTO~PLBSQUy&|_holQPv=gU=Li3FG-w;`cy>|}}J~{qT38{{S zOyc9mLcOet@;EQDh(~2|oJC2#U!H0AR8&3Q@8I*8Y3x^*GV0THCY!K44<5xyb=G?& zqUwc^kGeDrq)=s%E78jeE>hi?Rgh>SW-v`Z3j`a+0SvVdQ$+@JL`P)6IK5&QY{X=% zvN+=wx3Adm6wYlthty*b0{00xwq)MOqf8`!0AuGH2LNg>N~MVS*`5@qiK>JY{0-A} zRYlcM^srQ+ zv8X-XH;0Dw@zlWED==-tP#D`5vuT3{)a;&b+-CF_10mrT+M7rV>3jPSj7{mAlE=xA z$ackU2pl184$vmoK>zwrAnaXW2+NCV@1z*!T(;l@9Ko=Mt$8Ah?{JFOjhj&WV>hRH zFz2prV<2v^^Z@R@Mdt(NgY!0(Td=ssFlbx7OiJyfNtRSG@;v=8PaGH~S5`qV&ax31 zQE=C~utr#v3=@9`PUU@=KsH{q=e{B2#bMY z)!qea;?lVl7X%M+u?(0oyO@!31E)=#X2POZh9#fRy>VphnY$*hk*YbTcPPTzl}w$1o(8hs0HPH}p=Ry+T`(*g1?H ziR6cY`YDal_=U*M_6wOwOpeeseMg0!!h=YS~z~7~Tw?jML`!sMK IfV@Zl1zN|(4FCWD literal 0 HcmV?d00001 diff --git a/__pycache__/models.cpython-310.pyc b/__pycache__/models.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..b0a989251907ce5803af193c0a2c8f0fb58e3612 GIT binary patch literal 670 zcmYjP%Z}496txrQ(WD)*7!ZE|*|1=R5JDr02CZo zF^>)7jD~om6O;0kk`F}1iv1*tLAsB5rjiw@(j#`0iurWe>A|!<``Grd0qfIMZ<}`G zlS|#nzM`JJlMdFQ{Gp{tE^xaq2tYVhghz_<7#Jif!WAE4o+8T>+tVqMsPw>ijMeQ5|M-|`?!d12Kxe%>1AcQZ3Fr6B7@aID82C45d z3nBV2;6!bNtu{vRQKM{l3qK38ra?AA&dVOm7Od$!--Em9yKUWeMk={&YLo@qz1WU6!}4(J?hGL(jGjg3bdKv0opM)I z?hCimQm?PUjQ3I}nUgl3D~YGU_q1Ws)ON4sF~R zCo>ZGvn&MS#H5H*5Qrt>%ybZUTAUd5iRIlt$ii|SyEodytu?S(P~`AP1QWde8V#_5 z8&-%r1`-m(#|%>Z6=SShWFU>aVTh9!cV2Kx80%$mFs;Jl7trAW9Mz271qRAS*AUMF z)N61$Y#hvD=f^fH_ADirRkO6YimGgr%QE)LvbJWk0`&W3`F*2TM1x5B;NqYxb<^0O zLuDI}(R8`8I=BqSa2+(in0rG$nZNn;-Tcmj^K)?3V*Vcdw_w-v#@5>Cbv5^k2dK5b zT0c4xgs!Je^_b#{5BZ3<@gwzchgr z1QN)n%L9U|scn|k{pVSpyc#Pi>uOpboEk7@+dtB<|7TR(jS$W9>@}0Dn&3#?HsKVV zyTGrM;c54@u}jg$W;n1~KN{VHYT*QNkY=r#ZNe$GsK(o8cTG}wT34S9#V{Mme>z*d AG5`Po literal 0 HcmV?d00001 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