<commit_before>DocManager = require "./DocManager"
logger = require "logger-sharelatex"

module.exports = HttpController =
	getDoc: (req, res, next = (error) ->) ->
		project_id = req.params.project_id
		doc_id     = req.params.doc_id
		include_deleted = req.query?.include_deleted == "true"
		logger.log project_id: project_id, doc_id: doc_id, "getting doc"
		DocManager.getDoc project_id, doc_id, include_deleted: include_deleted, (error, doc) ->
			return next(error) if error?
			logger.log doc: doc, "got doc"
			if !doc?
				res.send 404
			else
				res.json HttpController._buildDocView(doc)

	getRawDoc: (req, res, next = (error)->)->
		project_id = req.params.project_id
		doc_id     = req.params.doc_id
		logger.log project_id: project_id, doc_id: doc_id, "getting raw doc"
		DocManager.getDoc project_id, doc_id, (error, doc) ->
			return next(error) if error?
			if !doc?
				res.send 404
			else
				res.setHeader('content-type', 'text/plain')
				res.send HttpController._buildRawDocView(doc)

	getAllDocs: (req, res, next = (error) ->) ->
		project_id = req.params.project_id
		logger.log project_id: project_id, "getting all docs"
		DocManager.getAllDocs project_id, (error, docs = []) ->
			return next(error) if error?
			docViews = []
			for doc in docs
				if doc? # There can end up being null docs for some reason :( (probably a race condition)
					docViews.push HttpController._buildDocView(doc)
				else
					logger.error err: new Error("null doc"), project_id: project_id, "encountered null doc"
			res.json docViews

	updateDoc: (req, res, next = (error) ->) ->
		project_id = req.params.project_id
		doc_id     = req.params.doc_id
		lines      = req.body?.lines

		if !lines? or lines not instanceof Array
			logger.error project_id: project_id, doc_id: doc_id, "no doc lines provided"
			res.send 400 # Bad Request
			return

		logger.log project_id: project_id, doc_id: doc_id, "updating doc"
		DocManager.updateDoc project_id, doc_id, lines, (error, modified, rev) ->
			return next(error) if error?
			res.json {
				modified: modified
				rev: rev
			}

	deleteDoc: (req, res, next = (error) ->) -><commit_msg>changed logging to easier to see when the http request starts<commit_after>DocManager = require "./DocManager"
logger = require "logger-sharelatex"

module.exports = HttpController =
	getDoc: (req, res, next = (error) ->) ->
		project_id = req.params.project_id
		doc_id     = req.params.doc_id
		include_deleted = req.query?.include_deleted == "true"
		logger.log project_id: project_id, doc_id: doc_id, "getting doc"
		DocManager.getDoc project_id, doc_id, include_deleted: include_deleted, (error, doc) ->
			return next(error) if error?
			logger.log doc: doc, "got doc"
			if !doc?
				res.send 404
			else
				res.json HttpController._buildDocView(doc)

	getRawDoc: (req, res, next = (error)->)->
		project_id = req.params.project_id
		doc_id     = req.params.doc_id
		logger.log project_id: project_id, doc_id: doc_id, "getting raw doc"
		DocManager.getDoc project_id, doc_id, (error, doc) ->
			return next(error) if error?
			if !doc?
				res.send 404
			else
				res.setHeader('content-type', 'text/plain')
				res.send HttpController._buildRawDocView(doc)

	getAllDocs: (req, res, next = (error) ->) ->
		project_id = req.params.project_id
		logger.log project_id: project_id, "getting all docs"
		DocManager.getAllDocs project_id, (error, docs = []) ->
			return next(error) if error?
			docViews = []
			for doc in docs
				if doc? # There can end up being null docs for some reason :( (probably a race condition)
					docViews.push HttpController._buildDocView(doc)
				else
					logger.error err: new Error("null doc"), project_id: project_id, "encountered null doc"
			res.json docViews

	updateDoc: (req, res, next = (error) ->) ->
		project_id = req.params.project_id
		doc_id     = req.params.doc_id
		lines      = req.body?.lines

		if !lines? or lines not instanceof Array
			logger.error project_id: project_id, doc_id: doc_id, "no doc lines provided"
			res.send 400 # Bad Request
			return

		logger.log project_id: project_id, doc_id: doc_id, "got http request to update doc"
		DocManager.updateDoc project_id, doc_id, lines, (error, modified, rev) ->
			return next(error) if error?
			res.json {
				modified: modified
				rev: rev
			}

	deleteDoc: (req, res, next = (error) ->) ->
{'fraction_non_alphanumeric': 0.11672794117647059, 'fraction_numerical': 0.004136029411764706, 'mean_word_length': 3.616118769883351, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, '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': '3428754', 'n_tokens_mistral': 1684, 'n_tokens_neox': 1469, 'n_words': 418}