* Upated message struct initialization to shorthand (cosmetic thing
* Updated message struct to use channel_id not _name + Base implementation f Message::get_time_range, yet to be tested
This commit is contained in:
parent
95414e21a3
commit
b917483dac
@ -4,39 +4,42 @@ use mysql_async::error::Error as SqlError;
|
|||||||
|
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
|
|
||||||
|
use serde::Serialize;
|
||||||
|
|
||||||
use crate::{Response, no_conn, sql_err};
|
use crate::{Response, no_conn, sql_err};
|
||||||
use crate::{UBigInt, BigInt, VarChar};
|
use crate::{UBigInt, BigInt, VarChar};
|
||||||
|
|
||||||
use crate::common::{FromDB};
|
use crate::common::FromDB;
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
|
#[derive(Serialize)]
|
||||||
pub struct Message {
|
pub struct Message {
|
||||||
pub id: UBigInt,
|
pub id: UBigInt,
|
||||||
pub time: BigInt,
|
pub time: BigInt,
|
||||||
pub content: VarChar,
|
pub content: VarChar,
|
||||||
pub author_id: UBigInt,
|
pub author_id: UBigInt,
|
||||||
pub channel_name: VarChar
|
pub channel_id: UBigInt
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl FromDB<Message, (BigInt, UBigInt)> for Message {
|
impl FromDB<Message, (BigInt, UBigInt)> for Message {
|
||||||
type Row = Option<(UBigInt, BigInt, VarChar, UBigInt, VarChar)>;
|
type Row = Option<(UBigInt, BigInt, VarChar, UBigInt, UBigInt)>;
|
||||||
|
|
||||||
async fn get(p: &Pool, id: UBigInt) -> Response<Self> {
|
async fn get(p: &Pool, id: UBigInt) -> Response<Self> {
|
||||||
//! Typically used as the backend to the .update(...) method to
|
//! Typically used as the backend to the .update(...) method to
|
||||||
//! pick out a message to later edit
|
//! pick out a message to later edit
|
||||||
if let Ok(conn) = p.get_conn().await {
|
if let Ok(conn) = p.get_conn().await {
|
||||||
let q = "SELECT id, time, content, author_id, channel_name WHERE id = :id";
|
let q = "SELECT id, time, content, author_id, channel_id WHERE id = :id";
|
||||||
let result: Result<(Conn, Self::Row), SqlError> =
|
let result: Result<(Conn, Self::Row), SqlError> =
|
||||||
conn.first_exec(q, params!{"id" => id}).await;
|
conn.first_exec(q, params!{"id" => id}).await;
|
||||||
if let Ok((_, row)) = result {
|
if let Ok((_, row)) = result {
|
||||||
return match row {
|
return match row {
|
||||||
Some(row) => Response::Row(Self {
|
Some(row) => Response::Row(Self {
|
||||||
id: id,
|
id,
|
||||||
time: row.1,
|
time: row.1,
|
||||||
content: row.2,
|
content: row.2,
|
||||||
author_id: row.3,
|
author_id: row.3,
|
||||||
channel_name: row.4
|
channel_id: row.4
|
||||||
}),
|
}),
|
||||||
None => Response::Empty
|
None => Response::Empty
|
||||||
}
|
}
|
||||||
@ -85,15 +88,15 @@ impl FromDB<Message, (BigInt, UBigInt)> for Message {
|
|||||||
let q = "SELECT id, time, content, author_id";
|
let q = "SELECT id, time, content, author_id";
|
||||||
if let Ok(query)= conn.prep_exec(q, params!{"time" => time, "cid" => channel_id}).await {
|
if let Ok(query)= conn.prep_exec(q, params!{"time" => time, "cid" => channel_id}).await {
|
||||||
let mapping_r = query.map_and_drop(|row| {
|
let mapping_r = query.map_and_drop(|row| {
|
||||||
let (id, time, content, uid): (UBigInt, BigInt, VarChar, UBigInt) =
|
let (id, time, content, author_id): (UBigInt, BigInt, VarChar, UBigInt) =
|
||||||
mysql_async::from_row(row);
|
mysql_async::from_row(row);
|
||||||
|
|
||||||
Message {
|
Message {
|
||||||
id: id,
|
id,
|
||||||
time: time,
|
time,
|
||||||
content: content,
|
content,
|
||||||
author_id: uid,
|
author_id,
|
||||||
channel_name: "".into() // no point at this point tbh
|
channel_id
|
||||||
}
|
}
|
||||||
}).await;
|
}).await;
|
||||||
|
|
||||||
@ -151,5 +154,32 @@ impl Message {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn get_time_range(p: &Pool, channel_id: UBigInt, start: BigInt, end: BigInt) -> Result<Response<Self>, SqlError> {
|
||||||
|
let conn = p.get_conn().await?;
|
||||||
|
let q = "SELECT id, time, content, author_id WHERE channel_id = :channel AND time >= :start AND time < :end";
|
||||||
|
|
||||||
|
let select_result = conn.prep_exec(
|
||||||
|
q, params!{
|
||||||
|
"start" => start,
|
||||||
|
"end" => end,
|
||||||
|
"channel" => channel_id
|
||||||
|
}).await?;
|
||||||
|
|
||||||
|
let(_conn, messages) = select_result.map_and_drop(|row| {
|
||||||
|
type Tuple = (UBigInt, BigInt, String, UBigInt);
|
||||||
|
let (id, time, content, author_id): Tuple = mysql_async::from_row(row);
|
||||||
|
Self {
|
||||||
|
id,
|
||||||
|
time,
|
||||||
|
content,
|
||||||
|
author_id,
|
||||||
|
channel_id
|
||||||
|
}
|
||||||
|
}).await?;
|
||||||
|
|
||||||
|
Ok(Response::Set(messages))
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user