mirror of
https://github.com/actix/examples
synced 2025-06-29 02:10:36 +02:00
Fix sqlx_todo CI checks (#415)
This commit is contained in:
committed by
GitHub
parent
75c19eb5c9
commit
7487a81e90
@ -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()
|
||||
|
@ -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)
|
||||
|
@ -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) => {
|
||||
|
Reference in New Issue
Block a user