<commit_before>CircleBlvd.Services.stories = function ($http) {

	var s = {};

	var getStory = function (storyId) {
		return s[storyId];
	};

	var saveStory = function (story, callback) {
		$http.put('/data/story/', story)
		.success(function (savedStory) {
			if (callback) {
				callback(savedStory);
			}
		})
		.error(function (data, status) {
			console.log(status);
			console.log(data);
		});
	};

	var usefulStories = function() {
		var s = {};
		s.first = undefined;

		return {
			setFirst: function (story) {
				if (s.first) {
					s.first.isFirstStory = false;
				}
				s.first = story;
				if (s.first) {
					s.first.isFirstStory = true;	
				}
			},
			getFirst: function () {
				return s.first;
			},
			hasFirst: function() {
				if (s.first) {
					return true;
				}
				else {
					return false;
				}
			}
		};
	}(); // closure

	var addStory = function (story, callback) {
		$http.post('/data/story/', story)
		.success(function (newStory) {
			s[newStory.id] = newStory;
			callback(newStory);
		})
		.error(function (data, status) {
			// TODO: Show that something went wrong.
			// Most likely there was a data conflict
			// that could not be resolved.
			console.log(status);
			console.log(data);
			callback(null);
		});
	};


	var insertFirstStory = function (story, projectId, callback) {
		var hadFirstStoryPreviously = usefulStories.hasFirst();
		if (hadFirstStoryPreviously) {
			story.nextId = usefulStories.getFirst().id;	
		}

		story.projectId = projectId;
		story.type = "story";

		addStory(story, function (newStory) {
			if (newStory) {
				var serverStory = getStory(newStory.id);
				if (newStory.isFirstStory) {
					usefulStories.setFirst(serverStory);	
				}
				else {
					// TODO: Probably want to refresh the whole list 
					// from the server, because some crazy things are
					// happening!
				}
				if (callback) {
					callback(serverStory);
				}	
			}
			else if (callback) {
				callback();
			}
		});
	};

	var isBacklogBroken = function () {
		var nextIdCounts = {};
		var isBroken = false;
		var currentStory = usefulStories.getFirst();
		while (currentStory && !isBroken) {
			if (!nextIdCounts[currentStory.id]) {
				nextIdCounts[currentStory.id] = 1;
			}
			else {
				nextIdCounts[currentStory.id]++;
				isBroken = true;
			}
			currentStory = getStory(currentStory.nextId);
		}

		return isBroken;
	};

	var getPreviousStory = function (story) {
		var previousStory = story;
		if (usefulStories.getFirst().id === story.id) {
			return undefined;
		}

		if (isBacklogBroken()) {
			return null;
		}

		var currentStory = usefulStories.getFirst();
		while (currentStory) {
			if (currentStory.nextId === serverStory.id) {
				previousStory = currentStory;
				return previousStory;
			}
			currentStory = getStory(currentStory.nextId);
		}

		// TODO: If we get here, the story doesn't exist.
		return previousStory;
	};

	// wrap around getting and setting the server-side stories,
	// so we can push to the server when we set things. there's
	// probably a better way / pattern for doing this. feel free
	// to implement it, future self.
	var stories = function() {

		return {
			setFirst: usefulStories.setFirst,
			getFirst: usefulStories.getFirst,
			hasFirst: usefulStories.hasFirst,

			init: function (data) {
				s = data;
			},
			add: addStory,
			insertFirst: insertFirstStory,
			move: function (story, newNextStory, callback) {
				var body = {};
				body.story = story;

				if (newNextStory) {
					body.newNextId = newNextStory.id;
				}
				else {
					body.newNextId = getLastStoryId();
				}

				$http.put('/data/story/move', body)
				.success(function (response) {
					// TODO: Move stuff around or something
					callback(null, response);
				})
				.error(function (data, status) {
					callback({
						status: status,
						data: data
					});
				});
			},
			save: saveStory,
			get: getStory, 
			getPrevious: getPreviousStory,
			set: function (storyId, story, callback) {
				if (s[storyId]) {
					s[storyId] = story;
					// update story
					stories.save(story, function (savedStory) {
						if (callback) {
							callback(savedStory);
						}
					});	
				}				
			},
			all: function() {
				return s;
			},
			remove: function (storyId) {
				// TODO: Right now the server-side is handled outside of
				// this class. Should probably make things be consistent. 
				if (s[storyId]) {
					delete s[storyId];
				}
			},
			isListBroken: isBacklogBroken
		};
	}(); // closure;



	return stories;
};
CircleBlvd.Services.stories.$inject = ['$http'];<commit_msg>Fix: Refactor: Moving 'serverStories' to 'stories'<commit_after>CircleBlvd.Services.stories = function ($http) {

	var s = {};

	var getStory = function (storyId) {
		return s[storyId];
	};

	var getLastStoryId = function (projectId) {
		return "last-" + projectId;
	};

	var saveStory = function (story, callback) {
		$http.put('/data/story/', story)
		.success(function (savedStory) {
			if (callback) {
				callback(savedStory);
			}
		})
		.error(function (data, status) {
			console.log(status);
			console.log(data);
		});
	};

	var usefulStories = function() {
		var s = {};
		s.first = undefined;

		return {
			setFirst: function (story) {
				if (s.first) {
					s.first.isFirstStory = false;
				}
				s.first = story;
				if (s.first) {
					s.first.isFirstStory = true;	
				}
			},
			getFirst: function () {
				return s.first;
			},
			hasFirst: function() {
				if (s.first) {
					return true;
				}
				else {
					return false;
				}
			}
		};
	}(); // closure

	var addStory = function (story, callback) {
		$http.post('/data/story/', story)
		.success(function (newStory) {
			s[newStory.id] = newStory;
			callback(newStory);
		})
		.error(function (data, status) {
			// TODO: Show that something went wrong.
			// Most likely there was a data conflict
			// that could not be resolved.
			console.log(status);
			console.log(data);
			callback(null);
		});
	};


	var insertFirstStory = function (story, projectId, callback) {
		var hadFirstStoryPreviously = usefulStories.hasFirst();
		if (hadFirstStoryPreviously) {
			story.nextId = usefulStories.getFirst().id;	
		}

		story.projectId = projectId;
		story.type = "story";

		addStory(story, function (newStory) {
			if (newStory) {
				var serverStory = getStory(newStory.id);
				if (newStory.isFirstStory) {
					usefulStories.setFirst(serverStory);	
				}
				else {
					// TODO: Probably want to refresh the whole list 
					// from the server, because some crazy things are
					// happening!
				}
				if (callback) {
					callback(serverStory);
				}	
			}
			else if (callback) {
				callback();
			}
		});
	};

	var isBacklogBroken = function () {
		var nextIdCounts = {};
		var isBroken = false;
		var currentStory = usefulStories.getFirst();
		while (currentStory && !isBroken) {
			if (!nextIdCounts[currentStory.id]) {
				nextIdCounts[currentStory.id] = 1;
			}
			else {
				nextIdCounts[currentStory.id]++;
				isBroken = true;
			}
			currentStory = getStory(currentStory.nextId);
		}

		return isBroken;
	};

	var getPreviousStory = function (story) {
		var previousStory = story;
		if (usefulStories.getFirst().id === story.id) {
			return undefined;
		}

		if (isBacklogBroken()) {
			return null;
		}

		var currentStory = usefulStories.getFirst();
		while (currentStory) {
			if (currentStory.nextId === serverStory.id) {
				previousStory = currentStory;
				return previousStory;
			}
			currentStory = getStory(currentStory.nextId);
		}

		// TODO: If we get here, the story doesn't exist.
		return previousStory;
	};

	// wrap around getting and setting the server-side stories,
	// so we can push to the server when we set things. there's
	// probably a better way / pattern for doing this. feel free
	// to implement it, future self.
	var stories = function() {

		return {
			setFirst: usefulStories.setFirst,
			getFirst: usefulStories.getFirst,
			hasFirst: usefulStories.hasFirst,

			init: function (data) {
				s = data;
			},
			add: addStory,
			insertFirst: insertFirstStory,
			move: function (story, newNextStory, callback) {
				var body = {};
				body.story = story;

				if (newNextStory) {
					body.newNextId = newNextStory.id;
				}
				else {
					body.newNextId = getLastStoryId(story.projectId);
				}

				$http.put('/data/story/move', body)
				.success(function (response) {
					// TODO: Move stuff around or something
					callback(null, response);
				})
				.error(function (data, status) {
					callback({
						status: status,
						data: data
					});
				});
			},
			save: saveStory,
			get: getStory, 
			getPrevious: getPreviousStory,
			set: function (storyId, story, callback) {
				if (s[storyId]) {
					s[storyId] = story;
					// update story
					stories.save(story, function (savedStory) {
						if (callback) {
							callback(savedStory);
						}
					});	
				}				
			},
			all: function() {
				return s;
			},
			remove: function (storyId) {
				// TODO: Right now the server-side is handled outside of
				// this class. Should probably make things be consistent. 
				if (s[storyId]) {
					delete s[storyId];
				}
			},
			isListBroken: isBacklogBroken
		};
	}(); // closure;



	return stories;
};
CircleBlvd.Services.stories.$inject = ['$http'];
{'fraction_non_alphanumeric': 0.13273001508295626, 'fraction_numerical': 0.00021547080370609782, 'mean_word_length': 2.7043096568236233, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3376834', 'n_tokens_mistral': 3977, 'n_tokens_neox': 3037, 'n_words': 745}