2022-11-09 10:05:00 +00:00
|
|
|
use crate::{
|
|
|
|
schema::local_site_rate_limit,
|
2022-11-19 04:33:54 +00:00
|
|
|
source::local_site_rate_limit::{
|
|
|
|
LocalSiteRateLimit,
|
|
|
|
LocalSiteRateLimitInsertForm,
|
|
|
|
LocalSiteRateLimitUpdateForm,
|
|
|
|
},
|
2022-11-09 10:05:00 +00:00
|
|
|
utils::{get_conn, DbPool},
|
|
|
|
};
|
2022-11-19 04:33:54 +00:00
|
|
|
use diesel::{dsl::insert_into, result::Error};
|
2022-11-09 10:05:00 +00:00
|
|
|
use diesel_async::RunQueryDsl;
|
2022-10-27 09:24:07 +00:00
|
|
|
|
|
|
|
impl LocalSiteRateLimit {
|
2022-11-09 10:05:00 +00:00
|
|
|
pub async fn read(pool: &DbPool) -> Result<Self, Error> {
|
|
|
|
let conn = &mut get_conn(pool).await?;
|
|
|
|
local_site_rate_limit::table.first::<Self>(conn).await
|
2022-10-27 09:24:07 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
pub async fn create(pool: &DbPool, form: &LocalSiteRateLimitInsertForm) -> Result<Self, Error> {
|
|
|
|
let conn = &mut get_conn(pool).await?;
|
2022-10-27 09:24:07 +00:00
|
|
|
insert_into(local_site_rate_limit::table)
|
|
|
|
.values(form)
|
|
|
|
.get_result::<Self>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2022-10-27 09:24:07 +00:00
|
|
|
}
|
2022-12-19 15:54:42 +00:00
|
|
|
pub async fn update(pool: &DbPool, form: &LocalSiteRateLimitUpdateForm) -> Result<(), Error> {
|
|
|
|
// avoid error "There are no changes to save. This query cannot be built"
|
|
|
|
if form.is_empty() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2022-10-27 09:24:07 +00:00
|
|
|
diesel::update(local_site_rate_limit::table)
|
|
|
|
.set(form)
|
|
|
|
.get_result::<Self>(conn)
|
2022-12-19 15:54:42 +00:00
|
|
|
.await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LocalSiteRateLimitUpdateForm {
|
|
|
|
fn is_empty(&self) -> bool {
|
|
|
|
self.message.is_none()
|
|
|
|
&& self.message_per_second.is_none()
|
|
|
|
&& self.post.is_none()
|
|
|
|
&& self.post_per_second.is_none()
|
|
|
|
&& self.register.is_none()
|
|
|
|
&& self.register_per_second.is_none()
|
|
|
|
&& self.image.is_none()
|
|
|
|
&& self.image_per_second.is_none()
|
|
|
|
&& self.comment.is_none()
|
|
|
|
&& self.comment_per_second.is_none()
|
|
|
|
&& self.search.is_none()
|
|
|
|
&& self.search_per_second.is_none()
|
|
|
|
&& self.updated.is_none()
|
2022-10-27 09:24:07 +00:00
|
|
|
}
|
|
|
|
}
|