new feature: order by reference times.
This commit is contained in:
parent
33142bb470
commit
74f04c3ce0
@ -14,6 +14,10 @@ pub struct Model {
|
|||||||
/// records
|
/// records
|
||||||
#[sea_orm(indexed, column_type = "Text", unique)]
|
#[sea_orm(indexed, column_type = "Text", unique)]
|
||||||
pub message: String,
|
pub message: String,
|
||||||
|
|
||||||
|
/// hot
|
||||||
|
#[sea_orm(default_value = "0")]
|
||||||
|
pub hot: i64,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||||
|
@ -2,6 +2,7 @@ pub use sea_orm_migration::prelude::*;
|
|||||||
|
|
||||||
mod m20220101_000001_create_table;
|
mod m20220101_000001_create_table;
|
||||||
mod m20220625_222908_message_unique;
|
mod m20220625_222908_message_unique;
|
||||||
|
mod m20220630_195724_for_hot;
|
||||||
|
|
||||||
pub struct Migrator;
|
pub struct Migrator;
|
||||||
|
|
||||||
@ -11,6 +12,7 @@ impl MigratorTrait for Migrator {
|
|||||||
vec![
|
vec![
|
||||||
Box::new(m20220101_000001_create_table::Migration),
|
Box::new(m20220101_000001_create_table::Migration),
|
||||||
Box::new(m20220625_222908_message_unique::Migration),
|
Box::new(m20220625_222908_message_unique::Migration),
|
||||||
|
Box::new(m20220630_195724_for_hot::Migration),
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
37
migration/src/m20220630_195724_for_hot.rs
Normal file
37
migration/src/m20220630_195724_for_hot.rs
Normal 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
|
||||||
|
}
|
||||||
|
}
|
@ -2,7 +2,7 @@ use migration::{Migrator, MigratorTrait};
|
|||||||
use models::prelude::*;
|
use models::prelude::*;
|
||||||
use sea_orm::{
|
use sea_orm::{
|
||||||
ActiveModelTrait, ColumnTrait, Database, DatabaseConnection, DatabaseTransaction, DbErr,
|
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};
|
use wd_log::{log_error_ln, log_info_ln, log_panic, log_warn_ln};
|
||||||
|
|
||||||
@ -119,7 +119,8 @@ impl Controller {
|
|||||||
let pagination = Record::find()
|
let pagination = Record::find()
|
||||||
.find_also_related(User)
|
.find_also_related(User)
|
||||||
.filter(RecordColumn::Message.contains(key_word.as_str()))
|
.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 {
|
Ok(PaginatedRecordData {
|
||||||
items_count: pagination.num_items().await?,
|
items_count: pagination.num_items().await?,
|
||||||
pages_count: pagination.num_pages().await?,
|
pages_count: pagination.num_pages().await?,
|
||||||
@ -172,6 +173,7 @@ impl Controller {
|
|||||||
/// del record when `/delete` command called.
|
/// del record when `/delete` command called.
|
||||||
pub async fn del_record(&self, id: i64, user_id: i64) -> Result<(), DbErr> {
|
pub async fn del_record(&self, id: i64, user_id: i64) -> Result<(), DbErr> {
|
||||||
let transaction = self.db.begin().await?;
|
let transaction = self.db.begin().await?;
|
||||||
|
|
||||||
if let Some(user) = self.get_user(&user_id, &transaction).await? {
|
if let Some(user) = self.get_user(&user_id, &transaction).await? {
|
||||||
RecordActiveModel {
|
RecordActiveModel {
|
||||||
id: Set(id),
|
id: Set(id),
|
||||||
@ -184,6 +186,19 @@ impl Controller {
|
|||||||
transaction.commit().await
|
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) {
|
pub fn err_handler(&self, error: DbErr) {
|
||||||
match error {
|
match error {
|
||||||
DbErr::Conn(err) => log_panic!("{}", err),
|
DbErr::Conn(err) => log_panic!("{}", err),
|
||||||
|
@ -78,6 +78,9 @@ impl BotServer {
|
|||||||
UpdateKind::Message(ref message) => self.message_handler(message).await,
|
UpdateKind::Message(ref message) => self.message_handler(message).await,
|
||||||
UpdateKind::InlineQuery(inline_query) => self.inline_query_hander(inline_query).await,
|
UpdateKind::InlineQuery(inline_query) => self.inline_query_hander(inline_query).await,
|
||||||
UpdateKind::CallbackQuery(callback) => self.callback_handler(callback).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,
|
kind => self.default_update_hander(&kind).await,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -86,6 +89,18 @@ impl BotServer {
|
|||||||
log_debug_ln!("non-supported kind {:?}", update_kind);
|
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) {
|
async fn callback_handler(&self, callback: &CallbackQuery) {
|
||||||
log_debug_ln!("callback={:#?}", callback);
|
log_debug_ln!("callback={:#?}", callback);
|
||||||
|
|
||||||
@ -241,7 +256,7 @@ impl BotServer {
|
|||||||
.add_record(user.id.0.try_into().unwrap(), &username, data.to_string())
|
.add_record(user.id.0.try_into().unwrap(), &username, data.to_string())
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
log_error_ln!("{}", err);
|
self.controller.err_handler(err);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let mut vars = HashMap::new();
|
let mut vars = HashMap::new();
|
||||||
|
Loading…
Reference in New Issue
Block a user