Skip to content

Commit

Permalink
starters: 0.12.0 updates (remove notes, devcontainer, prefix + more) (#…
Browse files Browse the repository at this point in the history
…846)

* saas: remove notes, devcontainer
* rest-api: clean up
* lw-service: remove devcontainer
* docs: clean up mention of notes and user/current
* align to how 0.12.0 will generate controllers, controller-local prefix
  • Loading branch information
jondot authored Oct 28, 2024
1 parent ab2956d commit f4fbc7c
Show file tree
Hide file tree
Showing 67 changed files with 120 additions and 1,001 deletions.
6 changes: 0 additions & 6 deletions docs-site/content/docs/getting-started/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,7 @@ impl Hooks for App {
fn routes() -> AppRoutes {
AppRoutes::with_default_routes()
.add_route(controllers::guide::routes())
.add_route(controllers::notes::routes())
.add_route(controllers::auth::routes())
.add_route(controllers::user::routes())
.add_route(controllers::home::routes()) // <--- add this
}
```
Expand Down Expand Up @@ -277,8 +275,6 @@ $ cargo loco routes
[POST] /api/auth/reset
[POST] /api/auth/verify
[GET] /home/hello <---- this is our new route!
[GET] /api/notes
[POST] /api/notes
..
..
$
Expand Down Expand Up @@ -388,12 +384,10 @@ src/models/
├── _entities
│   ├── articles.rs <-- sync'd from db schema, do not edit
│   ├── mod.rs
│   ├── notes.rs
│   ├── prelude.rs
│   └── users.rs
├── articles.rs <-- generated for you, your logic goes here.
├── mod.rs
├── notes.rs
└── users.rs
```

Expand Down
9 changes: 3 additions & 6 deletions docs-site/content/docs/the-app/controller.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,7 @@ $ cargo loco routes
[POST] /auth/register
[POST] /auth/reset
[POST] /auth/verify
[GET] /notes/
[POST] /notes/
[GET] /notes/:id
[DELETE] /notes/:id
[POST] /notes/:id
[GET] /user/current
[GET] /auth/current
```

This command will provide you with a comprehensive overview of the controllers currently registered in your system.
Expand Down Expand Up @@ -768,6 +763,8 @@ impl Hooks for App {

In many scenarios, when querying data and returning responses to users, pagination is crucial. In `Loco`, we provide a straightforward method to paginate your data and maintain a consistent pagination response schema for your API responses.

We assume you have a `notes` entity and/or scaffold (replace this with any entity you like).

## Using pagination

```rust
Expand Down
2 changes: 0 additions & 2 deletions docs-site/content/docs/the-app/models.md
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,6 @@ $ cargo loco generate model --link users_votes user:references movie:references
..
..
Writing src/models/_entities/movies.rs
Writing src/models/_entities/notes.rs
Writing src/models/_entities/users.rs
Writing src/models/_entities/mod.rs
Writing src/models/_entities/prelude.rs
Expand Down Expand Up @@ -736,7 +735,6 @@ impl Hooks for App {
//...
async fn truncate(db: &DatabaseConnection) -> Result<()> {
// truncate_table(db, users::Entity).await?;
// truncate_table(db, notes::Entity).await?;
Ok(())
}
Expand Down
9 changes: 1 addition & 8 deletions docs-site/content/docs/the-app/views.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,7 @@ impl Hooks for App {

fn routes(_ctx: &AppContext) -> AppRoutes {
AppRoutes::with_default_routes()
.add_route(controllers::notes::routes())
.add_route(controllers::auth::routes())
.add_route(controllers::user::routes())
// include your controller's routes here
.add_route(controllers::dashboard::routes())
}
Expand All @@ -198,12 +196,7 @@ $ cargo loco routes
[POST] /api/auth/register
[POST] /api/auth/reset
[POST] /api/auth/verify
[GET] /api/notes
[POST] /api/notes
[GET] /api/notes/:id
[DELETE] /api/notes/:id
[POST] /api/notes/:id
[GET] /api/user/current
[GET] /api/auth/current
[GET] /home <-- the corresponding URL for our new view
```

Expand Down
Empty file.
8 changes: 0 additions & 8 deletions starters/lightweight-service/.devcontainer/Dockerfile

This file was deleted.

10 changes: 0 additions & 10 deletions starters/lightweight-service/.devcontainer/compose.yaml

This file was deleted.

9 changes: 0 additions & 9 deletions starters/lightweight-service/.devcontainer/devcontainer.json

This file was deleted.

3 changes: 1 addition & 2 deletions starters/lightweight-service/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ impl Hooks for App {
}

fn routes(_ctx: &AppContext) -> AppRoutes {
AppRoutes::empty()
.prefix("/api")
AppRoutes::empty() // controller routes below
.add_route(controllers::home::routes())
}

Expand Down
2 changes: 1 addition & 1 deletion starters/lightweight-service/src/controllers/home.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ async fn current() -> Result<Response> {
}

pub fn routes() -> Routes {
Routes::new().add("/", get(current))
Routes::new().prefix("/api").add("/", get(current))
}
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.

7 changes: 1 addition & 6 deletions starters/rest-api/migration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +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![
// inject-below (do not remove this comment)
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.

14 changes: 2 additions & 12 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 @@ -43,11 +38,8 @@ impl Hooks for App {
}

fn routes(_ctx: &AppContext) -> AppRoutes {
AppRoutes::with_default_routes()
.prefix("/api")
.add_route(controllers::notes::routes())
AppRoutes::with_default_routes() // controller routes below
.add_route(controllers::auth::routes())
.add_route(controllers::user::routes())
}

async fn connect_workers(ctx: &AppContext, queue: &Queue) -> Result<()> {
Expand All @@ -61,13 +53,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(())
}
}
11 changes: 9 additions & 2 deletions 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,12 +139,19 @@ 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")
.prefix("/api/auth")
.add("/register", post(register))
.add("/verify", post(verify))
.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;
Loading

0 comments on commit f4fbc7c

Please sign in to comment.