Skip to content

Commit

Permalink
api in a working state
Browse files Browse the repository at this point in the history
  • Loading branch information
hubble459 committed Jan 18, 2023
1 parent 0141f7a commit 4b39880
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 2 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@
}
],
"discord.enabled": true,
"vscord.enabled": true,
}
82 changes: 82 additions & 0 deletions src/api/v1/route/chapter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
use crate::api::v1::data;
use crate::api::v1::data::paginate::Paginate;
use crate::api::v1::route::manga::MANGA_PARSER;
use actix_web::error::{ErrorInternalServerError, ErrorNotFound};
use actix_web::{web, Responder, Result};
use entity::chapter::{Column as ChapterColumn, Entity as chapter, Model as Chapter};
use manga_parser::parser::Parser;
use manga_parser::Url;
use sea_orm::{
ColumnTrait, DatabaseConnection, EntityTrait, PaginatorTrait, QueryFilter, QueryOrder,
};

#[get("")]
async fn index(
path: web::Path<i32>,
conn: web::Data<DatabaseConnection>,
query: web::Query<data::paginate::PaginateQuery>,
) -> Result<Paginate<Vec<Chapter>>> {
let db = conn.as_ref();
let manga_id = path.into_inner();

// Create paginate object
let paginate = chapter::find()
.filter(ChapterColumn::MangaId.eq(manga_id))
.order_by(ChapterColumn::Id, migration::Order::Asc)
.paginate(db, query.limit);

// Get max page
let amount = paginate
.num_items_and_pages()
.await
.map_err(ErrorInternalServerError)?;

// Get items from page
let items = paginate
.fetch_page(query.page)
.await
.map_err(ErrorInternalServerError)?;

Ok(Paginate {
total: amount.number_of_items,
max_page: amount.number_of_pages,
page: query.page,
limit: query.limit,
items,
})
}

#[get("/{chapter_id}")]
async fn get(
path: web::Path<(i32, i32)>,
conn: web::Data<DatabaseConnection>,
) -> Result<impl Responder> {
let (manga_id, chapter_id) = path.into_inner();
let db = conn.as_ref();

// Get chapter
let chap = chapter::find()
.filter(ChapterColumn::Id.eq(chapter_id))
.filter(ChapterColumn::MangaId.eq(manga_id))
.one(db)
.await
.map_err(ErrorInternalServerError)?
.ok_or(ErrorNotFound("Manga not found"))?;

// Get images
let images = MANGA_PARSER
.images(&Url::parse(&chap.url).unwrap())
.await
.map_err(ErrorInternalServerError)?;

Ok(web::Json(images))
}

pub fn routes() -> actix_web::Scope {
web::scope("/{manga_id}/chapter")
.service(index)
.service(get)
}

#[cfg(test)]
mod test {}
10 changes: 8 additions & 2 deletions src/api/v1/route/manga.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ use crate::api::v1::data::paginate::Paginate;
use crate::api::v1::util::search::manga::lucene_filter;
use crate::middleware::auth::AuthService;

use super::chapter;

lazy_static! {
pub static ref MANGA_PARSER: MangaParser = MangaParser::new();
}

pub const NEXT_UPDATE_QUERY: &str =
"(MAX(chapter.posted) + (MAX(chapter.posted) - MIN(chapter.posted)) / NULLIF(COUNT(*) - 1, 0))";

Expand Down Expand Up @@ -47,8 +53,7 @@ pub async fn save_manga(
) -> actix_web::Result<data::manga::Full> {
info!("Saving manga [{}]", url.to_string());

let manga_parser = MangaParser::new();
let m = manga_parser
let m = MANGA_PARSER
.manga(url)
.await
.map_err(ErrorInternalServerError)?;
Expand Down Expand Up @@ -283,6 +288,7 @@ pub fn routes() -> actix_web::Scope {
.service(store)
.service(get)
.service(delete)
.service(chapter::routes())
}

#[cfg(test)]
Expand Down
1 change: 1 addition & 0 deletions src/api/v1/route/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod user;
pub mod manga;
pub mod chapter;
pub mod reading;
1 change: 1 addition & 0 deletions src/middleware/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub struct AuthService {
pub user: User,
}

#[allow(dead_code)]
impl AuthService {
pub fn is_restricted(&self) -> bool {
self.user.permissions == 0
Expand Down

0 comments on commit 4b39880

Please sign in to comment.