Skip to content

Commit

Permalink
rest-api: clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
jondot committed Oct 14, 2024
1 parent 37103af commit 97143cb
Show file tree
Hide file tree
Showing 30 changed files with 62 additions and 471 deletions.
6 changes: 0 additions & 6 deletions starters/rest-api/.devcontainer/.env

This file was deleted.

8 changes: 0 additions & 8 deletions starters/rest-api/.devcontainer/Dockerfile

This file was deleted.

49 changes: 0 additions & 49 deletions starters/rest-api/.devcontainer/compose.yaml

This file was deleted.

9 changes: 0 additions & 9 deletions starters/rest-api/.devcontainer/devcontainer.json

This file was deleted.

2 changes: 2 additions & 0 deletions starters/rest-api/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 1 addition & 5 deletions starters/rest-api/migration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@
pub use sea_orm_migration::prelude::*;

mod m20220101_000001_users;
mod m20231103_114510_notes;

pub struct Migrator;

#[async_trait::async_trait]
impl MigratorTrait for Migrator {
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
vec![
Box::new(m20220101_000001_users::Migration),
Box::new(m20231103_114510_notes::Migration),
]
vec![Box::new(m20220101_000001_users::Migration)]
}
}
34 changes: 0 additions & 34 deletions starters/rest-api/migration/src/m20231103_114510_notes.rs

This file was deleted.

11 changes: 1 addition & 10 deletions starters/rest-api/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,7 @@ use loco_rs::{
use migration::Migrator;
use sea_orm::DatabaseConnection;

use crate::{
controllers,
models::_entities::{notes, users},
tasks,
workers::downloader::DownloadWorker,
};
use crate::{controllers, models::_entities::users, tasks, workers::downloader::DownloadWorker};

pub struct App;
#[async_trait]
Expand All @@ -45,9 +40,7 @@ impl Hooks for App {
fn routes(_ctx: &AppContext) -> AppRoutes {
AppRoutes::with_default_routes()
.prefix("/api")
.add_route(controllers::notes::routes())
.add_route(controllers::auth::routes())
.add_route(controllers::user::routes())
}

async fn connect_workers(ctx: &AppContext, queue: &Queue) -> Result<()> {
Expand All @@ -61,13 +54,11 @@ impl Hooks for App {

async fn truncate(db: &DatabaseConnection) -> Result<()> {
truncate_table(db, users::Entity).await?;
truncate_table(db, notes::Entity).await?;
Ok(())
}

async fn seed(db: &DatabaseConnection, base: &Path) -> Result<()> {
db::seed::<users::ActiveModel>(db, &base.join("users.yaml").display().to_string()).await?;
db::seed::<notes::ActiveModel>(db, &base.join("notes.yaml").display().to_string()).await?;
Ok(())
}
}
9 changes: 8 additions & 1 deletion starters/rest-api/src/controllers/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
_entities::users,
users::{LoginParams, RegisterParams},
},
views::auth::LoginResponse,
views::auth::{CurrentResponse, LoginResponse},
};
#[derive(Debug, Deserialize, Serialize)]
pub struct VerifyParams {
Expand Down Expand Up @@ -139,6 +139,12 @@ async fn login(State(ctx): State<AppContext>, Json(params): Json<LoginParams>) -
format::json(LoginResponse::new(&user, &token))
}

#[debug_handler]
async fn current(auth: auth::JWT, State(ctx): State<AppContext>) -> Result<Response> {
let user = users::Model::find_by_pid(&ctx.db, &auth.claims.pid).await?;
format::json(CurrentResponse::new(&user))
}

pub fn routes() -> Routes {
Routes::new()
.prefix("auth")
Expand All @@ -147,4 +153,5 @@ pub fn routes() -> Routes {
.add("/login", post(login))
.add("/forgot", post(forgot))
.add("/reset", post(reset))
.add("/current", get(current))
}
2 changes: 0 additions & 2 deletions starters/rest-api/src/controllers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
pub mod auth;
pub mod notes;
pub mod user;
75 changes: 0 additions & 75 deletions starters/rest-api/src/controllers/notes.rs

This file was deleted.

14 changes: 0 additions & 14 deletions starters/rest-api/src/controllers/user.rs

This file was deleted.

11 changes: 0 additions & 11 deletions starters/rest-api/src/fixtures/notes.yaml

This file was deleted.

1 change: 0 additions & 1 deletion starters/rest-api/src/models/_entities/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@
pub mod prelude;

pub mod notes;
pub mod users;
18 changes: 0 additions & 18 deletions starters/rest-api/src/models/_entities/notes.rs

This file was deleted.

1 change: 0 additions & 1 deletion starters/rest-api/src/models/_entities/prelude.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.0.0
pub use super::notes::Entity as Notes;
pub use super::users::Entity as Users;
1 change: 0 additions & 1 deletion starters/rest-api/src/models/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
pub mod _entities;
pub mod notes;
pub mod users;
7 changes: 0 additions & 7 deletions starters/rest-api/src/models/notes.rs

This file was deleted.

18 changes: 18 additions & 0 deletions starters/rest-api/src/views/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,21 @@ impl LoginResponse {
}
}
}

#[derive(Debug, Deserialize, Serialize)]
pub struct CurrentResponse {
pub pid: String,
pub name: String,
pub email: String,
}

impl CurrentResponse {
#[must_use]
pub fn new(user: &users::Model) -> Self {
Self {
pid: user.pid.to_string(),
name: user.name.clone(),
email: user.email.clone(),
}
}
}
1 change: 0 additions & 1 deletion starters/rest-api/src/views/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
pub mod auth;
pub mod user;
Loading

0 comments on commit 97143cb

Please sign in to comment.