From 74f04c3ce0432cb62b84a907d0842d321b5e9544 Mon Sep 17 00:00:00 2001 From: TonyChyi Date: Thu, 30 Jun 2022 22:04:57 +0800 Subject: [PATCH] new feature: order by reference times. --- entity/src/entities/record.rs | 4 +++ migration/src/lib.rs | 2 ++ migration/src/m20220630_195724_for_hot.rs | 37 +++++++++++++++++++++++ src/db_controller.rs | 19 ++++++++++-- src/telegram_bot.rs | 17 ++++++++++- 5 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 migration/src/m20220630_195724_for_hot.rs diff --git a/entity/src/entities/record.rs b/entity/src/entities/record.rs index aa220b0..c679955 100644 --- a/entity/src/entities/record.rs +++ b/entity/src/entities/record.rs @@ -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)] diff --git a/migration/src/lib.rs b/migration/src/lib.rs index 977a1cf..a0d69e2 100644 --- a/migration/src/lib.rs +++ b/migration/src/lib.rs @@ -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), ] } } diff --git a/migration/src/m20220630_195724_for_hot.rs b/migration/src/m20220630_195724_for_hot.rs new file mode 100644 index 0000000..17d7727 --- /dev/null +++ b/migration/src/m20220630_195724_for_hot.rs @@ -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 + } +} diff --git a/src/db_controller.rs b/src/db_controller.rs index db25408..a48e5ba 100644 --- a/src/db_controller.rs +++ b/src/db_controller.rs @@ -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), diff --git a/src/telegram_bot.rs b/src/telegram_bot.rs index fa964bf..31797fe 100644 --- a/src/telegram_bot.rs +++ b/src/telegram_bot.rs @@ -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::().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();