1
0
Fork 0
This commit is contained in:
Florian RICHER 2021-07-08 22:04:01 +02:00
parent 653267344f
commit ce7ef4807b
9 changed files with 77 additions and 23 deletions

View file

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="dataSourceStorageLocal" created-in="IU-211.7628.21">
<data-source name="SQLite" uuid="7d751e9c-3254-456f-8fa4-f0282582cf85">
<database-info product="SQLite" version="3.34.0" jdbc-version="2.1" driver-name="SQLite JDBC" driver-version="3.34.0" dbms="SQLITE" exact-version="3.34.0" exact-driver-version="3.34">
<identifier-quote-string>&quot;</identifier-quote-string>
</database-info>
<case-sensitivity plain-identifiers="mixed" quoted-identifiers="mixed" />
<secret-storage>master_key</secret-storage>
<auth-provider>no-auth</auth-provider>
<schema-mapping>
<introspection-scope>
<node kind="schema" negative="1" />
</introspection-scope>
</schema-mapping>
</data-source>
</component>
</project>

12
.idea/dataSources.xml Normal file
View file

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
<data-source source="LOCAL" name="SQLite" uuid="7d751e9c-3254-456f-8fa4-f0282582cf85">
<driver-ref>sqlite.xerial</driver-ref>
<synchronize>true</synchronize>
<jdbc-driver>org.sqlite.JDBC</jdbc-driver>
<jdbc-url>jdbc:sqlite:$PROJECT_DIR$/db/dev.db</jdbc-url>
<working-dir>$ProjectFileDir$</working-dir>
</data-source>
</component>
</project>

6
.idea/sqldialects.xml Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="SqlDialectMappings">
<file url="file://$PROJECT_DIR$/migrations/20210708082032_create_tasks_table/up.sql" dialect="SQLite" />
</component>
</project>

View file

@ -5,6 +5,7 @@
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
<excludeFolder url="file://$MODULE_DIR$/db" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />

8
src/controllers/mod.rs Normal file
View file

@ -0,0 +1,8 @@
use rocket::get;
pub mod tasks_controller;
#[get("/")]
pub fn index() -> &'static str {
"Hello, world!"
}

View file

@ -0,0 +1,8 @@
use rocket::serde::json::Json;
use rocket::get;
use crate::models::{DbConn, task::Task};
#[get("/tasks/<id>")]
pub async fn show(conn: DbConn, id: i32) -> Option<Json<Task>> {
Task::find_by(&conn, id).await.map(Json).ok()
}

View file

@ -2,24 +2,11 @@
#[macro_use] extern crate diesel_migrations;
mod models;
mod controllers;
use rocket::*;
use rocket::fairing::AdHoc;
use rocket_sync_db_pools::{database};
#[database("sqlite_logs")]
pub struct DbConn(diesel::SqliteConnection);
#[get("/")]
fn index() -> &'static str {
"Hello, world!"
}
#[get("/log/<id>")]
async fn get_log(conn: DbConn, id: i32) -> String {
let result = models::task::Task::all(&conn).await;
format!("test {}, {:?}", id, result)
}
use models::DbConn;
async fn run_migrations(rocket: Rocket<Build>) -> Rocket<Build> {
embed_migrations!();
@ -33,7 +20,10 @@ async fn run_migrations(rocket: Rocket<Build>) -> Rocket<Build> {
#[launch]
fn rocket() -> _ {
rocket::build()
.mount("/", routes![index, get_log])
.mount("/", routes![
controllers::index,
controllers::tasks_controller::show
])
.attach(DbConn::fairing())
.attach(AdHoc::on_ignite("Run Migrations", run_migrations))
}

View file

@ -1 +1,7 @@
use rocket_sync_db_pools::database;
use diesel::SqliteConnection;
#[database("sqlite_logs")]
pub struct DbConn(SqliteConnection);
pub mod task;

View file

@ -1,18 +1,15 @@
use rocket::serde::Serialize;
use diesel::{self, result::QueryResult, Queryable, Insertable, prelude::*};
use super::DbConn;
table! {
tasks {
tasks (id) {
id -> Nullable<Integer>,
description -> Text,
completed -> Bool,
}
}
use tasks::dsl::{tasks as all_tasks};
use crate::DbConn;
#[derive(Serialize, Queryable, Insertable, Debug, Clone)]
#[serde(crate = "rocket::serde")]
#[table_name="tasks"]
@ -24,8 +21,16 @@ pub struct Task {
impl Task {
pub async fn all(conn: &DbConn) -> QueryResult<Vec<Task>> {
conn.run(|c| {
all_tasks.order(tasks::id.desc()).load::<Task>(c)
conn.run(move |c| {
tasks::table.load(c)
}).await
}
pub async fn find_by(conn: &DbConn, id: i32) -> QueryResult<Task> {
conn.run(move |c| {
tasks::table
.filter(tasks::id.eq(id))
.first(c)
}).await
}
}