Func
This commit is contained in:
21 files changed
+590
-70
No files matched your search
@@ -1,3 +1,5 @@
|
||||
use sqlx::SqlitePool;
|
||||
|
||||
#[derive(sqlx::FromRow)]
|
||||
pub struct Paste {
|
||||
pub id: String,
|
||||
@@ -19,3 +21,33 @@ pub enum PasteType {
|
||||
TextV1 = 0,
|
||||
UrlV1 = 100,
|
||||
}
|
||||
impl Paste {
|
||||
pub async fn read(
|
||||
db: &SqlitePool,
|
||||
id: &str,
|
||||
paste_type: PasteType,
|
||||
) -> Result<Option<Self>, sqlx::Error> {
|
||||
let mut transaction = db.begin().await?;
|
||||
let paste = sqlx::query_as::<_, Self>(
|
||||
"UPDATE notes SET views = views + 1
|
||||
WHERE type = ? AND id = ?
|
||||
AND (expiration IS NULL OR expiration > unixepoch())
|
||||
AND (max_views IS NULL OR views < max_views)
|
||||
RETURNING *",
|
||||
)
|
||||
.bind(paste_type)
|
||||
.bind(id)
|
||||
.fetch_optional(&mut *transaction)
|
||||
.await?;
|
||||
if let Some(paste) = &paste
|
||||
&& paste.max_views.is_some_and(|max| paste.views >= max)
|
||||
{
|
||||
sqlx::query("DELETE FROM notes WHERE id = ?")
|
||||
.bind(&paste.id)
|
||||
.execute(&mut *transaction)
|
||||
.await?;
|
||||
}
|
||||
transaction.commit().await?;
|
||||
Ok(paste)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user