<commit_before>"use strict";

module.exports = {
	many : {
		GET : function (request, view) {
			var post_request = null;
			var _self = this;
			var sort = request.url.query.sort;
			var pager = {
				page : request.url.query.page,
				per_page : request.url.query.per_page
			};

			if (request.url.query.user) {
				this.project('official/users').model('users').load(request.url.query.user)
					.ready(function (user) {
						if (user) {
							_self.model('posts').getForUser(user, pager, sort).preload('user_id')
								.ready(function (posts) {
									view.set('posts', posts);
									view.render('many');
								})
								.error(view);
						} else {
							view.statusNotFound();
						}
					})
					.error(view);
			} else {
				_self.model('posts').getAll(pager, sort).preload('user_id')
					.ready(function (posts) {
						if (request.cur_user) {
							view.set('authenticated_user', request.cur_user);
							view.child('add').render('add');
						}

						view.set('posts', posts);
						view.render('many');
					})
					.error(view);
			}
		},
		POST : function (request, view) {
			var _self = this;

			if (request.cur_user) {
				var post = new (_self.model('posts')).Model();
				post.title = request.body.title;
				post.body = request.body.body;
				post.user_id = request.cur_user.id;

				post.save()
					.ready(function (post) {
						view.statusRedirect('/?sort=alphabetical');
					})
					.error(view)
					.validationError(function (errors) {
						view.set('invalid_fields', errors);
						view.render('add');
					});
			} else {
				view.statusUnauthorized();
			}
		}
	}, 
	one : {
		GET : function (request, view) {
			// todo: maybe we can have a common template that loads the post from the id url. some of this code matches DELETE and PATCH
			var _self = this;

			if (!request.url.query.id) {
				return view.statusNotFound();
			}

			_self.model('posts').load(request.url.query.id)
				.preload('user_id')
				.ready(function (post) {
					view.set('post', post);
					if (request.cur_user && request.cur_user.id === post.user_id) {
						view.render('one.auth');
					} else {
						view.render('one');
					}
				})
				.error(view);
		},
		DELETE : function (request, view) {
			// todo: maybe we can have a common template that loads the post from the id url. tons of this code matches GET and PATCH
			var _self = this;
			
			if (!request.url.query.id) {
				return view.statusNotFound();
			}

			if (!request.cur_user) {
				return view.statusUnauthorized();
			}

			_self.model('posts').load(request.url.query.id)
				.ready(function (post) {
					if (!post) {
						return  view.statusNotFound();
					}

					if (post.user_id !== request.cur_user.id) {
						return view.statusUnauthorized();
					} else {
						post.delete()<commit_msg>add a missing 404 error<commit_after>"use strict";

module.exports = {
	many : {
		GET : function (request, view) {
			var post_request = null;
			var _self = this;
			var sort = request.url.query.sort;
			var pager = {
				page : request.url.query.page,
				per_page : request.url.query.per_page
			};

			if (request.url.query.user) {
				this.project('official/users').model('users').load(request.url.query.user)
					.ready(function (user) {
						if (user) {
							_self.model('posts').getForUser(user, pager, sort).preload('user_id')
								.ready(function (posts) {
									view.set('posts', posts);
									view.render('many');
								})
								.error(view);
						} else {
							view.statusNotFound();
						}
					})
					.error(view);
			} else {
				_self.model('posts').getAll(pager, sort).preload('user_id')
					.ready(function (posts) {
						if (request.cur_user) {
							view.set('authenticated_user', request.cur_user);
							view.child('add').render('add');
						}

						view.set('posts', posts);
						view.render('many');
					})
					.error(view);
			}
		},
		POST : function (request, view) {
			var _self = this;

			if (request.cur_user) {
				var post = new (_self.model('posts')).Model();
				post.title = request.body.title;
				post.body = request.body.body;
				post.user_id = request.cur_user.id;

				post.save()
					.ready(function (post) {
						view.statusRedirect('/?sort=alphabetical');
					})
					.error(view)
					.validationError(function (errors) {
						view.set('invalid_fields', errors);
						view.render('add');
					});
			} else {
				view.statusUnauthorized();
			}
		}
	}, 
	one : {
		GET : function (request, view) {
			// todo: maybe we can have a common template that loads the post from the id url. some of this code matches DELETE and PATCH
			var _self = this;

			if (!request.url.query.id) {
				return view.statusNotFound();
			}

			_self.model('posts').load(request.url.query.id)
				.preload('user_id')
				.ready(function (post) {
					if (!post) {
						return view.statusNotFound();
					}

					view.set('post', post);
					if (request.cur_user && request.cur_user.id === post.user_id) {
						view.render('one.auth');
					} else {
						view.render('one');
					}
				})
				.error(view);
		},
		DELETE : function (request, view) {
			// todo: maybe we can have a common template that loads the post from the id url. tons of this code matches GET and PATCH
			var _self = this;
			
			if (!request.url.query.id) {
				return view.statusNotFound();
			}

			if (!request.cur_user) {
				return view.statusUnauthorized();
			}

			_self.model('posts').load(request.url.query.id)
				.ready(function (post) {
					if (!post) {
						return  view.statusNotFound();
					}

					if (post.user_id !== request.cur_user.id) {
						return view.statusUnauthorized();
					} else {
						post.delete()
{'fraction_non_alphanumeric': 0.15803382663847781, 'fraction_numerical': 0.0005285412262156448, 'mean_word_length': 2.451063829787234, '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': '5096610', 'n_tokens_mistral': 2548, 'n_tokens_neox': 1862, 'n_words': 387}