Last cleanup

This commit is contained in:
2025-06-03 19:50:43 +02:00
parent f1d23cb495
commit d1832bc794
205 changed files with 178 additions and 36164 deletions

View File

@@ -1,13 +1,15 @@
use actix_web::http::header::ContentType;
use actix_web::{get, post, web, HttpResponse, Responder};
use serde::Deserialize;
use gamenight_database::user::{count_users_with_email, count_users_with_username};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use validator::ValidateArgs;
use validator::{Validate, ValidateArgs, ValidationError};
use crate::models::login::Login;
use crate::models::registration::Registration;
use crate::models::token::Token;
use crate::models::user::User;
use crate::request::requests::{Login, Register, RegisterContext};
use crate::request::error::ApiError;
use crate::request::responses::LoginResponse;
use crate::request::authorization::get_token;
use serde_json;
use gamenight_database::{DbPool, GetConnection};
@@ -23,8 +25,8 @@ impl From<Login> for gamenight_database::user::LoginUser {
}
}
impl From<Register> for gamenight_database::user::Register {
fn from(val: Register) -> Self {
impl From<Registration> for gamenight_database::user::Register {
fn from(val: Registration) -> Self {
gamenight_database::user::Register {
email: val.email,
username: val.username,
@@ -33,6 +35,61 @@ impl From<Register> for gamenight_database::user::Register {
}
}
pub struct RegisterContext<'v_a> {
pub pool: &'v_a DbPool
}
pub fn unique_username(username: &String, context: &RegisterContext) -> Result<(), ValidationError> {
let mut conn = context.pool.get_conn();
match count_users_with_username(&mut conn, username)
{
Ok(0) => Ok(()),
Ok(_) => Err(ValidationError::new("User already exists")),
Err(_) => Err(ValidationError::new("Database error while validating user")),
}
}
pub fn unique_email(email: &String, context: &RegisterContext) -> Result<(), ValidationError> {
let mut conn = context.pool.get_conn();
match count_users_with_email(&mut conn, email)
{
Ok(0) => Ok(()),
Ok(_) => Err(ValidationError::new("email already exists")),
Err(_) => Err(ValidationError::new("Database error while validating email"))
}
}
#[derive(Serialize, Deserialize, Clone, Validate)]
#[validate(context = RegisterContext::<'v_a>)]
pub struct ValidatableRegistration {
#[validate(
length(min = 1),
custom(function = "unique_username", use_context)
)]
pub username: String,
#[validate(
email,
custom(function = "unique_email", use_context)
)]
pub email: String,
#[validate(length(min = 10), must_match(other = "password_repeat", ))]
pub password: String,
pub password_repeat: String,
}
impl From<Registration> for ValidatableRegistration {
fn from(value: Registration) -> Self {
Self {
username: value.username,
email: value.email,
password: value.password,
password_repeat: value.password_repeat
}
}
}
#[get("/token")]
pub async fn login(pool: web::Data<DbPool>, login_data: web::Json<Login>) -> Result<impl Responder, ApiError> {
let data = login_data.into_inner();
@@ -44,7 +101,7 @@ pub async fn login(pool: web::Data<DbPool>, login_data: web::Json<Login>) -> Res
.await?
{
let token = get_token(&user)?;
let response = LoginResponse::success(user.id, token);
let response = Token{ jwt_token: Some(token) };
Ok(HttpResponse::Ok()
.content_type(ContentType::json())
.body(serde_json::to_string(&response)?)
@@ -56,9 +113,10 @@ pub async fn login(pool: web::Data<DbPool>, login_data: web::Json<Login>) -> Res
}
#[post("/user")]
pub async fn register(pool: web::Data<DbPool>, register_data: web::Json<Register>) -> Result<impl Responder, ApiError> {
pub async fn register(pool: web::Data<DbPool>, register_data: web::Json<Registration>) -> Result<impl Responder, ApiError> {
web::block(move || -> Result<(), ApiError> {
register_data.validate_with_args(&RegisterContext{pool: &pool})?;
let validatable_registration: ValidatableRegistration = register_data.clone().into();
validatable_registration.validate_with_args(&RegisterContext{pool: &pool})?;
let register_request = register_data.into_inner().into();
let mut conn = pool.get_conn();
gamenight_database::register(&mut conn, register_request)?;