<commit_before>'use strict';

angular.module('groceryLister.groceryList', ['ngRoute'])

.config(['$routeProvider', function($routeProvider) {
  $routeProvider.when('/groceryList', {
    templateUrl: 'groceryList/groceryList.html',
    controller: 'GroceryListCtrl'
  })
}])

.filter('unassignedProducts', function() {
  return function (products) {
    var availableProducts = []

    angular.forEach(products, function(product) {

    //  if (assignedProducts.indexOf(product) == -1) {
        availableProducts.push(product)
    //  }

    })

    return availableProducts
  }
})

.controller('GroceryListCtrl',  ['$scope', '$http', function($scope, $http) {
  $scope.assignProduct = function(product) {

    var response = $http({
      method: 'GET',
      url: $scope.groceryList._links.products.href,
      headers: {
        'Content-Type': 'text/uri-list'
      },
      data: $scope.assignedProducts
    }).then(
        function(data, status, headers, config) {
          console.log("POST successful adding product ", product.name, " to grocery list ", $scope.groceryList.name)
          loadGroceryList($http, $scope)
        },
        function(data, status, headers, config) {
          console.log("POST adding product ", product.name, " to grocery list ", $scope.groceryList.name, data)
        }
    )

  }

  loadGroceryList($http, $scope)
  loadAvailableProducts($http, $scope)

}])

function loadGroceryList(http, scope) {
  http.get("http://localhost:9000/grocerylist?sort=lastModified&size=1")
      .then(
          function(data, status, headers, config) {<commit_msg>Adding product to lists now persist on back end and force-refreshes list<commit_after>'use strict';

angular.module('groceryLister.groceryList', ['ngRoute'])

.config(['$routeProvider', function($routeProvider) {
  $routeProvider.when('/groceryList', {
    templateUrl: 'groceryList/groceryList.html',
    controller: 'GroceryListCtrl'
  })
}])

.filter('unassignedProducts', function() {
  return function (products) {
    var availableProducts = []

    angular.forEach(products, function(product) {

    //  if (assignedProducts.indexOf(product) == -1) {
        availableProducts.push(product)
    //  }

    })

    return availableProducts
  }
})

.controller('GroceryListCtrl',  ['$scope', '$http', function($scope, $http) {
  $scope.assignProduct = function(product) {

    console.log("URL: ", $scope.groceryList._links.products.href)
    console.log("data: ", product._links.self.href)

    $http({
      method: 'POST',
      url: $scope.groceryList._links.products.href,
      headers: {
        'Content-Type': 'text/uri-list'
      },
      data: product._links.self.href
    }).then(
        function(data, status, headers, config) {
          console.log("POST successful adding product ", product.name, " to grocery list ", $scope.groceryList.name)
          var i = $scope.availableProducts._embedded.product.indexOf(product)
          $scope.availableProducts._embedded.product.splice(i, 1)
          loadGroceryList($http, $scope)
        },
        function(data, status, headers, config) {
          console.log("POST adding product ", product.name, " to grocery list ", $scope.groceryList.name, data)
        }
    )

  }

  loadGroceryList($http, $scope)
  loadAvailableProducts($http, $scope)

}])

function loadGroceryList(http, scope) {
  http.get("http://localhost:9000/grocerylist?sort=lastModified&size=1")
      .then(
          function(data, status, headers, config) {
{'fraction_non_alphanumeric': 0.1497280274835385, 'fraction_numerical': 0.0037217291726309764, 'mean_word_length': 3.3566084788029924, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6789926', 'n_tokens_mistral': 1115, 'n_tokens_neox': 1048, 'n_words': 192}