Add get_tasks endpoint
This commit is contained in:
parent
14de8cd479
commit
517794d0db
5
server/Cargo.lock
generated
5
server/Cargo.lock
generated
@ -365,6 +365,7 @@ dependencies = [
|
|||||||
"iana-time-zone",
|
"iana-time-zone",
|
||||||
"js-sys",
|
"js-sys",
|
||||||
"num-traits",
|
"num-traits",
|
||||||
|
"serde",
|
||||||
"wasm-bindgen",
|
"wasm-bindgen",
|
||||||
"windows-targets 0.52.5",
|
"windows-targets 0.52.5",
|
||||||
]
|
]
|
||||||
@ -464,6 +465,7 @@ dependencies = [
|
|||||||
"diesel_derives",
|
"diesel_derives",
|
||||||
"itoa",
|
"itoa",
|
||||||
"pq-sys",
|
"pq-sys",
|
||||||
|
"serde_json",
|
||||||
"uuid",
|
"uuid",
|
||||||
]
|
]
|
||||||
|
|
||||||
@ -907,6 +909,8 @@ dependencies = [
|
|||||||
"diesel",
|
"diesel",
|
||||||
"diesel_migrations",
|
"diesel_migrations",
|
||||||
"dotenvy",
|
"dotenvy",
|
||||||
|
"serde",
|
||||||
|
"serde_json",
|
||||||
"uuid",
|
"uuid",
|
||||||
]
|
]
|
||||||
|
|
||||||
@ -1363,6 +1367,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "a183cf7feeba97b4dd1c0d46788634f6221d87fa961b305bed08c851829efcc0"
|
checksum = "a183cf7feeba97b4dd1c0d46788634f6221d87fa961b305bed08c851829efcc0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"getrandom",
|
"getrandom",
|
||||||
|
"serde",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -6,9 +6,11 @@ license = "GPL-3.0-only"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web = "4.5.1"
|
actix-web = "4.5.1"
|
||||||
chrono = "0.4.38"
|
chrono = { version = "0.4.38", features = ["serde"] }
|
||||||
colored = "2.1.0"
|
colored = "2.1.0"
|
||||||
diesel = { version = "2.1.6", features = ["postgres", "uuid", "chrono"] }
|
diesel = { version = "2.1.6", features = ["postgres", "uuid", "chrono", "serde_json"] }
|
||||||
diesel_migrations = { version = "2.1.0", features = ["postgres"] }
|
diesel_migrations = { version = "2.1.0", features = ["postgres"] }
|
||||||
dotenvy = "0.15.7"
|
dotenvy = "0.15.7"
|
||||||
uuid = { version = "1.8.0", features = ["v4"] }
|
serde = { version = "1.0.200", features = ["derive"] }
|
||||||
|
serde_json = "1.0.116"
|
||||||
|
uuid = { version = "1.8.0", features = ["v4", "serde"] }
|
||||||
|
16
server/src/api/mod.rs
Normal file
16
server/src/api/mod.rs
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
// SPDX-License-Identifier: GPL-3.0-Only
|
||||||
|
// Copyright (C) 2024 Luke Harding
|
||||||
|
|
||||||
|
use actix_web::{get, HttpResponse, Responder};
|
||||||
|
|
||||||
|
use crate::db;
|
||||||
|
|
||||||
|
#[get("/get_tasks")]
|
||||||
|
pub async fn get_tasks() -> impl Responder {
|
||||||
|
let tasks = match db::get_tasks().await {
|
||||||
|
Ok(tasks) => tasks,
|
||||||
|
Err(e) => return HttpResponse::InternalServerError().body(e.to_string()),
|
||||||
|
};
|
||||||
|
|
||||||
|
HttpResponse::Ok().json(tasks)
|
||||||
|
}
|
@ -3,10 +3,12 @@
|
|||||||
|
|
||||||
use std::{env, error};
|
use std::{env, error};
|
||||||
|
|
||||||
use diesel::{Connection, PgConnection};
|
use diesel::{Connection, PgConnection, QueryDsl, RunQueryDsl, SelectableHelper};
|
||||||
use diesel::pg::Pg;
|
use diesel::pg::Pg;
|
||||||
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
|
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
|
||||||
|
|
||||||
|
use models::Task;
|
||||||
|
|
||||||
pub mod models;
|
pub mod models;
|
||||||
|
|
||||||
pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!("./migrations");
|
pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!("./migrations");
|
||||||
@ -24,3 +26,11 @@ pub fn run_migrations(
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn get_tasks() -> Result<Vec<Task>, Box<dyn error::Error>> {
|
||||||
|
use crate::schema::tasks::dsl::*;
|
||||||
|
|
||||||
|
let conn = &mut establish_connection()?;
|
||||||
|
|
||||||
|
Ok(tasks.select(Task::as_select()).load(conn)?)
|
||||||
|
}
|
||||||
|
@ -3,9 +3,10 @@
|
|||||||
|
|
||||||
use chrono::NaiveDateTime;
|
use chrono::NaiveDateTime;
|
||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
#[derive(Queryable, Selectable)]
|
#[derive(Queryable, Selectable, Serialize, Deserialize)]
|
||||||
#[diesel(table_name = crate::schema::tasks)]
|
#[diesel(table_name = crate::schema::tasks)]
|
||||||
#[diesel(check_for_backend(diesel::pg::Pg))]
|
#[diesel(check_for_backend(diesel::pg::Pg))]
|
||||||
pub struct Task {
|
pub struct Task {
|
||||||
|
@ -3,11 +3,12 @@
|
|||||||
|
|
||||||
use std::{env, io};
|
use std::{env, io};
|
||||||
|
|
||||||
use actix_web::{App, get, HttpResponse, HttpServer, Responder};
|
use actix_web::{App, HttpServer};
|
||||||
|
|
||||||
|
mod api;
|
||||||
pub mod db;
|
pub mod db;
|
||||||
pub mod util;
|
|
||||||
pub mod schema;
|
pub mod schema;
|
||||||
|
pub mod util;
|
||||||
|
|
||||||
#[actix_web::main]
|
#[actix_web::main]
|
||||||
async fn main() -> io::Result<()> {
|
async fn main() -> io::Result<()> {
|
||||||
@ -58,13 +59,8 @@ async fn main() -> io::Result<()> {
|
|||||||
|
|
||||||
util::notice_println(format!("Starting server on {}:{}", bind_addr, port));
|
util::notice_println(format!("Starting server on {}:{}", bind_addr, port));
|
||||||
|
|
||||||
HttpServer::new(|| App::new().service(hello))
|
HttpServer::new(|| App::new().service(api::get_tasks))
|
||||||
.bind((bind_addr, port))?
|
.bind((bind_addr, port))?
|
||||||
.run()
|
.run()
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/")]
|
|
||||||
async fn hello() -> impl Responder {
|
|
||||||
HttpResponse::Ok().body("Hello World!")
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user