use std::fs; use rocket::get; use rocket::State; use rocket::http::Status; use rocket::response::{content, status}; use rocket::serde::json::Json; use rusqlite::params; use crate::database::WordDb; #[get("/frontend")] pub fn frontend() -> Option> { match fs::read_to_string("static/index.html") { Ok(file) => Some(content::Html(file)), Err(_) => None } } #[get("/langs//words/")] pub fn get_entries(db: &State, lang: &str, word: &str) -> status::Custom> { let connection = db.connect(); let mut statement = connection.prepare(&format!( "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//words?&&")] pub fn get_entries_like(db: &State, lang: &str, like: &str, limit: usize, offset: usize) -> Json> { 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(); let mut words = Vec::new(); while let Some(row) = rows.next().unwrap() { words.push(row.get(0).unwrap()); } Json(words) }