Adding fill functions
This commit is contained in:
parent
440e897edf
commit
162ba78430
3 changed files with 53 additions and 3 deletions
|
|
@ -2,7 +2,11 @@ use crate::{
|
|||
Result,
|
||||
Item,
|
||||
Channel,
|
||||
channel::UnparsedChannel,
|
||||
channel::{
|
||||
UnparsedChannel,
|
||||
ChannelId,
|
||||
},
|
||||
user::UserId,
|
||||
};
|
||||
use sqlx::SqlitePool;
|
||||
|
||||
|
|
@ -10,17 +14,60 @@ pub struct FeedId(pub i64);
|
|||
impl From<i64> for FeedId { fn from(id: i64) -> Self { FeedId(id) } }
|
||||
impl From<FeedId> for i64 { fn from(id: FeedId) -> Self { id.0 } }
|
||||
|
||||
pub struct UnparsedFeed {
|
||||
pub id: i64,
|
||||
pub title: String,
|
||||
pub user_id: i64
|
||||
}
|
||||
impl UnparsedFeed {
|
||||
pub fn parse(self) -> Result<Feed> {
|
||||
Ok(Feed {
|
||||
id: FeedId(self.id),
|
||||
title: self.title,
|
||||
user_id: UserId(self.user_id),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Feed {
|
||||
pub id: FeedId,
|
||||
pub title: String,
|
||||
pub user_id: UserId,
|
||||
}
|
||||
|
||||
impl Feed {
|
||||
pub async fn create(
|
||||
pool: &SqlitePool, id: UserId, name: &str
|
||||
) -> Result<Self> {
|
||||
let new_feed = sqlx::query_as!(
|
||||
UnparsedFeed,
|
||||
"INSERT INTO feeds (user_id, title)
|
||||
VALUES (?, ?)
|
||||
RETURNING id as `id!`, user_id, title",
|
||||
id.0, name
|
||||
).fetch_one(pool).await?.parse();
|
||||
|
||||
new_feed
|
||||
}
|
||||
|
||||
pub async fn add_channel(
|
||||
pool: &SqlitePool, id: FeedId, channel_id: ChannelId
|
||||
) -> Result<()> {
|
||||
sqlx::query!(
|
||||
"INSERT INTO feed_channels (feed_id, channel_id)
|
||||
VALUES (?, ?)",
|
||||
id.0, channel_id.0
|
||||
).execute(pool).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn get_items(
|
||||
pool: &SqlitePool, id: FeedId, limit: u8, offset: u32
|
||||
) -> Result<Vec<Item>> {
|
||||
let items = sqlx::query_as!(
|
||||
Item,
|
||||
"SELECT item_id as id FROM feed_items
|
||||
"SELECT item_id as id FROM feed_items
|
||||
WHERE feed_id = ? AND archived = FALSE
|
||||
ORDER BY score DESC
|
||||
LIMIT ? OFFSET ?",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue