
from collections import Counter
from typing import Dict, Sequence

from ....database import db

from ..article.models.article import ArticleNumber

from .models.order_item import OrderItem
from .models.payment import PaymentState


def count_ordered_articles(article_number: ArticleNumber
                          ) -> Dict[PaymentState, int]:
    """Count how often the article has been ordered, grouped by the
    order's payment state.
    """
    order_items = OrderItem.query \
        .filter_by(article_number=article_number) \
        .options(
            db.joinedload('order'),
            db.joinedload('article'),
        ) \
        .all()

    # Ensure every payment state is present in the resulting dictionary,
    # even if no orders of the corresponding payment state exist for the
    # article.
    counter = Counter({state: 0 for state in PaymentState})

    for order_item in order_items:
        counter[order_item.order.payment_state] += order_item.quantity

    return dict(counter)


def get_order_items_for_article(article_number: ArticleNumber
                               ) -> Sequence[OrderItem]:
    """Return all order items for that article."""
    return OrderItem.query \
        .filter_by(article_number=article_number) \
        .all()


from collections import Counter
from typing import Dict, Sequence

from ....database import db

from ..article.models.article import ArticleNumber

from .models.order_item import OrderItem, OrderItemTuple
from .models.payment import PaymentState


def count_ordered_articles(article_number: ArticleNumber
                          ) -> Dict[PaymentState, int]:
    """Count how often the article has been ordered, grouped by the
    order's payment state.
    """
    order_items = OrderItem.query \
        .filter_by(article_number=article_number) \
        .options(
            db.joinedload('order'),
            db.joinedload('article'),
        ) \
        .all()

    # Ensure every payment state is present in the resulting dictionary,
    # even if no orders of the corresponding payment state exist for the
    # article.
    counter = Counter({state: 0 for state in PaymentState})

    for order_item in order_items:
        counter[order_item.order.payment_state] += order_item.quantity

    return dict(counter)


def get_order_items_for_article(article_number: ArticleNumber
                               ) -> Sequence[OrderItemTuple]:
    """Return all order items for that article."""
    order_items = OrderItem.query \
        .filter_by(article_number=article_number) \
        .all()

    return [item.to_tuple() for item in order_items]

Return `OrderItemTuple`s rather than order item database entities from service
Return `OrderItemTuple`s rather than order item database entities from service

  
  from collections import Counter
  from typing import Dict, Sequence
  
  from ....database import db
  
  from ..article.models.article import ArticleNumber
  
- from .models.order_item import OrderItem
+ from .models.order_item import OrderItem, OrderItemTuple
  from .models.payment import PaymentState
  
  
  def count_ordered_articles(article_number: ArticleNumber
                            ) -> Dict[PaymentState, int]:
      """Count how often the article has been ordered, grouped by the
      order's payment state.
      """
      order_items = OrderItem.query \
          .filter_by(article_number=article_number) \
          .options(
              db.joinedload('order'),
              db.joinedload('article'),
          ) \
          .all()
  
      # Ensure every payment state is present in the resulting dictionary,
      # even if no orders of the corresponding payment state exist for the
      # article.
      counter = Counter({state: 0 for state in PaymentState})
  
      for order_item in order_items:
          counter[order_item.order.payment_state] += order_item.quantity
  
      return dict(counter)
  
  
  def get_order_items_for_article(article_number: ArticleNumber
-                                ) -> Sequence[OrderItem]:
+                                ) -> Sequence[OrderItemTuple]:
      """Return all order items for that article."""
-     return OrderItem.query \
+     order_items = OrderItem.query \
          .filter_by(article_number=article_number) \
          .all()
  
+     return [item.to_tuple() for item in order_items]
+ 
Return `OrderItemTuple`s rather than order item database entities from service
## Code Before:

from collections import Counter
from typing import Dict, Sequence

from ....database import db

from ..article.models.article import ArticleNumber

from .models.order_item import OrderItem
from .models.payment import PaymentState


def count_ordered_articles(article_number: ArticleNumber
                          ) -> Dict[PaymentState, int]:
    """Count how often the article has been ordered, grouped by the
    order's payment state.
    """
    order_items = OrderItem.query \
        .filter_by(article_number=article_number) \
        .options(
            db.joinedload('order'),
            db.joinedload('article'),
        ) \
        .all()

    # Ensure every payment state is present in the resulting dictionary,
    # even if no orders of the corresponding payment state exist for the
    # article.
    counter = Counter({state: 0 for state in PaymentState})

    for order_item in order_items:
        counter[order_item.order.payment_state] += order_item.quantity

    return dict(counter)


def get_order_items_for_article(article_number: ArticleNumber
                               ) -> Sequence[OrderItem]:
    """Return all order items for that article."""
    return OrderItem.query \
        .filter_by(article_number=article_number) \
        .all()

## Instruction:
Return `OrderItemTuple`s rather than order item database entities from service
## Code After:

from collections import Counter
from typing import Dict, Sequence

from ....database import db

from ..article.models.article import ArticleNumber

from .models.order_item import OrderItem, OrderItemTuple
from .models.payment import PaymentState


def count_ordered_articles(article_number: ArticleNumber
                          ) -> Dict[PaymentState, int]:
    """Count how often the article has been ordered, grouped by the
    order's payment state.
    """
    order_items = OrderItem.query \
        .filter_by(article_number=article_number) \
        .options(
            db.joinedload('order'),
            db.joinedload('article'),
        ) \
        .all()

    # Ensure every payment state is present in the resulting dictionary,
    # even if no orders of the corresponding payment state exist for the
    # article.
    counter = Counter({state: 0 for state in PaymentState})

    for order_item in order_items:
        counter[order_item.order.payment_state] += order_item.quantity

    return dict(counter)


def get_order_items_for_article(article_number: ArticleNumber
                               ) -> Sequence[OrderItemTuple]:
    """Return all order items for that article."""
    order_items = OrderItem.query \
        .filter_by(article_number=article_number) \
        .all()

    return [item.to_tuple() for item in order_items]
