<commit_before>import Foundation
import Vapor
import Fluent

public class BlogPost: Model {
    
    static fileprivate let databaseTableName = "blogposts"
    public var id: Node?
    public var exists: Bool = false
    
    public var title: String
    public var contents: String
    public var author: Node?
    public var created: Date
    public var lastEdited: Date?
    
    init(title: String, contents: String, author: BlogUser, creationDate: Date) {
        self.id = nil
        self.title = title
        self.contents = contents
        self.author = author.id
        self.created = creationDate
        self.lastEdited = nil
    }
    
    required public init(node: Node, in context: Context) throws {
        id = try node.extract("id")
        title = try node.extract("title")
        contents = try node.extract("contents")
        author = try node.extract("bloguser_id")
        let createdTime: Double = try node.extract("created")
        let lastEditedTime: Double? = try? node.extract("last_edited")
        
        created = Date(timeIntervalSince1970: createdTime)
        
        
        if let lastEditedTime = lastEditedTime {
            lastEdited = Date(timeIntervalSince1970: lastEditedTime)
        }
    }
}

extension BlogPost: NodeRepresentable {
    public func makeNode(context: Context) throws -> Node {
        let createdTime = created.timeIntervalSince1970
        
        var node = try Node(node: [
            "id": id,
            "title": title,
            "contents": contents,
            "bloguser_id": author,
            "created": createdTime
            ])
        
        if let lastEdited = lastEdited {
            node["last_edited"] = lastEdited.timeIntervalSince1970.makeNode()
        }
        
        let dateFormatter = DateFormatter()
        dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
        dateFormatter.dateStyle = .full
        dateFormatter.timeStyle = .none
        let createdDate = dateFormatter.string(from: created)
        
        switch context {
        case BlogPostContext.shortSnippet:
            node = try Node(node: [
                "id": id,
                "title": title,
                "author_name": try getAuthor()?.name.makeNode(),
                "short_snippet": shortSnippet().makeNode(),
                "created_date": createdDate.makeNode()
                ])
        case BlogPostContext.longSnippet:
            node = try Node(node: [
                "id": id,
                "title": title,
                "author_name": try getAuthor()?.name.makeNode(),
                "long_snippet": longSnippet().makeNode(),
                "created_date": createdDate.makeNode()
                ])
            
            let allLabels = try labels()
            
            if allLabels.count > 0 {
                node["labels"] = try allLabels.makeNode()
            }
            
        case BlogPostContext.all:
            let allLabels = try labels()
            
            if allLabels.count > 0 {
                node["labels"] = try allLabels.makeNode()
            }
            
            node["long_snippet"] = longSnippet().makeNode()
            node["created_date"] = createdDate.makeNode()
            
            if let lastEdited = lastEdited {
                let lastEditedDate = dateFormatter.string(from: lastEdited)
                node["last_edited_date"] = lastEditedDate.makeNode()
            }
            
            node["author_name"] = try getAuthor()?.name.makeNode()
            node["short_snippet"] = shortSnippet().makeNode()
            node["long_snippet"] = longSnippet().makeNode()
        default: break
        }
        
        return node
    }
}

extension BlogPost {
    
    public static func prepare(_ database: Database) throws {
        try database.create(databaseTableName) { posts in
            posts.id()
            posts.string("title")
            posts.custom("contents", type: "TEXT")
            posts.parent(BlogUser.self, optional: false)
            posts.double("created")
            posts.double("lastedited", optional: true)
        }
    }
    
    public static func revert(_ database: Database) throws {
        try database.delete(databaseTableName)
    }
}

public enum BlogPostContext: Context {
    case all
    case shortSnippet
    case longSnippet
}

extension BlogPost {
    func getAuthor() throws -> BlogUser? {
        return try parent(author, nil, BlogUser.self).get()
    }
}
<commit_msg>Add slugUrl property to the model<commit_after>import Foundation
import Vapor
import Fluent

public class BlogPost: Model {
    
    static fileprivate let databaseTableName = "blogposts"
    public var id: Node?
    public var exists: Bool = false
    
    public var title: String
    public var contents: String
    public var author: Node?
    public var created: Date
    public var lastEdited: Date?
    public var slugUrl: String?
    
