This repository has been archived on 2025-04-28. You can view files and clone it, but cannot push or open issues or pull requests.
rust-web/src/question.rs
David Westgate 03257ef1e3 comment code
2024-04-27 01:46:30 -07:00

61 lines
1.6 KiB
Rust

/// Contains struct definitions regarding questions
use crate::*;
#[derive(Deserialize, Serialize, Clone, Debug)]
pub struct QuestionDTO {
pub id: u8,
pub title: String,
pub content: String,
pub tags: Option<Vec<String>>,
}
/// Question Data Transfer Object, a representation of the expected serialized JSON formated of questions regarding requests, responses, and our question json file
impl QuestionDTO {
pub fn to_entity(&self) -> (u8, Question) {
(
self.id,
Question {
title: self.title.clone(),
content: self.content.clone(),
tags: self.tags.clone(),
},
)
}
}
impl IntoResponse for &QuestionDTO {
fn into_response(self) -> Response {
(StatusCode::OK, Json(&self)).into_response()
}
}
/// Question 'entity' used for in-memory interactions of questions by the store
#[derive(Deserialize, Serialize, Clone, Debug)]
pub struct Question {
title: String,
content: String,
tags: Option<Vec<String>>,
}
impl Question {
pub fn _new(_id: u8, title: String, content: String, tags: Option<Vec<String>>) -> Self {
Question {
title,
content,
tags,
}
}
pub fn to_dto(&self, id: u8) -> QuestionDTO {
QuestionDTO {
id,
title: self.title.clone(),
content: self.content.clone(),
tags: self.tags.clone(),
}
}
}
impl IntoResponse for &Question {
fn into_response(self) -> Response {
(StatusCode::OK, Json(&self)).into_response()
}
}