From 50c17393e27fff387b7ad4eb1be613ec697e3439 Mon Sep 17 00:00:00 2001 From: Mika Date: Sun, 15 Jun 2025 11:59:54 +0200 Subject: [PATCH] refactor: replace LoginResponse and LogoutResponse with MessageResponse unified response instead of a struct for each response --- crates/backend/src/controller/auth.rs | 33 ++++++++------------------- crates/backend/src/error.rs | 19 ++++++++++++++- crates/backend/src/main.rs | 4 +--- 3 files changed, 28 insertions(+), 28 deletions(-) diff --git a/crates/backend/src/controller/auth.rs b/crates/backend/src/controller/auth.rs index 407deaa..12158b2 100644 --- a/crates/backend/src/controller/auth.rs +++ b/crates/backend/src/controller/auth.rs @@ -4,10 +4,13 @@ use actix_web::{ web::{self, ServiceConfig}, }; use log::debug; -use serde::{Deserialize, Serialize}; +use serde::Deserialize; use utoipa::ToSchema; -use crate::{Database, error::ApiError}; +use crate::{ + Database, + error::{ApiError, MessageResponse}, +}; #[derive(Deserialize, ToSchema)] pub struct LoginRequest { @@ -17,20 +20,6 @@ pub struct LoginRequest { pub password: String, } -#[derive(Serialize, ToSchema)] -pub struct LoginResponse { - /// Success message - pub message: String, -} - -#[derive(Serialize, ToSchema)] -pub struct LogoutResponse { - /// Logout confirmation message - pub message: String, -} - -// TODO: Implement generic ApiResponse type to reduce boilerplate - pub fn setup(cfg: &mut ServiceConfig) { cfg.service(login).service(logout); } @@ -43,7 +32,7 @@ pub fn setup(cfg: &mut ServiceConfig) { description = "Authenticate a user with username and password", request_body = LoginRequest, responses( - (status = 200, description = "Login successful", body = LoginResponse), + (status = 200, description = "Login successful", body = MessageResponse), (status = 400, description = "Invalid credentials"), (status = 409, description = "User already logged in"), (status = 500, description = "Internal server error") @@ -67,9 +56,7 @@ pub async fn login( session.insert("user", user_id)?; - Ok(HttpResponse::Ok().json(LoginResponse { - message: "Login successful".to_string(), - })) + Ok(HttpResponse::Ok().json(MessageResponse::new("Login successful"))) } #[utoipa::path( @@ -79,7 +66,7 @@ pub async fn login( summary = "User logout", description = "Log out the currently authenticated user and clear session", responses( - (status = 200, description = "Logout successful", body = LogoutResponse), + (status = 200, description = "Logout successful", body = MessageResponse), (status = 500, description = "Internal server error") ) )] @@ -89,7 +76,5 @@ pub async fn logout(session: Session, request: HttpRequest) -> Result> for ApiError { }) } } + +#[derive(Serialize, ToSchema)] +pub struct MessageResponse { + /// Response message + pub message: String, +} + +impl MessageResponse { + /// Create a new message response + pub fn new(message: impl Into) -> Self { + Self { + message: message.into(), + } + } +} diff --git a/crates/backend/src/main.rs b/crates/backend/src/main.rs index c37069e..fb7a662 100644 --- a/crates/backend/src/main.rs +++ b/crates/backend/src/main.rs @@ -1,4 +1,3 @@ -use actix_files::NamedFile; use actix_session::{SessionMiddleware, storage::RedisSessionStore}; use actix_web::cookie::SameSite; use actix_web::{App, HttpServer, cookie::Key, middleware::Logger, web}; @@ -34,8 +33,7 @@ struct AppConfig { ), components(schemas( controller::auth::LoginRequest, - controller::auth::LoginResponse, - controller::auth::LogoutResponse, + error::MessageResponse, )), tags( (name = "auth", description = "Authentication endpoints"),