1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
use rouille; use rouille::router; use serde::Deserialize; use serde::Serialize; use serde_json; use log::warn; use crate::errors::Error; use crate::errors::ErrorKind; use super::schema::questions; #[derive(Queryable, Serialize, Deserialize, Clone, Debug)] pub struct Question { pub id: u64, pub category_id: u64, pub title: String, pub correct_answer: String, pub incorrect_answer_1: String, pub incorrect_answer_2: String, pub incorrect_answer_3: String, } #[derive(Insertable, Serialize, Deserialize, Debug)] #[table_name = "questions"] pub struct NewRawQuestion { pub title: String, pub category_id: u64, pub correct_answer: String, pub incorrect_answer_1: String, pub incorrect_answer_2: String, pub incorrect_answer_3: String, } #[derive(Serialize, Deserialize, Debug)] pub struct NewQuestion { pub title: String, pub correct_answer: String, pub incorrect_answer_1: String, pub incorrect_answer_2: String, pub incorrect_answer_3: String, } #[derive(AsChangeset, Serialize, Deserialize, Debug)] #[table_name = "questions"] pub struct PartialQuestion { pub title: Option<String>, pub category_id: Option<u64>, pub correct_answer: Option<String>, pub incorrect_answer_1: Option<String>, pub incorrect_answer_2: Option<String>, pub incorrect_answer_3: Option<String>, } #[derive(Serialize, Deserialize, Debug)] pub struct QuestionList { pub questions: Vec<Question>, } #[derive(Serialize, Deserialize, Debug)] pub struct AnonymousQuestion { pub id: u64, pub title: String, pub answer_1: String, pub answer_2: String, pub answer_3: String, pub answer_4: String, } #[derive(Serialize, Deserialize, Debug)] pub struct AnonymousQuestionList { pub questions: Vec<AnonymousQuestion>, } #[derive(Serialize, Deserialize, Debug)] pub struct ResponseQuestion { pub id: u64, pub answer: String, } #[derive(Serialize, Deserialize, Debug)] pub struct ResponseQuestionList { pub questions: Vec<ResponseQuestion>, } pub enum QuestionRequest { GetQuestions, UpdateQuestion(u64, PartialQuestion), CreateQuestion(NewRawQuestion), DeleteQuestion(u64), } impl QuestionRequest { pub fn from_rouille(request: &rouille::Request) -> Result<QuestionRequest, Error> { router!(request, (GET) (/) => { Ok(QuestionRequest::GetQuestions) }, (POST) (/) => { let request_body = request.data() .ok_or(Error::new(ErrorKind::Body))?; let new_question: NewRawQuestion = serde_json::from_reader(request_body)?; Ok(QuestionRequest::CreateQuestion(new_question)) }, (PUT) (/{id: u64}) => { let request_body = request.data() .ok_or(Error::new(ErrorKind::Body))?; let partial_question: PartialQuestion = serde_json::from_reader(request_body)?; Ok(QuestionRequest::UpdateQuestion(id, partial_question)) }, (DELETE) (/{id: u64}) => { Ok(QuestionRequest::DeleteQuestion(id)) }, _ => { warn!("Could not create a question request for the given rouille request"); Err(Error::new(ErrorKind::NotFound)) } ) } } pub enum QuestionResponse { OneQuestion(Question), ManyQuestions(QuestionList), NoResponse, } impl QuestionResponse { pub fn to_rouille(self) -> rouille::Response { match self { QuestionResponse::OneQuestion(question) => rouille::Response::json(&question), QuestionResponse::ManyQuestions(questions) => rouille::Response::json(&questions), QuestionResponse::NoResponse => rouille::Response::empty_204(), } } }