local item = require'./models/item'

local lineitem = {}

lineitem.setdb = function(db)
  lineitem.db = db
end

lineitem.new = function(t)
  local li = {}
  li.type = 'lineitem'
  li.cart_id = t.cart.id
  li.item_id = t.item.id
  li.quantity = t.quantity or 1
  local item_record = t.item
  local cart_record = t.cart
  local user_record = lineitem.db:getrecord(cart_record.user_id)
  li.price = item.priceof(item_record, user_record)
  lineitem.db:insertrecord(li)
  table.insert(cart_record.lineitem_ids, li.id)
  cart_record:save()
  return li
end

return lineitem