@@ -10,82 +10,27 @@ import { MovieService } from '../services/movieService'
1010@Injectable ( )
1111export class UserService {
1212
13- users : Object
1413 activeUser : UserModel
1514 headers
1615
1716 constructor ( public libraryService : LibraryService , public movieService : MovieService , public http : Http ) {
1817 this . headers = new Headers ( )
1918 this . headers . append ( "Content-Type" , "application/json" )
20- /*this.users = {}
21- this.activeUser = null
22- const userData = [
23- { "_id": 123, "firstname": "Alice", "lastname": "Wonderland", "username": "alice", "password": "alice",
24- libraries: [{
25- id: 1, name: "sample1", movies: [{ id: 1, imdbId: "tt0120338", title: "Titanic" }],
26- comments: [], ratings: [], user: { username: "alice", id: 123 }
27- },
28- {
29- id: 2, name: "sample2", movies: [
30- { id: 2, imdbId: "tt0096895", title: "Batman" },
31- { id: 3, imdbId: "tt1431045", title: "Deadpool" }],
32- comments: [], ratings: [], user: { username: "alice", id: 123 }
33- }] },
34- { "_id": 234, "firstname": "Bob", "lastname": "Hope", "username": "bob", "password": "bob",
35- libraries: [{
36- id: 3, name: "sample3", movies: [
37- { id: 1, imdbId: "tt0120338", title: "Titanic" },
38- { id: 3, imdbId: "tt1431045", title: "Deadpool" }],
39- comments: [], ratings: [], user: { username: "bob", id: 234 }
40- }]
41- },
42- { "_id": 345, "firstname": "Charlie", "lastname": "Brown", "username": "charlie", "password": "charlie" },
43- { "_id": 456, "firstname": "Dan", "lastname": "Craig", "username": "dan", "password": "dan" },
44- { "_id": 567, "firstname": "Edward", "lastname": "Norton", "username": "ed", "password": "ed"}
45- ]
46- // Create users based on userData
47- _.forEach(userData, (data) => {
48- let user: UserModel = UserModel.newUser(data)
49- this.users[user.id] = user
50- })*/
5119 }
5220
5321 findUserByCredentials ( username : string , password : string ) { //, callback: Function) {
5422 return this . http . get ( `/api/project/user?username=${ username } &password=${ password } ` ,
5523 { headers : this . headers } )
56- /*let user: UserModel
57- for (user of <Array<UserModel>> _.values(this.users)) {
58- if (user.username === username && user.password === password) {
59- callback(user)
60- return
61- }
62- }
63- callback(null)*/
6424 }
6525
6626 findAllUsers ( callback ) {
6727 return this . http . get ( '/api/project/user' ,
6828 { headers : this . headers } )
69- //callback(_.values(this.users))
7029 }
7130
7231 createUser ( user ) {
7332 return this . http . post ( '/api/project/user' , JSON . stringify ( { user } ) ,
7433 { headers : this . headers } )
75- /*user.id = (new Date).getTime()
76- // ensure unique username
77- if (user.username && !this.duplicateUsername(user.username)) {
78- this.users[user.id] = user
79- callback(user)
80- } else {
81- callback(null)
82- }*/
83- }
84-
85- duplicateUsername ( name ) {
86- return _ . some ( < Array < UserModel > > _ . values ( this . users ) , user => {
87- return user . username === name
88- } )
8934 }
9035
9136 login ( user : UserModel ) {
@@ -100,25 +45,10 @@ export class UserService {
10045 getUserById ( id : number ) {
10146 return this . http . get ( `/api/project/user/${ id } ` ,
10247 { headers : this . headers } )
103- //this.users[id]
10448 }
10549
10650 removeLibrary ( libId , userId ) {
10751 return this . http . delete ( `/api/project/user/${ userId } /library/${ libId } ` , { headers : this . headers } )
10852 }
10953
110- addMovie ( movie : MovieModel ) {
111- if ( this . activeUser . libraries . length > 0 ) {
112- this . activeUser . libraries [ 0 ] . movies . push ( movie )
113- this . addLibrary ( this . activeUser . libraries [ 0 ] )
114- }
115- }
116-
117- addLibrary ( library : LibraryModel ) {
118- if ( this . activeUser ) {
119- this . users [ this . activeUser . _id ] . libraries . push ( library )
120- this . libraryService . addLibrary ( library )
121- }
122- }
123-
12454}
0 commit comments