<commit_before>import Vapor
import FluentProvider

final class Comment: Model {
	let storage = Storage()

	var nickname: String
	var text: String
	var postId: Identifier?

	init(nickname: String, text: String, post: Post? = nil) throws {
		self.nickname = nickname
		self.text = text
		postId = try post?.assertExists()
	}

	// MARK: - Row
	init(row: Row) throws {
		nickname = try row.get(Properties.nickname)
		text = try row.get(Properties.text)
		postId = try row.get(Post.foreignIdKey)
	}

	func makeRow() throws -> Row {
		var row = Row()

		try row.set(Properties.nickname, nickname)
		try row.set(Properties.text, text)
		try row.set(Post.foreignIdKey, postId)

		return row
	}
}

extension Comment {
	var post: Parent<Comment, Post> {
		return parent(id: postId)
	}
}

// MARK: - Preparation
extension Comment: Preparation {
	struct Properties {
		static let id = "id"
		static let nickname = "nickname"
		static let text = "text"
		static let post = "post"
		static let createdAt = "createdAt"
	}

	static func prepare(_ database: Database) throws {
		try database.create(self) { builder in
			builder.id()
			builder.string(Properties.nickname)
			builder.string(Properties.text)
			builder.foreignKey(for: Post.self)
		}
	}

	static func revert(_ database: Database) throws {
		try database.delete(self)
	}
}

// MARK: JSONConvertible
extension Comment: JSONConvertible {
	convenience init(json: JSON) throws {
		try self.init(
			nickname: try json.get(Properties.nickname),
			text: try json.get(Properties.text)
		)
	}

	func makeJSON() throws -> JSON {
		var json = JSON()

		try json.set(Properties.id, id)
		try json.set(Properties.nickname, nickname)
		try json.set(Properties.text, text)
		try json.set(Post.foreignIdKey, postId)
		try json.set(Properties.createdAt, createdAt)

		return json
	}
}

// MARK: - NodeConvertible
extension Comment: NodeConvertible {
	func makeNode(in context: Context?) throws -> Node {
		return try Node(makeJSON())
	}
}

// MARK: - ResponseRepresentable
extension Comment: ResponseRepresentable {
}

// MARK: - TimeStampable
extension Comment: Timestampable {
}

// MARK: - SoftDeletable
extension Comment: SoftDeletable {
}

// MARK: - Request
extension Request {
	func comment() throws -> Comment {
		let comment: Comment

		if let json = json {
			comment = try Comment(json: json)
		} else if let nickname = data[Comment.Properties.nickname]?.string,
			let text = data[Comment.Properties.text]?.string {

			comment = try Comment(nickname: nickname, text: text)
		} else {
			throw Abort(.badRequest)
		}

		comment.postId = try parameters.next(Post.self).id

		return comment
	}
}
<commit_msg>Cleaned up Comment's Request extension<commit_after>import Vapor
import FluentProvider

final class Comment: Model {
	let storage = Storage()

	var nickname: String
	var text: String
	var postId: Identifier?

	init(nickname: String, text: String, post: Post? = nil) throws {
		self.nickname = nickname
		self.text = text
		postId = try post?.assertExists()
	}

	// MARK: - Row
	init(row: Row) throws {
		nickname = try row.get(Properties.nickname)
		text = try row.get(Properties.text)
		postId = try row.get(Post.foreignIdKey)
	}

	func makeRow() throws -> Row {
		var row = Row()

		try row.set(Properties.nickname, nickname)
		try row.set(Properties.text, text)
		try row.set(Post.foreignIdKey, postId)

		return row
	}
}

extension Comment {
	var post: Parent<Comment, Post> {
		return parent(id: postId)
	}
}

// MARK: - Preparation
extension Comment: Preparation {
	struct Properties {
		static let id = "id"
		static let nickname = "nickname"
		static let text = "text"
		static let post = "post"
		static let createdAt = "createdAt"
	}

	static func prepare(_ database: Database) throws {
		try database.create(self) { builder in
			builder.id()
			builder.string(Properties.nickname)
			builder.string(Properties.text)
			builder.foreignKey(for: Post.self)
		}
	}

	static func revert(_ database: Database) throws {
		try database.delete(self)
	}
}

// MARK: JSONConvertible
extension Comment: JSONConvertible {
	convenience init(json: JSON) throws {
		try self.init(
			nickname: try json.get(Properties.nickname),
			text: try json.get(Properties.text)
		)
	}

	func makeJSON() throws -> JSON {
		var json = JSON()

		try json.set(Properties.id, id)
		try json.set(Properties.nickname, nickname)
		try json.set(Properties.text, text)
		try json.set(Post.foreignIdKey, postId)
		try json.set(Properties.createdAt, createdAt)

		return json
	}
}

// MARK: - NodeConvertible
extension Comment: NodeConvertible {
	convenience init(node: Node) throws {
		try self.init(
			nickname: try node.get(Properties.nickname),
			text: try node.get(Properties.text)
		)
	}

	func makeNode(in context: Context?) throws -> Node {
		return try Node(makeJSON())
	}
}

// MARK: - ResponseRepresentable
extension Comment: ResponseRepresentable {
}

// MARK: - TimeStampable
extension Comment: Timestampable {
}

// MARK: - SoftDeletable
extension Comment: SoftDeletable {
}

// MARK: - Request
extension Request {
	func comment() throws -> Comment {
		let comment: Comment

		if let json = json {
			comment = try Comment(json: json)
		} else if let node = formURLEncoded {
			comment = try Comment(node: node)
		} else {
			throw Abort(.badRequest)
		}

		comment.postId = try parameters.next(Post.self).id

		return comment
	}
}

{'fraction_non_alphanumeric': 0.11390284757118928, 'fraction_numerical': 0.0, 'mean_word_length': 3.7223198594024605, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, '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': '4646659', 'n_tokens_mistral': 1929, 'n_tokens_neox': 1727, 'n_words': 485}