1
0
mirror of https://github.com/actix/examples synced 2025-06-28 18:00:37 +02:00

Fix sqlx_todo CI checks (#415)

This commit is contained in:
Daniel T. Rodrigues
2021-03-03 11:37:17 -03:00
committed by GitHub
parent 75c19eb5c9
commit 7487a81e90
12 changed files with 262 additions and 26 deletions

View File

@ -1,4 +1,4 @@
HOST=127.0.0.1
PORT=5000
DATABASE_URL="postgres://user:pass@192.168.33.11/actix_sqlx_todo"
DATABASE_URL=sqlite://database_interactions/sqlx_todo/test.db
RUST_LOG=sqlx_todo=info,actix=info

View File

@ -1,2 +1,3 @@
/target
.env
test.db

View File

@ -11,7 +11,7 @@ actix-web = "3"
listenfd = "0.3.3"
serde = "1.0.106"
serde_json = "1.0.51"
sqlx = { version = "0.3", features = [ "postgres" ] }
sqlx = { version = "0.3", features = [ "sqlite" ] }
dotenv = "0.15.0"
env_logger = "0.7.1"
log = "0.4.8"

View File

@ -1,13 +1,13 @@
# actix-sqlx-todo
Example Todo application using Actix-web and [SQLx](https://github.com/launchbadge/sqlx) with posgresql
Example Todo application using Actix-web and [SQLx](https://github.com/launchbadge/sqlx) with sqlite
# Usage
## Prerequisites
* Rust
* PostgreSQL
* SQLite
## Change into the project sub-directory
@ -20,7 +20,7 @@ cd examples/sqlx_todo
## Set up the database
* Create new database using `schema.sql`
* Copy `.env-example` into `.env` and adjust DATABASE_URL to match your PostgreSQL address, username and password
* Copy `.env-example` into `.env` and adjust DATABASE_URL to match your SQLite address, username and password
## Run the application
@ -30,4 +30,4 @@ To run the application execute:
cargo run
```
By default application will be available on `http://localhost:5000`. If you wish to change address or port you can do it inside `.env` file
By default application will be available on `http://localhost:5000`. If you wish to change address or port you can do it inside `.env` file

View File

@ -1,5 +1,5 @@
CREATE TABLE IF NOT EXISTS todos (
id SERIAL PRIMARY KEY,
id INTEGER PRIMARY KEY NOT NULL,
description TEXT NOT NULL,
done BOOLEAN NOT NULL DEFAULT FALSE
);

View File

@ -5,7 +5,7 @@ use actix_web::{web, App, HttpResponse, HttpServer, Responder};
use anyhow::Result;
use dotenv::dotenv;
use listenfd::ListenFd;
use sqlx::PgPool;
use sqlx::SqlitePool;
use std::env;
// import todo module (routes and model)
@ -35,7 +35,7 @@ async fn main() -> Result<()> {
let database_url =
env::var("DATABASE_URL").expect("DATABASE_URL is not set in .env file");
let db_pool = PgPool::new(&database_url).await?;
let db_pool = SqlitePool::new(&database_url).await?;
let mut server = HttpServer::new(move || {
App::new()

View File

@ -2,8 +2,8 @@ use actix_web::{Error, HttpRequest, HttpResponse, Responder};
use anyhow::Result;
use futures::future::{ready, Ready};
use serde::{Deserialize, Serialize};
use sqlx::postgres::PgRow;
use sqlx::{FromRow, PgPool, Row};
use sqlx::sqlite::SqliteRow;
use sqlx::{FromRow, Row, SqlitePool};
// this struct will use to receive user input
#[derive(Serialize, Deserialize)]
@ -36,7 +36,7 @@ impl Responder for Todo {
// Implementation for Todo struct, functions for read/write/update and delete todo from database
impl Todo {
pub async fn find_all(pool: &PgPool) -> Result<Vec<Todo>> {
pub async fn find_all(pool: &SqlitePool) -> Result<Vec<Todo>> {
let mut todos = vec![];
let recs = sqlx::query!(
r#"
@ -59,7 +59,7 @@ impl Todo {
Ok(todos)
}
pub async fn find_by_id(id: i32, pool: &PgPool) -> Result<Todo> {
pub async fn find_by_id(id: i32, pool: &SqlitePool) -> Result<Todo> {
let rec = sqlx::query!(
r#"
SELECT * FROM todos WHERE id = $1
@ -76,12 +76,12 @@ impl Todo {
})
}
pub async fn create(todo: TodoRequest, pool: &PgPool) -> Result<Todo> {
pub async fn create(todo: TodoRequest, pool: &SqlitePool) -> Result<Todo> {
let mut tx = pool.begin().await?;
let todo = sqlx::query("INSERT INTO todos (description, done) VALUES ($1, $2) RETURNING id, description, done")
.bind(&todo.description)
.bind(todo.done)
.map(|row: PgRow| {
.map(|row: SqliteRow| {
Todo {
id: row.get(0),
description: row.get(1),
@ -95,13 +95,13 @@ impl Todo {
Ok(todo)
}
pub async fn update(id: i32, todo: TodoRequest, pool: &PgPool) -> Result<Todo> {
pub async fn update(id: i32, todo: TodoRequest, pool: &SqlitePool) -> Result<Todo> {
let mut tx = pool.begin().await.unwrap();
let todo = sqlx::query("UPDATE todos SET description = $1, done = $2 WHERE id = $3 RETURNING id, description, done")
.bind(&todo.description)
.bind(todo.done)
.bind(id)
.map(|row: PgRow| {
.map(|row: SqliteRow| {
Todo {
id: row.get(0),
description: row.get(1),
@ -115,7 +115,7 @@ impl Todo {
Ok(todo)
}
pub async fn delete(id: i32, pool: &PgPool) -> Result<u64> {
pub async fn delete(id: i32, pool: &SqlitePool) -> Result<u64> {
let mut tx = pool.begin().await?;
let deleted = sqlx::query("DELETE FROM todos WHERE id = $1")
.bind(id)

View File

@ -1,9 +1,9 @@
use crate::todo::{Todo, TodoRequest};
use actix_web::{delete, get, post, put, web, HttpResponse, Responder};
use sqlx::PgPool;
use sqlx::SqlitePool;
#[get("/todos")]
async fn find_all(db_pool: web::Data<PgPool>) -> impl Responder {
async fn find_all(db_pool: web::Data<SqlitePool>) -> impl Responder {
let result = Todo::find_all(db_pool.get_ref()).await;
match result {
Ok(todos) => HttpResponse::Ok().json(todos),
@ -13,7 +13,7 @@ async fn find_all(db_pool: web::Data<PgPool>) -> impl Responder {
}
#[get("/todo/{id}")]
async fn find(id: web::Path<i32>, db_pool: web::Data<PgPool>) -> impl Responder {
async fn find(id: web::Path<i32>, db_pool: web::Data<SqlitePool>) -> impl Responder {
let result = Todo::find_by_id(id.into_inner(), db_pool.get_ref()).await;
match result {
Ok(todo) => HttpResponse::Ok().json(todo),
@ -24,7 +24,7 @@ async fn find(id: web::Path<i32>, db_pool: web::Data<PgPool>) -> impl Responder
#[post("/todo")]
async fn create(
todo: web::Json<TodoRequest>,
db_pool: web::Data<PgPool>,
db_pool: web::Data<SqlitePool>,
) -> impl Responder {
let result = Todo::create(todo.into_inner(), db_pool.get_ref()).await;
match result {
@ -37,7 +37,7 @@ async fn create(
async fn update(
id: web::Path<i32>,
todo: web::Json<TodoRequest>,
db_pool: web::Data<PgPool>,
db_pool: web::Data<SqlitePool>,
) -> impl Responder {
let result =
Todo::update(id.into_inner(), todo.into_inner(), db_pool.get_ref()).await;
@ -48,7 +48,7 @@ async fn update(
}
#[delete("/todo/{id}")]
async fn delete(id: web::Path<i32>, db_pool: web::Data<PgPool>) -> impl Responder {
async fn delete(id: web::Path<i32>, db_pool: web::Data<SqlitePool>) -> impl Responder {
let result = Todo::delete(id.into_inner(), db_pool.get_ref()).await;
match result {
Ok(rows) => {