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

Initial Ordering implementation #7

Merged
merged 12 commits into from
Aug 5, 2024
Merged

Conversation

PaulJWright
Copy link
Owner

@PaulJWright PaulJWright commented Aug 4, 2024

This MR addresses an initial attempt at:

  1. Database Setup: SQLite database using alembic for migrations. SQLite was selected for its simplicity and portability, as it enables a single-file database (data/orders.db), well-suited for Docker environments with volume mounting.

  2. GET Endpoint for Orders: GET endpoint to read orders from the database and provide the results through an API. This endpoint is consumed by a Streamlit app to display order data (see screenshots below).

  3. POST endpoint for creating orders. Note that this will need future updates to handle stock management. The Streamlit app was updated to display orders in a table format.

This is designed using an OrdersService to handle the Business logic, and uses a Repository Pattern (OrdersRepository) a UnitOfWork to handle interaction with the database.

# weird_salads.api.app
def get_orders():
    with UnitOfWork() as unit_of_work:
        repo = OrdersRepository(unit_of_work.session)
        orders_service = OrdersService(repo)
        results = orders_service.list_orders()
    return {"orders": [result.dict() for result in results]}

where the OrdersRepository directly queries the DB through the sqlalchemy model, OrderModel. For example, the OrdersRepository has the the list method that queries the OrderModel, and returns as a list of Order objects (Order is a class to hold the data).

# weird_salads.orders.repository.orders_repository
class OrdersRepository:
    def __init__(self, session):
        self.session = session

    def add(self, item):
        pass

    def _get(self, id: str):
        pass

    def get(self, id):
        pass
       
    def list(self):
        query = self.session.query(OrderModel)
        records = query.all()
        return [Order(**record.dict()) for record in records]
        
    def update(self, id):
        pass

    def delete(self, id):
        pass

and is accessed through the OrdersService from the get_order method:

class OrdersService:
    def __init__(self, orders_repository: OrdersRepository):
        self.orders_repository = orders_repository

    def place_order(self, item):
        pass

    def get_order(self, order_id: str):
        pass

    def list_orders(self):
        """
        get all orders
        """
        return self.orders_repository.list()

Screenshot 2024-08-05 at 01 15 17

Screenshot 2024-08-05 at 10 01 51

@PaulJWright PaulJWright changed the title Order implementation Initial Ordering implementation Aug 4, 2024
@PaulJWright PaulJWright force-pushed the feature/orders_implementation branch from 469eaf4 to 955788d Compare August 5, 2024 00:10
@PaulJWright PaulJWright merged commit 9c2a883 into main Aug 5, 2024
1 check passed
@PaulJWright PaulJWright deleted the feature/orders_implementation branch August 5, 2024 09:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant