Init
This commit is contained in:
@@ -33,3 +33,15 @@ impl From<CreateCategoryRequest> for category::ActiveModel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl CreateCategoryRequest {
|
||||||
|
pub fn apply_to(self, mut am: category::ActiveModel) -> category::ActiveModel {
|
||||||
|
am.name = Set(self.name);
|
||||||
|
am
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
pub struct ListCategoryQuery {
|
||||||
|
pub server_id: Uuid,
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
// On importe le modèle pour pouvoir mapper
|
use crate::interfaces::http::dto::category::CategoryResponse;
|
||||||
|
use crate::interfaces::http::dto::channel::ChannelResponse;
|
||||||
use crate::models::server;
|
use crate::models::server;
|
||||||
|
use crate::repositories::types::{ServerExplorerItem, ServerTree};
|
||||||
use sea_orm::Set;
|
use sea_orm::Set;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
@@ -39,10 +41,23 @@ impl From<CreateServerRequest> for server::ActiveModel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl CreateServerRequest {
|
||||||
|
pub fn apply_to(self, mut am: server::ActiveModel) -> server::ActiveModel {
|
||||||
|
am.name = Set(self.name);
|
||||||
|
am.password = Set(self.password);
|
||||||
|
am
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
#[serde(tag = "type", rename_all = "snake_case")]
|
#[serde(tag = "type", rename_all = "snake_case")]
|
||||||
pub enum TreeItemType {
|
pub enum TreeItemType {
|
||||||
// todo : faire le CategoryResponse et ChannelResponse
|
Category {
|
||||||
|
#[serde(flatten)]
|
||||||
|
category: CategoryResponse,
|
||||||
|
channels: Vec<ChannelResponse>,
|
||||||
|
},
|
||||||
|
Channel(ChannelResponse),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
@@ -50,3 +65,23 @@ pub enum TreeItemType {
|
|||||||
pub struct ServerTreeResponse {
|
pub struct ServerTreeResponse {
|
||||||
items: Vec<TreeItemType>,
|
items: Vec<TreeItemType>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<ServerTree> for ServerTreeResponse {
|
||||||
|
fn from(layout: ServerTree) -> Self {
|
||||||
|
Self {
|
||||||
|
items: layout
|
||||||
|
.items
|
||||||
|
.into_iter()
|
||||||
|
.map(|item| match item {
|
||||||
|
ServerExplorerItem::Category(cat, chans) => TreeItemType::Category {
|
||||||
|
category: CategoryResponse::from(cat),
|
||||||
|
channels: chans.into_iter().map(ChannelResponse::from).collect(),
|
||||||
|
},
|
||||||
|
ServerExplorerItem::Channel(chan) => {
|
||||||
|
TreeItemType::Channel(ChannelResponse::from(chan))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -107,7 +107,7 @@ pub async fn category_update(
|
|||||||
|
|
||||||
// todo : voir pour virer le into_active_model pour utiliser le dto
|
// todo : voir pour virer le into_active_model pour utiliser le dto
|
||||||
let category: category::Model = serializer
|
let category: category::Model = serializer
|
||||||
.apply_to_active_model(active)
|
.apply_to(active)
|
||||||
.update(app_state.db.get_connection())
|
.update(app_state.db.get_connection())
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
use crate::app::AppState;
|
use crate::app::AppState;
|
||||||
use crate::interfaces::http::dto::server::ServerResponse;
|
use crate::interfaces::http::dto::server::{
|
||||||
|
CreateServerRequest, ServerResponse, ServerTreeResponse,
|
||||||
|
};
|
||||||
|
use crate::models::server;
|
||||||
use crate::network::http::{AppRouter, HTTPError};
|
use crate::network::http::{AppRouter, HTTPError};
|
||||||
use crate::serializers::{ServerSerializer, ServerTreeSerializer};
|
|
||||||
use axum::extract::{Path, State};
|
use axum::extract::{Path, State};
|
||||||
use axum::http::StatusCode;
|
use axum::http::StatusCode;
|
||||||
use axum::routing::get;
|
use axum::routing::get;
|
||||||
@@ -46,9 +48,9 @@ pub async fn server_detail(
|
|||||||
|
|
||||||
pub async fn server_create(
|
pub async fn server_create(
|
||||||
State(state): State<AppState>,
|
State(state): State<AppState>,
|
||||||
Json(serializer): Json<ServerSerializer>,
|
Json(serializer): Json<CreateServerRequest>,
|
||||||
) -> Result<Json<ServerResponse>, HTTPError> {
|
) -> Result<Json<ServerResponse>, HTTPError> {
|
||||||
let active = serializer.into_active_model();
|
let active: server::ActiveModel = serializer.into();
|
||||||
let server = state.repositories.server.create(active).await?;
|
let server = state.repositories.server.create(active).await?;
|
||||||
|
|
||||||
Ok(Json(ServerResponse::from(server)))
|
Ok(Json(ServerResponse::from(server)))
|
||||||
@@ -57,7 +59,7 @@ pub async fn server_create(
|
|||||||
pub async fn server_update(
|
pub async fn server_update(
|
||||||
State(state): State<AppState>,
|
State(state): State<AppState>,
|
||||||
Path(id): Path<Uuid>,
|
Path(id): Path<Uuid>,
|
||||||
Json(serializer): Json<ServerSerializer>,
|
Json(serializer): Json<CreateServerRequest>,
|
||||||
) -> Result<Json<ServerResponse>, HTTPError> {
|
) -> Result<Json<ServerResponse>, HTTPError> {
|
||||||
let am_server = state
|
let am_server = state
|
||||||
.repositories
|
.repositories
|
||||||
@@ -70,7 +72,7 @@ pub async fn server_update(
|
|||||||
let server = state
|
let server = state
|
||||||
.repositories
|
.repositories
|
||||||
.server
|
.server
|
||||||
.update(serializer.apply_to_active_model(am_server))
|
.update(serializer.apply_to(am_server))
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok(Json(ServerResponse::from(server)))
|
Ok(Json(ServerResponse::from(server)))
|
||||||
@@ -104,8 +106,8 @@ pub async fn server_password(
|
|||||||
pub async fn tree(
|
pub async fn tree(
|
||||||
State(state): State<AppState>,
|
State(state): State<AppState>,
|
||||||
Path(id): Path<Uuid>,
|
Path(id): Path<Uuid>,
|
||||||
) -> Result<Json<ServerTreeSerializer>, HTTPError> {
|
) -> Result<Json<ServerTreeResponse>, HTTPError> {
|
||||||
let layout = state.repositories.server.get_tree(id).await?;
|
let layout = state.repositories.server.get_tree(id).await?;
|
||||||
|
|
||||||
Ok(Json(ServerTreeSerializer::from(layout)))
|
Ok(Json(ServerTreeResponse::from(layout)))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user