Improve frontend

This commit is contained in:
Augusto Gunsch 2021-12-26 00:03:45 -03:00
parent b918498930
commit 0064653cdf
No known key found for this signature in database
GPG Key ID: F7EEFE29825C72DC
5 changed files with 60 additions and 36 deletions

View File

@ -47,13 +47,18 @@ impl WordDb {
transaction.execute(&format!("
CREATE TABLE {}_words (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
word TINYTEXT UNIQUE NOT NULL,
word TINYTEXT NOT NULL,
type_id INTEGER NOT NULL,
content MEDIUMTEXT NOT NULL,
FOREIGN KEY (type_id)
REFERENCES {}_types (id)
)", &lang.code, &lang.code), []).unwrap();
transaction.execute(&format!("
CREATE INDEX word_index
ON {}_words (word)
", &lang.code), []).unwrap();
transaction.commit().unwrap();
}
@ -102,8 +107,7 @@ impl WordDb {
}
println!("Parsing data...");
let entries = WiktionaryEntries::parse_data(data)
.merge_duplicates();
let entries = WiktionaryEntries::parse_data(data);
println!("Inserting data...");
self.insert_entries(lang, entries);

View File

@ -1,7 +1,6 @@
//mod database;
use rocket::routes;
use rocket::fs::FileServer;
use rocket::data::{Limits, ToByteUnit};
use clap::{App, AppSettings, Arg, SubCommand};
//use database::WordDb;
mod database;
@ -83,8 +82,8 @@ async fn main() {
rocket::custom(figment)
.manage(db)
.mount("/static", FileServer::from("static/"))
.mount("/", routes![routes::get_word,
routes::get_word_like,
.mount("/", routes![routes::get_entries,
routes::get_entries_like,
routes::frontend])
.launch()
.await.unwrap();

View File

@ -18,34 +18,46 @@ pub fn frontend() -> Option<content::Html<String>> {
}
#[get("/langs/<lang>/words/<word>")]
pub fn get_word(db: &State<WordDb>, lang: &str, word: &str) -> Option<content::Json<String>> {
let connection = db.connect();
let word = connection.query_row(&format!(
"SELECT content FROM {}_words WHERE word = ?",
lang),
[word],
|row| row.get(0)
);
match word {
Ok(w) => Some(content::Json(w)),
Err(_) => None
}
}
#[get("/langs/<lang>/words?<like>&<limit>&<offset>")]
pub fn get_word_like(db: &State<WordDb>, lang: &str, like: &str, limit: usize, offset: usize) -> Json<Vec<String>> {
pub fn get_entries(db: &State<WordDb>, lang: &str, word: &str) -> status::Custom<content::Json<String>> {
let connection = db.connect();
let mut statement = connection.prepare(&format!(
"SELECT word
FROM {}_words
WHERE word LIKE ?
ORDER BY length(word) ASC
LIMIT ?
OFFSET ?",
lang)
"SELECT content
FROM {}_words
WHERE word = ?",
lang)
).unwrap();
let mut rows = statement.query([word]).unwrap();
let mut words = String::new();
words.push('[');
while let Some(row) = rows.next().unwrap() {
let content: String = row.get(0).unwrap();
words.push_str(&content);
words.push(',');
}
if words.pop().unwrap() == '[' {
words.push('[');
}
words.push(']');
status::Custom(Status::Ok, content::Json(words))
}
#[get("/langs/<lang>/words?<like>&<limit>&<offset>")]
pub fn get_entries_like(db: &State<WordDb>, lang: &str, like: &str, limit: usize, offset: usize) -> Json<Vec<String>> {
let connection = db.connect();
let mut statement = connection.prepare(&format!(
"SELECT word
FROM {}_words
WHERE word LIKE ?
ORDER BY length(word) ASC
LIMIT ?
OFFSET ?",
lang)
).unwrap();
let mut rows = statement.query(params![format!("%{}%", like), limit, offset]).unwrap();

View File

@ -31,7 +31,9 @@ function getWord(word) {
success: (data) => {
$('#ajax-content').html(generateHtml(data))
searchBar.autocomplete('close');
}
},
error: err => console.error(err)
})
}
@ -90,6 +92,12 @@ function generateTable(schemas, pos, forms) {
let unusedCells = forms.filter(cell => !cell.used);
if(schema.ignoreUnused) {
unusedCells = unusedCells.filter(cell =>
!schema.ignoreUnused.map(tags => tags.every(tag => cell.tags.includes(tag)))
);
}
if(unusedCells.length > 0) {
html += '<h3>Other</h3>';
html += generateList(unusedCells);
@ -101,10 +109,6 @@ function generateTable(schemas, pos, forms) {
function generateHtml(data) {
let html = '';
if(Object.prototype.toString.call(data) !== '[object Array]') {
data = [data];
}
data.forEach(entry => {
html += `<h1>${entry.word} <span class="pos">(${entry.pos})</span></h1>`
if('senses' in entry) {

View File

@ -142,6 +142,11 @@
},
{
"pos": "adj",
"ignoreUnused": [
["dative", "feminine", "singular"],
["instrumental", "feminine", "singular"],
["locative", "masculine", "neuter", "singular"]
],
"rows": [
[
{