Skip to content

Commit bedeeb3

Browse files
author
Ian Redpath
committed
various bug fixes
1 parent 79d59df commit bedeeb3

File tree

7 files changed

+42
-114
lines changed

7 files changed

+42
-114
lines changed

public/project/components/libraries/library.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ export class Library {
6464
this.library = resp.json().library
6565
}
6666
})
67-
//_.remove(this.library.movies, mov => { return mov.imdbId === id })
6867
}
6968

7069
}

public/project/components/movies/movie.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,30 @@ export class Movie {
3030
this.fetchingMovie = true
3131
this.fetchingLibraries = true
3232
const imdbId: string = params.get('movie')
33-
movieService.get(imdbId)
33+
/*movieService.get(imdbId)
3434
.subscribe(
3535
resp => {
3636
if (resp.json().movie) {
3737
this.movie = resp.json().movie
38-
}
38+
}
3939
this.fetchingLibraries = false
40-
})
40+
})*/
4141
omdbService.findMovieById(imdbId)
4242
.subscribe(
43-
data => { this.omdbMovie = OmdbMovieModel.newMovie(data.json()) },
43+
data => {
44+
this.omdbMovie = OmdbMovieModel.newMovie(data.json())
45+
movieService.getOrCreate(imdbId, this.omdbMovie.title)
46+
.subscribe(resp => {
47+
if (resp.json().movie) {
48+
this.movie = resp.json().movie
49+
} else {
50+
console.log('error fetching movie')
51+
}
52+
this.fetchingLibraries = false
53+
})
54+
},
4455
err => { alert(err); this.omdbMovie = OmdbMovieModel.emptyMovie() },
45-
() => { this.fetchingMovie = false}
56+
() => { this.fetchingMovie = false }
4657
)
4758
}
4859
// eventually, this will be part of a new component and logic will be moved out of here
@@ -57,12 +68,6 @@ export class Movie {
5768
} else {
5869
alert("please select a library first")
5970
}
60-
/*
61-
if(this.userService.getActiveUser()) {
62-
const movieToAdd: MovieModel = this.movieService.transformFromOmdb(this.movie)
63-
this.userService.addMovie(movieToAdd)
64-
} else {
65-
alert('sign in to add a movie!')
66-
}*/
71+
6772
}
6873
}

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
</div>
77
<div class="col-sm-6 col-md-5">
88
<h1>{{user.username}}</h1>
9-
<div class="follow-links">
10-
<a href="">Following</a>
11-
<a href="">Followers</a>
12-
</div>
139
</div>
1410
<div class="col-sm-2 col-md-4"></div>
1511
</div>

public/project/services/movieService.ts

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,45 +11,20 @@ export class MovieService {
1111
constructor(public http: Http) {
1212
this.headers = new Headers()
1313
this.headers.append("Content-Type", "application/json")
14-
/*const sampleData = [
15-
{ id: 1, imdbId: "tt0120338", title: "Titanic" },
16-
{ id: 2, imdbId: "tt0096895", title: "Batman" },
17-
{ id: 3, imdbId: "tt1431045", title: "Deadpool" }
18-
]
19-
_.forEach(sampleData, mov => {
20-
let model: MovieModel = MovieModel.newMovie(mov)
21-
this.addMovie(model)
22-
})*/
2314
}
2415

2516
get(id: string) {
2617
return this.http.get(`/api/project/movie/${id}`,
2718
{ headers: this.headers })
28-
//return this.movies[id]
2919
}
3020
getAll() {
3121
return this.http.get('/api/project/movie',
3222
{ headers: this.headers })
33-
//return <Array<MovieModel>> _.values(this.movies)
3423
}
3524
addMovie(movie: MovieModel) {
3625
return this.http.post('/api/project/movie', JSON.stringify({ movie }),
3726
{ headers: this.headers })
38-
//this.movies[movie.imdbId] = movie
3927
}
40-
/*
41-
transformFromOmdb(movie: OmdbMovieModel) {
42-
// if it exists, return it
43-
const exists: MovieModel = _.find(this.getAll(), mov => { return mov.imdbId === movie.imdbId })
44-
if(exists) {
45-
return exists
46-
} else {
47-
// add it, then return
48-
const newMovie: MovieModel = MovieModel.newMovie(movie)
49-
this.addMovie(newMovie)
50-
return newMovie
51-
}
52-
}*/
5328

5429
getMoviesForUser(id: string) {
5530
return this.http.get(`/api/project/movie?user=${id}`,
@@ -61,4 +36,8 @@ export class MovieService {
6136
{ headers: this.headers })
6237
}
6338

39+
getOrCreate(imdbid: string, title: string) {
40+
return this.http.get(`/api/project/movie?id=${imdbid}&title=${title}`, { headers: this.headers })
41+
}
42+
6443
}

public/project/services/userService.ts

Lines changed: 0 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -10,82 +10,27 @@ import { MovieService } from '../services/movieService'
1010
@Injectable()
1111
export 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
}

server/project/data/database.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,17 @@ export default class Database {
180180
_.remove(movie.libraries, lib => { return (<any>lib).id.toString() === libraryId })
181181
return library
182182
}
183+
184+
getMovie(id: string, title: string) {
185+
let movie = _.find(this.movies, mov => { return mov.imdbId === id })
186+
if (movie) {
187+
return movie
188+
} else {
189+
movie = {}
190+
movie.libraries = []
191+
movie.imdbId = id
192+
movie.title = title
193+
return this.createMovie(movie)
194+
}
195+
}
183196
}

server/project/endpoints/movieEndpoints.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
export default function(app, db) {
22

33
app.get('/api/project/movie', (req, res) => {
4-
const movies = db.getAllMovies()
5-
res.status(200).send({ movies })
4+
const query = req.query
5+
if (query.id && query.title) {
6+
const movie = db.getMovie(query.id, query.title)
7+
res.status(200).send({ movie })
8+
} else {
9+
const movies = db.getAllMovies()
10+
res.status(200).send({ movies })
11+
}
612
})
713

814
app.get('/api/project/movie/:id', (req, res) => {

0 commit comments

Comments
 (0)