Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating Backend Folder Structure #313

Closed
bgmrsln opened this issue Oct 14, 2023 · 4 comments
Closed

Creating Backend Folder Structure #313

bgmrsln opened this issue Oct 14, 2023 · 4 comments
Assignees
Labels
effort: level 3 How much effort is needed for this task priority: high Issue is important, must be resolved in a reasonable time state: assigned This task is currently assigned to a contributor state: In progress This task is currently being developed team: back-end Issue is part of back-end development

Comments

@bgmrsln
Copy link
Contributor

bgmrsln commented Oct 14, 2023

Issue Description

Creating a well-organized folder structure for a backend project is essential for maintainability, collaboration, and ease of development. Structure should include

Disaster-Response-Platform/

  • Backend
    • main.py

    • Controllers

      • user_controller.py
      • resource_controller.py
      • need_controller.py
      • user_profile_controller.py
    • Models

      • user_model.py
      • resource_model.py
      • need_model.py
    • Database

      • mongo.py
      • ...
    • Services

      • user_service.py
      • resource_service.py
      • need_service.py
      • user_profile_service.py
    • Config

      • config.py
      • ...
    • Tests

      • user_test.py
      • resource_test.py
      • need_test.py
      • user_profile_test.py
    • Dockerfile

    • requirements.txt

    • .env

    • .gitignore

    • README.md

    • ...

Used link for the structure: https://medium.com/@ketansomvanshi007/structuring-a-fastapi-app-an-in-depth-guide-cdec3b8f4710

Deadline of the Issue

17.10.2023

Reviewer

Mehmet Kuzulugil

@bgmrsln bgmrsln added state: assigned This task is currently assigned to a contributor priority: high Issue is important, must be resolved in a reasonable time state: In progress This task is currently being developed effort: level 3 How much effort is needed for this task team: back-end Issue is part of back-end development labels Oct 14, 2023
@bgmrsln bgmrsln self-assigned this Oct 14, 2023
@mehmetkuzu
Copy link
Contributor

Seems good. Thanks.

Only:

  1. the sample file extensions: .py
  2. Services folder is for API functions? Like the "routers" folder we had in practice-app?

@bgmrsln
Copy link
Contributor Author

bgmrsln commented Oct 14, 2023

  1. the sample file extensions: .py
    Fixed it.
  2. Services folder is for API functions? Like the "routers" folder we had in practice-app?
    Services for the actual functions. And controller is for the API part. Let me explain with an example:
    Model:
from pydantic import BaseModel
from bson import ObjectId

class UserCreate(BaseModel):
    username: str
    password: str

class User(UserCreate):
    id: ObjectId

class UserInDB(User):
    hashed_password: str

Services:

from pymongo import MongoClient
from bson import ObjectId
from models.user_model import UserCreate, UserInDB

class UserService:
    def __init__(self):
        self.client = MongoClient("mongodb://localhost:27017/")
        self.db = self.client["user_db"]
        self.users_collection = self.db["users"]

    def create_user(self, user: UserCreate):
        user_dict = user.dict()
        user_dict['hashed_password'] = "hashed_password_here"  # Hash the password
        inserted = self.users_collection.insert_one(user_dict)
        user_id = inserted.inserted_id
        return UserInDB(**user.dict(), id=user_id, hashed_password="hashed_password_here")

    def get_user_by_id(self, user_id: ObjectId):
        user_dict = self.users_collection.find_one({"_id": user_id})
        if user_dict:
            return UserInDB(**user_dict)
        return None

    def get_user_by_username(self, username: str):
        user_dict = self.users_collection.find_one({"username": username})
        if user_dict:
            return UserInDB(**user_dict)
        return None

Controllers:

from fastapi import APIRouter, Depends, HTTPException
from models.user_model import User, UserCreate
from services.user_service import UserService

router = APIRouter()
user_service = UserService()

@router.post("/users/", response_model=User)
def create_user(user: UserCreate):
    user_in_db = user_service.get_user_by_username(user.username)
    if user_in_db:
        raise HTTPException(status_code=400, detail="Username already registered")
    return user_service.create_user(user)

@router.get("/users/{user_id}", response_model=User)
def read_user(user_id: str):
    try:
        user_id = ObjectId(user_id)
        user = user_service.get_user_by_id(user_id)
        if user is None:
            raise HTTPException(status_code=404, detail="User not found")
        return user
    except ValueError:
        raise HTTPException(status_code=400, detail="Invalid user ID")

@mehmetkuzu
Copy link
Contributor

Got it.
Great.

@mehmetkuzu
Copy link
Contributor

It seems its ok to close this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
effort: level 3 How much effort is needed for this task priority: high Issue is important, must be resolved in a reasonable time state: assigned This task is currently assigned to a contributor state: In progress This task is currently being developed team: back-end Issue is part of back-end development
Projects
None yet
Development

No branches or pull requests

2 participants