new feature: order by reference times.

This commit is contained in:
TonyChyi 2022-06-30 22:04:57 +08:00
parent 33142bb470
commit 74f04c3ce0
5 changed files with 76 additions and 3 deletions

View File

@ -14,6 +14,10 @@ pub struct Model {
/// records
#[sea_orm(indexed, column_type = "Text", unique)]
pub message: String,
/// hot
#[sea_orm(default_value = "0")]
pub hot: i64,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]

View File

@ -2,6 +2,7 @@ pub use sea_orm_migration::prelude::*;
mod m20220101_000001_create_table;
mod m20220625_222908_message_unique;
mod m20220630_195724_for_hot;
pub struct Migrator;
@ -11,6 +12,7 @@ impl MigratorTrait for Migrator {
vec![
Box::new(m20220101_000001_create_table::Migration),
Box::new(m20220625_222908_message_unique::Migration),
Box::new(m20220630_195724_for_hot::Migration),
]
}
}

View File

@ -0,0 +1,37 @@
use models::prelude::Record;
use sea_orm_migration::prelude::*;
pub struct Migration;
impl MigrationName for Migration {
fn name(&self) -> &str {
"m20220630_195724_for_hot"
}
}
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.alter_table(
Table::alter()
.table(Record)
.add_column_if_not_exists(
ColumnDef::new(Alias::new("hot")).big_integer().default(0),
)
.to_owned(),
)
.await
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.alter_table(
Table::alter()
.table(Record)
.drop_column(Alias::new("hot"))
.to_owned(),
)
.await
}
}

View File

@ -2,7 +2,7 @@ use migration::{Migrator, MigratorTrait};
use models::prelude::*;
use sea_orm::{
ActiveModelTrait, ColumnTrait, Database, DatabaseConnection, DatabaseTransaction, DbErr,
EntityTrait, PaginatorTrait, QueryFilter, Set, TransactionTrait,
EntityTrait, PaginatorTrait, QueryFilter, QueryOrder, Set, TransactionTrait,
};
use wd_log::{log_error_ln, log_info_ln, log_panic, log_warn_ln};
@ -119,7 +119,8 @@ impl Controller {
let pagination = Record::find()
.find_also_related(User)
.filter(RecordColumn::Message.contains(key_word.as_str()))
.paginate(&self.db, PAGE_SIZE * 2); // 50 records seems ok.
.order_by_desc(RecordColumn::Hot)
.paginate(&self.db, 50); // 50 records seems ok.
Ok(PaginatedRecordData {
items_count: pagination.num_items().await?,
pages_count: pagination.num_pages().await?,
@ -172,6 +173,7 @@ impl Controller {
/// del record when `/delete` command called.
pub async fn del_record(&self, id: i64, user_id: i64) -> Result<(), DbErr> {
let transaction = self.db.begin().await?;
if let Some(user) = self.get_user(&user_id, &transaction).await? {
RecordActiveModel {
id: Set(id),
@ -184,6 +186,19 @@ impl Controller {
transaction.commit().await
}
pub async fn update_record_hot(&self, id: i64) -> Result<(), DbErr> {
let transcation = self.db.begin().await?;
if let Some(record) = Record::find_by_id(id).one(&transcation).await? {
let hot = record.hot;
let mut record_active: RecordActiveModel = record.into();
record_active.hot = Set(hot + 1);
record_active.save(&transcation).await?;
}
transcation.commit().await
}
pub fn err_handler(&self, error: DbErr) {
match error {
DbErr::Conn(err) => log_panic!("{}", err),

View File

@ -78,6 +78,9 @@ impl BotServer {
UpdateKind::Message(ref message) => self.message_handler(message).await,
UpdateKind::InlineQuery(inline_query) => self.inline_query_hander(inline_query).await,
UpdateKind::CallbackQuery(callback) => self.callback_handler(callback).await,
UpdateKind::ChosenInlineResult(result) => {
self.chosen_inline_result_handler(result).await;
}
kind => self.default_update_hander(&kind).await,
}
}
@ -86,6 +89,18 @@ impl BotServer {
log_debug_ln!("non-supported kind {:?}", update_kind);
}
async fn chosen_inline_result_handler(&self, result: &ChosenInlineResult) {
log_debug_ln!("chosen_result={:?}", result);
if let Err(error) = self
.controller
.update_record_hot(result.result_id.parse::<i64>().unwrap())
.await
{
self.controller.err_handler(error);
}
}
async fn callback_handler(&self, callback: &CallbackQuery) {
log_debug_ln!("callback={:#?}", callback);
@ -241,7 +256,7 @@ impl BotServer {
.add_record(user.id.0.try_into().unwrap(), &username, data.to_string())
.await
{
log_error_ln!("{}", err);
self.controller.err_handler(err);
return;
}
let mut vars = HashMap::new();