diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7716d0a --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +secrets.env +/env +/__pycache__ \ No newline at end of file diff --git a/README.md b/README.md index 7432a62..7f9c2a5 100644 --- a/README.md +++ b/README.md @@ -1,55 +1 @@ -# Marriage Matchmaking App - -## Brief Description -The Marriage Matchmaking App is a simple backend application designed to help users find potential matches based on their profile information. The app allows users to create, read, update, and delete profiles with details such as name, age, gender, email, city, and interests. - -## What is Provided? -This project provides a basic skeleton for a FastAPI-based backend application. -#### Github repo: **https://github.com/abhishek-UM/UrbanMatch-PythonTask/tree/master** - -The provided code includes: - -### Basic Project Structure: - -- **main.py** : The main application file with basic CRUD operations for user profiles. -- **models.py**: SQLAlchemy models defining the User schema. -- **database.py**: Database configuration and setup. -- **schemas.py**: Pydantic schemas for data validation and serialization. - -### Functionality: - -- Create User Endpoint: Create a new user profile. -- Read Users Endpoint: Retrieve a list of user profiles. -- Read User by ID Endpoint: Retrieve a user profile by ID. -- SQLite Database: The application uses SQLite as the database to store user profiles. - -## What is Required? -### Tasks: -1. Add User Update Endpoint: - - Implement an endpoint to update user details by ID in the main.py file. -2. Add User Deletion Endpoint: - - Implement an endpoint to delete a user profile by ID. -3. Find Matches for a User: - - Implement an endpoint to find potential matches for a user based on their profile information. -4. Add Email Validation: - - Add validation to ensure the email field in user profiles contains valid email addresses. - -## Instructions: -Implement the required endpoints and email validation: - -1. Add the necessary code for the update, delete, match and validation endpoints -2. Test Your Implementation: - 1. Verify that users can be updated and deleted correctly. - 2. Check that matches are correctly retrieved for a given user. - 3. Ensure email validation is working as expected. - -## Submit Your Work: -Provide the updated code files (main.py, models.py, database.py, and schemas.py). -Include a brief report explaining your approach and any assumptions made. - - -### Prerequisites -- Python 3.7+ -- FastAPI -- SQLAlchemy -- SQLite +# Marriage Matchmaking App Assignment - Saurabh Dhingra diff --git a/database.py b/database.py index accf2f5..8dd46b8 100644 --- a/database.py +++ b/database.py @@ -2,9 +2,12 @@ from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker -// Free to use remote db or create a local database. Modify the URl appropriately -SQLALCHEMY_DATABASE_URL = "sqlite:///./test.db" +from dotenv import load_dotenv +import os +load_dotenv("secrets.env") + +SQLALCHEMY_DATABASE_URL = os.getenv("DB_CONNECTION_STRING") engine = create_engine(SQLALCHEMY_DATABASE_URL) SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) Base = declarative_base() diff --git a/main.py b/main.py index f83e868..76df9ea 100644 --- a/main.py +++ b/main.py @@ -34,3 +34,56 @@ def read_user(user_id: int, db: Session = Depends(get_db)): raise HTTPException(status_code=404, detail="User not found") return user +@app.put("/users/{user_id}", response_model=schemas.User) +async def update_user(user_id: int, user: schemas.UserUpdate, db: Session = Depends(get_db)): + db_user = db.query(models.User).filter(models.User.id == user_id).first() + if db_user is None: + raise HTTPException(status_code=404, detail="User not found") + for key, value in user.dict(exclude_unset=True).items(): + setattr(db_user, key, value) + db.commit() + db.refresh(db_user) + return db_user + +from typing import Optional + +@app.get("/users/{user_id}/matches", response_model=list[schemas.User]) +def find_potential_matches(user_id: int, requirements: schemas.MatchRequirements, db: Session = Depends(get_db)): + user = db.query(models.User).filter(models.User.id == user_id).first() + if user is None: + raise HTTPException(status_code=404, detail="User not found") + + opposite_gender = "m" if user.gender == "f" else "f" + + if requirements.min_age is None: + min_age = user.age - 2 + else: + min_age = int(requirements.min_age) + + if requirements.max_age is None: + max_age = user.age + 2 + else: + max_age = int(requirements.max_age) + + + query = db.query(models.User).filter( + models.User.gender == opposite_gender, + models.User.age >= min_age, + models.User.age <= max_age, + ) + + if requirements.city: + query = query.filter(models.User.city == requirements.city) + + matches = query.all() + return matches + +@app.delete("/users/{user_id}", response_model=dict) +async def delete_user(user_id: int, db: Session = Depends(get_db)): + db_user = db.query(models.User).filter(models.User.id == user_id).first() + if db_user is None: + raise HTTPException(status_code=404, detail="User not found") + db.delete(db_user) + db.commit() + return {"message": "User deleted successfully"} + diff --git a/models.py b/models.py index 0e42748..9ef9fe2 100644 --- a/models.py +++ b/models.py @@ -5,10 +5,10 @@ class User(Base): __tablename__ = "users" id = Column(Integer, primary_key=True, index=True) - name = Column(String, index=True) + name = Column(String(30), index=True) age = Column(Integer) - gender = Column(String) - email = Column(String, unique=True, index=True) - city = Column(String, index=True) - interests = Column(ARRAY(String)) + gender = Column(String(1)) + email = Column(String(40), unique=True, index=True) + city = Column(String(30), index=True) + interests = Column(ARRAY(String(20))) diff --git a/schemas.py b/schemas.py index 5872710..3b7540a 100644 --- a/schemas.py +++ b/schemas.py @@ -1,20 +1,32 @@ -from pydantic import BaseModel -from typing import List +from typing import List, Optional +from pydantic import BaseModel, EmailStr class UserBase(BaseModel): name: str age: int gender: str - email: str + email: EmailStr city: str - interests: List[str] + interests: str class UserCreate(UserBase): pass +class UserUpdate(BaseModel): + name: Optional[str] = None + age: Optional[int] = None + gender: Optional[str] = None + email: Optional[EmailStr] = None + city: Optional[str] = None + interests: Optional[str] = None + class User(UserBase): id: int class Config: orm_mode = True +class MatchRequirements(BaseModel): + min_age: Optional[int] = None + max_age: Optional[int] = None + city: Optional[str] = None