Skip to content

Commit 79d59df

Browse files
author
Ian Redpath
committed
remove movie from library functionality
1 parent ee59353 commit 79d59df

File tree

3 files changed

+13
-26
lines changed

3 files changed

+13
-26
lines changed

public/project/components/users/user.ts

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ export class User {
5050
.subscribe(resp => {
5151
if (resp.json().library) {
5252
const id = resp.json().library.id
53+
this.user.libraries.push(resp.json().library)
54+
this.userService.login(this.user)
5355
this.router.navigate(["/Library", { library: id }])
5456
} else {
5557
alert(`error creating library ${this.newLibraryName}`)
@@ -58,13 +60,7 @@ export class User {
5860
} else {
5961
alert("Make sure to name your library first!")
6062
}
61-
/*
62-
let newLib: LibraryModel = LibraryModel.defaultLibrary(this.user)
63-
newLib.id = new Date().getTime()
64-
newLib.name = this.getNextLibraryName()
65-
this.userService.addLibrary(newLib)
66-
this.router.navigate(["/Library", { library: newLib.id }])
67-
*/
63+
6864
}
6965

7066
removeLibrary(id: number) {
@@ -74,16 +70,6 @@ export class User {
7470
_.remove(this.user.libraries, lib => { return (<any>lib).id === id })
7571
}
7672
})
77-
/*
78-
_.remove(this.user.libraries, lib => { return lib.id === id })
79-
this.libraryService.removeLibrary(id)
80-
*/
81-
}
82-
getMovies() {
83-
/*this.movieService.getMoviesForUser(this.user.id)
84-
.subscribe(resp => {
8573

86-
})
87-
*///return _.uniq(_.flatten(this.user.libraries))
8874
}
8975
}

public/project/components/users/user.view.html

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,4 @@ <h3>Libraries</h3>
2626
</div>
2727
</div>
2828
</div>
29-
<div>
30-
<h3>Highest Rated Movies</h3>
31-
<div class="movies"></div>
32-
</div>
3329
</div>

server/project/data/database.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ export default class Database {
7979

8080
createLibrary(library) {
8181
library.id = this.randomId++
82+
library.movies = []
8283
this.libraries.push(library)
8384
const user = library.user
8485
console.log(user)
@@ -154,25 +155,29 @@ export default class Database {
154155
if (_.find(movie.libraries, lib => { return (<any>lib).id === libraryId })) {
155156
return movie
156157
}
158+
const library = _.find(this.libraries, lib => { return lib.id.toString() === libraryId })
159+
library.movies.push(movie)
157160
let retVal
158161
_.forEach(this.movies, mov => {
159162
if (mov.id === movie.id) {
160-
mov.libraries.push(libraryId)
163+
mov.libraries.push(library)
161164
retVal = mov
162165
}
163166
})
164167
return retVal
165168
}
166169

167170
deleteMovie(id: string) {
168-
const removed = _.find(this.movies, movie => { return movie.id === id })
169-
_.remove(this.movies, movie => { return movie.id === id })
171+
const removed = _.find(this.movies, movie => { return movie.imdbId === id })
172+
_.remove(this.movies, movie => { return movie.imdbId === id })
170173
return removed
171174
}
172175

173176
deleteMovieFromLibrary(movieId: string, libraryId: string) {
174-
let library = _.find(this.libraries, lib => { return lib.id === libraryId })
175-
_.remove(library.movies, mov => { return (<any>mov).id === movieId })
177+
let library = _.find(this.libraries, lib => { return lib.id.toString() === libraryId })
178+
_.remove(library.movies, mov => { return (<any>mov).imdbId === movieId })
179+
let movie = _.find(this.movies, mov => { return mov.imdbId === movieId })
180+
_.remove(movie.libraries, lib => { return (<any>lib).id.toString() === libraryId })
176181
return library
177182
}
178183
}

0 commit comments

Comments
 (0)