Skip to content
Permalink
621bd8b08f
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
558 lines (502 sloc) 20.6 KB
// The user controller will deal with
angular.module('routerApp').controller('userController', function ($q, $window, $scope, $state, $stateParams, $location, $http, $cookies, urlService, $rootScope, HolderService, $uibModal) {
/* Begin Function Definitions */
// Logging out
$scope.logout = function () {
$cookies.remove('userid');
$state.go('home');
}
// Customer Information Editing
// Retrieve Data
$scope.getUserInfo = function () {
var customerid = $cookies.get('userid');
console.log(customerid);
$scope.user = {};
$http.get("http://localhost:50031/api/GetCustomer?PersonID=" + customerid)
.success(function (response) {
$scope.user = angular.copy(response);
console.log(response);
})
}
// Update User Information
$scope.updateUser = function () {
var promise = $http({
method: "post",
url: "http://localhost:50031/api/UpdateCustomer",
headers: {
contentType: "application/json"
},
data: $scope.user
}).
success(function (data, status, headers, config) {
$scope.messages = 'You have successfully updated your information. ';
}).
error(function (error, status, headers, config) {
$scope.messages = 'There was a network error. Try again later.';
});
}
// Add Payment
$scope.addPayment = function (data) {
var pay = HolderService.getBlankPayment();
console.log(data);
pay.Type = data.type;
pay.is_default = 0;
pay.CustomerId = $cookies.get('userid');
if (data.token != undefined) pay.token = data.token;
if (data.email != undefined) pay.email = data.email;
if (data.num != undefined) pay.cardnum = data.num
if (data.date != undefined) pay.expr_date = data.date;
if (data.default != undefined) pay.is_default = data.default;
console.log("below is pay");
console.log(pay);
var promise = $http({
method: "post",
url: "http://localhost:50031/api/AddPayment",
headers: {
contentType: "application/json"
},
data: pay
}).
success(function (data, status, headers, config) {
$scope.messages = 'You have successfully updated your information. ';
}).
error(function (error, status, headers, config) {
$scope.messages = 'There was a network error. Try again later.';
});
console.log(pay);
}
// Dealing with Search
$scope.columns = [
{ field: 'TrackName', displayName: ' Track Name', width: "20%" },
{ field: 'artist', displayName: ' Artist Name', width: "20%" },
{ field: 'AlbumTitle', displayName: ' Album ', width: "15%" },
{ field: 'MediaType', displayName: ' Media ', width: "10%" },
{ field: 'Genre', displayName: ' Genre ', width: "10%" },
{ field: 'Clip', displayName: ' Clip ', width: "10%", cellTemplate: '<audio id="audio1"><source src="http://a1310.phobos.apple.com/us/r1000/167/Music3/v4/00/23/38/0023383d-1616-fbe2-b3a3-7f2de268911b/mzaf_7209253486009554987.plus.aac.p.m4a"></source>Your browser isn\'t invited for super fun audio time.</audio><center> <button data-ng-click="grid.appScope.play()" class="btn btn-normal btn-xs"> <i class="glyphicon glyphicon-play"></i> </button></center>' },
{ field: 'UnitPrice', displayName: ' Price ', width: "10%" }
];
$scope.orderColumns = [
{ field: 'TrackId', displayName: ' ', width: "30%", cellTemplate: '<center><button data-ng-click="grid.appScope.addToCart({{row.entity.TrackId}})" class="btn btn-success btn-xs"> + </button></center></td>' },
{ field: 'TrackName', displayName: ' Track Name', width: "20%" },
{ field: 'artist', displayName: ' Artist Name', width: "20%" },
{ field: 'AlbumTitle', displayName: ' Album ', width: "15%" },
{ field: 'MediaType', displayName: ' Media ', width: "10%" },
{ field: 'Genre', displayName: ' Genre ', width: "10%" },
{ field: 'Clip', displayName: ' Clip ', width: "10%" },
{ field: 'UnitPrice', displayName: ' Price ', width: "10%" }
];
$scope.gridOptions = {
data: null,
enableSorting: true,
columnDefs: $scope.columns,
onRegisterApi: function (gridApi) {
$scope.gridApi = gridApi;
var cellTemplate = 'ui-grid/selectionRowHeader';
}
};
$scope.orderOptions = {
data: null,
enableSorting: true,
columnDefs: $scope.orderColumns,
onRegisterApi: function (gridApi) {
$scope.gridApi = gridApi;
var cellTemplate = 'ui-grid/selectionRowHeader';
}
};
// Search Function
$scope.search = function (token, category) {
console.log(token);
console.log(category);
$http.get("http://localhost:50031/api/Search?search=" + token)
.success(function (response) {
$scope.data = angular.copy(response);
console.log(response);
if (category == "Track") {
$scope.gridOptions.data = $scope.data.Track;
$scope.orderOptions.data = $scope.data.Track;
}
else if (category == "Artist") {
$scope.gridOptions.data = $scope.data.Artist;
$scope.orderOptions.data = $scope.data.Artist;
}
else if (category == "Composer") {
$scope.gridOptions.data = $scope.data.Composer;
$scope.orderOptions.data = $scope.data.Composer;
}
else if (category == "Genre") {
$scope.gridOptions.data = $scope.data.Genre;
$scope.orderOptions.data = $scope.data.Genre;
}
else if (category == "Media") {
$scope.gridOptions.data = $scope.data.Media;
$scope.orderOptions.data = $scope.data.Media;
}
});
}
$scope.authenticateUser = function (id, pass) {
return $q(function (resolve, reject) {
var param = id + " " + pass;
var promise = $http.post(url + "/authenticateUser", param)
.success(function (data, status) {
console.log(data);
if (data.STATUS == 555) {
$scope.authenticated = false;
resolve('We did it');
} else {
$scope.authenticated = true;
resolve('We did it');
}
})
.error(function (data, status, headers, config) {
$scope.messages = 'There was a network error. Try again later.';
reject('We did not do it');
});
})
}
$scope.addToCart = function (trackid) {
console.log(trackid + "the track id");
var promise = $http({
method: "get",
url: "http://localhost:50031/api/AddTrackOrder?trackId=" + trackid + "&orderId=" + $scope.currOrderID,
}).
success(function (response) {
console.log(response);
});
promise.then(function () {
console.log($scope.currOrderID);
$http.get("http://localhost:50031/api/GetOrderTracks?orderId=" + $scope.currOrderID)
.success(function (response) {
$scope.currOrder = angular.copy(response);
console.log($scope.currOrder);
})
});
}
// functions for adding playlists to cart
$scope.addPLToCart = function (plid) {
console.log("cats");
console.log(plid);
var promise = $http.get("http://localhost:50031/api/GetPlaylist?PlaylistID=" + plid)
.success(function (response) {
$scope.currplaylist = angular.copy(response);
console.log($scope.currplaylist);
});
promise.then(function () {
console.log($scope.currplaylist);
var end = $scope.currplaylist.length;
for (var i = 0; i < end; i++) {
$scope.addToCart($scope.currplaylist[i].TrackId);
}
});
}
$scope.addMPLToCart = function (plid) {
console.log("cats");
console.log(plid);
var promise = $http.get("http://localhost:50031/api/GetPlaylist?PlaylistID=" + plid)
.success(function (response) {
$scope.currplaylist = angular.copy(response);
console.log($scope.currplaylist);
});
promise.then(function () {
console.log($scope.currplaylist);
var end = $scope.currplaylist.length;
for (var i = 0; i < end; i++) {
$scope.addToCart($scope.currplaylist[i].TrackId);
}
});
}
// Order Functions
// Get Playlists
$scope.getPlaylists = function () {
$http.get("http://localhost:50031/api/GetPlaylistList")
.success(function (response) {
$scope.playlists = angular.copy(response);
console.log($scope.playlists);
})
};
// Get Custom Playlists
$scope.getMyPlaylists = function () {
$http.get("http://localhost:50031/api/GetCustomPlaylists?CustomerID=" + $cookies.get('userid'))
.success(function (response) {
$scope.myPlaylists = angular.copy(response);
console.log($scope.myPlaylists);
})
};
// Get Playlist Tracks
$scope.getPlaylistTracks = function (id) {
$http.get("http://localhost:50031/api/GetPlaylist?PlaylistID=" + id)
.success(function (response) {
$scope.currplaylist = angular.copy(response);
console.log($scope.currplaylist);
})
};
//Play Audio Clip
$scope.play = function () {
var audio = document.getElementById("audio1");
audio.play();
};
// Get CustomPlaylist Tracks
$scope.getMyPlaylistTracks = function (id) {
console.log("Custom Playlist tracks");
$http.get("http://localhost:50031/api/GetCustomPlaylist?PlaylistID=" + id)
.success(function (response) {
$scope.currplaylist = angular.copy(response);
console.log($scope.currplaylist);
})
};
// Open window for playlist
$scope.open = function (playlist) {
$scope.getPlaylistTracks(playlist.PlaylistID);
$scope.currname = playlist.Name;
var modalInstance = $uibModal.open({
animation: $scope.animationsEnabled,
templateUrl: 'pages/user-modal.html',
scope: $scope,
controller: 'userController',
resolve: {
items: function () {
return $scope.items;
}
}
});
}
// Open window for custom playlist
$scope.openCustom = function (playlist) {
$scope.getMyPlaylistTracks(playlist.PlaylistID);
$scope.currname = playlist.Name;
var modalInstance = $uibModal.open({
animation: $scope.animationsEnabled,
templateUrl: 'pages/user-modal.html',
scope: $scope,
controller: 'userController',
resolve: {
items: function () {
return $scope.items;
}
}
});
}
// Open window for orders
$scope.openOrder = function (orderid) {
console.log(orderid);
$scope.catcats = orderid;
$scope.getOrderTracks(orderid.invoiceId);
var modalInstance = $uibModal.open({
animation: $scope.animationsEnabled,
templateUrl: 'pages/user-ordermodal.html',
scope: $scope,
controller: 'userController',
resolve: {
items: function () {
return $scope.items;
}
}
});
}
// Get order tracks for an order number
$scope.getOrderTracks = function (orderid) {
$http.get("http://localhost:50031/api/GetOrderTracks?orderId=" + orderid)
.success(function (response) {
console.log(response);
$scope.currordertracks = angular.copy(response);
console.log($scope.currordertracks);
})
};
// Get the current order for the customer
$scope.getMyOrder = function () {
var promise = $http({
method: "get",
url: "http://localhost:50031/api/GetCustomerOrders?CustomerId=" + $cookies.get('userid'),
}).
success(function (response) {
if (response.length == 0) {
$http.get("http://localhost:50031/api/CreateOrder?CustomerID=" + $cookies.get('userid'))
.success(function (response) {
$scope.currOrderID = angular.copy(response);
})
}
else {
$scope.currOrderID = angular.copy(response);
}
});
promise.then(function () {
console.log($scope.currOrderID);
$http.get("http://localhost:50031/api/GetOrderTracks?orderId=" + $scope.currOrderID)
.success(function (response) {
$scope.currOrder = angular.copy(response);
console.log($scope.currOrder);
})
});
};
// get Payment info for the current user
$scope.getPayments = function () {
$http.get("http://localhost:50031/api/GetPayment?CustomerID=" + $cookies.get('userid'))
.success(function (response) {
$scope.currpayments = angular.copy(response);
console.log($scope.currpayments);
})
};
// From shopping cart to invoice
$scope.buy = function (payid) {
console.log(payid);
$http.get("http://localhost:50031/api/GenerateInvoice?custId=" + $cookies.get('userid') + "&payId=" + payid + "&orderId=" + $scope.currOrderID)
.success(function (response) {
$http.get("http://localhost:50031/api/CreateOrder?CustomerID=" + $cookies.get('userid'))
.success(function (response) {
$state.go($state.current, {}, { reload: true });
$scope.messages = "Your order has been submitted.";
})
})
}
// Initialize Order Informations
$scope.initOrders = function () {
$scope.getPlaylists();
$scope.getMyPlaylists();
$scope.getMyOrder();
$scope.getPayments();
}
// Initialize User Portal Informations
$scope.initPortal = function () {
$scope.getMyOrders();
}
// Get Invoice for a specific user
$scope.getMyOrders = function () {
$http.get("http://localhost:50031/api/GetCustomerInvoice?custId=" + $cookies.get('userid'))
.success(function (response) {
$scope.currorders = angular.copy(response);
console.log($scope.currorders);
})
}
/* End Function Definitions */
/* Begin Logic */
//Playlist Editor
$scope.editing = false;
$scope.currentPlaylist = 0;
$scope.addingTrack = false;
$scope.playlistGridOptions = {
columnDefs: [
{ field: 'PlaylistID', displayName: 'Playlist Id' },
{ field: 'Name', displayName: 'Playlist Name' },
{ field: 'Edit', cellTemplate: '<center><button ng-click="grid.addScope.debug();">Edit</button></center>' },
{ field: 'Delete', cellTemplate: '<center><button ng-click="grid.addScope.deletePlaylist(row.entity.PlaylistID);">Delete</button></center>' }
]
}
$scope.addCustomPlaylist = function () {
var sub = HolderService.getBlankCustomPlaylist();
sub.CustomerId = $cookies.get('userid');
sub.Name = prompt("Enter the name of the playlist");
if (name != null) {
var promise = $http({
method: "post",
url: "http://localhost:50031/api/AddCustomPlaylist",
headers: {
contentType: "application/json"
},
data: sub
}).
success(function (data, status, headers, config) {
$scope.messages = 'You created a playlist!';
alert($scope.messages);
}).
error(function (error, status, headers, config) {
$scope.messages = 'There was a network error. Try again later.';
alert($scope.messages);
});
}
window.setTimeout($scope.getCustomPlaylists(), 1000);
}
$scope.toEdit = function (playlistID) {
$scope.editing = true;
$scope.currentPlaylist = playlistID;
$scope.getCustomPlaylistTracks(playlistID);
alert($scope.currentPlaylist + "");
}
$scope.getCustomPlaylists = function () {
$scope.playlists = [];
$http.get("http://localhost:50031/api/GetCustomPlaylists?CustomerID=" + $cookies.get('userid'))
.success(function (response) {
$scope.playlists = angular.copy(response);
$scope.playlistGridOptions.data = angular.copy(response);
console.log(response);
})
}
$scope.deletePlaylist = function (playlistID) {
if (confirm("Are you sure you want to delete this playlist? \nPress OK to confirm") == true) {
$scope.dropCustomPlaylist(playlistID);
}
window.setTimeout($scope.getCustomPlaylists(), 1000);
}
$scope.dropCustomPlaylist = function (playlistID) {
$http.get("http://localhost:50031/api/DropCustomPlaylist?PlaylistID=" + playlistID)
.success(function (response) {
console.log(response);
})
}
$scope.getCustomPlaylistTracks = function (playlistID) {
$scope.playlistTracks = [];
console.log(playlistID);
$http.get("http://localhost:50031/api/GetCustomPlaylist?PlaylistID=" + playlistID)
.success(function (response) {
$scope.playlistTracks = angular.copy(response);
console.log(response);
})
}
$scope.addToCustomPlaylist = function (trackID) {
console.log(trackID);
$http.get("http://localhost:50031/api/AddTrackCustomPlaylist?PlaylistID=" + $scope.currentPlaylist + "&TrackID=" + trackID)
.success(function (response) {
console.log(response);
})
window.setTimeout($scope.getCustomPlaylistTracks($scope.currentPlaylist), 1000);
}
$scope.deleteTrack = function (trackID) {
if (confirm("Are you sure you want to delete this track from this playlist? \nPress OK to confirm") == true) {
$scope.dropCustomTrack(trackID);
}
window.setTimeout($scope.getCustomPlaylistTracks($scope.currentPlaylist), 1000);
}
$scope.dropCustomTrack = function (trackID) {
$http.get("http://localhost:50031/api/RemoveCustomTrack?PlaylistID=" + $scope.currentPlaylist + "&TrackID=" + trackID)
.success(function (response) {
console.log(response);
})
}
$scope.trackSearchGridOptions = {
enableSorting: true,
columnDefs: [
{ field: 'TrackId', displayName: ' ', width: "5%", cellTemplate: '<center><button ng-click="grid.appScope.addToCustomPlaylist(row.entity.TrackId);" class="btn btn-success btn-xs"> + </button></center>' },
{ field: 'TrackName', displayName: ' Track Name', width: "20%" },
{ field: 'artist', displayName: ' Artist Name', width: "20%" },
{ field: 'AlbumTitle', displayName: ' Album ', width: "15%" },
{ field: 'MediaType', displayName: ' Media ', width: "10%" },
{ field: 'Genre', displayName: ' Genre ', width: "10%" },
{ field: 'Clip', displayName: ' Clip ', width: "10%" },
{ field: 'UnitPrice', displayName: ' Price ', width: "10%" }
]
};
$scope.trackSearch = function (token, category) {
console.log(token);
console.log(category);
console.log($scope.data);
$http.get("http://localhost:50031/api/Search?search=" + token)
.success(function (response) {
$scope.data = angular.copy(response);
console.log(response);
if (category == "Track") {
$scope.trackSearchGridOptions.data = $scope.data.Track;
}
else if (category == "Artist") {
$scope.trackSearchGridOptions.data = $scope.data.Artist;
}
else if (category == "Composer") {
$scope.trackSearchGridOptions.data = $scope.data.Composer;
}
else if (category == "Genre") {
$scope.trackSearchGridOptions.data = $scope.data.Genre;
}
else if (category == "Media") {
$scope.trackSearchGridOptions.data = $scope.data.Media;
}
});
}
//End Playlist Editor
});