init
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import {onMounted, ref} from 'vue';
|
import {computed, onMounted, ref} from 'vue';
|
||||||
import {useRoute} from 'vue-router';
|
import {useRoute} from 'vue-router';
|
||||||
import {storeToRefs} from 'pinia'; // Import crucial
|
import {storeToRefs} from 'pinia'; // Import crucial
|
||||||
import {useMessageStore} from '@/stores/message';
|
import {useMessageStore} from '@/stores/message';
|
||||||
@@ -11,15 +11,15 @@ const messageStore = useMessageStore();
|
|||||||
const {messages, loading} = storeToRefs(messageStore);
|
const {messages, loading} = storeToRefs(messageStore);
|
||||||
|
|
||||||
const newMessage = ref('');
|
const newMessage = ref('');
|
||||||
|
const channelId = computed(() => route.params.channelId as string);
|
||||||
|
|
||||||
const sendMessage = async () => {
|
const sendMessage = async () => {
|
||||||
if (!newMessage.value.trim()) return;
|
if (!newMessage.value.trim()) return;
|
||||||
|
|
||||||
const content = newMessage.value;
|
const content = newMessage.value;
|
||||||
const channelId = route.params.id as string;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await messageStore.sendMessage(channelId, content);
|
await messageStore.sendMessage(channelId.value, content);
|
||||||
newMessage.value = ''; // On vide le champ après succès
|
newMessage.value = ''; // On vide le champ après succès
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// Gérer l'erreur (ex: notification toast)
|
// Gérer l'erreur (ex: notification toast)
|
||||||
@@ -27,9 +27,8 @@ const sendMessage = async () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
const channelId = route.params.id as string;
|
|
||||||
if (channelId) {
|
if (channelId) {
|
||||||
messageStore.fetchMessages(channelId);
|
messageStore.fetchMessages(channelId.value);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
@@ -48,14 +47,14 @@ onMounted(() => {
|
|||||||
>
|
>
|
||||||
<template v-slot:prepend>
|
<template v-slot:prepend>
|
||||||
<v-avatar color="grey-lighten-2" size="40">
|
<v-avatar color="grey-lighten-2" size="40">
|
||||||
<v-img v-if="msg.user.avatar" :src="msg.user.avatar"></v-img>
|
<!-- <v-img v-if="msg.user.avatar" :src="msg.user.avatar"></v-img>-->
|
||||||
<v-icon v-else icon="mdi-account"></v-icon>
|
<v-icon icon="mdi-account"></v-icon>
|
||||||
</v-avatar>
|
</v-avatar>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<v-list-item-title class="d-flex align-center">
|
<v-list-item-title class="d-flex align-center">
|
||||||
<span class="font-weight-bold text-subtitle-1 mr-2">{{ msg.user.name }}</span>
|
<span class="font-weight-bold text-subtitle-1 mr-2">{{ msg.user_id }}</span>
|
||||||
<span class="text-caption text-grey">{{ msg.timestamp }}</span>
|
<span class="text-caption text-grey">{{ msg.created_at }}</span>
|
||||||
</v-list-item-title>
|
</v-list-item-title>
|
||||||
|
|
||||||
<v-list-item-subtitle class="text-body-1 text-high-emphasis opacity-100">
|
<v-list-item-subtitle class="text-body-1 text-high-emphasis opacity-100">
|
||||||
|
|||||||
@@ -3,9 +3,12 @@ import {useApi} from "@/composables/useApi.ts";
|
|||||||
|
|
||||||
interface Message {
|
interface Message {
|
||||||
id: string;
|
id: string;
|
||||||
|
channel_id: string;
|
||||||
|
user_id: string;
|
||||||
content: string;
|
content: string;
|
||||||
user: { name: string; avatar: string };
|
created_at: string;
|
||||||
timestamp: string;
|
updated_at: string | null;
|
||||||
|
reply_to_id: string | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useMessageStore = defineStore("message", {
|
export const useMessageStore = defineStore("message", {
|
||||||
@@ -16,10 +19,16 @@ export const useMessageStore = defineStore("message", {
|
|||||||
actions: {
|
actions: {
|
||||||
async fetchMessages(channel_id: string) {
|
async fetchMessages(channel_id: string) {
|
||||||
this.loading = true;
|
this.loading = true;
|
||||||
|
|
||||||
|
// Query params
|
||||||
|
let params = new URLSearchParams();
|
||||||
|
params.append("channel_id", channel_id);
|
||||||
|
const queryString = params.toString();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const api = useApi();
|
const api = useApi();
|
||||||
// Utilisation du paramètre pour cibler le channel
|
// Utilisation du paramètre pour cibler le channel
|
||||||
const response = await api.get(`/api/messages/${channel_id}`);
|
const response = await api.get(`/messages${queryString ? `?${queryString}` : ""}`);
|
||||||
this.messages = await response.json();
|
this.messages = await response.json();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Erreur lors du chargement des messages:", error);
|
console.error("Erreur lors du chargement des messages:", error);
|
||||||
@@ -29,9 +38,14 @@ export const useMessageStore = defineStore("message", {
|
|||||||
},
|
},
|
||||||
async sendMessage(channelId: string, content: string) {
|
async sendMessage(channelId: string, content: string) {
|
||||||
const api = useApi();
|
const api = useApi();
|
||||||
|
console.log("channelId", channelId);
|
||||||
try {
|
try {
|
||||||
// Envoi au serveur pour persistance
|
// Envoi au serveur pour persistance
|
||||||
const response = await api.post(`/api/messages/${channelId}`, {content});
|
const response = await api.post('/messages', {
|
||||||
|
channel_id: channelId,
|
||||||
|
content: content,
|
||||||
|
reply_to_id: null
|
||||||
|
});
|
||||||
const newMessage = await response.json();
|
const newMessage = await response.json();
|
||||||
|
|
||||||
// Ajout local immédiat (optimistic update)
|
// Ajout local immédiat (optimistic update)
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use crate::models::message;
|
use crate::models::message;
|
||||||
use crate::repositories::{AnyResult, RepositoryContext};
|
use crate::repositories::{AnyResult, RepositoryContext};
|
||||||
use sea_orm::{ActiveModelTrait, EntityTrait};
|
use sea_orm::{ActiveModelTrait, ColumnTrait, EntityTrait, QueryFilter};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
@@ -19,6 +19,13 @@ impl MessageRepository {
|
|||||||
.await?)
|
.await?)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn get_by_channel(&self, channel_id: uuid::Uuid) -> AnyResult<Vec<message::Model>> {
|
||||||
|
Ok(message::Entity::find()
|
||||||
|
.filter(message::Column::ChannelId.eq(channel_id))
|
||||||
|
.all(&self.context.db)
|
||||||
|
.await?)
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn update(&self, active: message::ActiveModel) -> AnyResult<message::Model> {
|
pub async fn update(&self, active: message::ActiveModel) -> AnyResult<message::Model> {
|
||||||
let message = active.update(&self.context.db).await?;
|
let message = active.update(&self.context.db).await?;
|
||||||
self.context.events.emit("message_updated", message.clone());
|
self.context.events.emit("message_updated", message.clone());
|
||||||
|
|||||||
@@ -25,3 +25,8 @@ pub struct CreateMessageRequest {
|
|||||||
pub struct UpdateMessageRequest {
|
pub struct UpdateMessageRequest {
|
||||||
pub content: String,
|
pub content: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(serde::Deserialize, utoipa::IntoParams)]
|
||||||
|
pub struct MessageFilters {
|
||||||
|
pub channel_id: Option<uuid::Uuid>,
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
|
use super::dto::{CreateMessageRequest, MessageFilters, MessageResponse, UpdateMessageRequest};
|
||||||
use crate::core::state::AppState;
|
use crate::core::state::AppState;
|
||||||
use crate::http::context::CurrentUser;
|
use crate::http::context::CurrentUser;
|
||||||
use crate::http::error::HTTPError;
|
use crate::http::error::HTTPError;
|
||||||
use crate::routes::message::dto::{CreateMessageRequest, MessageResponse, UpdateMessageRequest};
|
|
||||||
use crate::routes::message::mapper;
|
use crate::routes::message::mapper;
|
||||||
use axum::{
|
use axum::{
|
||||||
extract::{Path, State},
|
extract::{Path, Query, State},
|
||||||
http::StatusCode,
|
http::StatusCode,
|
||||||
Json,
|
Json,
|
||||||
};
|
};
|
||||||
@@ -18,12 +18,24 @@ use uuid::Uuid;
|
|||||||
(status = 200, description = "Liste des messages récupérée avec succès", body = [MessageResponse]),
|
(status = 200, description = "Liste des messages récupérée avec succès", body = [MessageResponse]),
|
||||||
(status = 500, description = "Erreur interne du serveur")
|
(status = 500, description = "Erreur interne du serveur")
|
||||||
),
|
),
|
||||||
|
params(
|
||||||
|
MessageFilters
|
||||||
|
),
|
||||||
tag = "Messages"
|
tag = "Messages"
|
||||||
)]
|
)]
|
||||||
pub async fn get_all(
|
pub async fn get_all(
|
||||||
State(state): State<AppState>,
|
State(state): State<AppState>,
|
||||||
|
Query(filters): Query<MessageFilters>,
|
||||||
) -> Result<Json<Vec<MessageResponse>>, HTTPError> {
|
) -> Result<Json<Vec<MessageResponse>>, HTTPError> {
|
||||||
let messages = state.repositories.message.get_all().await?;
|
let messages = if let Some(channel_id) = filters.channel_id {
|
||||||
|
state
|
||||||
|
.repositories
|
||||||
|
.message
|
||||||
|
.get_by_channel(channel_id)
|
||||||
|
.await?
|
||||||
|
} else {
|
||||||
|
state.repositories.message.get_all().await?
|
||||||
|
};
|
||||||
Ok(Json(
|
Ok(Json(
|
||||||
messages
|
messages
|
||||||
.into_iter()
|
.into_iter()
|
||||||
|
|||||||
Reference in New Issue
Block a user