-
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
817 additions
and
17 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use crate::database::Database; | ||
use crate::dto::external_id::{ExternalIdFilter, ExternalIdResponse, NewExternalId}; | ||
use crate::dto::page::{Page, Pagination}; | ||
use crate::error_handling::{ApiError, ApiPageResult, ApiRawResult}; | ||
use crate::guards::MatcherAuthGuard; | ||
use crate::services; | ||
use rocket::response::status; | ||
use rocket::serde::uuid::Uuid; | ||
use rocket::{post, serde::json::Json}; | ||
use rocket_okapi::okapi::openapi3::OpenApi; | ||
use rocket_okapi::settings::OpenApiSettings; | ||
use rocket_okapi::{openapi, openapi_get_routes_spec}; | ||
|
||
pub fn get_routes_and_docs(settings: &OpenApiSettings) -> (Vec<rocket::Route>, OpenApi) { | ||
openapi_get_routes_spec![settings: new_external_id, get_external_ids] | ||
} | ||
/// Create a new extra | ||
#[openapi(tag = "External Id")] | ||
#[post("/", format = "json", data = "<data>")] | ||
async fn new_external_id( | ||
db: Database<'_>, | ||
data: Json<NewExternalId>, | ||
_matcher: MatcherAuthGuard, | ||
) -> ApiRawResult<status::Created<Json<ExternalIdResponse>>> { | ||
if data.package_id.is_some() && data.artist_id.is_some() | ||
|| (data.package_id.is_none() && data.artist_id.is_none()) | ||
{ | ||
return Err(ApiError::ValidationError( | ||
"There should be exactly one parent ID set.".to_owned(), | ||
)); | ||
} | ||
if let Some(rating) = data.rating { | ||
if rating < 0 || rating > 100 { | ||
return Err(ApiError::ValidationError( | ||
"Rating should be between 0 and 100.".to_owned(), | ||
)); | ||
} | ||
} | ||
services::external_ids::create(&data.0, db.into_inner()) | ||
.await | ||
.map_or_else( | ||
|e| Err(ApiError::from(e)), | ||
|v| Ok(status::Created::new("").body(Json(v.into()))), | ||
) | ||
} | ||
|
||
/// Get many External IDs | ||
#[openapi(tag = "Extras")] | ||
#[get("/?<artist>&<package>&<pagination..>")] | ||
async fn get_external_ids( | ||
db: Database<'_>, | ||
artist: Option<Uuid>, | ||
package: Option<Uuid>, | ||
pagination: Pagination, | ||
) -> ApiPageResult<ExternalIdResponse> { | ||
if artist.is_some() && package.is_some() { | ||
return Err(ApiError::ValidationError( | ||
"There should be exactly one parent ID set.".to_owned(), | ||
)); | ||
} | ||
services::external_ids::find_many( | ||
&ExternalIdFilter { artist, package }, | ||
&pagination, | ||
db.into_inner(), | ||
) | ||
.await | ||
.map(|items| Page::from(items)) | ||
.map_or_else(|e| Err(ApiError::from(e)), |v| Ok(v)) | ||
} |
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,5 +1,6 @@ | ||
pub mod artists; | ||
pub mod chapters; | ||
pub mod external_ids; | ||
pub mod extras; | ||
pub mod files; | ||
pub mod images; | ||
|
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,62 @@ | ||
use crate::swagger_examples::*; | ||
use entity::external_id; | ||
use rocket::serde::uuid::Uuid; | ||
use rocket_okapi::okapi::schemars; | ||
use rocket_okapi::okapi::schemars::JsonSchema; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
/// An External ID data type | ||
#[derive(Serialize, JsonSchema, FromForm)] | ||
pub struct ExternalIdResponse { | ||
#[schemars(example = "example_uuid")] | ||
pub id: Uuid, | ||
pub url: String, | ||
pub value: String, | ||
#[schemars(example = "example_description")] | ||
pub description: Option<String>, | ||
#[schemars(example = "example_rating")] | ||
pub rating: Option<i16>, | ||
#[schemars(example = "example_provider_name")] | ||
pub provider_name: String, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub artist_id: Option<Uuid>, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub package_id: Option<Uuid>, | ||
} | ||
|
||
impl From<external_id::Model> for ExternalIdResponse { | ||
fn from(value: external_id::Model) -> Self { | ||
ExternalIdResponse { | ||
id: value.id, | ||
url: value.url, | ||
value: value.value, | ||
provider_name: value.provider_name, | ||
description: value.description, | ||
rating: value.rating, | ||
artist_id: value.artist_id, | ||
package_id: value.package_id, | ||
} | ||
} | ||
} | ||
|
||
/// DTO to create a new ExternalId | ||
#[derive(Deserialize, Serialize, JsonSchema, Clone)] | ||
#[serde(crate = "rocket::serde")] | ||
pub struct NewExternalId { | ||
pub url: String, | ||
pub value: String, | ||
#[schemars(example = "example_description")] | ||
pub description: Option<String>, | ||
#[schemars(example = "example_rating")] | ||
pub rating: Option<i16>, | ||
#[schemars(example = "example_provider_name")] | ||
pub provider_name: String, | ||
pub artist_id: Option<Uuid>, | ||
pub package_id: Option<Uuid>, | ||
} | ||
|
||
/// Filters for External IDs | ||
pub struct ExternalIdFilter { | ||
pub artist: Option<Uuid>, | ||
pub package: Option<Uuid>, | ||
} |
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,5 +1,6 @@ | ||
pub mod artist; | ||
pub mod chapter; | ||
pub mod external_id; | ||
pub mod extra; | ||
pub mod file; | ||
pub mod image; | ||
|
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use entity::external_id; | ||
use sea_orm::{ | ||
ColumnTrait, ConnectionTrait, DbErr, EntityTrait, QueryFilter, QuerySelect, QueryTrait, Set, | ||
}; | ||
|
||
use crate::dto::{ | ||
external_id::{ExternalIdFilter, ExternalIdResponse, NewExternalId}, | ||
page::Pagination, | ||
}; | ||
|
||
use super::{artist, package}; | ||
|
||
pub async fn create<'s, 'a, C>( | ||
dto: &NewExternalId, | ||
connection: &'a C, | ||
) -> Result<external_id::Model, DbErr> | ||
where | ||
C: ConnectionTrait, | ||
{ | ||
if let Some(artist_id) = dto.artist_id { | ||
artist::find(&artist_id.to_string(), connection).await?; | ||
} else if let Some(package_id) = dto.package_id { | ||
package::find(&package_id.to_string(), connection).await?; | ||
} | ||
external_id::Entity::insert(external_id::ActiveModel { | ||
description: Set(dto.description.clone()), | ||
rating: Set(dto.rating), | ||
provider_name: Set(dto.provider_name.clone()), | ||
value: Set(dto.value.clone()), | ||
url: Set(dto.url.clone()), | ||
package_id: Set(dto.package_id), | ||
artist_id: Set(dto.artist_id), | ||
..Default::default() | ||
}) | ||
.exec_with_returning(connection) | ||
.await | ||
} | ||
|
||
pub async fn find_many<'a, C>( | ||
filters: &ExternalIdFilter, | ||
pagination: &Pagination, | ||
connection: &'a C, | ||
) -> Result<Vec<ExternalIdResponse>, DbErr> | ||
where | ||
C: ConnectionTrait, | ||
{ | ||
let query = external_id::Entity::find() | ||
.apply_if(filters.artist, |q, artist_uuid| { | ||
q.filter(external_id::Column::ArtistId.eq(artist_uuid)) | ||
}) | ||
.apply_if(filters.package, |q, package_uuid| { | ||
q.filter(external_id::Column::PackageId.eq(package_uuid)) | ||
}) | ||
.offset(pagination.skip) | ||
.limit(pagination.take); | ||
|
||
query | ||
.all(connection) | ||
.await | ||
.map(|items| items.into_iter().map(|item| item.into()).collect()) | ||
} |
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,5 +1,6 @@ | ||
pub mod artist; | ||
pub mod chapter; | ||
pub mod external_ids; | ||
pub mod extra; | ||
pub mod file; | ||
pub mod housekeeping; | ||
|
Oops, something went wrong.