summaryrefslogtreecommitdiff
path: root/app/models/cart.rb
blob: b08fc13c734b6a5214ce266fccd3ca4546732464 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# frozen_string_literal: true

# Cart
class Cart < ApplicationRecord
  has_one :user_account, dependent: :destroy
  has_many :product_carts
  has_many :products, through: :product_carts

  def delete_product(product_id)
    relation = product_carts.find_by(product_id:, cart_id:)

    return false if relation.nil?

    relation.destroy and return true
  end

  def add_product(product_id, quantity)
    product_carts.create(product_id:, quantity:)
  end
end