From: Mischa POSLAWSKY Date: Sun, 24 May 2020 01:49:31 +0000 (+0200) Subject: word: writer page to edit database entries X-Git-Tag: v1.13~265 X-Git-Url: http://git.shiar.net/sheet.git/commitdiff_plain/e4d544e9178552fdf5f7caf2d5869b502110fcd9 word: writer page to edit database entries Initial form to maintain a larger amount of words. --- diff --git a/tools/word.pg.sql b/tools/word.pg.sql index 8a5fec6..3c4f264 100644 --- a/tools/word.pg.sql +++ b/tools/word.pg.sql @@ -1,13 +1,19 @@ CREATE TABLE word ( form text NOT NULL, + lang text NOT NULL DEFAULT 'eng', cat integer REFERENCES word (id), + ref integer REFERENCES word (id), source text, thumb text[], + wptitle text, created timestamptz DEFAULT now(), id serial NOT NULL PRIMARY KEY ); COMMENT ON COLUMN word.form IS 'preferred textual representation'; +COMMENT ON COLUMN word.lang IS 'ISO 639-3 language code'; COMMENT ON COLUMN word.cat IS 'hierarchical classification'; +COMMENT ON COLUMN word.ref IS 'reference to equivalent eng translation'; COMMENT ON COLUMN word.source IS 'URI of downloaded image'; COMMENT ON COLUMN word.thumb IS 'ImageMagick convert options to create thumbnail from source image'; +COMMENT ON COLUMN word.wptitle IS 'reference Wikipedia article'; diff --git a/writer.plp b/writer.plp new file mode 100644 index 0000000..ce421b8 --- /dev/null +++ b/writer.plp @@ -0,0 +1,66 @@ +<(common.inc.plp)><: + +Html({ + title => 'words cheat sheet admin', + version => '1.0', + nocache => 1, +}); + +use List::Util qw( pairs pairkeys ); + +my $db = eval { + my @dbinfo = ( + 'DBI:Pg:dbname=sheet;host=localhost', 'sheetadmin', 'fairuse', + ) or die "database not configured\n"; + require DBIx::Simple; + DBIx::Simple->new(@dbinfo[0..2], { + RaiseError => 1, + pg_enable_utf8 => 1, + }); +} or Abort('Database error', 501, $@); + +my @wordcols = ( + form => 'Translation', + ref => 'Reference', + cat => 'Category', + lang => 'Language', + source => 'Image URL', + thumb => 'Convert options', + wptitle => 'Wikipedia', +); +my $find = $Request ? {id => $Request} : undef; + +my $row; +if ($ENV{REQUEST_METHOD} eq 'POST') { + $row = {%post{ pairkeys @wordcols }}; + $_ = length ? $_ : undef for values %{$row}; + eval { + my %res = (returning => $Request ? '*' : 'lang, cat'); + my $query = $find ? $db->update(word => $row, $find, \%res) : + $db->insert(word => $row, \%res); + $row = $query->hash; + } or Alert("Entry could not be saved", $@); +} +elsif ($find) { + $row = $db->select(word => '*', $find)->hash + or Abort("Word not found", 404); +} + +my $title = $find ? "entry #$Request" : 'new entry'; +:> +

Words <:= $title :>

+ +
+
+<: + +for my $col (pairs @wordcols) { + printf '
' + . '
', + $col->key, $col->value, $row->{$col->key} // ''; + say '
'; +} +:> +
+

+