Skip to content

Commit

Permalink
stubs service used by default
Browse files Browse the repository at this point in the history
  • Loading branch information
Yegor Sytnyk committed Oct 20, 2016
1 parent e973ec7 commit 32f8378
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/components/App.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, {Component} from 'react';
import noMovie from '../media/no-movie.png';
import '../styles/App.css';
import moviesService from '../movieService';
import moviesService from '../services/movieServiceStubs';
import {Button, Glyphicon, Pagination, ButtonToolbar, DropdownButton, MenuItem} from 'react-bootstrap';
import toastr from 'toastr';
import Confirm from './common/Confirm';
Expand Down
File renamed without changes.
117 changes: 117 additions & 0 deletions src/services/movieServiceStubs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import moviesData from '../../db.json';

export default {
getMovies,
deleteMovie,
saveMovie,
getGenres
};

const pageSize = 10;

function getMovies(page, sortBy, searchStr) {
let movies = searchMovies(moviesData.movies, searchStr);

sortMovies(movies, sortBy);

let result = getPage(movies, page, pageSize);

return Promise.resolve({
total: movies.length,
dataItems: result
});
}

function sortMovies(movies, sortBy) {
if (sortBy === 'title') {
movies.sort((x, y) => x.title.localeCompare(y.title));
}

if (sortBy === 'year') {
movies.sort((x, y) => x.year - y.year);
}

if (sortBy === 'runtime') {
movies.sort((x, y) => x.runtime - y.runtime);
}
}

function searchMovies(movies, searchStr) {
if (!searchStr) return movies;

let textSearchFields = ['title', 'year', 'actors', 'director', 'plot'];

return movies.filter(movie => {
for (let field of textSearchFields) {
if (containsString(movie[field], searchStr)) return true;
}

for (let genre of movie.genres) {
if (containsString(genre, searchStr)) return true;
}

return false;
});
}

function containsString(obj, searchStr) {
return obj.toString().toLowerCase().indexOf(searchStr.toLowerCase()) !== -1
}

function getPage (movies, page, perPage) {
var start = (page - 1) * perPage;
var end = page * perPage;
return movies.slice(start, end);
}

function deleteMovie(id) {
let movies = moviesData.movies;

for (let i = 0; i < movies.length; i++) {
if (movies[i].id === id) {
movies.splice(i, 1);
}
}

return Promise.resolve(null);
}

function saveMovie(movie) {
if (movie.id) return updateMovie(movie);

return addMovie(movie);
}

function updateMovie(movie) {
let movies = moviesData.movies;

for (let i = 0; i < movies.length; i++) {
if (movies[i].id === movie.id) {
movies[i] = movie;
}
}

return Promise.resolve(null);
}

function addMovie(movie) {
let movies = moviesData.movies;

let maxId = 0;

for (let i = 0; i < movies.length; i++) {
if (movies[i].id > maxId) {
maxId = movies[i].id;
}
}

movie.id = maxId + 1;

movies.push(movie);

return Promise.resolve(null);
}

function getGenres() {
return Promise.resolve(moviesData.genres);
}

0 comments on commit 32f8378

Please sign in to comment.