<commit_before>angular.module('kassa').service('BasketService', [
  '$http'
  '$location'
  'UserService'
  'ProductService'
  ($http, $location, User, Product)->
    products = []
    buyer = null

    changeAmount = (entry, amount)->
      if entry.amount + amount < 1
        entry.amount = 1
      else if entry.amount + amount > entry.product.stock
        entry.amount = entry.product.stock
      else
        entry.amount += amount

    remove = (entry)->
      products.splice(products.indexOf(entry), 1)

    empty = ->
      searchObj = $location.search()
      delete searchObj.buyer
      delete searchObj[entry.product.name] for entry in products

      products.splice(0, products.length)
      $location.search(searchObj).replace()

    entryAmountReducer = (sum, entry)-> sum+entry.amount
    productCount = -> products.reduce entryAmountReducer, 0

    entryPrice = (entry)-> (entry.amount * entry.product.price)
    entryPriceReducer = (sum, entry)-> sum + entryPrice(entry)

    price = (entry)->
      if entry?
        entryPrice(entry)
      else
        products.reduce(entryPriceReducer, 0.0)

    isBuyable = -> products.length > 0 && exports.buyer

    entryByProductName = (name)->
      return entry for entry in products when entry.product.name == name
      null

    delayedAddProduct = (name, amount)->
      entry = {product: {name: name, id: name}, amount}
      products.push entry
      (product)-> entry.product = product

    resolveProducts = ->
      for own k, v of $location.search()
        v = parseInt(v)
        continue if isNaN(v)
        entry = entryByProductName(k)
        if entry?
          entry.amount = v
        else
          Product.find(k).then delayedAddProduct(k, v)
      products

    resolveBuyer = ->
      username = $location.search().buyer
      if buyer?.username == username
        buyer
      else if username?
        #always return the current promise if resolving to prevent multiple requests being run for the same resource
        return buyer if buyer?.then?
        buyer = User.find(username).then (user)-> buyer = user
    #return api-object with methods/objects accessible from outside
    exports = {
      changeAmount: changeAmount
      remove: remove
      empty: empty
      productCount: productCount
      price: price
      isBuyable: isBuyable
      products: resolveProducts
      buyer: resolveBuyer
    }
])<commit_msg>Fix basket removal/addition/change logic<commit_after>angular.module('kassa').service('BasketService', [
  '$http'
  '$location'
  'UserService'
  'ProductService'
  ($http, $location, User, Product)->
    products = []
    buyer = null

    changeAmount = (entry, amount)->
      name = entry.product.name
      currentAmount = parseInt($location.search()[name])
      unless currentAmount + amount < 1
        newAmount = currentAmount + amount
        $location.search(name, newAmount).replace()

    empty = ->
      searchObj = $location.search()
      delete searchObj.buyer
      delete searchObj[entry.product.name] for entry in products

      products.splice(0, products.length)
      $location.search(searchObj).replace()

    entryAmountReducer = (sum, entry)-> sum+entry.amount
    productCount = -> products.reduce entryAmountReducer, 0

    entryPrice = (entry)-> (entry.amount * entry.product.price)
    entryPriceReducer = (sum, entry)-> sum + entryPrice(entry)

    price = (entry)->
      if entry?
        entryPrice(entry)
      else
        products.reduce(entryPriceReducer, 0.0)

    isBuyable = -> products.length > 0 && exports.buyer

    entryByProductName = (entries, name)->
      return entry for entry in entries when entry.product.name == name
      null

    delayedAddProduct = (name, amount)->
      entry = {product: {name: name, id: name}, amount}
      products.push entry
      (product)-> entry.product = product

    resolveProducts = ->
      oldProducts = products
      products = []
      for own k, v of $location.search()
        v = parseInt(v)
        continue if isNaN(v)

        #update amount or add a new product if non-existent
        entry = entryByProductName(oldProducts, k)
        if entry?
          entry.amount = v
          products.push entry
        else
          Product.find(k).then delayedAddProduct(k, v)

      products

    resolveBuyer = ->
      username = $location.search().buyer
      if buyer?.username == username
        buyer
      else if username?
        #always return the current promise if resolving to prevent multiple requests being run for the same resource
        return buyer if buyer?.then?
        buyer = User.find(username).then (user)-> buyer = user

    setFromBuy = (buy)->
      empty()
      search = $location.search()
      search.buyer = buy.buyer.username
      for entry in buy.consistsOfProducts
        search[entry.product.name] = entry.amount
      $location.search(search).replace()

    #return api-object with methods/objects accessible from outside
    exports = {
      changeAmount: changeAmount
      empty: empty
      productCount: productCount
      price: price
      isBuyable: isBuyable
      products: resolveProducts
      buyer: resolveBuyer
      setFromBuy: setFromBuy
    }
])
{'fraction_non_alphanumeric': 0.10375191424196019, 'fraction_numerical': 0.002679938744257274, 'mean_word_length': 2.5043594902749833, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1081857', 'n_tokens_mistral': 1480, 'n_tokens_neox': 1412, 'n_words': 402}