init
This commit is contained in:
@@ -1 +1 @@
|
||||
pub struct Message {}
|
||||
// Domain model for Message - currently unused in favor of models::message
|
||||
|
||||
@@ -1,10 +1,27 @@
|
||||
use chrono::{DateTime, Utc};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use utoipa::ToSchema;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct CreateMessageRequest {}
|
||||
#[derive(Debug, Serialize, Deserialize, ToSchema)]
|
||||
pub struct MessageResponse {
|
||||
pub id: Uuid,
|
||||
pub channel_id: Uuid,
|
||||
pub user_id: Uuid,
|
||||
pub content: String,
|
||||
pub created_at: DateTime<Utc>,
|
||||
pub updated_at: Option<DateTime<Utc>>,
|
||||
pub reply_to_id: Option<Uuid>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct UpdateMessageRequest {}
|
||||
#[derive(Debug, Serialize, Deserialize, ToSchema)]
|
||||
pub struct CreateMessageRequest {
|
||||
pub channel_id: Uuid,
|
||||
pub content: String,
|
||||
pub reply_to_id: Option<Uuid>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct MessageResponse {}
|
||||
#[derive(Debug, Serialize, Deserialize, ToSchema)]
|
||||
pub struct UpdateMessageRequest {
|
||||
pub content: String,
|
||||
}
|
||||
|
||||
+177
-12
@@ -1,22 +1,187 @@
|
||||
use axum::http::StatusCode;
|
||||
use axum::response::IntoResponse;
|
||||
use crate::core::state::AppState;
|
||||
use crate::http::context::CurrentUser;
|
||||
use crate::http::error::HTTPError;
|
||||
use crate::routes::message::dto::{CreateMessageRequest, MessageResponse, UpdateMessageRequest};
|
||||
use crate::routes::message::mapper;
|
||||
use axum::{
|
||||
extract::{Path, State},
|
||||
http::StatusCode,
|
||||
Json,
|
||||
};
|
||||
use uuid::Uuid;
|
||||
|
||||
pub async fn get_all() -> impl IntoResponse {
|
||||
StatusCode::NOT_IMPLEMENTED
|
||||
/// Liste tous les messages
|
||||
#[utoipa::path(
|
||||
get,
|
||||
path = "/messages",
|
||||
responses(
|
||||
(status = 200, description = "Liste des messages récupérée avec succès", body = [MessageResponse]),
|
||||
(status = 500, description = "Erreur interne du serveur")
|
||||
),
|
||||
tag = "Messages"
|
||||
)]
|
||||
pub async fn get_all(
|
||||
State(state): State<AppState>,
|
||||
) -> Result<Json<Vec<MessageResponse>>, HTTPError> {
|
||||
let messages = state.repositories.message.get_all().await?;
|
||||
Ok(Json(
|
||||
messages
|
||||
.into_iter()
|
||||
.map(mapper::message_model_to_message_response)
|
||||
.collect(),
|
||||
))
|
||||
}
|
||||
|
||||
pub async fn get_by_id() -> impl IntoResponse {
|
||||
StatusCode::NOT_IMPLEMENTED
|
||||
/// Récupère un message par son ID
|
||||
#[utoipa::path(
|
||||
get,
|
||||
path = "/messages/{id}",
|
||||
responses(
|
||||
(status = 200, description = "Message trouvé", body = MessageResponse),
|
||||
(status = 404, description = "Message non trouvé"),
|
||||
(status = 500, description = "Erreur interne du serveur")
|
||||
),
|
||||
params(
|
||||
("id" = Uuid, Path, description = "ID du message")
|
||||
),
|
||||
tag = "Messages"
|
||||
)]
|
||||
pub async fn get_by_id(
|
||||
State(state): State<AppState>,
|
||||
Path(id): Path<Uuid>,
|
||||
) -> Result<Json<MessageResponse>, HTTPError> {
|
||||
let message = state
|
||||
.repositories
|
||||
.message
|
||||
.get_by_id(id)
|
||||
.await?
|
||||
.ok_or(HTTPError::NotFound)?;
|
||||
|
||||
Ok(Json(mapper::message_model_to_message_response(message)))
|
||||
}
|
||||
|
||||
pub async fn create() -> impl IntoResponse {
|
||||
StatusCode::NOT_IMPLEMENTED
|
||||
/// Crée un nouveau message
|
||||
#[utoipa::path(
|
||||
post,
|
||||
path = "/messages",
|
||||
request_body = CreateMessageRequest,
|
||||
responses(
|
||||
(status = 201, description = "Message créé avec succès", body = MessageResponse),
|
||||
(status = 400, description = "Données invalides (canal non trouvé)"),
|
||||
(status = 500, description = "Erreur interne du serveur")
|
||||
),
|
||||
tag = "Messages"
|
||||
)]
|
||||
pub async fn create(
|
||||
user: CurrentUser,
|
||||
State(state): State<AppState>,
|
||||
Json(payload): Json<CreateMessageRequest>,
|
||||
) -> Result<(StatusCode, Json<MessageResponse>), HTTPError> {
|
||||
// Vérifier que le canal existe
|
||||
state
|
||||
.repositories
|
||||
.channel
|
||||
.get_by_id(payload.channel_id)
|
||||
.await?
|
||||
.ok_or(HTTPError::BadRequest("Channel not found".to_string()))?;
|
||||
|
||||
// Optionnel: vérifier reply_to_id
|
||||
if let Some(reply_id) = payload.reply_to_id {
|
||||
state
|
||||
.repositories
|
||||
.message
|
||||
.get_by_id(reply_id)
|
||||
.await?
|
||||
.ok_or(HTTPError::BadRequest(
|
||||
"Parent message not found".to_string(),
|
||||
))?;
|
||||
}
|
||||
|
||||
let active_model = mapper::create_request_to_am(user.id, payload);
|
||||
let message = state.repositories.message.create(active_model).await?;
|
||||
Ok((
|
||||
StatusCode::CREATED,
|
||||
Json(mapper::message_model_to_message_response(message)),
|
||||
))
|
||||
}
|
||||
|
||||
pub async fn update() -> impl IntoResponse {
|
||||
StatusCode::NOT_IMPLEMENTED
|
||||
/// Met à jour un message existant
|
||||
#[utoipa::path(
|
||||
put,
|
||||
path = "/messages/{id}",
|
||||
request_body = UpdateMessageRequest,
|
||||
responses(
|
||||
(status = 200, description = "Message mis à jour avec succès", body = MessageResponse),
|
||||
(status = 403, description = "Interdit (pas l'auteur)"),
|
||||
(status = 404, description = "Message non trouvé"),
|
||||
(status = 500, description = "Erreur interne du serveur")
|
||||
),
|
||||
params(
|
||||
("id" = Uuid, Path, description = "ID du message")
|
||||
),
|
||||
tag = "Messages"
|
||||
)]
|
||||
pub async fn update(
|
||||
user: CurrentUser,
|
||||
State(state): State<AppState>,
|
||||
Path(id): Path<Uuid>,
|
||||
Json(payload): Json<UpdateMessageRequest>,
|
||||
) -> Result<Json<MessageResponse>, HTTPError> {
|
||||
// Vérifier l'existence
|
||||
let message = state
|
||||
.repositories
|
||||
.message
|
||||
.get_by_id(id)
|
||||
.await?
|
||||
.ok_or(HTTPError::NotFound)?;
|
||||
|
||||
// Vérifier que l'utilisateur est l'auteur
|
||||
if message.user_id != user.id && !user.is_superuser {
|
||||
return Err(HTTPError::Forbidden);
|
||||
}
|
||||
|
||||
let active_model = mapper::update_request_to_am(message, payload);
|
||||
let message = state.repositories.message.update(active_model).await?;
|
||||
|
||||
Ok(Json(mapper::message_model_to_message_response(message)))
|
||||
}
|
||||
|
||||
pub async fn delete() -> impl IntoResponse {
|
||||
StatusCode::NOT_IMPLEMENTED
|
||||
/// Supprime un message
|
||||
#[utoipa::path(
|
||||
delete,
|
||||
path = "/messages/{id}",
|
||||
responses(
|
||||
(status = 204, description = "Message supprimé avec succès"),
|
||||
(status = 403, description = "Interdit (pas l'auteur ou admin)"),
|
||||
(status = 404, description = "Message non trouvé"),
|
||||
(status = 500, description = "Erreur interne du serveur")
|
||||
),
|
||||
params(
|
||||
("id" = Uuid, Path, description = "ID du message")
|
||||
),
|
||||
tag = "Messages"
|
||||
)]
|
||||
pub async fn delete(
|
||||
user: CurrentUser,
|
||||
State(state): State<AppState>,
|
||||
Path(id): Path<Uuid>,
|
||||
) -> Result<StatusCode, HTTPError> {
|
||||
// Vérifier l'existence pour l'autorisation
|
||||
let message = state
|
||||
.repositories
|
||||
.message
|
||||
.get_by_id(id)
|
||||
.await?
|
||||
.ok_or(HTTPError::NotFound)?;
|
||||
|
||||
// Autoriser si auteur ou superuser
|
||||
if message.user_id != user.id && !user.is_superuser {
|
||||
return Err(HTTPError::Forbidden);
|
||||
}
|
||||
|
||||
if state.repositories.message.delete(id).await? {
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
} else {
|
||||
Err(HTTPError::NotFound)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,44 @@
|
||||
use super::{domain::Message, dto::MessageResponse};
|
||||
use crate::models::message;
|
||||
use crate::routes::message::dto::{CreateMessageRequest, MessageResponse, UpdateMessageRequest};
|
||||
use chrono::Utc;
|
||||
use sea_orm::Set;
|
||||
use uuid::Uuid;
|
||||
|
||||
pub fn to_response(_item: Message) -> MessageResponse {
|
||||
todo!()
|
||||
pub fn message_model_to_message_response(model: message::Model) -> MessageResponse {
|
||||
MessageResponse {
|
||||
id: model.id,
|
||||
channel_id: model.channel_id,
|
||||
user_id: model.user_id,
|
||||
content: model.content,
|
||||
created_at: model.created_at,
|
||||
updated_at: model.updated_at,
|
||||
reply_to_id: model.reply_to_id,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_request_to_am(user_id: Uuid, payload: CreateMessageRequest) -> message::ActiveModel {
|
||||
message::ActiveModel {
|
||||
id: Set(Uuid::now_v7()),
|
||||
channel_id: Set(payload.channel_id),
|
||||
user_id: Set(user_id),
|
||||
content: Set(payload.content),
|
||||
created_at: Set(Utc::now()),
|
||||
updated_at: Set(None),
|
||||
reply_to_id: Set(payload.reply_to_id),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_request_to_am(
|
||||
model: message::Model,
|
||||
payload: UpdateMessageRequest,
|
||||
) -> message::ActiveModel {
|
||||
message::ActiveModel {
|
||||
id: Set(model.id),
|
||||
channel_id: Set(model.channel_id),
|
||||
user_id: Set(model.user_id),
|
||||
content: Set(payload.content),
|
||||
created_at: Set(model.created_at),
|
||||
updated_at: Set(Some(Utc::now())),
|
||||
reply_to_id: Set(model.reply_to_id),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
use axum::{Router, routing::get};
|
||||
|
||||
use super::handlers;
|
||||
use crate::core::state::AppState;
|
||||
use axum::{routing::get, Router};
|
||||
|
||||
pub fn router() -> Router {
|
||||
pub fn router() -> Router<AppState> {
|
||||
Router::new()
|
||||
.route("/messages", get(handlers::get_all).post(handlers::create))
|
||||
.route(
|
||||
|
||||
@@ -1,21 +1 @@
|
||||
use super::domain::Message;
|
||||
|
||||
pub async fn find_all() -> Vec<Message> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
pub async fn find_by_id(_id: u64) -> Option<Message> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
pub async fn create(_item: Message) -> Message {
|
||||
todo!()
|
||||
}
|
||||
|
||||
pub async fn update(_id: u64, _item: Message) -> Option<Message> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
pub async fn delete(_id: u64) -> bool {
|
||||
todo!()
|
||||
}
|
||||
// Service layer for Message - currently unused in the simplified architecture
|
||||
|
||||
Reference in New Issue
Block a user