Skip to content

Commit

Permalink
feat(pet) : pet image upload function added
Browse files Browse the repository at this point in the history
  • Loading branch information
shahedamin committed Apr 19, 2024
1 parent 53dd658 commit c757973
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 3 deletions.
Binary file modified pet/__pycache__/database.cpython-311.pyc
Binary file not shown.
Binary file modified pet/__pycache__/main.cpython-311.pyc
Binary file not shown.
Binary file modified pet/__pycache__/models.cpython-311.pyc
Binary file not shown.
Binary file added pet/__pycache__/schemas.cpython-311.pyc
Binary file not shown.
Binary file modified pet/__pycache__/services.cpython-311.pyc
Binary file not shown.
17 changes: 14 additions & 3 deletions pet/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

from fastapi import FastAPI, HTTPException, Depends,Path
from services import create_pet, get_pet_by_id, get_pets_by_gender_from_db, get_pets_by_location_from_db, get_pets_by_type_from_db
from fastapi import FastAPI, HTTPException, Depends,Path,UploadFile, File


from services import create_pet, get_pet_by_id, get_pets_by_gender_from_db, get_pets_by_location_from_db, get_pets_by_type_from_db, upload_pet_image
import schemas
from models import Pet
from schemas import PetCreate
Expand Down Expand Up @@ -73,4 +75,13 @@ def delete_pet_by_id(pet_id: int, db: Session = Depends(get_db)):
raise HTTPException(status_code=404, detail="Pet not found")
db.delete(pet)
db.commit()
return {"message": "Pet deleted successfully"}
return {"message": "Pet deleted successfully"}

# upload pet image
@app.post("/pets/{pet_id}/upload_image")
async def image_upload(pet_id: int, image: UploadFile = File(...), db: Session = Depends(get_db)):
try:
upload_pet_image(db, pet_id, image)
return {"message": "Image uploaded successfully."}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
1 change: 1 addition & 0 deletions pet/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Pet(Base):
pet_age = Column(Integer)
description = Column(String(50))
location_city = Column(String(10))
pet_image = Column(String(200))

owner = relationship("User", back_populates="pets")

Expand Down
18 changes: 18 additions & 0 deletions pet/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from sqlalchemy.orm import Session
from models import Pet
from schemas import PetCreate
from fastapi import HTTPException, UploadFile


def create_pet(db: Session, pet_data: PetCreate):
try:
Expand Down Expand Up @@ -34,3 +36,19 @@ def get_pets_by_gender_from_db(db: Session, pet_gender: str) -> List[Optional[Pe
def get_pets_by_location_from_db(db: Session, location_city: str) -> List[Optional[Pet]]:
pets = db.query(Pet).filter(Pet.location_city == location_city).all()
return pets

# Upload pet image
def upload_pet_image(db: Session, pet_id: int, image: UploadFile):
try:
pet = db.query(Pet).filter(Pet.pet_id == pet_id).first()
if not pet:
raise HTTPException(status_code=404, detail="Pet not found")
file_location = f"H:/pet images/{image.filename}"
with open(file_location, "wb+") as file_object:
file_object.write(image.file.read())
pet.pet_image = file_location
db.commit()
db.refresh(pet)
except Exception as e:
db.rollback()
raise e

0 comments on commit c757973

Please sign in to comment.