Skip to content

Commit

Permalink
fix(helpers/movies-helper): read file from disk instead of require to…
Browse files Browse the repository at this point in the history
… have latest information

fix #75
  • Loading branch information
maximodleon committed Sep 15, 2018
1 parent c891d81 commit 9fe9887
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion helpers/movies-helper.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const movies = require('../assets/movies.json')
const fs = require('fs')
const memoize = require('memoizee')

/* maxAge = one week (3 hours)
Expand All @@ -14,6 +14,7 @@ const maxAge = 3600 * 1000 * 3
* @return {String []} string array with all movie titles from database
*/
const getMovies = () => {
const movies = getMoviesFromDisk()
return movies.map(movie => movie.title)
}

Expand All @@ -24,6 +25,7 @@ const getMovies = () => {
* @return {Object} object containing details for the movies, if found.
*/
const getMovieDetails = title => {
const movies = getMoviesFromDisk()
return movies.filter(movie => movie.title === title).map(movie => {
return {
trailer: movie.trailer,
Expand All @@ -42,6 +44,7 @@ const getMovieDetails = title => {
* @return {String []} String array containing the name of the theaters where the movie is showing
*/
const getTheaterForMovie = title => {
const movies = getMoviesFromDisk()
const movie = movies.filter(movie => movie.title === title)[0]
return Object.keys(movie.showings)
}
Expand All @@ -54,10 +57,22 @@ const getTheaterForMovie = title => {
* @return {Object []} Array with the hours and day the movie is showing in the theater
*/
const getShowingForTheater = (movieTitle, theaterName) => {
const movies = getMoviesFromDisk()
const movie = movies.filter(movie => movie.title === movieTitle)[0]
return movie.showings[theaterName]
}

const getMoviesFromDisk = () => {
let movies
try {
movies = fs.readFileSync('assets/movies.json')
} catch (error) {
console.log('error opening movies file', error)
return []
}
return JSON.parse(movies)
}

module.exports = {
getMovies: memoize(getMovies, { maxAge }),
getMovieDetails: memoize(getMovieDetails, { maxAge }),
Expand Down

0 comments on commit 9fe9887

Please sign in to comment.