    init(title: String, contents: String, author: BlogUser, creationDate: Date, slugUrl: String) {
        self.id = nil
        self.title = title
        self.contents = contents
        self.author = author.id
        self.created = creationDate
        self.slugUrl = slugUrl
        self.lastEdited = nil
    }
    
    required public init(node: Node, in context: Context) throws {
        id = try node.extract("id")
        title = try node.extract("title")
        contents = try node.extract("contents")
        author = try node.extract("bloguser_id")
        slugUrl = try node.extract("slug_url")
        let createdTime: Double = try node.extract("created")
        let lastEditedTime: Double? = try? node.extract("last_edited")
        
        created = Date(timeIntervalSince1970: createdTime)
        
        if let lastEditedTime = lastEditedTime {
            lastEdited = Date(timeIntervalSince1970: lastEditedTime)
        }
    }
}

extension BlogPost: NodeRepresentable {
    public func makeNode(context: Context) throws -> Node {
        let createdTime = created.timeIntervalSince1970
        
        var node = try Node(node: [
            "id": id,
            "title": title,
            "contents": contents,
            "bloguser_id": author,
            "created": createdTime,
            "slug_url": slugUrl
            ])
        
        if let lastEdited = lastEdited {
            node["last_edited"] = lastEdited.timeIntervalSince1970.makeNode()
        }
        
        let dateFormatter = DateFormatter()
        dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
        dateFormatter.dateStyle = .full
        dateFormatter.timeStyle = .none
        let createdDate = dateFormatter.string(from: created)
        
        switch context {
        case BlogPostContext.shortSnippet:
            node = try Node(node: [
                "id": id,
                "title": title,
                "author_name": try getAuthor()?.name.makeNode(),
                "short_snippet": shortSnippet().makeNode(),
                "created_date": createdDate.makeNode(),
                "slug_url": slugUrl
                ])
        case BlogPostContext.longSnippet:
            node = try Node(node: [
                "id": id,
                "title": title,
                "author_name": try getAuthor()?.name.makeNode(),
                "long_snippet": longSnippet().makeNode(),
                "created_date": createdDate.makeNode(),
                "slug_url": slugUrl
                ])
            
            let allLabels = try labels()
            
            if allLabels.count > 0 {
                node["labels"] = try allLabels.makeNode()
            }
            
        case BlogPostContext.all:
            let allLabels = try labels()
            
            if allLabels.count > 0 {
                node["labels"] = try allLabels.makeNode()
            }
            
            node["long_snippet"] = longSnippet().makeNode()
            node["created_date"] = createdDate.makeNode()
            
            if let lastEdited = lastEdited {
                let lastEditedDate = dateFormatter.string(from: lastEdited)
                node["last_edited_date"] = lastEditedDate.makeNode()
            }
            
            node["author_name"] = try getAuthor()?.name.makeNode()
            node["short_snippet"] = shortSnippet().makeNode()
            node["long_snippet"] = longSnippet().makeNode()
        default: break
        }
        
        return node
    }
}

extension BlogPost {
    
    public static func prepare(_ database: Database) throws {
        try database.create(databaseTableName) { posts in
            posts.id()
            posts.string("title")
            posts.custom("contents", type: "TEXT")
            posts.parent(BlogUser.self, optional: false)
            posts.double("created")
            posts.double("lastedited", optional: true)
            posts.string("slugUrl")
        }
    }
    
    public static func revert(_ database: Database) throws {
        try database.delete(databaseTableName)
    }
}

public enum BlogPostContext: Context {
    case all
    case shortSnippet
    case longSnippet
}

extension BlogPost {
    func getAuthor() throws -> BlogUser? {
        return try parent(author, nil, BlogUser.self).get()
    }
}

{'fraction_non_alphanumeric': 0.09605249005055394, 'fraction_numerical': 0.003872216844143272, 'mean_word_length': 1.8141646489104115, 'pattern_counts': {'":': 33, '<': 3, '<?xml version=': 0, '>': 11, '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': '2636130', 'n_tokens_mistral': 2483, 'n_tokens_neox': 2405, 'n_words': 595}