<commit_before>(function() {
  'use strict';

  // Products controller
  angular
    .module('products')
    .controller('ProductsController', ProductsController);

  ProductsController.$inject = ['$scope', '$state', '$window', 'Authentication', 'productResolve', 'Upload', 'Notification'];

  function ProductsController($scope, $state, $window, Authentication, product, Upload, Notification) {
    var vm = this;

    vm.authentication = Authentication;
    vm.product = product;
    vm.error = null;
    vm.form = {};
    vm.fileSelected = false;
    vm.remove = remove;
    vm.save = save;
    vm.edit = edit;

    // TODO Remove auto fill
    if (vm.product === undefined || vm.product === null || vm.product.name === undefined || vm.product.name === '') {
      vm.product.name = 'Test';
      vm.product.startingBid = 30;
      vm.product.bidIncrement = 1;
      vm.product.quantity = 1;
      vm.product.category = 'category';
      vm.product.location = 'Dallas';
      vm.product.endsAt = new Date();
    }

    vm.makeBid = makeBid;
    if (vm.product.currentBid === undefined || vm.product.currentBid === null)
      vm.minBid = vm.product.startingBid;
    else
      vm.minBid = vm.product.currentBid.amount + vm.product.bidIncrement;
    vm.newBid = vm.minBid;

    vm.product.endsAt = new Date(vm.product.endsAt);
    vm.isActive = vm.product.endsAt > new Date() && !vm.product.canceled && !vm.product.sold;
    vm.isAdmin = false;
    var roles = vm.authentication.user.roles;
    for (var i = 0; i < roles.length; i++) {
      if (roles[i] === 'admin')
        vm.isAdmin = true;
    }

    // Remove existing Product
    function remove() {
      if ($window.confirm('Are you sure you want to delete?')) {
        vm.product.$remove($state.go('products.list'));
      }
    }

    function edit() {
      $state.go('products.edit', {
        productId: vm.product._id
      });
    }

    function makeBid() {
      vm.product.$bid({
        amount: vm.newBid
      }, successCallback, errorCallback);
    }

    function makePayment() {
      vm.product.paid = true;
      vm.product.$update(successCallback, errorCallback);
    }

    // Save Product
    function save(isValid) {
      if (!isValid) {
        $scope.$broadcast('show-errors-check-validity', 'vm.form.productForm');
        return false;
      }

      console.log('IMAGE URL');
      console.log(vm.product.imageURL);

      console.log(vm.product);

      // TODO: move create/update logic to service
      if (vm.product._id) {
        vm.product.$update(successCallback, errorCallback);
      } else {
        vm.product.$save(successCallback, errorCallback);
      }
    }

    function successCallback(res) {
      $state.go('products.view', {
        productId: res._id
      });
    }

    function errorCallback(res) {<commit_msg>Added Notification<commit_after>(function() {
  'use strict';

  // Products controller
  angular
    .module('products')
    .controller('ProductsController', ProductsController);

  ProductsController.$inject = ['$scope', '$state', '$window', 'Authentication', 'productResolve', 'Upload', 'Notification'];

  function ProductsController($scope, $state, $window, Authentication, product, Upload, Notification) {
    var vm = this;

    vm.authentication = Authentication;
    vm.product = product;
    vm.error = null;
    vm.form = {};
    vm.fileSelected = false;
    vm.remove = remove;
    vm.save = save;
    vm.edit = edit;

    // TODO Remove auto fill
    if (vm.product === undefined || vm.product === null || vm.product.name === undefined || vm.product.name === '') {
      vm.product.name = 'Test';
      vm.product.startingBid = 30;
      vm.product.bidIncrement = 1;
      vm.product.quantity = 1;
      vm.product.category = 'category';
      vm.product.location = 'Dallas';
      vm.product.endsAt = new Date();
    }

    vm.makeBid = makeBid;
    if (vm.product.currentBid === undefined || vm.product.currentBid === null)
      vm.minBid = vm.product.startingBid;
    else
      vm.minBid = vm.product.currentBid.amount + vm.product.bidIncrement;
    vm.newBid = vm.minBid;

    vm.product.endsAt = new Date(vm.product.endsAt);
    vm.isActive = vm.product.endsAt > new Date() && !vm.product.canceled && !vm.product.sold;
    vm.isAdmin = false;
    var roles = vm.authentication.user.roles;
    for (var i = 0; i < roles.length; i++) {
      if (roles[i] === 'admin')
        vm.isAdmin = true;
    }

    // Remove existing Product
    function remove() {
      if ($window.confirm('Are you sure you want to delete?')) {
        vm.product.$remove($state.go('products.list'));
      }
    }

    function edit() {
      $state.go('products.edit', {
        productId: vm.product._id
      });
    }

    function makeBid() {
      vm.product.$bid({
        amount: vm.newBid
      }, successCallback, errorCallback);
    }

    function makePayment() {
      vm.product.paid = true;
      vm.product.$update(successCallback, errorCallback);
    }

    // Save Product
    function save(isValid) {
      if (!isValid) {
        $scope.$broadcast('show-errors-check-validity', 'vm.form.productForm');
        return false;
      }

      console.log('IMAGE URL');
      console.log(vm.product.imageURL);

      console.log(vm.product);

      // TODO: move create/update logic to service
      if (vm.product._id) {
        vm.product.$update(successCallback, errorCallback);
      } else {
        vm.product.$save(successCallback, errorCallback);
      }
    }

    function successCallback(res) {
      Notification.success('Wohoo! Bid placed successfully!');
      $state.go('products.view', {
        productId: res._id
      });
    }

    function errorCallback(res) {
      Notification.error('Oops! Failed to bid!');
{'fraction_non_alphanumeric': 0.14944521497919555, 'fraction_numerical': 0.0017337031900138697, 'mean_word_length': 2.5523399014778323, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1115484', 'n_tokens_mistral': 1802, 'n_tokens_neox': 1749, 'n_words': 363}