Skip to content

Commit

Permalink
feat: implement Client::get_person
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrielFR committed Apr 2, 2022
1 parent 573add8 commit 84f2baf
Show file tree
Hide file tree
Showing 7 changed files with 314 additions and 32 deletions.
137 changes: 137 additions & 0 deletions queries/get_person.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
# SPDX-License-Identifier: MIT
# Copyright (c) 2022 Andriel Ferreira <https://github.com/AndrielFR>

query ($id: Int) {
Staff (id: $id) {
id
name {
first
middle
last
full
native
alternative
userPreferred
}
languageV2
image {
large
medium
}
description(asHtml: true)
primaryOccupations
gender
dateOfBirth {
year
month
day
}
dateOfDeath {
year
month
day
}
age
yearsActive
homeTown
bloodType
isFavourite
isFavouriteBlocked
siteUrl
staffMedia(sort: POPULARITY) {
nodes {
id
idMal
title {
romaji
english
native
userPreferred
}
type
format
status(version: 2)
description(asHtml: true)
coverImage {
extraLarge
large
medium
color
}
bannerImage
averageScore
meanScore
}
}
characters(sort: RELEVANCE) {
nodes {
id
name {
first
middle
last
full
native
alternative
alternativeSpoiler
userPreferred
}
image {
large
medium
}
description(asHtml: true)
siteUrl
}
}
characterMedia(sort: POPULARITY) {
edges {
node {
id
idMal
title {
romaji
english
native
userPreferred
}
type
format
status(version: 2)
description(asHtml: true)
coverImage {
extraLarge
large
medium
color
}
bannerImage
averageScore
meanScore
}
id
characters {
id
name {
first
middle
last
full
native
alternative
alternativeSpoiler
userPreferred
}
image {
large
medium
}
description(asHtml: true)
siteUrl
}
}
}
favourites
modNotes
}
}
13 changes: 12 additions & 1 deletion src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,21 @@ impl Client {
self.get_character(variables).await
}

pub async fn get_person(&self, id: i64) -> Result<crate::models::Person> {
let data = self
.request("person", "get", serde_json::json!({ "id": id }))
.await
.unwrap();
let mut person = crate::models::Person::parse(&data["data"]["Staff"]);
person.is_full_loaded = true;

Ok(person)
}

