Added user management (no api yet)

This commit is contained in:
2026-07-14 00:27:22 -04:00
parent 98a75479f8
commit fcde51ab3c
2 changed files with 47 additions and 1 deletions

View File

@@ -1,13 +1,22 @@
use std::error::Error;
use std::env::var;
use axum::routing::get;
use axum::{serve, Router};
use tokio::net::TcpListener;
use sqlx::postgres::PgPool;
use dotenvy::dotenv;
mod users;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
dotenv()?;
let db_url = var("DATABASE_URL")?;
let pool = PgPool::connect(&db_url).await?;
let app = Router::new()
.route("/", get(hello));
.route("/", get(hello))
.with_state(pool);
let listener = TcpListener::bind("0.0.0.0:80").await?;
Ok(serve(listener, app).await?)
}

37
src/users.rs Normal file
View File

@@ -0,0 +1,37 @@
use std::error::Error;
use axum::routing::{post, get};
use sqlx::{PgPool, query, query_as, types::Uuid};
struct User {
id: Uuid,
steamid: String,
name: String,
avatar: String
}
impl User {
fn new(steamid: String, name: String, avatar: String) -> User {
User {
id: Uuid::nil(),
steamid,
name,
avatar
}
}
async fn create(self, pool: PgPool) -> Result<(), Box<dyn Error>>{
query!("INSERT INTO users (steamid, name, avatar) VALUES ($1, $2, $3)", self.steamid, self.name, self.avatar).execute(&pool).await?;
Ok(())
}
async fn delete(self, pool: PgPool) -> Result<(), Box<dyn Error>>{
query!("DELETE FROM users WHERE id=$1", self.id).execute(&pool).await?;
Ok(())
}
async fn update(self, pool: PgPool) -> Result<(), Box<dyn Error>>{
query!("UPDATE users SET steamid=$1, name=$2, avatar=$3 WHERE id=$4", self.steamid, self.name, self.avatar, self.id).execute(&pool).await?;
Ok(())
}
}