iT邦幫忙

2026 iThome 鐵人賽

DAY 16
0

前情提要

前一回,根據入庫需求,實作了 POST /api/inventory/scan
接著來做出庫需要的 API

Design API

POST /api/inventory/remove

Request

{
  "barcode": "471xxxxxxx"
}

Backend

barcode
   ↓
Product Lookup
   ↓
find earliest-expiring InventoryItem
   ↓
create REMOVE event
   ↓
update InventoryItem

Response

{
  "status": "ok",
  "product": {
    "name": "光泉鮮乳"
  },
  "inventory_item": {
    "id": 101,
    "expiration_date": "2026-10-01"
  },
  "event": "REMOVE"
}

API 實作

def remove_by_barcode(barcode: str):
    product = product_repository.find_by_barcode(barcode)

    if product is None:
        raise ProductNotFoundError()

    item = inventory_repository.find_next_for_removal(
        product_id=product["id"]
    )

    if item is None:
        raise InventoryNotFoundError()

    inventory_event_repository.create(
        inventory_item_id=item["id"],
        event_type="REMOVE"
    )

    inventory_repository.update_status(
        item["id"],
        "REMOVED"
    )

    return item

上一篇
【Day 15】從 Barcode 到庫存:讓掃描結果真正進入智慧冰箱
下一篇
【Day 17】保存期限與即期提醒規則
系列文
窮鬼冰箱升級計劃 共 17 篇
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言