Skip to content

Commit

Permalink
return feed
Browse files Browse the repository at this point in the history
  • Loading branch information
zaknesler committed Apr 25, 2024
1 parent bb090fb commit 6336790
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 9 deletions.
3 changes: 3 additions & 0 deletions crates/blend-db/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ pub enum DbError {
#[error("invalid database name")]
InvalidDatabaseName,

#[error("could not fetch row after insertion")]
CouldNotFindInsertedRow,

#[error(transparent)]
SqlError(#[from] sqlx::Error),

Expand Down
27 changes: 21 additions & 6 deletions crates/blend-db/src/repo/feed.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::{error::DbResult, model};
use crate::{
error::{DbError, DbResult},
model,
};
use chrono::{DateTime, Utc};

pub struct FeedRepo {
Expand All @@ -24,14 +27,21 @@ impl FeedRepo {
.map_err(|err| err.into())
}

pub async fn create_feed(&self, data: CreateFeedParams) -> DbResult<i64> {
pub async fn get_feed(&self, id: i64) -> DbResult<Option<model::Feed>> {
sqlx::query_as!(model::Feed, "SELECT * FROM feeds WHERE id = ?1", id)
.fetch_optional(&self.ctx.db)
.await
.map_err(|err| err.into())
}

pub async fn create_feed(&self, data: CreateFeedParams) -> DbResult<model::Feed> {
let mut conn = self.ctx.db.acquire().await?;

let id: i64 = sqlx::query!(
r#"
INSERT INTO feeds ( title, url, published_at, updated_at )
VALUES ( ?1, ?2, ?3, ?4 )
"#,
INSERT INTO feeds ( title, url, published_at, updated_at )
VALUES ( ?1, ?2, ?3, ?4 )
"#,
data.title,
data.url,
data.published_at,
Expand All @@ -41,6 +51,11 @@ impl FeedRepo {
.await?
.last_insert_rowid();

Ok(id)
let feed = self
.get_feed(id)
.await?
.ok_or_else(|| DbError::CouldNotFindInsertedRow)?;

Ok(feed)
}
}
5 changes: 3 additions & 2 deletions ui/src/components/demo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@ import { createMutation, useQueryClient } from '@tanstack/solid-query';
import { type Component, Match, Switch, createSignal } from 'solid-js';
import { addFeed } from '../api/feeds';
import { Button } from './form/button';
import { QUERY_KEYS } from '../constants/query';

export const Demo: Component = () => {
const queryClient = useQueryClient();
const [input, setInput] = createSignal('https://blog.rust-lang.org/feed.xml');

const add = createMutation(() => ({
mutationKey: ['feed.add'],
mutationKey: [QUERY_KEYS.FEEDS_ADD],
mutationFn: addFeed,
}));

const handleClick = async () => {
if (!input()) return;
await add.mutateAsync({ url: input() });
queryClient.invalidateQueries({ queryKey: ['feeds'] });
queryClient.invalidateQueries({ queryKey: [QUERY_KEYS.FEEDS] });
};

return (
Expand Down
4 changes: 4 additions & 0 deletions ui/src/constants/query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const QUERY_KEYS = {
FEEDS: 'feeds',
FEEDS_ADD: 'feeds.add',
} as const;
3 changes: 2 additions & 1 deletion ui/src/hooks/useFeeds.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { createQuery } from '@tanstack/solid-query';
import { getFeeds } from '../api/feeds';
import { QUERY_KEYS } from '../constants/query';

export const useFeeds = () =>
createQuery(() => ({
queryKey: ['feeds'],
queryKey: [QUERY_KEYS.FEEDS],
queryFn: getFeeds,
}));

0 comments on commit 6336790

Please sign in to comment.