{"size": 1441, "ext": "js", "max_stars_repo_path": "app/main/services/subjects-serv.js", "max_stars_repo_name": "mmnaseri/vif", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "app/main/services/subjects-serv.js", "max_issues_repo_name": "mmnaseri/vif", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "app/main/services/subjects-serv.js", "max_forks_repo_name": "mmnaseri/vif", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 26.6851851852, "max_line_length": 77, "alphanum_fraction": 0.612074948}
'use strict';
angular.module('main')
	.service('SubjectsService', function (DataStore, $q) {
		var subjects = DataStore('subjects');
		this.load = function (all) {
			all.length = 0;
			return subjects.all().then(function (entities) {
				angular.forEach(entities, function (entity) {
					all.push(entity);
				});
			});
		};
		this.add = function (subject) {
			delete subject.$id;
			return subjects.save(subject);
		};
		this.save = function (subject) {
			if (!angular.isString(subject.$id)) {
				return;
			}
			return subjects.save(subject);
		};
		this.remove = function (subject) {
			var promises = [];
			subjects.all().then(function (allSubjects) {
				//remove this subject from any subject that has it as a dependency
				angular.forEach(allSubjects, function (other) {
					var modified = false;
					angular.forEach(other.dependencies || [], function (dependency, index) {
						if (!modified && dependency.$id === subject.$id) {
							other.dependencies.splice(index, 1);
							modified = true;
						}
					});
					if (modified) {
						promises.push(subjects.save(other));
					}
				});
			});
			//remove all the sessions for this subject
			promises.push(DataStore('sessions').all().where({
				subject: {
					$id: subject.$id
				}
			}).remove());
			return $q.all(promises).then(function () {
				return subjects.remove(subject);
			});
		};
		this.get = function (id) {
			return subjects.load(id);
		};
	});
