File size: 1,260 Bytes
cce1e0e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from sqlalchemy.orm import Session
from typing import Dict, Any, Optional
from app.models.item import Item
from app.schemas.item import ItemCreate

def update_item(db: Session, item_id: int, item: ItemCreate) -> Optional[Item]:
    update_data = item.dict(exclude_unset=True)
    result = db.query(Item).filter(Item.id == item_id).update(update_data)

    
    if result == 0:
      return None
    


    db.commit()
    updated_item = db.query(Item).filter(Item.id == item_id).first()
    db.refresh(updated_item)
    return updated_item





    """
    Update an existing item in the database.
    
    Args:
        db (Session): Database session
        item_id (int): ID of the item to update
        item (ItemCreate): New item data

    Returns:
        Optional[Item]: The updated item or None if not found
    """

    
    # TODO: Implement the update_item function
    # The function should:
    # 1. Get the item with the given item_id from the database
    # 2. If the item doesn't exist, return None
    # 3. Update the item's attributes with the new values
    # 4. Commit the changes to the database
    # 5. Refresh the item from the database
    # 6. Return the updated item
    
    # Delete the code below and implement your solution