Skip to content

Commit 187711e

Browse files
committed
setup
1 parent fafeef1 commit 187711e

14 files changed

Lines changed: 172 additions & 9 deletions

File tree

assets/js/app.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

resources/js/misc/SetupPage.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export function setupPage() {
1818
this.submitting = true;
1919
this.errorMessage = '';
2020

21-
const res = await http.post('/api/setup', {
21+
const res = await http.post('/api/misc/setup', {
2222
name: this.name,
2323
email: this.email,
2424
password: this.password,
@@ -28,7 +28,7 @@ export function setupPage() {
2828
this.submitting = false;
2929

3030
if (res.ok) {
31-
window.location.href = '/admin/login';
31+
window.location.href = '/auth/login';
3232
} else {
3333
this.errorMessage = res.error || 'Setup failed';
3434
}

src/api/rest_api_routes.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ pub fn rest_api_routes(state: AppState) -> crate::error::Result<Router> {
1515
"/api/auth/login",
1616
axum::routing::post(crate::interfaces::api::auth::login_handler),
1717
)
18+
.route(
19+
"/api/misc/setup",
20+
axum::routing::post(crate::interfaces::api::misc::setup_handler::setup_handler),
21+
)
1822
.leptos_routes_with_context(
1923
&state,
2024
routes,

src/avored_state.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ use axum::extract::FromRef;
44
use leptos::config::LeptosOptions;
55

66
use crate::{
7-
core::application::use_cases::AuthUseCase,
8-
infrastructure::persistence::AuthRepositoryImpl,
9-
providers::{
7+
core::application::use_cases::{AuthUseCase, MiscUseCase}, infrastructure::persistence::{AuthRepositoryImpl, misc_repository::MiscRepositoryImpl}, providers::{
108
avored_config_provider::AvoRedConfigProvider,
119
avored_database_provider::AvoRedDatabaseProvider,
1210
},
@@ -19,6 +17,9 @@ pub struct AppState {
1917
/// Auth use case with SurrealDB persistence repository
2018
pub auth_use_case: AuthUseCase<AuthRepositoryImpl>,
2119

20+
21+
pub misc_use_case: MiscUseCase<MiscRepositoryImpl>,
22+
2223
/// Database provider for `AvoRed` (SurrealDB).
2324
pub database_provider: Arc<AvoRedDatabaseProvider>,
2425

@@ -41,11 +42,16 @@ impl AppState {
4142
let auth_repository = AuthRepositoryImpl::new(avored_database_provider.clone());
4243
let auth_use_case = AuthUseCase::new(auth_repository);
4344

45+
let misc_repository = MiscRepositoryImpl::new(avored_database_provider.clone());
46+
let misc_use_case = MiscUseCase::new(misc_repository);
47+
48+
4449
Ok(Self {
4550
leptos_options,
46-
auth_use_case,
4751
database_provider: avored_database_provider,
4852
config: Arc::new(config),
53+
auth_use_case,
54+
misc_use_case
4955
})
5056
}
5157
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use crate::core::domain::{entities::{User, user::StorableUser}, repositories::MiscRepository};
2+
3+
#[derive(Clone)]
4+
pub struct MiscUseCase<R>
5+
where
6+
R: MiscRepository,
7+
{
8+
repository: R,
9+
}
10+
11+
impl<R> MiscUseCase<R>
12+
where
13+
R: MiscRepository,
14+
{
15+
pub fn new(repository: R) -> Self {
16+
Self { repository }
17+
}
18+
19+
pub async fn setup(&self, storable_user: StorableUser) -> crate::error::Result<User> {
20+
println!("->> {:<12} - setup", "MISC_USE_CASE");
21+
22+
self.repository.create_user(storable_user).await
23+
}
24+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
pub mod auth_use_case;
22

33
pub use auth_use_case::AuthUseCase;
4+
5+
6+
pub mod misc_use_case;
7+
8+
pub use misc_use_case::MiscUseCase;

src/core/domain/entities/user.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ pub struct User {
99
}
1010

1111

12+
13+
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
14+
pub struct StorableUser {
15+
pub name: String,
16+
pub email: String,
17+
pub password: String
18+
}
19+
20+
21+
1222
#[cfg(feature = "ssr")]
1323
impl TryFrom<surrealdb::types::Object> for User {
1424
type Error = crate::error::Error;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
use crate::core::domain::entities::User;
2+
use crate::core::domain::entities::user::StorableUser;
3+
use crate::error::Result;
4+
5+
#[async_trait::async_trait]
6+
pub trait MiscRepository: Send + Sync {
7+
async fn create_user(&self, storable_user: StorableUser) -> Result<User>;
8+
}
9+

src/core/domain/repositories/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@
33
pub mod auth_repository;
44

55
pub use auth_repository::AuthRepository;
6+
7+
pub mod misc_repository;
8+
9+
pub use misc_repository::MiscRepository;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use surrealdb::types::Value;
2+
3+
use crate::core::domain::entities::User;
4+
use crate::core::domain::entities::user::StorableUser;
5+
use crate::core::domain::repositories::MiscRepository;
6+
use crate::error::Result;
7+
use crate::infrastructure::persistence::into_iter_objects;
8+
use crate::providers::avored_database_provider::AvoRedDatabaseProvider;
9+
use std::collections::BTreeMap;
10+
use std::sync::Arc;
11+
12+
#[derive(Clone)]
13+
pub struct MiscRepositoryImpl {
14+
pub database_provider: Arc<AvoRedDatabaseProvider>,
15+
}
16+
17+
impl MiscRepositoryImpl {
18+
pub fn new(database_provider: Arc<AvoRedDatabaseProvider>) -> Self {
19+
Self { database_provider }
20+
}
21+
}
22+
23+
#[async_trait::async_trait]
24+
impl MiscRepository for MiscRepositoryImpl {
25+
async fn create_user(&self, storable_user: StorableUser) -> Result<User> {
26+
let (datastore, database_session) = &self.database_provider.db;
27+
28+
let sql = "CREATE users SET name=$name, email=$email, password=$password;";
29+
let data: BTreeMap<String, Value> = [
30+
("name".into(), Value::String(storable_user.name.into())),
31+
("email".into(), Value::String(storable_user.email.into())),
32+
("password".into(), Value::String(storable_user.password.into())),
33+
]
34+
.into();
35+
36+
let responses = datastore
37+
.execute(sql, database_session, Some(data.into()))
38+
.await?;
39+
40+
let result_object = into_iter_objects(responses)?
41+
.next()
42+
.ok_or_else(|| crate::error::Error::Generic("No user returned from insert".to_string()))??;
43+
44+
let user: User = result_object.try_into()?;
45+
46+
Ok(user)
47+
}
48+
}
49+

0 commit comments

Comments
 (0)