Implement more mutations

Implement mutations:
- create language
- delete language
- user follows a language
- user unfollows a language
- new word

Context's user_auth now contains either the user's ID or nothing if
not authentificated.
This commit is contained in:
2023-01-24 01:05:18 +01:00
parent 51f0fc3108
commit a624636939
4 changed files with 423 additions and 16 deletions

View File

@@ -1,6 +1,15 @@
use juniper::FieldResult;
use std::str::FromStr;
use crate::db::{models::users::User, DatabaseError};
use juniper::FieldResult;
use uuid::Uuid;
use crate::db::{
models::{
languages::{Language, NewLanguage, UserFollowLanguage},
users::User,
},
DatabaseError,
};
use super::Context;
@@ -9,7 +18,7 @@ pub struct Mutation;
#[juniper::graphql_object(Context = Context)]
impl Mutation {
fn api_version(context: &Context) -> String {
if context.user_auth {
if context.user_auth.is_some() {
"0.1 (authentified)"
} else {
"0.1 (not authentified)"
@@ -49,4 +58,103 @@ impl Mutation {
.into())
}
}
pub fn user_follow_language(
context: &Context,
language: String,
) -> FieldResult<Language> {
if let Some(userid) = &context.user_auth {
match Uuid::from_str(&language) {
Err(e) => Err(DatabaseError::new(
format!(
"Could not parse {language} as a valid UUID: {e:?}"
),
"Bad Request",
)
.into()),
Ok(lang) => UserFollowLanguage::user_follow_language(
context,
&userid.to_string(),
lang,
)
.map_err(Into::into),
}
} else {
Err(DatabaseError::new(
"User not authentificated, cannot proceed",
"Unauthorized",
)
.into())
}
}
pub fn user_unfollow_language(
context: &Context,
language: String,
) -> FieldResult<Language> {
if let Some(userid) = &context.user_auth {
match Uuid::from_str(&language) {
Err(e) => Err(DatabaseError::new(
format!(
"Could not parse {language} as a valid UUID: {e:?}"
),
"Bad Request",
)
.into()),
Ok(lang) => UserFollowLanguage::user_unfollow_language(
context,
&userid.to_string(),
lang,
)
.map_err(Into::into),
}
} else {
Err(DatabaseError::new(
"User not authentificated, cannot proceed",
"Unauthorized",
)
.into())
}
}
pub fn new_language(
context: &Context,
language: NewLanguage,
) -> FieldResult<Language> {
if let Some(owner) = &context.user_auth {
language.insert_db(&context.db, owner).map_err(Into::into)
} else {
Err(DatabaseError::new(
"User not authentificated, cannot create new language",
"Unauthorized",
)
.into())
}
}
pub fn delete_language(
context: &Context,
language: String,
) -> FieldResult<Option<Language>> {
if context.user_auth.is_some() {
match Uuid::from_str(&language) {
Ok(uuid) => Language::delete(context, uuid)
.map(|_| None)
.map_err(Into::into),
Err(e) => Err(DatabaseError::new(
format!(
"Could not parse {language} as a valid UUID: {e:?}"
),
"Bad Request",
)
.into()),
}
} else {
Err(DatabaseError::new(
"User not authentificated, cannot create new language",
"Unauthorized",
)
.into())
}
}
}