-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
125 additions
and
131 deletions.
There are no files selected for viewing
44 changes: 0 additions & 44 deletions
44
.sqlx/query-14f422398a6bab143a4b7204e1f5341f965207e340dd30721078f24cf0cd396d.json
This file was deleted.
Oops, something went wrong.
12 changes: 0 additions & 12 deletions
12
.sqlx/query-14ffb21f2a2d6a5f70a6a28f01eda42bbbdb5fb3c83ce9f08b7434f4c892b78b.json
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[package] | ||
name = "blend-context" | ||
version.workspace = true | ||
edition.workspace = true | ||
|
||
[dependencies] | ||
blend-config = { path = "../blend-config" } | ||
thiserror = { workspace = true } | ||
sqlx = { workspace = true, features = ["sqlite"] } |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod client; | ||
pub mod error; | ||
pub mod model; | ||
pub mod repo; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,15 @@ | ||
use chrono::NaiveDateTime; | ||
use serde::{Deserialize, Serialize}; | ||
use sqlx::prelude::FromRow; | ||
use ts_rs::TS; | ||
|
||
#[derive(TS)] | ||
#[ts(export, export_to = "../../../ui/src/types/bindings/feed.ts")] | ||
#[derive(Debug, Serialize, Deserialize)] | ||
#[derive(Debug, Serialize, Deserialize, FromRow)] | ||
pub struct Feed { | ||
pub id: i64, | ||
pub url: Option<String>, | ||
pub title: Option<String>, | ||
pub published_at: Option<String>, | ||
pub updated_at: Option<String>, | ||
// published_at: Option<chrono::DateTime<chrono::Utc>>, | ||
// updated_at: Option<chrono::DateTime<chrono::Utc>>, | ||
// entries: Vec<Entry>, | ||
pub published_at: Option<NaiveDateTime>, | ||
pub updated_at: Option<NaiveDateTime>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use crate::{error::DbResult, model}; | ||
use chrono::{DateTime, Utc}; | ||
|
||
pub struct FeedRepo { | ||
ctx: blend_context::Context, | ||
} | ||
|
||
pub struct CreateFeedParams { | ||
pub title: Option<String>, | ||
pub url: Option<String>, | ||
pub published_at: Option<DateTime<Utc>>, | ||
pub updated_at: Option<DateTime<Utc>>, | ||
} | ||
|
||
impl FeedRepo { | ||
pub fn new(ctx: blend_context::Context) -> Self { | ||
Self { ctx } | ||
} | ||
|
||
pub async fn get_feeds(&self) -> DbResult<Vec<model::Feed>> { | ||
sqlx::query_as!(model::Feed, "SELECT * FROM feeds") | ||
.fetch_all(&self.ctx.db) | ||
.await | ||
.map_err(|err| err.into()) | ||
} | ||
|
||
pub async fn create_feed(&self, data: CreateFeedParams) -> DbResult<i64> { | ||
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 ) | ||
"#, | ||
data.title, | ||
data.url, | ||
data.published_at, | ||
data.updated_at, | ||
) | ||
.execute(&mut *conn) | ||
.await? | ||
.last_insert_rowid(); | ||
|
||
Ok(id) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod feed; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,49 @@ | ||
use crate::{context::Context, error::WebResult}; | ||
use crate::error::WebResult; | ||
use axum::{ | ||
extract::State, | ||
response::IntoResponse, | ||
routing::{get, post}, | ||
Json, Router, | ||
}; | ||
use blend_db::model; | ||
use serde::{Deserialize, Serialize}; | ||
use blend_db::repo; | ||
use serde::Deserialize; | ||
use serde_json::json; | ||
|
||
pub fn router(ctx: Context) -> Router { | ||
pub fn router(ctx: blend_context::Context) -> Router { | ||
Router::new() | ||
.route("/", get(index)) | ||
.route("/create", get(create)) | ||
.route("/parse", post(parse)) | ||
.route("/add", post(add)) | ||
// .route_layer(middleware::from_fn_with_state( | ||
// ctx.clone(), | ||
// crate::middleware::auth::middleware, | ||
// )) | ||
.with_state(ctx) | ||
} | ||
|
||
async fn index(State(ctx): State<Context>) -> WebResult<impl IntoResponse> { | ||
let feeds: Vec<model::Feed> = sqlx::query_as!(model::Feed, "SELECT * FROM feeds") | ||
.fetch_all(&ctx.db) | ||
.await?; | ||
|
||
async fn index(State(ctx): State<blend_context::Context>) -> WebResult<impl IntoResponse> { | ||
let feeds = repo::feed::FeedRepo::new(ctx).get_feeds().await?; | ||
Ok(Json(json!({ "data": feeds }))) | ||
} | ||
|
||
async fn create(State(ctx): State<Context>) -> WebResult<impl IntoResponse> { | ||
let mut conn = ctx.db.acquire().await?; | ||
|
||
let title = "Rust Blog"; | ||
let url = "https://blog.rust-lang.org/feed.xml"; | ||
|
||
// Insert the task, then obtain the ID of this row | ||
sqlx::query!( | ||
r#" | ||
INSERT INTO feeds ( title, url ) | ||
VALUES ( ?1, ?2 ) | ||
"#, | ||
title, | ||
url | ||
) | ||
.execute(&mut *conn) | ||
.await? | ||
.last_insert_rowid(); | ||
|
||
Ok("ok") | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
struct ParseFeedParams { | ||
struct AddFeedParams { | ||
url: String, | ||
} | ||
|
||
async fn parse(Json(data): Json<ParseFeedParams>) -> WebResult<impl IntoResponse> { | ||
async fn add( | ||
State(ctx): State<blend_context::Context>, | ||
Json(data): Json<AddFeedParams>, | ||
) -> WebResult<impl IntoResponse> { | ||
let parsed = blend_parse::parse_url(&data.url).await?; | ||
|
||
let title = parsed.title.map(|title| title.content); | ||
|
||
#[derive(Debug, Serialize)] | ||
struct Response { | ||
title: Option<String>, | ||
} | ||
let feed = repo::feed::FeedRepo::new(ctx) | ||
.create_feed(repo::feed::CreateFeedParams { | ||
title: parsed.title.map(|title| title.content), | ||
url: Some("https://blog.rust-lang.org/feed.xml".to_string()), | ||
published_at: parsed.published, | ||
updated_at: parsed.updated, | ||
}) | ||
.await?; | ||
|
||
let res = Response { title }; | ||
Ok(Json(json!({"data": res}))) | ||
Ok(Json(json!({ "data": feed }))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.