Skip to content

Commit

Permalink
single image support added
Browse files Browse the repository at this point in the history
  • Loading branch information
indpurvesh committed Nov 19, 2024
1 parent d6faac8 commit 1408a3e
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 62 deletions.
4 changes: 2 additions & 2 deletions src/api/handlers/page/fetch_page_api_handler.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Arc;

use crate::models::page_model::NewPageModel;
use crate::models::page_model::PageModel;
use crate::{
avored_state::AvoRedState, error::Result
};
Expand Down Expand Up @@ -41,5 +41,5 @@ pub async fn fetch_page_api_handler(
#[derive(Serialize, Debug)]
pub struct FetchPageResponse {
pub status: bool,
pub page_model: NewPageModel
pub page_model: PageModel
}
10 changes: 4 additions & 6 deletions src/api/handlers/page/store_page_api_handler.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::sync::Arc;

use crate::error::Error;
use crate::models::page_model::{CreatablePageField, NewCreatablePageModel, NewPageModel};
use crate::models::page_model::{CreatablePageField, CreatablePageModel, PageModel};
use crate::models::validation_error::ErrorResponse;
use crate::{
avored_state::AvoRedState, error::Result
Expand All @@ -15,7 +15,7 @@ pub async fn store_page_api_handler(
Extension(logged_in_user): Extension<LoggedInUser>,
state: State<Arc<AvoRedState>>,
Json(payload): Json<StorePageRequest>,
) -> Result<Json<ApiResponse<NewPageModel>>> {
) -> Result<Json<ApiResponse<PageModel>>> {
println!("->> {:<12} - store_page_api_handler", "HANDLER");
let error_messages = payload.validate(&state).await?;

Expand All @@ -36,7 +36,7 @@ pub async fn store_page_api_handler(
return Err(Error::BadRequest(error_response));
}

let mut creatable_page = NewCreatablePageModel {
let mut creatable_page = CreatablePageModel {
name: payload.name,
identifier: payload.identifier,
status: payload.status,
Expand All @@ -58,10 +58,8 @@ pub async fn store_page_api_handler(

let created_page_model = state
.page_service
.new_create_page(&state.db, creatable_page)
.create_page(&state.db, creatable_page)
.await?;
// println!("PAge payload: {:?}", payload);
// let created_page_model = NewPageModel::default();

let response = ApiResponse {
status: true,
Expand Down
13 changes: 3 additions & 10 deletions src/api/handlers/page/update_page_api_handler.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::sync::Arc;

use crate::error::Error;
use crate::models::page_model::{NewPageModel, NewUpdatablePageModel, UpdatablePageField};
use crate::models::page_model::{PageModel, UpdatablePageModel, UpdatablePageField};
use crate::{
api::handlers::page::request::update_page_request::UpdatePageRequest,
avored_state::AvoRedState, error::Result
Expand All @@ -17,7 +17,7 @@ pub async fn update_page_api_handler(
AxumPath(page_id): AxumPath<String>,
state: State<Arc<AvoRedState>>,
Json(payload): Json<UpdatePageRequest>,
) -> Result<Json<ApiResponse<NewPageModel>>> {
) -> Result<Json<ApiResponse<PageModel>>> {
println!("->> {:<12} - update_page_api_handler", "HANDLER");

let has_permission_bool = state
Expand All @@ -44,7 +44,7 @@ pub async fn update_page_api_handler(
.find_by_id(&state.db, page_id)
.await?;

let mut updatable_page = NewUpdatablePageModel {
let mut updatable_page = UpdatablePageModel {
id: page_model.id,
name: payload.name,
identifier: payload.identifier,
Expand Down Expand Up @@ -79,10 +79,3 @@ pub async fn update_page_api_handler(

Ok(Json(response))
}


// #[derive(Serialize, Debug)]
// pub struct UpdatablePageResponse {
// pub status: bool,
// pub page_model: NewPageModel
// }
22 changes: 14 additions & 8 deletions src/models/page_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use super::{BaseModel, Pagination};
// region: Page model structs and enums

#[derive(Serialize, Debug, Deserialize, Clone, Default)]
pub struct NewPageModel {
pub struct PageModel {
pub id: String,
pub name: String,
pub identifier: String,
Expand Down Expand Up @@ -53,7 +53,8 @@ pub enum PageFieldType {
Select,
TextEditor,
Radio,
Checkbox
Checkbox,
SingleImage
}

#[derive(Deserialize, Debug, Clone, Serialize)]
Expand Down Expand Up @@ -246,9 +247,9 @@ impl TryFrom<PageCheckboxFieldData> for Value {


// region: impl surreal Object for page model structs
impl TryFrom<Object> for NewPageModel {
impl TryFrom<Object> for PageModel {
type Error = Error;
fn try_from(val: Object) -> Result<NewPageModel> {
fn try_from(val: Object) -> Result<PageModel> {

let id = val.get("id").get_id()?;
let name = val.get("name").get_string()?;
Expand Down Expand Up @@ -294,7 +295,7 @@ impl TryFrom<Object> for NewPageModel {
let created_by = val.get("created_by").get_string()?;
let updated_by = val.get("updated_by").get_string()?;

Ok(NewPageModel {
Ok(PageModel {
id,
name,
identifier,
Expand Down Expand Up @@ -346,6 +347,9 @@ impl TryFrom<Object> for PageFieldModel {
"Checkbox" => {
PageFieldType::Checkbox
},
"SingleImage" => {
PageFieldType::SingleImage
},

_ => PageFieldType::default()
};
Expand Down Expand Up @@ -429,6 +433,7 @@ impl TryFrom<Object> for PageFieldModel {
};

let field_data = match field_type_str.as_str() {

"Select" => {
let options = match val.get("field_data") {
Some(val) => {
Expand Down Expand Up @@ -461,6 +466,7 @@ impl TryFrom<Object> for PageFieldModel {

options
},

"Radio" => {
let options = match val.get("field_data") {
Some(val) => {
Expand Down Expand Up @@ -630,12 +636,12 @@ impl TryFrom<Object> for PageRadioFieldData {

#[derive(Serialize, Debug, Deserialize, Clone, Default)]
pub struct PagePagination {
pub data: Vec<NewPageModel>,
pub data: Vec<PageModel>,
pub pagination: Pagination,
}

#[derive(Serialize, Debug, Deserialize, Clone)]
pub struct NewCreatablePageModel {
pub struct CreatablePageModel {
pub name: String,
pub identifier: String,
pub status: PageStatus,
Expand All @@ -654,7 +660,7 @@ pub struct CreatablePageField {
}

#[derive(Serialize, Debug, Deserialize, Clone)]
pub struct NewUpdatablePageModel {
pub struct UpdatablePageModel {
pub id: String,
pub name: String,
pub identifier: String,
Expand Down
44 changes: 23 additions & 21 deletions src/repositories/page_repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use surrealdb::kvs::Datastore;
use surrealdb::sql::{Datetime, Value};

use crate::error::{Error, Result};
use crate::models::page_model::{NewCreatablePageModel, NewPageModel, NewUpdatablePageModel, PageDataType, PageFieldContentType, PageFieldData, PageFieldType, PageStatus, PutPageIdentifierModel};
use crate::models::page_model::{CreatablePageModel, PageModel, UpdatablePageModel, PageDataType, PageFieldContentType, PageFieldData, PageFieldType, PageStatus, PutPageIdentifierModel};
use crate::models::ModelCount;
use crate::PER_PAGE;

Expand All @@ -26,7 +26,7 @@ impl PageRepository {
start: i64,
order_column: String,
order_type: String,
) -> Result<Vec<NewPageModel>> {
) -> Result<Vec<PageModel>> {
let sql = format!("\
SELECT * \
FROM type::table($table) \
Expand All @@ -41,12 +41,12 @@ impl PageRepository {
]);
let responses = datastore.execute(&sql, database_session, Some(vars)).await?;

let mut page_list: Vec<NewPageModel> = Vec::new();
let mut page_list: Vec<PageModel> = Vec::new();

for object in into_iter_objects(responses)? {
let page_object = object?;

let page_model: Result<NewPageModel> = page_object.try_into();
let page_model: Result<PageModel> = page_object.try_into();
page_list.push(page_model?);
}
Ok(page_list)
Expand Down Expand Up @@ -94,7 +94,7 @@ impl PageRepository {
datastore: &Datastore,
database_session: &Session,
page_id: String,
) -> Result<NewPageModel> {
) -> Result<PageModel> {
let sql =
"SELECT * FROM type::thing($table, $id);";
let vars: BTreeMap<String, Value> = [
Expand All @@ -111,7 +111,7 @@ impl PageRepository {
None => Err(Error::Generic("no record found".to_string())),
};

let page_model: Result<NewPageModel> = result_object?.try_into();
let page_model: Result<PageModel> = result_object?.try_into();

page_model
}
Expand All @@ -120,7 +120,7 @@ impl PageRepository {
&self,
datastore: &Datastore,
database_session: &Session,
) -> Result<Vec<NewPageModel>> {
) -> Result<Vec<PageModel>> {
let sql =
"SELECT * FROM type::table($table);";
let vars: BTreeMap<String, Value> = [
Expand All @@ -130,12 +130,12 @@ impl PageRepository {

let responses = datastore.execute(sql, database_session, Some(vars)).await?;

let mut page_list: Vec<NewPageModel> = Vec::new();
let mut page_list: Vec<PageModel> = Vec::new();

for object in into_iter_objects(responses)? {
let page_object = object?;

let page_model: Result<NewPageModel> = page_object.try_into();
let page_model: Result<PageModel> = page_object.try_into();
page_list.push(page_model?);
}
Ok(page_list)
Expand Down Expand Up @@ -167,7 +167,7 @@ impl PageRepository {
datastore: &Datastore,
database_session: &Session,
put_page_identifier_model: PutPageIdentifierModel,
) -> Result<NewPageModel> {
) -> Result<PageModel> {
let sql = "UPDATE type::thing($table, $id)
SET
identifier = $identifier,
Expand All @@ -190,17 +190,17 @@ impl PageRepository {
Some(object) => object,
None => Err(Error::Generic("no record found".to_string())),
};
let updated_model: Result<NewPageModel> = result_object?.try_into();
let updated_model: Result<PageModel> = result_object?.try_into();

updated_model
}

pub async fn new_create_page (
pub async fn create_page (
&self,
datastore: &Datastore,
database_session: &Session,
creatable_page_model: NewCreatablePageModel
) -> Result<NewPageModel> {
creatable_page_model: CreatablePageModel
) -> Result<PageModel> {
let sql = "CREATE type::table($table) CONTENT $data";

let mut page_fields: Vec<Value> = vec![];
Expand All @@ -218,7 +218,8 @@ impl PageRepository {
PageFieldType::Select => "Select".into(),
PageFieldType::TextEditor => "TextEditor".into(),
PageFieldType::Radio => "Radio".into(),
PageFieldType::Checkbox => "Checkbox".into()
PageFieldType::Checkbox => "Checkbox".into(),
PageFieldType::SingleImage => "SingleImage".into()
};

let field_content_value: Value = match created_page_field.field_content {
Expand Down Expand Up @@ -301,17 +302,17 @@ impl PageRepository {
None => Err(Error::Generic("no record found".to_string())),
};

let model: Result<NewPageModel> = result_object?.try_into();
let model: Result<PageModel> = result_object?.try_into();

model
}

pub async fn new_update_page (
pub async fn update_page (
&self,
datastore: &Datastore,
database_session: &Session,
updatable_page_model: NewUpdatablePageModel
) -> Result<NewPageModel> {
updatable_page_model: UpdatablePageModel
) -> Result<PageModel> {
let sql = "UPDATE type::thing($table, $id) CONTENT $data";

let mut page_fields: Vec<Value> = vec![];
Expand All @@ -328,7 +329,8 @@ impl PageRepository {
PageFieldType::Select => "Select".into(),
PageFieldType::TextEditor => "TextEditor".into(),
PageFieldType::Radio => "Radio".into(),
PageFieldType::Checkbox => "Checkbox".into()
PageFieldType::Checkbox => "Checkbox".into(),
PageFieldType::SingleImage => "SingleImage".into()
};
let field_content_value: Value = match updatable_page_field.field_content {
PageFieldContentType::TextContentType { text_value } => text_value.try_into()?,
Expand Down Expand Up @@ -411,7 +413,7 @@ impl PageRepository {
None => Err(Error::Generic("no record found".to_string())),
};

let model: Result<NewPageModel> = result_object?.try_into();
let model: Result<PageModel> = result_object?.try_into();

model
}
Expand Down
8 changes: 4 additions & 4 deletions src/responses/page/mod.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
use serde::Serialize;
use crate::models::page_model::NewPageModel;
use crate::models::page_model::PageModel;
use crate::responses::ApiResponse;
use crate::error::Result;

#[derive(Serialize)]
pub struct PutPageIdentifierResponse {
pub page: NewPageModel
pub page: PageModel
}


#[derive(Serialize)]
pub struct FetchPageCmsResponse {
pub page_model: NewPageModel
pub page_model: PageModel
}

impl NewPageModel {
impl PageModel {
pub fn convert_to_response(&self) -> Result<ApiResponse<FetchPageCmsResponse>>
{
Ok(ApiResponse {
Expand Down
Loading

0 comments on commit 1408a3e

Please sign in to comment.