Finalizing type schema and user tests
This commit is contained in:
parent
162ba78430
commit
e07a98ddbb
2 changed files with 143 additions and 28 deletions
|
|
@ -10,41 +10,42 @@ use crate::{
|
|||
};
|
||||
use sqlx::SqlitePool;
|
||||
|
||||
pub struct FeedId(pub i64);
|
||||
impl From<i64> for FeedId { fn from(id: i64) -> Self { FeedId(id) } }
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct FeedId(i64);
|
||||
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,
|
||||
id: FeedId,
|
||||
title: String,
|
||||
}
|
||||
|
||||
impl Feed {
|
||||
pub fn id(&self) -> FeedId { self.id }
|
||||
pub fn title(&self) -> &str { &self.title }
|
||||
|
||||
pub async fn create(
|
||||
pool: &SqlitePool, id: UserId, name: &str
|
||||
) -> Result<Self> {
|
||||
let int_id = i64::from(id);
|
||||
let new_feed = sqlx::query_as!(
|
||||
UnparsedFeed,
|
||||
"INSERT INTO feeds (user_id, title)
|
||||
VALUES (?, ?)
|
||||
RETURNING id as `id!`, user_id, title",
|
||||
id.0, name
|
||||
RETURNING id as `id!`, title",
|
||||
int_id, name
|
||||
).fetch_one(pool).await?.parse();
|
||||
|
||||
new_feed
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue