Skip to content

Commit

Permalink
feat: add work-in-progress translation suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
simongoricar committed Feb 20, 2024
1 parent 8e5c0b4 commit 810808f
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 1 deletion.
8 changes: 7 additions & 1 deletion kolomoni/src/api/v1/dictionary.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
use actix_web::{web, Scope};

use self::{english_word::english_dictionary_router, slovene_word::slovene_dictionary_router};
use self::{
english_word::english_dictionary_router,
slovene_word::slovene_dictionary_router,
suggestions::suggested_translations_router,
};

pub mod english_word;
pub mod slovene_word;
pub mod suggestions;


#[rustfmt::skip]
pub fn dictionary_router() -> Scope {
web::scope("/dictionary")
.service(slovene_dictionary_router())
.service(english_dictionary_router())
.service(suggested_translations_router())
}
45 changes: 45 additions & 0 deletions kolomoni/src/api/v1/dictionary/suggestions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use actix_web::{post, web, Scope};
use kolomoni_auth::Permission;
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;

use crate::{
api::errors::EndpointResult,
authentication::UserAuthenticationExtractor,
require_authentication,
require_permission,
state::ApplicationState,
};


#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, ToSchema)]
pub struct TranslationSuggestionRequest {
english_word_id: String,
slovene_word_id: String,
}

#[post("")]
pub async fn suggest_a_translation(
state: ApplicationState,
authentication: UserAuthenticationExtractor,
request_body: web::Json<TranslationSuggestionRequest>,
) -> EndpointResult {
let authenticated_user = require_authentication!(authentication);
require_permission!(
state,
authenticated_user,
Permission::SuggestionCreate
);

// TODO Continue from here.


todo!();
}


#[rustfmt::skip]
pub fn suggested_translations_router() -> Scope {
web::scope("/suggestion")
.service(suggest_a_translation)
}
2 changes: 2 additions & 0 deletions kolomoni_database/src/mutation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ mod user;
mod user_role;
mod word_english;
mod word_slovene;
mod word_translation_suggestion;

pub use user::*;
pub use user_role::*;
pub use word_english::*;
pub use word_slovene::*;
pub use word_translation_suggestion::*;
37 changes: 37 additions & 0 deletions kolomoni_database/src/mutation/word_translation_suggestion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use chrono::Utc;
use miette::{Context, IntoDiagnostic, Result};
use sea_orm::{ActiveModelTrait, ActiveValue, ConnectionTrait, TransactionTrait};
use uuid::Uuid;

use crate::entities::word_translation_suggestion;



pub struct NewTranslationSuggestion {
pub english_word_id: Uuid,
pub slovene_word_id: Uuid,
}


pub struct TranslationSuggestionMutation;

impl TranslationSuggestionMutation {
pub async fn create<C: ConnectionTrait + TransactionTrait>(
database: &C,
new_translation_suggestion: NewTranslationSuggestion,
) -> Result<word_translation_suggestion::Model> {
let active_suggestion = word_translation_suggestion::ActiveModel {
english_word_id: ActiveValue::Set(new_translation_suggestion.english_word_id),
slovene_word_id: ActiveValue::Set(new_translation_suggestion.slovene_word_id),
suggested_at: ActiveValue::Set(Utc::now().fixed_offset()),
};

let new_suggestion_model = active_suggestion
.insert(database)
.await
.into_diagnostic()
.wrap_err("Failed while inserting new translation suggestion into the database.")?;

Ok(new_suggestion_model)
}
}

0 comments on commit 810808f

Please sign in to comment.