pub async fn mutate(
&self,
_media_type: &str,
_id: i32,
_id: i64,
_variables: serde_json::Value,
) -> Result<bool> {
todo!()
Expand Down
2 changes: 1 addition & 1 deletion src/models/anime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ impl Anime {
let mut staff: Vec<Person> = Vec::with_capacity(nodes.len());

for node in nodes {
staff.push(Person::parse(node, None));
staff.push(Person::parse(node));
}

anime.staff = Some(staff);
Expand Down
2 changes: 1 addition & 1 deletion src/models/manga.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ impl Manga {
let mut staff: Vec<Person> = Vec::with_capacity(nodes.len());

for node in nodes {
staff.push(Person::parse(node, None));
staff.push(Person::parse(node));
}

manga.staff = Some(staff);
Expand Down
184 changes: 155 additions & 29 deletions src/models/person.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,41 @@
// Copyright (c) 2022 Andriel Ferreira <https://github.com/AndrielFR>

use crate::models::Character;
use crate::models::Date;
use crate::models::Gender;
use crate::models::Image;
use crate::models::Language;
use crate::models::Name;
use crate::models::User;

use crate::Result;

#[derive(Debug, Default, Clone, PartialEq)]
pub struct Person {
id: i64,
name: Name,
language: Language,
image: Option<Image>,
description: Option<String>,
primary_occupations: Option<Vec<String>>,
gender: Gender,
date_of_birth: Option<i64>,
date_of_death: Option<i64>,
age: Option<i64>,
years_active: Option<(i64, i64)>,
home_town: Option<String>,
blood_type: Option<String>,
is_favourite: Option<bool>,
is_favourite_blocked: Option<bool>,
url: String,
characters: Option<Vec<Character>>,
submitter: Option<User>,
submitter_status: Option<i64>,
submitter_notes: Option<String>,
favourites: i64,
mod_notes: Option<String>,
pub id: i64,
pub name: Name,
pub language: Language,
pub image: Option<Image>,
pub description: Option<String>,
pub primary_occupations: Option<Vec<String>>,
pub gender: Gender,
pub date_of_birth: Option<Date>,
pub date_of_death: Option<Date>,
pub age: Option<i64>,
pub years_active: Option<(u64, u64)>,
pub home_town: Option<String>,
pub blood_type: Option<String>,
pub is_favourite: Option<bool>,
pub is_favourite_blocked: Option<bool>,
pub url: String,
pub characters: Option<Vec<Character>>,
pub favourites: i64,
pub mod_notes: Option<String>,
pub(crate) is_full_loaded: bool,
}

impl Person {
pub(crate) fn parse(data: &serde_json::Value, person: Option<Person>) -> Self {
let mut person = match person {
Some(person) => person,
None => Person::default(),
};
pub(crate) fn parse(data: &serde_json::Value) -> Self {
let mut person = Person::default();

person.id = data["id"].as_i64().unwrap();

Expand Down Expand Up @@ -81,14 +76,145 @@ impl Person {
person.name = name;
}

if let Some(language) = data["languageV2"].as_str() {
person.language = match language.to_uppercase().as_str() {
"ENGLISH" => Language::English,
"KOREAN" => Language::Korean,
"ITALIAN" => Language::Italian,
"SPANISH" => Language::Spanish,
"PORTUGUESE" => Language::Portuguese,
"FRENCH" => Language::French,
"GERMAN" => Language::German,
"HEBREW" => Language::Hebrew,
"HUNGARIAN" => Language::Hungarian,
"CHINESE" => Language::Chinese,
"ARABIC" => Language::Arabic,
"FILIPINO" => Language::Filipino,
"CATALAN" => Language::Catalan,
"FINNISH" => Language::Finnish,
"TURKISH" => Language::Turkish,
"DUTCH" => Language::Dutch,
"SWEDISH" => Language::Swedish,
"THAI" => Language::Thai,
"TAGALOG" => Language::Tagalog,
"MALAYSIAN" => Language::Malaysian,
"INDONESIAN" => Language::Indonesian,
"VIETNAMESE" => Language::Vietnamese,
"NEPALI" => Language::Nepali,
"HINDI" => Language::Hindi,
"URDU" => Language::Urdu,
_ => Language::default(),
};
}

if let Some(image_object) = data["image"].as_object() {
person.image = Some(Image {
large: image_object["large"].as_str().unwrap().to_string(),
medium: image_object["medium"].as_str().unwrap().to_string(),
});
}

if let Some(date_of_birth) = data["dateOfBirth"].as_object() {
let mut date = Date::default();

if let Some(year) = date_of_birth["year"].as_i64() {
date.year = Some(year);
}
if let Some(month) = date_of_birth["month"].as_i64() {
date.month = Some(month);
}
if let Some(day) = date_of_birth["day"].as_i64() {
date.day = Some(day);
}

person.date_of_birth = Some(date);
}

if let Some(date_of_death) = data["dateOfDeath"].as_object() {
let mut date = Date::default();

if let Some(year) = date_of_death["year"].as_i64() {
date.year = Some(year);
}
if let Some(month) = date_of_death["month"].as_i64() {
date.month = Some(month);
}
if let Some(day) = date_of_death["day"].as_i64() {
date.day = Some(day);
}

person.date_of_death = Some(date);
}

if let Some(age) = data["age"].as_i64() {
person.age = Some(age);
}

if let Some(years_active) = data["yearsActive"].as_array() {
person.years_active = match years_active.len() {
2 => Some((
years_active[0].as_u64().unwrap(),
years_active[1].as_u64().unwrap(),
)),
1 => Some((years_active[0].as_u64().unwrap(), 0)),
_ => None,
};
}

if let Some(home_town) = data["homeTown"].as_str() {
person.home_town = Some(home_town.to_string());
}

if let Some(blood_type) = data["bloodType"].as_str() {
person.blood_type = Some(blood_type.to_string());
}

if let Some(is_favourite) = data["isFavourite"].as_bool() {
person.is_favourite = Some(is_favourite);
}

if let Some(is_favourite_blocked) = data["isFavouriteBlocked"].as_bool() {
person.is_favourite_blocked = Some(is_favourite_blocked);
}

person.url = data["siteUrl"].as_str().unwrap().to_string();

if let Some(characters) = data["characters"].as_object() {
if let Some(nodes) = characters["nodes"].as_array() {
let mut characters: Vec<Character> = Vec::with_capacity(nodes.len());

for node in nodes {
characters.push(Character::parse(node));
}

person.characters = Some(characters);
}
}

person.favourites = data["favourites"].as_i64().unwrap();

if let Some(mod_notes) = data["modNotes"].as_str() {
person.mod_notes = Some(mod_notes.to_string());
}

person
}

pub async fn load_full(self) -> crate::Result<Self> {
if !self.is_full_loaded {
let mut person = crate::Client::new().get_person(self.id).await.unwrap();
person.is_full_loaded = true;
Ok(person)
} else {
panic!("This person is already full loaded")
}
}

pub async fn get_medias<T>() -> Result<T> {
todo!()
}

pub async fn get_character_medias<T>() -> Result<T> {
pub async fn get_character_medias<T>(_character_id: i64) -> Result<T> {
todo!()
}
}
Loading

0 comments on commit 84f2baf

Please sign in to comment.