-
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
12 changed files
with
587 additions
and
684 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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,93 @@ | ||
use axum::{ | ||
extract::{Path, State}, | ||
http::StatusCode, | ||
Json, | ||
}; | ||
use collie::{ | ||
model::feed::{self, Feed, FeedToCreate, FeedToUpdate}, | ||
producer::{ | ||
syndication::{fetch_content, fetch_feed_title, find_feed_link, Feed as SyndicationFeed}, | ||
worker::create_new_items, | ||
}, | ||
}; | ||
|
||
use crate::serve::AppState; | ||
|
||
pub async fn create( | ||
State(AppState { conn, config }): State<AppState>, | ||
Json(arg): Json<FeedToCreate>, | ||
) -> (StatusCode, Json<bool>) { | ||
if arg.link.is_empty() { | ||
return (StatusCode::BAD_REQUEST, Json(false)); | ||
} | ||
|
||
let proxy = config.producer.proxy; | ||
let html_content = fetch_content(&arg.link, proxy.as_deref()).unwrap(); | ||
let is_feed = html_content.parse::<SyndicationFeed>().is_ok(); | ||
|
||
let link = if is_feed { | ||
arg.link.clone() | ||
} else if let Some(rss_link) = find_feed_link(&html_content).unwrap() { | ||
rss_link | ||
} else { | ||
return (StatusCode::BAD_REQUEST, Json(false)); | ||
}; | ||
|
||
let title = match fetch_feed_title(&link, proxy.as_deref()) { | ||
Ok(title) => title, | ||
Err(_) => return (StatusCode::INTERNAL_SERVER_ERROR, Json(false)), | ||
}; | ||
|
||
let arg = FeedToCreate { | ||
title, | ||
link, | ||
fetch_old_items: arg.fetch_old_items, | ||
}; | ||
|
||
match feed::create(&conn, &arg) { | ||
Ok(_) => { | ||
let _ = create_new_items(&conn, proxy.as_deref()); | ||
(StatusCode::OK, Json(true)) | ||
} | ||
Err(_) => (StatusCode::INTERNAL_SERVER_ERROR, Json(false)), | ||
} | ||
} | ||
|
||
pub async fn read_all( | ||
State(AppState { conn, .. }): State<AppState>, | ||
) -> (StatusCode, Json<Vec<Feed>>) { | ||
match feed::read_all(&conn) { | ||
Ok(feeds) => (StatusCode::OK, Json(feeds)), | ||
Err(_) => (StatusCode::INTERNAL_SERVER_ERROR, Json(vec![])), | ||
} | ||
} | ||
|
||
pub async fn read( | ||
State(AppState { conn, .. }): State<AppState>, | ||
Path(id): Path<i32>, | ||
) -> (StatusCode, Json<Option<Feed>>) { | ||
match feed::read(&conn, id) { | ||
Ok(feed) => (StatusCode::OK, Json(feed)), | ||
Err(_) => (StatusCode::INTERNAL_SERVER_ERROR, Json(None)), | ||
} | ||
} | ||
|
||
pub async fn update( | ||
State(AppState { conn, .. }): State<AppState>, | ||
Json(arg): Json<FeedToUpdate>, | ||
) -> (StatusCode, Json<bool>) { | ||
match feed::update(&conn, &arg) { | ||
Ok(_) => (StatusCode::OK, Json(true)), | ||
Err(_) => (StatusCode::INTERNAL_SERVER_ERROR, Json(false)), | ||
} | ||
} | ||
|
||
pub async fn delete( | ||
State(AppState { conn, .. }): State<AppState>, | ||
Path(id): Path<i32>, | ||
) -> (StatusCode, Json<bool>) { | ||
match feed::delete(&conn, id) { | ||
Ok(_) => (StatusCode::OK, Json(true)), | ||
Err(_) => (StatusCode::INTERNAL_SERVER_ERROR, Json(false)), | ||
} | ||
} |
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,80 @@ | ||
use axum::{extract::State, http::StatusCode, Json}; | ||
use collie::model::item::{ | ||
self, Item, ItemReadOption, ItemToCreate, ItemToUpdate, ItemToUpdateAll, | ||
}; | ||
|
||
use crate::serve::AppState; | ||
|
||
pub async fn create( | ||
State(AppState { conn, .. }): State<AppState>, | ||
Json(arg): Json<ItemToCreate>, | ||
) -> (StatusCode, Json<bool>) { | ||
match item::create(&conn, &arg) { | ||
Ok(count) => { | ||
if count > 0 { | ||
(StatusCode::OK, Json(true)) | ||
} else { | ||
(StatusCode::INTERNAL_SERVER_ERROR, Json(false)) | ||
} | ||
} | ||
Err(_) => (StatusCode::INTERNAL_SERVER_ERROR, Json(false)), | ||
} | ||
} | ||
|
||
pub async fn read_all( | ||
State(AppState { conn, .. }): State<AppState>, | ||
Json(arg): Json<ItemReadOption>, | ||
) -> (StatusCode, Json<Vec<Item>>) { | ||
match item::read_all(&conn, &arg) { | ||
Ok(items) => (StatusCode::OK, Json(items)), | ||
Err(_) => (StatusCode::INTERNAL_SERVER_ERROR, Json(vec![])), | ||
} | ||
} | ||
|
||
pub async fn count_all( | ||
State(AppState { conn, .. }): State<AppState>, | ||
Json(arg): Json<ItemReadOption>, | ||
) -> (StatusCode, Json<i64>) { | ||
match item::count_all(&conn, &arg) { | ||
Ok(count) => { | ||
if count > 0 { | ||
(StatusCode::OK, Json(count)) | ||
} else { | ||
(StatusCode::INTERNAL_SERVER_ERROR, Json(0)) | ||
} | ||
} | ||
Err(_) => (StatusCode::INTERNAL_SERVER_ERROR, Json(0)), | ||
} | ||
} | ||
|
||
pub async fn update( | ||
State(AppState { conn, .. }): State<AppState>, | ||
Json(arg): Json<ItemToUpdate>, | ||
) -> (StatusCode, Json<bool>) { | ||
match item::update(&conn, &arg) { | ||
Ok(count) => { | ||
if count > 0 { | ||
(StatusCode::OK, Json(true)) | ||
} else { | ||
(StatusCode::INTERNAL_SERVER_ERROR, Json(false)) | ||
} | ||
} | ||
Err(_) => (StatusCode::INTERNAL_SERVER_ERROR, Json(false)), | ||
} | ||
} | ||
|
||
pub async fn update_all( | ||
State(AppState { conn, .. }): State<AppState>, | ||
Json(arg): Json<ItemToUpdateAll>, | ||
) -> (StatusCode, Json<bool>) { | ||
match item::update_all(&conn, &arg) { | ||
Ok(count) => { | ||
if count > 0 { | ||
(StatusCode::OK, Json(true)) | ||
} else { | ||
(StatusCode::INTERNAL_SERVER_ERROR, Json(false)) | ||
} | ||
} | ||
Err(_) => (StatusCode::INTERNAL_SERVER_ERROR, Json(false)), | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
This file was deleted.
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
Oops, something went wrong.