Emmanuel Frimpong Asante
"Update system"
c514c85
raw
history blame
4.02 kB
# controllers/inventory.py
from flask import request, jsonify
from models.schemas.inventory_schema import create_inventory_item, update_inventory_item, get_inventory_item_by_id, \
get_all_inventory_items
from controllers.data_logging import log_inventory_addition, log_inventory_update
# Add a new inventory item
def add_inventory_item():
"""
Adds a new inventory item to the system.
Expects:
- user_id: The ID of the user adding the item.
- item_name: Name of the inventory item.
- category: Category of the item (e.g., "Feed", "Medicine").
- quantity: Total quantity of the item.
- unit: Unit of measurement (e.g., "kg", "liters").
- description: Optional. Additional notes or description about the item.
Returns:
- JSON response with the result of the addition.
"""
try:
# Extract data from the request
user_id = request.json.get('user_id')
item_name = request.json.get('item_name')
category = request.json.get('category')
quantity = request.json.get('quantity')
unit = request.json.get('unit')
description = request.json.get('description')
if not user_id or not item_name or not category or not quantity or not unit:
return jsonify({"error": "Missing required fields"}), 400
# Add the inventory item
item_id = create_inventory_item(item_name, category, quantity, unit, user_id, description)
# Log the inventory addition
log_inventory_addition(user_id, item_name, category, quantity, unit, description)
return jsonify({"message": "Inventory item added successfully", "item_id": item_id}), 201
except Exception as e:
return jsonify({"error": str(e)}), 500
# Update an existing inventory item
def update_inventory_item_by_id():
"""
Updates an existing inventory item.
Expects:
- user_id: The ID of the user updating the item.
- item_id: The ID of the inventory item to update.
- updated_fields: Dictionary containing fields to update (e.g., "quantity", "used_quantity").
Returns:
- JSON response with the result of the update.
"""
try:
# Extract data from the request
user_id = request.json.get('user_id')
item_id = request.json.get('item_id')
updated_fields = request.json.get('updated_fields')
if not user_id or not item_id or not updated_fields:
return jsonify({"error": "Missing required fields"}), 400
# Update the inventory item
update_success = update_inventory_item(item_id, updated_fields)
if update_success:
# Log the inventory update
log_inventory_update(user_id, item_id, updated_fields)
return jsonify({"message": "Inventory item updated successfully"}), 200
else:
return jsonify({"message": "Inventory item update failed"}), 400
except Exception as e:
return jsonify({"error": str(e)}), 500
# Get an inventory item by its ID
def get_inventory_by_id(item_id):
"""
Retrieves an inventory item by its item_id (MongoDB ObjectId).
Expects:
- item_id: ID of the inventory item.
Returns:
- JSON response with the inventory item details.
"""
try:
inventory_item = get_inventory_item_by_id(item_id)
if not inventory_item:
return jsonify({"message": "Inventory item not found"}), 404
return jsonify(inventory_item), 200
except Exception as e:
return jsonify({"error": str(e)}), 500
# Get all inventory items
def get_all_inventory():
"""
Retrieves all inventory items.
Returns:
- JSON response with the list of all inventory items.
"""
try:
inventory_items = get_all_inventory_items()
if not inventory_items:
return jsonify({"message": "No inventory items found"}), 404
return jsonify(inventory_items), 200
except Exception as e:
return jsonify({"error": str(e)}), 500