From 8a01a12f418f0f85820960b4f34e6bff4efd576c Mon Sep 17 00:00:00 2001 From: AndyChen Date: Fri, 14 Apr 2017 21:26:50 +0800 Subject: [PATCH] refactor imdbTask code --- src/task/imdbTask.ts | 13 ++++--------- src/test/imdbTask.test.ts | 2 +- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/src/task/imdbTask.ts b/src/task/imdbTask.ts index 22222d3..4ed0c87 100644 --- a/src/task/imdbTask.ts +++ b/src/task/imdbTask.ts @@ -5,7 +5,6 @@ import Movie from "../models/movie"; export async function updateImdbInfo() { let movieInfos = await getNewImdbInfos(); - removeEmptyInfos(movieInfos); logResult(movieInfos); return updateYahooMovies(movieInfos); } @@ -33,13 +32,6 @@ function filterNeedCrawlMovie({ englishTitle, releaseDate, imdbLastCrawlTime }: return shouldCrawl; } -function removeEmptyInfos(movieInfos: Movie[]){ - movieInfos.forEach(info=>{ - !info.imdbID && delete info.imdbID; - !info.imdbRating && delete info.imdbRating; - }) -} - function logResult(movieInfos: Movie[]) { const foundMovies = movieInfos.filter(movie => movie.imdbID); const notfoundMovieIds = movieInfos.filter(movie => !movie.imdbID).map(movie => movie.yahooId); @@ -48,5 +40,8 @@ function logResult(movieInfos: Movie[]) { } function updateYahooMovies(movieInfos: Movie[]) { - return movieInfos.map(imdbInfo => db.updateDocument({ yahooId: imdbInfo.yahooId }, imdbInfo, "yahooMovies")) + return movieInfos.map(({ yahooId, imdbID, imdbRating, imdbLastCrawlTime }) => { + const newInfo = imdbID ? { imdbID, imdbRating, imdbLastCrawlTime } : { imdbLastCrawlTime } + return db.updateDocument({ yahooId }, newInfo, "yahooMovies") + }) } \ No newline at end of file diff --git a/src/test/imdbTask.test.ts b/src/test/imdbTask.test.ts index a6c2787..aeea90f 100644 --- a/src/test/imdbTask.test.ts +++ b/src/test/imdbTask.test.ts @@ -44,7 +44,7 @@ describe('imdbTask', () => { await updateImdbInfo(); sandbox.assert.calledWith(stubUpdateDocument, { yahooId: yahooMovie.yahooId }, - { yahooId: yahooMovie.yahooId, imdbLastCrawlTime: moment().format('YYYY-MM-DDTHH') }); + { imdbLastCrawlTime: moment().format('YYYY-MM-DDTHH') }); }); }); }); \ No newline at end of file