Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/Tenses Page #6

Merged
merged 6 commits into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
226 changes: 206 additions & 20 deletions conjugation-api/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ enum Tense {
Futuro,
PresentePerfecto,
Pluscuamperfecto,
Condicional,
FuturoPerfecto,
PreteritoAnterior,
CondicionalPerfecto,
}

impl Display for Tense {
Expand All @@ -26,6 +30,10 @@ impl Display for Tense {
Tense::Futuro => write!(f, "Futuro"),
Tense::PresentePerfecto => write!(f, "Presente perfecto"),
Tense::Pluscuamperfecto => write!(f, "Pluscuamperfecto"),
Tense::Condicional => write!(f, "Condicional"),
Tense::FuturoPerfecto => write!(f, "Futuro perfecto"),
Tense::PreteritoAnterior => write!(f, "Pretérito anterior"),
Tense::CondicionalPerfecto => write!(f, "Condicional perfecto"),
}
}
}
Expand All @@ -41,6 +49,43 @@ impl FromStr for Tense {
"Futuro" => Ok(Self::Futuro),
"Presente perfecto" => Ok(Self::PresentePerfecto),
"Pluscuamperfecto" => Ok(Self::Pluscuamperfecto),
"Condicional" => Ok(Self::Condicional),
"Futuro perfecto" => Ok(Self::FuturoPerfecto),
"Pretérito anterior" => Ok(Self::PreteritoAnterior),
"Condicional perfecto" => Ok(Self::CondicionalPerfecto),
_ => Err(()),
}
}
}

#[derive(Clone, juniper::GraphQLEnum)]
enum Mood {
Indicativo,
Subjuntivo,
ImperativoAfirmativo,
ImperativoNegativo,
}

impl Display for Mood {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Indicativo => write!(f, "Indicativo"),
Self::Subjuntivo => write!(f, "Subjuntivo"),
Self::ImperativoAfirmativo => write!(f, "Imperativo Afirmativo"),
Self::ImperativoNegativo => write!(f, "Imperativo Negativo"),
}
}
}

impl FromStr for Mood {
type Err = ();

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"Indicativo" => Ok(Self::Indicativo),
"Subjuntivo" => Ok(Self::Subjuntivo),
"Imperativo Afirmativo" => Ok(Self::ImperativoAfirmativo),
"Imperativo Negativo" => Ok(Self::ImperativoNegativo),
_ => Err(()),
}
}
Expand Down Expand Up @@ -77,18 +122,29 @@ impl Conjugation {
}

