<commit_before>import Vapor
import HTTP
import Foundation

final class PostController: ResourceRepresentable {
    func index(request: Request) throws -> ResponseRepresentable {
        // get all the post objects from the database
        return try Post.all().makeNode().converted(to: JSON.self)
    }

    func create(request: Request) throws -> ResponseRepresentable {
        var post = try request.post()
        post.id = UUID().uuidString.makeNode()

        try post.save()

        return post
    }

    func show(request: Request, post: Post) throws -> ResponseRepresentable {
        return post
    }

    func delete(request: Request, post: Post) throws -> ResponseRepresentable {
        try post.delete()

        return JSON([:])
    }

    func update(request: Request, post: Post) throws -> ResponseRepresentable {
        let new = try request.post()
        var post = post
        post.content = new.content
        try post.save()

        return post
    }

    func makeResource() -> Resource<Post> {
        return Resource(
            index: index,
            store: create,
            show: show,
            modify: update,
            destroy: delete
        )
    }
}

extension Request {
    func post() throws -> Post {
        guard let json = json else { throw Abort.badRequest }
        return try Post(node: json)
    }
}<commit_msg>Add check for post data<commit_after>import Vapor
import HTTP
import Foundation

final class PostController: ResourceRepresentable {
    func index(request: Request) throws -> ResponseRepresentable {
        // get all the post objects from the database
        return try Post.all().makeNode().converted(to: JSON.self)
    }

    func create(request: Request) throws -> ResponseRepresentable {
        var post = try request.post()
        post.id = UUID().uuidString.makeNode()

        try post.save()

        return post
    }

    func show(request: Request, post: Post) throws -> ResponseRepresentable {
        return post
    }

    func delete(request: Request, post: Post) throws -> ResponseRepresentable {
        try post.delete()

        return JSON([:])
    }

    func update(request: Request, post: Post) throws -> ResponseRepresentable {
        let new = try request.post()
        var post = post
        post.content = new.content
        try post.save()

        return post
    }

    func makeResource() -> Resource<Post> {
        return Resource(
            index: index,
            store: create,
            show: show,
            modify: update,
            destroy: delete
        )
    }
}

extension Request {
    func post() throws -> Post {
        guard let json = json,
              let _ = json["title"]?.string,
              let _ = json["content"]?.string,
              let _ = json["author"]?.string else { throw Abort.badRequest }

        return try Post(node: json)
    }
}
{'fraction_non_alphanumeric': 0.09665170866413532, 'fraction_numerical': 0.0, 'mean_word_length': 2.053740779768177, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, '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': '5001826', 'n_tokens_mistral': 772, 'n_tokens_neox': 741, 'n_words': 238}