{"size": 1033, "ext": "js", "max_stars_repo_path": "app/services/items-storage/items-storage.service.js", "max_stars_repo_name": "Nako68l/dairy-app", "max_stars_repo_stars_event_min_datetime": "2016-12-05T22:05:06.000Z", "max_stars_repo_stars_event_max_datetime": "2016-12-05T22:05:06.000Z", "max_issues_repo_path": "app/services/items-storage/items-storage.service.js", "max_issues_repo_name": "Nako68l/dairy-app", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "app/services/items-storage/items-storage.service.js", "max_forks_repo_name": "Nako68l/dairy-app", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 32.28125, "max_line_length": 85, "alphanum_fraction": 0.6737657309}
angular.module('itemsStorageService')
    .factory('itemsStorageService', itemsStorageService);

itemsStorageService.$inject = ['$window'];

function itemsStorageService($window) {
    const storageKey = 'items';

    return {
        saveOne: saveItemToLS,
        getAll: getAllItemsFromLS,
        removeById: removeItemFromLS,
    };

    function saveItemToLS(item) {
        let items = angular.fromJson($window.localStorage.getItem(storageKey)) || [];
        items.push(item);
        $window.localStorage.setItem(storageKey, angular.toJson(items));
    }

    function getAllItemsFromLS() {
        return angular.fromJson($window.localStorage.getItem(storageKey)) || [];
    }

    function removeItemFromLS(id) {
        let items = angular.fromJson($window.localStorage.getItem(storageKey));
        let filteredItems = items.filter(el => !angular.equals(el.id, id));
        $window.localStorage.setItem(storageKey, angular.toJson(filteredItems));
        $window.localStorage.removeItem('comments' + '_' + id);
    }
}
