Skip to content

Commit

Permalink
feat: update GraphQL queries and models to include additional fields
Browse files Browse the repository at this point in the history
- Updated `get_anime.graphql` and `get_manga.graphql` to use `edges` instead of `nodes` for characters and include additional fields such as `description`, `gender`, and `role`.
- Added `isAdult` field to `search_anime.graphql` and `search_manga.graphql`.
- Modified `Client` implementation to handle the new `isAdult` field.
- Changed `Anime` and `Manga` models to use a non-optional `is_adult` field.
- Updated `Anime::characters` method to handle the new structure with `edges` and include character roles.
- Added `From<&str>` and `From<String>` implementations for `CharacterRole` enum in `character.rs`.
  • Loading branch information
AndrielFR committed Jan 12, 2025
1 parent 66bc9ba commit a22330a
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 44 deletions.
38 changes: 21 additions & 17 deletions queries/get_anime.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -92,24 +92,28 @@ query ($id: Int) {
}
}
characters(sort: RELEVANCE) {
nodes {
id
name {
first
middle
last
full
native
alternative
alternativeSpoiler
userPreferred
}
image {
large
medium
edges {
node {
id
name {
first
middle
last
full
native
alternative
alternativeSpoiler
userPreferred
}
image {
large
medium
}
description(asHtml: true)
gender
siteUrl
}
description(asHtml: true)
siteUrl
role
}
}
staff(sort: RELEVANCE) {
Expand Down
38 changes: 21 additions & 17 deletions queries/get_manga.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -89,24 +89,28 @@ query ($id: Int) {
}
}
characters(sort: RELEVANCE) {
nodes {
id
name {
first
middle
last
full
native
alternative
alternativeSpoiler
userPreferred
}
image {
large
medium
edges {
node {
id
name {
first
middle
last
full
native
alternative
alternativeSpoiler
userPreferred
}
image {
large
medium
}
description(asHtml: true)
gender
siteUrl
}
description(asHtml: true)
siteUrl
role
}
}
staff(sort: RELEVANCE) {
Expand Down
1 change: 1 addition & 0 deletions queries/search_anime.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ query($search: String, $page: Int = 1, $per_page: Int = 10) {
bannerImage
averageScore
meanScore
isAdult
siteUrl
}
}
Expand Down
1 change: 1 addition & 0 deletions queries/search_manga.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ query($search: String, $page: Int = 1, $per_page: Int = 10) {
bannerImage
averageScore
meanScore
isAdult
siteUrl
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ impl Client {
banner: media["bannerImage"].as_str().map(String::from),
average_score: media["averageScore"].as_u64().map(|x| x as u8),
mean_score: media["meanScore"].as_u64().map(|x| x as u8),
is_adult: media["isAdult"].as_bool().unwrap(),
url: media["siteUrl"].as_str().unwrap().to_string(),

client: self.clone(),
Expand Down Expand Up @@ -452,6 +453,7 @@ impl Client {
banner: media["bannerImage"].as_str().map(String::from),
average_score: media["averageScore"].as_u64().map(|x| x as u8),
mean_score: media["meanScore"].as_u64().map(|x| x as u8),
is_adult: media["isAdult"].as_bool().unwrap(),
url: media["siteUrl"].as_str().unwrap().to_string(),

client: self.clone(),
Expand Down
25 changes: 20 additions & 5 deletions src/models/anime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ pub struct Anime {
/// Whether the anime is favourite blocked or not.
pub is_favourite_blocked: Option<bool>,
/// Whether the anime is adult or not.
pub is_adult: Option<bool>,
pub is_adult: bool,
/// The next airing episode of the anime.
pub next_airing_episode: Option<AiringSchedule>,
/// The external links of the anime.
Expand Down Expand Up @@ -184,16 +184,31 @@ impl Anime {

/// Returns the characters of the anime.
pub fn characters(&self) -> Vec<Character> {
self.characters
let edges = self
.characters
.as_object()
.unwrap()
.get("nodes")
.get("edges")
.unwrap()
.as_array()
.unwrap()
.iter()
.map(|c| serde_json::from_value(c.clone()).unwrap())
.collect()
.map(|e| e.as_object().unwrap())
.collect::<Vec<_>>();

let mut characters = Vec::with_capacity(edges.len());

for edge in edges {
let node = edge.get("node").unwrap();
let role = edge.get("role").unwrap().as_str().unwrap();

let mut character = serde_json::from_value::<Character>(node.clone()).unwrap();
character.role = Some(role.into());

characters.push(character);
}

characters
}

/// Returns the relations of the anime.
Expand Down
16 changes: 16 additions & 0 deletions src/models/character.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,22 @@ pub enum CharacterRole {
Supporting,
}

impl From<&str> for CharacterRole {
fn from(value: &str) -> Self {
match value {
"MAIN" => CharacterRole::Main,
"SUPPORTING" => CharacterRole::Supporting,
_ => CharacterRole::Background,
}
}
}

impl From<String> for CharacterRole {
fn from(value: String) -> Self {
CharacterRole::from(value.as_str())
}
}

impl Display for CharacterRole {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Expand Down
25 changes: 20 additions & 5 deletions src/models/manga.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ pub struct Manga {
/// Whether the manga is blocked or not.
pub is_favourite_blocked: Option<bool>,
/// Whether the manga is adult or not.
pub is_adult: Option<bool>,
pub is_adult: bool,
/// The external links of the manga.
pub external_links: Option<Vec<Link>>,
/// The site URL of the manga.
Expand Down Expand Up @@ -170,16 +170,31 @@ impl Manga {

/// Returns the characters of the manga.
pub fn characters(&self) -> Vec<Character> {
self.characters
let edges = self
.characters
.as_object()
.unwrap()
.get("nodes")
.get("edges")
.unwrap()
.as_array()
.unwrap()
.iter()
.map(|c| serde_json::from_value(c.clone()).unwrap())
.collect()
.map(|e| e.as_object().unwrap())
.collect::<Vec<_>>();

let mut characters = Vec::with_capacity(edges.len());

for edge in edges {
let node = edge.get("node").unwrap();
let role = edge.get("role").unwrap().as_str().unwrap();

let mut character = serde_json::from_value::<Character>(node.clone()).unwrap();
character.role = Some(role.into());

characters.push(character);
}

characters
}

/// Returns the relations of the manga.
Expand Down

0 comments on commit a22330a

Please sign in to comment.