only one method to execute queries is needed apparently
This commit is contained in:
14
src/db.rs
14
src/db.rs
@ -1,6 +1,6 @@
|
||||
use crate::data::{Game, Player, Team};
|
||||
use actix_web::{Error, error, web};
|
||||
use rusqlite::{Row, params};
|
||||
use rusqlite::Row;
|
||||
use serde_json::{Map, Value};
|
||||
use std::collections::HashMap;
|
||||
|
||||
@ -14,18 +14,6 @@ const GAMES_QUERY: &str =
|
||||
const TEAMS_QUERY: &str =
|
||||
"select game, team, min(color), group_concat(player) from teams group by game, team";
|
||||
|
||||
pub async fn execute_query(pool: &Pool, query: &str) -> Result<String, Error> {
|
||||
let pool = pool.clone();
|
||||
let conn = web::block(move || pool.get())
|
||||
.await?
|
||||
.map_err(error::ErrorInternalServerError)?;
|
||||
match conn.execute(query, params![]) {
|
||||
Ok(val) => println!("{val:?}"),
|
||||
Err(er) => println!("error: {er:?}"),
|
||||
}
|
||||
Ok(String::from("success"))
|
||||
}
|
||||
|
||||
pub async fn execute_stmt(pool: &Pool, query: &str) -> Result<String, Error> {
|
||||
let pool = pool.clone();
|
||||
let conn = web::block(move || pool.get())
|
||||
|
||||
@ -65,7 +65,6 @@ struct GamesActor {
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct QueryForm {
|
||||
query: String,
|
||||
is_read: bool,
|
||||
}
|
||||
|
||||
impl Actor for GamesActor {
|
||||
@ -181,11 +180,7 @@ async fn list_of_games(games: web::Data<Arc<Mutex<Vec<Game>>>>) -> impl Responde
|
||||
#[post("/admin/query")]
|
||||
async fn admin_query(pool: web::Data<Pool>, form: web::Form<QueryForm>) -> impl Responder {
|
||||
let res: String;
|
||||
if form.is_read {
|
||||
res = db::execute_stmt(&pool, &form.query).await.unwrap();
|
||||
} else {
|
||||
res = db::execute_query(&pool, &form.query).await.unwrap();
|
||||
}
|
||||
res = db::execute_stmt(&pool, &form.query).await.unwrap();
|
||||
HttpResponse::Ok()
|
||||
.content_type("application/json")
|
||||
.body(res)
|
||||
|
||||
@ -76,7 +76,6 @@
|
||||
method: "POST",
|
||||
body: new URLSearchParams({
|
||||
'query': document.getElementById("query").value,
|
||||
'is_read': true,
|
||||
})
|
||||
})
|
||||
.then(res => {
|
||||
|
||||
Reference in New Issue
Block a user