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::repositories::types::{ServerExplorerItem, ServerTree};
|
||||
use sea_orm::Set;
|
||||
use serde::{Deserialize, Serialize};
|
||||
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)]
|
||||
#[serde(tag = "type", rename_all = "snake_case")]
|
||||
pub enum TreeItemType {
|
||||
// todo : faire le CategoryResponse et ChannelResponse
|
||||
Category {
|
||||
#[serde(flatten)]
|
||||
category: CategoryResponse,
|
||||
channels: Vec<ChannelResponse>,
|
||||
},
|
||||
Channel(ChannelResponse),
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
@@ -50,3 +65,23 @@ pub enum TreeItemType {
|
||||
pub struct ServerTreeResponse {
|
||||
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
|
||||
let category: category::Model = serializer
|
||||
.apply_to_active_model(active)
|
||||
.apply_to(active)
|
||||
.update(app_state.db.get_connection())
|
||||
.await?;
|
||||
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
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::serializers::{ServerSerializer, ServerTreeSerializer};
|
||||
use axum::extract::{Path, State};
|
||||
use axum::http::StatusCode;
|
||||
use axum::routing::get;
|
||||
@@ -46,9 +48,9 @@ pub async fn server_detail(
|
||||
|
||||
pub async fn server_create(
|
||||
State(state): State<AppState>,
|
||||
Json(serializer): Json<ServerSerializer>,
|
||||
Json(serializer): Json<CreateServerRequest>,
|
||||
) -> Result<Json<ServerResponse>, HTTPError> {
|
||||
let active = serializer.into_active_model();
|
||||
let active: server::ActiveModel = serializer.into();
|
||||
let server = state.repositories.server.create(active).await?;
|
||||
|
||||
Ok(Json(ServerResponse::from(server)))
|
||||
@@ -57,7 +59,7 @@ pub async fn server_create(
|
||||
pub async fn server_update(
|
||||
State(state): State<AppState>,
|
||||
Path(id): Path<Uuid>,
|
||||
Json(serializer): Json<ServerSerializer>,
|
||||
Json(serializer): Json<CreateServerRequest>,
|
||||
) -> Result<Json<ServerResponse>, HTTPError> {
|
||||
let am_server = state
|
||||
.repositories
|
||||
@@ -70,7 +72,7 @@ pub async fn server_update(
|
||||
let server = state
|
||||
.repositories
|
||||
.server
|
||||
.update(serializer.apply_to_active_model(am_server))
|
||||
.update(serializer.apply_to(am_server))
|
||||
.await?;
|
||||
|
||||
Ok(Json(ServerResponse::from(server)))
|
||||
@@ -104,8 +106,8 @@ pub async fn server_password(
|
||||
pub async fn tree(
|
||||
State(state): State<AppState>,
|
||||
Path(id): Path<Uuid>,
|
||||
) -> Result<Json<ServerTreeSerializer>, HTTPError> {
|
||||
) -> Result<Json<ServerTreeResponse>, HTTPError> {
|
||||
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