#[derive(Clone)]
struct ConjugatedVerb {
struct Verb {
infinitive: String,
infinitive_english: String,
gerundio: String,
gerundio_english: String,
tenses: Vec<VerbTense>,
}

#[derive(Clone)]
struct VerbTense {
infinitive: String,
verb_english: Option<String>,
tense: Tense,
mood: Mood,
conjugations: Vec<Conjugation>,
}

impl From<Verb> for ConjugatedVerb {
fn from(value: Verb) -> Self {
let Verb {
impl From<RepositoryConjugations> for VerbTense {
fn from(value: RepositoryConjugations) -> Self {
let RepositoryConjugations {
infinitive,
tense,
mood,
verb_english,
form_1s,
form_2s,
Expand All @@ -100,10 +156,12 @@ impl From<Verb> for ConjugatedVerb {

let mut conjugations = vec![];
if let Some(spanish) = form_1s {
conjugations.push(Conjugation {
pronoun: Pronoun::Yo,
spanish,
})
if !spanish.is_empty() {
conjugations.push(Conjugation {
pronoun: Pronoun::Yo,
spanish,
})
}
}
if let Some(spanish) = form_2s {
conjugations.push(Conjugation {
Expand All @@ -118,10 +176,12 @@ impl From<Verb> for ConjugatedVerb {
})
}
if let Some(spanish) = form_1p {
conjugations.push(Conjugation {
pronoun: Pronoun::Nosotros,
spanish,
})
if !spanish.is_empty() {
conjugations.push(Conjugation {
pronoun: Pronoun::Nosotros,
spanish,
})
}
}
if let Some(spanish) = form_2p {
conjugations.push(Conjugation {
Expand All @@ -140,15 +200,29 @@ impl From<Verb> for ConjugatedVerb {
infinitive,
verb_english,
tense: Tense::from_str(tense.as_str()).unwrap_or(Tense::Presente),
mood: Mood::from_str(mood.as_str()).unwrap_or(Mood::Indicativo),
conjugations,
}
}
}

#[derive(Clone, FromRow, Debug)]
struct Verb {
struct RepositoryInfinitive {
infinitive: String,
infinitive_english: String,
}

#[derive(Clone, FromRow, Debug)]
struct RepositoryGerund {
gerund: String,
gerund_english: String,
}

#[derive(Clone, FromRow, Debug)]
struct RepositoryConjugations {
infinitive: String,
tense: String,
mood: String,
verb_english: Option<String>,
form_1s: Option<String>,
form_2s: Option<String>,
Expand All @@ -159,8 +233,37 @@ struct Verb {
}

#[juniper::graphql_object]
#[graphql(description = "A Conjugated Verb")]
impl ConjugatedVerb {
#[graphql(description = "A Verb")]
impl Verb {
#[graphql(description = "Infinitive form of the verb")]
fn infinitive(&self) -> &str {
&self.infinitive
}

#[graphql(description = "English translation of the infinitive")]
fn infinitive_english(&self) -> &str {
&self.infinitive_english
}

#[graphql(description = "Gerundio")]
fn gerundio(&self) -> &str {
&self.gerundio
}

#[graphql(description = "English translation of the gerundio form")]
fn gerundio_english(&self) -> &str {
&self.gerundio_english
}

#[graphql(description = "Tenses")]
fn tenses(&self) -> &Vec<VerbTense> {
&self.tenses
}
}

#[juniper::graphql_object]
#[graphql(description = "A Verb Tense")]
impl VerbTense {
#[graphql(description = "Infinitive form of the verb")]
fn infinitive(&self) -> &str {
&self.infinitive
Expand All @@ -176,6 +279,34 @@ impl ConjugatedVerb {
&self.tense
}

#[graphql(description = "Mood the verb has been conjugated")]
fn mood(&self) -> &Mood {
&self.mood
}

#[graphql(description = "Title of the combined tense and mood")]
fn title(&self) -> &str {
match (&self.tense, &self.mood) {
(Tense::Presente, Mood::Indicativo) => "Presente de Indicativo",
(Tense::Imperfecto, Mood::Indicativo) => "Imperfecto de Indicativo",
(Tense::Preterito, Mood::Indicativo) => "Pretérito",
(Tense::Futuro, Mood::Indicativo) => "Futuro",
(Tense::Condicional, Mood::Indicativo) => "Potencial Simple",
(Tense::Presente, Mood::Subjuntivo) => "Presente de Subjuntivo",
(Tense::Imperfecto, Mood::Subjuntivo) => "Imperfecto de Subjuntivo",
(Tense::PresentePerfecto, Mood::Indicativo) => "Perfecto de Indicativo",
(Tense::Pluscuamperfecto, Mood::Indicativo) => "Pluscuamperfecto de Indicativo",
(Tense::PreteritoAnterior, Mood::Indicativo) => "Pretérito Anterior",
(Tense::FuturoPerfecto, Mood::Indicativo) => "Futuro Perfecto",
(Tense::CondicionalPerfecto, Mood::Indicativo) => "Potencial Compuesto",
(Tense::PresentePerfecto, Mood::Subjuntivo) => "Perfecto de Subjuntivo",
(Tense::Pluscuamperfecto, Mood::Subjuntivo) => "Pluscuamperfecto de Subjuntivo",
(Tense::Presente, Mood::ImperativoAfirmativo) => "Imperativo Afirmativo",
(Tense::Presente, Mood::ImperativoNegativo) => "Imperativo Negativo",
_ => "",
}
}

#[graphql(description = "First person singular")]
fn conjugations(&self) -> &Vec<Conjugation> {
&self.conjugations
Expand All @@ -193,13 +324,68 @@ pub struct QueryRoot;
#[juniper::graphql_object(context = State)]
impl QueryRoot {
#[graphql(description = "get a verb")]
async fn verb(
async fn verb(context: &State, infinitive: String) -> Option<Verb> {
let mut query_builder: QueryBuilder<Sqlite> = QueryBuilder::new(
"SELECT infinitive, infinitive_english FROM infinitive WHERE infinitive = ",
);
query_builder.push_bind(infinitive.clone());

let query = query_builder.build_query_as::<RepositoryInfinitive>();

let RepositoryInfinitive {
infinitive: inf,
infinitive_english,
} = query
.fetch_optional(&context.pool)
.await
.unwrap_or_default()?;

let mut query_builder: QueryBuilder<Sqlite> =
QueryBuilder::new("SELECT gerund, gerund_english FROM gerund WHERE infinitive = ");
query_builder.push_bind(infinitive.clone());

let query = query_builder.build_query_as::<RepositoryGerund>();

let RepositoryGerund {
gerund,
gerund_english,
} = query
.fetch_optional(&context.pool)
.await
.unwrap_or_default()?;

let mut query_builder: QueryBuilder<Sqlite> =
QueryBuilder::new("SELECT infinitive, tense, mood, verb_english, form_1s, form_2s, form_3s, form_1p, form_2p, form_3p FROM verbs WHERE NOT (mood = 'Subjuntivo' AND (tense = 'Futuro' OR tense = 'Futuro perfecto')) AND infinitive = ");

query_builder.push_bind(infinitive);

let query = query_builder.build_query_as::<RepositoryConjugations>();

let tenses: Vec<VerbTense> = query
.fetch_all(&context.pool)
.await
.unwrap_or_default()
.into_iter()
.map(VerbTense::from)
.collect();

Some(Verb {
infinitive: inf,
infinitive_english,
gerundio: gerund,
gerundio_english: gerund_english,
tenses,
})
}

#[graphql(description = "get a conjugated verb")]
async fn verb_tense(
context: &State,
infinitive: Option<String>,
tenses: Option<Vec<Tense>>,
) -> Option<ConjugatedVerb> {
) -> Option<VerbTense> {
let mut query_builder: QueryBuilder<Sqlite> =
QueryBuilder::new("SELECT infinitive, tense, verb_english, form_1s, form_2s, form_3s, form_1p, form_2p, form_3p FROM verbs WHERE mood = 'Indicativo'");
QueryBuilder::new("SELECT infinitive, tense, mood, verb_english, form_1s, form_2s, form_3s, form_1p, form_2p, form_3p FROM verbs WHERE mood = 'Indicativo'");

if let Some(infinitive) = infinitive {
query_builder.push(" AND infinitive = ");
Expand All @@ -226,13 +412,13 @@ impl QueryRoot {

query_builder.push(" ORDER BY RANDOM() LIMIT 1");

let query = query_builder.build_query_as::<Verb>();
let query = query_builder.build_query_as::<RepositoryConjugations>();

query
.fetch_optional(&context.pool)
.await
.unwrap_or_default()
.map(ConjugatedVerb::from)
.map(VerbTense::from)
}
}

Expand Down
32 changes: 30 additions & 2 deletions conjugation-ui/schema/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,39 @@ schema {
}
type QueryRoot {
"get a verb"
verb(infinitive: String, tenses: [Tense!]): ConjugatedVerb
verb(infinitive: String!): Verb
"get a conjugated verb"
verbTense(infinitive: String, tenses: [Tense!]): VerbTense
}
type Verb {
"Infinitive form of the verb"
infinitive: String!
"English translation of the infinitive"
infinitiveEnglish: String!
"Gerundio"
gerundio: String!
"English translation of the gerundio form"
gerundioEnglish: String!
"Tenses"
tenses: [VerbTense!]!
}
type Conjugation {
"Pronoun used for the conjugation"
pronoun: Pronoun!
"Conjugated verb in spanish"
spanish: String!
}
type ConjugatedVerb {
type VerbTense {
"Infinitive form of the verb"
infinitive: String!
"English form of the verb"
verbEnglish: String
"Tense the verb has been conjugated"
tense: Tense!
"Mood the verb has been conjugated"
mood: Mood!
"Title of the combined tense and mood"
title: String!
"First person singular"
conjugations: [Conjugation!]!
}
Expand All @@ -36,5 +54,15 @@ enum Tense {
FUTURO
PRESENTE_PERFECTO
PLUSCUAMPERFECTO
CONDICIONAL
FUTURO_PERFECTO
PRETERITO_ANTERIOR
CONDICIONAL_PERFECTO
}
enum Mood {
INDICATIVO
SUBJUNTIVO
IMPERATIVO_AFIRMATIVO
IMPERATIVO_NEGATIVO
}

Loading
Loading