first implementation of /users/create

This commit is contained in:
Martin Brodbeck 2023-09-26 10:38:50 +02:00
parent 8e9cd3f63d
commit fed0d3616e
3 changed files with 150 additions and 3 deletions

View file

@ -2,6 +2,16 @@ use axum::{http::StatusCode, routing::get, routing::post, routing::put, Json, Ro
use serde_json::{json, Value};
use serde::Deserialize;
use redis::Commands;
#[derive(Deserialize)]
pub struct CreateUser {
username: String,
password: String,
}
#[tokio::main]
async fn main() {
// build our application with a single route
@ -24,7 +34,28 @@ async fn root() -> &'static str {
"KOreader sync server"
}
async fn create_user() {}
async fn create_user(Json(payload): Json<CreateUser>) -> (StatusCode, String) {
let client = redis::Client::open("redis://127.0.0.1/").unwrap();
let mut con = client.get_connection().unwrap();
let username = payload.username;
let password = payload.password;
let user_key = format!("user:{username}:key");
let does_exist: bool = con.exists(&user_key).unwrap();
if does_exist == false {
let _: () = con.set(&user_key, password).unwrap();
} else {
return (
StatusCode::PAYMENT_REQUIRED,
"Username is already registered.".to_owned(),
);
}
(StatusCode::CREATED, format!("username = {username}"))
}
async fn auth_user() {}
@ -33,5 +64,5 @@ async fn update_progress() {}
async fn get_progress() {}
async fn healthcheck() -> (StatusCode, Json<Value>) {
(StatusCode::OK, Json(json!({ "state": "OK" })))
(StatusCode::OK, Json(json!({ "state" : "OK" })))
}