115 lines
3.2 KiB
Python
115 lines
3.2 KiB
Python
from typing import Dict, List, Optional
|
|
from datetime import datetime
|
|
|
|
# In-memory store for fruits since no model was provided
|
|
_fruits_store: List[Dict[str, any]] = []
|
|
|
|
def add_fruit(fruit_name: str, quantity: int) -> Dict[str, any]:
|
|
"""
|
|
Adds a fruit to the in-memory store with the given quantity.
|
|
|
|
Args:
|
|
fruit_name (str): Name of the fruit to add
|
|
quantity (int): Quantity of the fruit
|
|
|
|
Returns:
|
|
Dict[str, any]: The newly added fruit entry
|
|
"""
|
|
if not validate_fruit_data(fruit_name, quantity):
|
|
raise ValueError("Invalid fruit data provided")
|
|
|
|
new_fruit = {
|
|
"name": fruit_name.lower(),
|
|
"quantity": quantity,
|
|
"added_at": datetime.now()
|
|
}
|
|
|
|
# Check if fruit already exists
|
|
existing_fruit = get_fruit_by_name(fruit_name)
|
|
if existing_fruit:
|
|
existing_fruit["quantity"] += quantity
|
|
existing_fruit["updated_at"] = datetime.now()
|
|
return existing_fruit
|
|
|
|
_fruits_store.append(new_fruit)
|
|
return new_fruit
|
|
|
|
def get_fruit_by_name(fruit_name: str) -> Optional[Dict[str, any]]:
|
|
"""
|
|
Retrieves a fruit by its name from the in-memory store.
|
|
|
|
Args:
|
|
fruit_name (str): Name of the fruit to retrieve
|
|
|
|
Returns:
|
|
Optional[Dict[str, any]]: The fruit entry if found, None otherwise
|
|
"""
|
|
fruit_name = fruit_name.lower()
|
|
for fruit in _fruits_store:
|
|
if fruit["name"] == fruit_name:
|
|
return fruit
|
|
return None
|
|
|
|
def get_all_fruits() -> List[Dict[str, any]]:
|
|
"""
|
|
Retrieves all fruits from the in-memory store.
|
|
|
|
Returns:
|
|
List[Dict[str, any]]: List of all fruit entries
|
|
"""
|
|
return _fruits_store
|
|
|
|
def update_fruit_quantity(fruit_name: str, new_quantity: int) -> Optional[Dict[str, any]]:
|
|
"""
|
|
Updates the quantity of a specific fruit.
|
|
|
|
Args:
|
|
fruit_name (str): Name of the fruit to update
|
|
new_quantity (int): New quantity value
|
|
|
|
Returns:
|
|
Optional[Dict[str, any]]: Updated fruit entry if found, None otherwise
|
|
"""
|
|
if not validate_fruit_data(fruit_name, new_quantity):
|
|
raise ValueError("Invalid fruit data provided")
|
|
|
|
fruit = get_fruit_by_name(fruit_name)
|
|
if fruit:
|
|
fruit["quantity"] = new_quantity
|
|
fruit["updated_at"] = datetime.now()
|
|
return fruit
|
|
return None
|
|
|
|
def delete_fruit(fruit_name: str) -> bool:
|
|
"""
|
|
Deletes a fruit from the in-memory store.
|
|
|
|
Args:
|
|
fruit_name (str): Name of the fruit to delete
|
|
|
|
Returns:
|
|
bool: True if fruit was deleted, False if not found
|
|
"""
|
|
fruit_name = fruit_name.lower()
|
|
for i, fruit in enumerate(_fruits_store):
|
|
if fruit["name"] == fruit_name:
|
|
_fruits_store.pop(i)
|
|
return True
|
|
return False
|
|
|
|
def validate_fruit_data(fruit_name: str, quantity: int) -> bool:
|
|
"""
|
|
Validates fruit data before processing.
|
|
|
|
Args:
|
|
fruit_name (str): Name of the fruit to validate
|
|
quantity (int): Quantity to validate
|
|
|
|
Returns:
|
|
bool: True if data is valid, False otherwise
|
|
"""
|
|
if not fruit_name or not isinstance(fruit_name, str) or len(fruit_name.strip()) == 0:
|
|
return False
|
|
if not isinstance(quantity, int) or quantity < 0:
|
|
return False
|
|
return True |