0
0
Microservicessystem_design~10 mins

Aggregates and entities in Microservices - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define an aggregate root in a microservice.

Microservices
class Order([1]):
    def __init__(self, order_id):
        self.order_id = order_id
        self.items = []
Drag options to blanks, or click blank then click option'
AAggregateRoot
BEntity
CService
DRepository
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Entity' instead of 'AggregateRoot' causes loss of aggregate control.
Using 'Service' or 'Repository' confuses domain concepts with infrastructure.
2fill in blank
medium

Complete the code to add an entity inside an aggregate.

Microservices
class OrderItem:
    def __init__(self, product_id, quantity):
        self.product_id = product_id
        self.quantity = [1]
Drag options to blanks, or click blank then click option'
Aprice
Border_id
Cquantity
Dstatus
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning 'price' or 'status' instead of 'quantity' causes incorrect data storage.
Using 'order_id' here is wrong because it's not part of the item itself.
3fill in blank
hard

Fix the error in the method that adds an entity to the aggregate.

Microservices
class Order:
    def __init__(self):
        self.items = []

    def add_item(self, item):
        if item.product_id not in [i.product_id for i in self.items]:
            self.items.[1](item)
Drag options to blanks, or click blank then click option'
Aextend
Bappend
Cinsert
Dremove
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'extend' causes an error because it expects an iterable.
Using 'remove' deletes items instead of adding.
4fill in blank
hard

Fill both blanks to correctly check entity existence and update quantity.

Microservices
def update_item_quantity(self, product_id, quantity):
    for item in self.items:
        if item.[1] == product_id:
            item.[2] = quantity
            return True
    return False
Drag options to blanks, or click blank then click option'
Aproduct_id
Bquantity
Cstatus
Dorder_id
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'status' or 'order_id' causes wrong attribute access.
Mixing up the attributes leads to logic errors.
5fill in blank
hard

Fill all three blanks to create a dictionary of product IDs and quantities from entities.

Microservices
def get_items_summary(self):
    return {item.[1]: item.[2] for item in self.items if item.[3] > 0}
Drag options to blanks, or click blank then click option'
Aproduct_id
Bquantity
Dstatus
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'status' instead of 'quantity' breaks the filter logic.
Mixing keys and values causes incorrect dictionary structure.