<commit_before>use Utiaji::App;
use Utiaji::DB;
use Utiaji::Log;

# setup:
# createdb utiaji
# psql utiaji -c "create table kv(k varchar not null primary key, v jsonb)"

unit class Utiaji::App::Getset is Utiaji::App;

has $.db = Utiaji::DB.new;

method BUILD {

    given self.routes {
        .get('/',
            sub ($req,$res) {
                self.render: $res, text => 'Welcome to Utiaji.'
            }
        );

        .get('/get/_key',
            sub ($req,$res,$/) {
                $.db.query: "select v from kv where k=?", $<key>
                    or return self.render: $res, :404status;
                my $json = $.db.json or return self.render: $res, :404status;
                self.render: $res, :$json
            }
        );

        .post('/set/_key',
            sub ($req,$res,$/) {
                trace "running POST /set/_key";
                my $json = $req.json;
                my $key = $<key>;
                unless $json {
                    return self.render: $res, :400status,
                        json => { status => "fail",
                                  reason => "missing or invalid json" }
                }
                $.db.query(q[insert into kv (k,v) values (?, ?)], $key, :$json)
                    or return self.render: $res,
                        json => { status => "fail", reason => $.db.errors },
                        status => 409;
                trace "rendering ok";
                self.render: $res, json => { status => 'ok' }
            }
        );

        .post('/del/_key',
            sub ($req,$res,$/) {
                my $json = $req.json;
                $.db.query: "delete from kv where k = ?", $<key>
                    or return self.render: $res, :400status,
                       json => { status => "fail", reason => $.db.errors };

                return self.render: $res, json => { status => 'ok' }
            }
        );

    }
}
<commit_msg>fixed getset<commit_after>use Utiaji::App;
use Utiaji::DB;
use Utiaji::Log;

# setup:
# createdb utiaji
# psql utiaji -c "create table kv(k varchar not null primary key, v jsonb)"

unit class Utiaji::App::Getset is Utiaji::App;

has $.db = Utiaji::DB.new;

method BUILD {

    given self.routes {
        .get('/',
            sub ($req,$res) {
                self.render: $res, text => 'Welcome to Utiaji.'
            }
        );

        .get('/get/∙key',
            sub ($req,$res,$/) {
                $.db.query: "select v from kv where k=?", $<key>
                    or return self.render: $res, :404status;
                my $json = $.db.json or return self.render: $res, :404status;
                self.render: $res, :$json
            }
        );

        .post('/set/∙key',
            sub ($req,$res,$/) {
                trace "running POST /set/_key";
                my $json = $req.json;
                my $key = $<key>;
                unless $json {
                    return self.render: $res, :400status,
                        json => { status => "fail",
                                  reason => "missing or invalid json" }
                }
                $.db.query(q[insert into kv (k,v) values (?, ?)], $key, :$json)
                    or return self.render: $res,
                        json => { status => "fail", reason => $.db.errors },
                        status => 409;
                trace "rendering ok";
                self.render: $res, json => { status => 'ok' }
            }
        );

        .post('/del/∙key',
            sub ($req,$res,$/) {
                my $json = $req.json;
                $.db.query: "delete from kv where k = ?", $<key>
                    or return self.render: $res, :400status,
                       json => { status => "fail", reason => $.db.errors };

                return self.render: $res, json => { status => 'ok' }
            }
        );

    }
}

{'fraction_non_alphanumeric': 0.15544174135723432, 'fraction_numerical': 0.0076824583866837385, 'mean_word_length': 1.2218430034129693, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7627147', 'n_tokens_mistral': 1202, 'n_tokens_neox': 1108, 'n_words': 305}