From 4da5c12c334d368a0fc247f9cd1c12a23383b7a0 Mon Sep 17 00:00:00 2001 From: David Westgate Date: Fri, 26 Apr 2024 19:18:14 -0700 Subject: [PATCH] add question.rs draft --- src/question.rs | 69 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 58 insertions(+), 11 deletions(-) diff --git a/src/question.rs b/src/question.rs index 96ea397..17a5e9e 100644 --- a/src/question.rs +++ b/src/question.rs @@ -1,14 +1,61 @@ -struct Question { - id: QuestionId, +use std::collections::HashMap; + + + +struct Store { + questions: HashMap, +} + +impl Store { + fn new() -> Self { + Store { + questions: HashMap::new(), + } + } + fn add(mut self, question: Question) -> Result { + match self.questions.get(&question.id) { + Some(_) => Err(format!("Question with id {} already exists", question.id)), + None => Ok(self + .questions + .insert(question.id.clone(), question) + .unwrap()), + } + } + fn remove(mut self, id: u8) -> Result { + match self.questions.remove(&id) { + Some(question) => Ok(question), + None => Err(format!("Question with id {} does not exist", id)), + } + } + fn fetch_one(self, id: u8) -> Result { + match self.questions.get(&id) { + Some(question) => Ok(question.clone()), + None => Err(format!("Question with id {} does not exist", id)), + } + } + fn fetch_all(self) -> Vec { + self.questions.values().cloned().collect() + } + fn update(mut self, question: Question) -> Result { + match self.questions.get(&question.id) { + Some(_) => Ok(self + .questions + .insert(question.id.clone(), question) + .unwrap()), + None => Err(format!("Question with id {} does not exists", question.id)), + } + } +} +#[derive(Clone)] +pub(crate) struct Question { + id: u8, title: String, content: String, tags: Option>, } -struct QuestionId(String); - -impl Question{ - fn new(id: QuestionId, title: String, content: String, tags: Option>) -> Self { +impl Question { + fn new(id: u8, title: String, content: String, tags: Option>) -> Self { Question { id, title, @@ -18,8 +65,8 @@ impl Question{ } } -impl IntoResponse for &Question { - fn into_response(self) -> Response { - (StatusCode::OK, Json(&self)).into_response() - } -} \ No newline at end of file +// impl IntoResponse for &Question { +// fn into_response(self) -> Response { +// (StatusCode::OK, Json(&self)).into_response() +// } +// }