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

split db_classes.py #79

Open
wants to merge 28 commits into
base: dev
Choose a base branch
from
Open

split db_classes.py #79

wants to merge 28 commits into from

Conversation

cootook
Copy link
Owner

@cootook cootook commented Jan 27, 2025

split db_classes into dedicated files
each file contains one model
database init and migration are done

seeding functions to be changed accordingly

related PR #71

image
image

that was not touched yet

canceled_by_client: Mapped[Optional[bool]] = mapped_column(default = False)

@staticmethod
def create(user_id, service, date_time, slot_id, description):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't keep any logic in the model, even this.

description: Mapped[str] = mapped_column(default = "")
deleted: Mapped[bool] = mapped_column(default = False)

def create(self, name, description):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same applies here. You shouldn't add it here. You'd better use a repository for it. But you can also create model without repositories anywhere in your code but models shouldn't contain these methods

count_slots_created = 0

count_for_cycle = how_many_days_for_advance_to_populate_slot_table + 1
for x in reversed(range(how_many_days_for_advance_to_populate_slot_table + 1)):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. You are keeping the logic in the model. This is a bad practice. It's better to keep it separated in things like services and repositories.

deleted_at: Mapped[Optional[datetime.datetime]]
deleted_by_id = mapped_column(ForeignKey("user.id"), nullable=True)

@staticmethod
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

@DispooL
Copy link

DispooL commented Jan 27, 2025

You should also keep migrations separate. Don't create one migration for all tables

depends_on = None


def upgrade():
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all tables in the migration. not the best

count_slots_created = 0

count_for_cycle = self.config.HOW_FAR_IN_FUTURE_CREATE_SLOTS + 1
for x in reversed(range(self.config.HOW_FAR_IN_FUTURE_CREATE_SLOTS + 1)):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Repository methods should only handle database operations to maintain clean architecture:

  1. Current issues:

    • Business logic (time calculations, slot generation rules) mixed with db operations
    • Hard to test business rules independently of database
    • Complex method handling multiple responsibilities
    • Difficult to reuse slot creation logic elsewhere
  2. Better approach:

    • Move business logic to service layer
    • Keep repository focused on CRUD operations
    • Separate slot generation rules from database access
    • Make code more testable and maintainable


class AppointmentRepository(BaseRepository):
def create(self, **kwargs):
new_appointment = AppointmentModel(sms_confirmation_code=randrange(1000, 9999, 11), **kwargs)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using **kwargs in repository methods creates several problems:

  1. Lack of type safety - no validation or type hints for required fields
  2. Hidden dependencies - unclear what fields are needed for appointment creation
  3. Potential errors - typos in field names won't be caught until runtime
  4. Poor documentation - developers need to look at model to know required params

Better approach would be explicit params like:
def create(self, customer_id: int, slot_id: int, service_id: int):
This makes the contract clear and catches issues at compile time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: In review
Development

Successfully merging this pull request may close these issues.

2 participants