-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
415 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
crates/migration/src/m20220716_000007_fix_blok_priority_trigger.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use crate::utils::macros::exec_stmt; | ||
use sea_orm_migration::{prelude::*, MigrationName}; | ||
|
||
pub struct Migration; | ||
|
||
impl MigrationName for Migration { | ||
fn name(&self) -> &str { | ||
"m20220716_000007_fix_blok_priority_trigger" | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl MigrationTrait for Migration { | ||
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { | ||
exec_stmt!( | ||
manager, | ||
r#" | ||
create or replace function tg_bloks__set_priority() returns trigger as $$ | ||
declare | ||
max_priority integer; | ||
begin | ||
-- Set default priority to maximum | ||
if new.priority is null or new.priority = 0 then | ||
select max(b1.priority) into max_priority from bloks b1 where b1.page_id = new.page_id; | ||
new.priority := coalesce(max_priority + 1, 1); | ||
end if; | ||
-- Offset other priorities if conflict | ||
update bloks b2 set priority = b2.priority + 1 where b2.page_id = new.page_id and b2.priority = new.priority and b2.id != new.id; | ||
return new; | ||
end; | ||
$$ language plpgsql volatile; | ||
"# | ||
)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { | ||
exec_stmt!( | ||
manager, | ||
r#" | ||
create or replace function tg_bloks__set_priority() returns trigger as $$ | ||
declare | ||
max_priority integer; | ||
begin | ||
-- Set default priority to maximum | ||
if new.priority is null or new.priority = 0 then | ||
select max(b1.priority) into max_priority from bloks b1 where b1.page_id = new.page_id; | ||
new.priority := coalesce(max_priority + 1, 0); | ||
end if; | ||
-- Offset other priorities if conflict | ||
update bloks b2 set priority = b2.priority + 1 where b2.page_id = new.page_id and b2.priority = new.priority and b2.id != new.id; | ||
return new; | ||
end; | ||
$$ language plpgsql volatile; | ||
"# | ||
)?; | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,14 @@ | ||
use crate::services::blok::routes::{create_blok, delete_blok, get_blok, update_blok}; | ||
use crate::services::blok::routes::{create_blok, delete_blok, get_blok, patch_blok, update_blok}; | ||
use actix_web::{web::scope, Scope}; | ||
|
||
mod models; | ||
pub(crate) mod models; | ||
mod routes; | ||
|
||
pub use models::BlokOutput; | ||
|
||
pub fn blok_service() -> Scope { | ||
scope("/blok") | ||
.service(get_blok) | ||
.service(create_blok) | ||
.service(update_blok) | ||
.service(patch_blok) | ||
.service(delete_blok) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use entity::blok; | ||
use getset::Getters; | ||
use sea_orm::ActiveValue::Set; | ||
use sea_orm::NotSet; | ||
use serde::Deserialize; | ||
use serde_json::Value; | ||
|
||
#[derive(Deserialize, Clone, Getters)] | ||
#[serde(rename_all = "camelCase")] | ||
#[getset(get = "pub")] | ||
pub struct BlokInput { | ||
page_id: i32, | ||
component_id: String, | ||
props: Value, | ||
priority: Option<i32>, | ||
} | ||
|
||
impl BlokInput { | ||
pub fn active_model(&self) -> blok::ActiveModel { | ||
blok::ActiveModel { | ||
page_id: Set(self.page_id.to_owned()), | ||
component_id: Set(self.component_id.to_owned()), | ||
props: Set(self.props.to_owned()), | ||
priority: self.priority.map(Set).unwrap_or(NotSet), | ||
..Default::default() | ||
} | ||
} | ||
} |
29 changes: 3 additions & 26 deletions
29
crates/server/src/services/blok/models.rs → ...r/src/services/blok/models/blok_output.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
crates/server/src/services/blok/models/blok_patch_input.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
use crate::utils::serde_json_patch::Patch; | ||
use getset::Getters; | ||
use serde::Deserialize; | ||
use serde_json::Value; | ||
|
||
#[derive(Deserialize, Clone, Getters)] | ||
#[serde(rename_all = "camelCase")] | ||
#[getset(get = "pub")] | ||
pub struct BlokPatchInput { | ||
#[serde(default)] | ||
page_id: Patch<i32>, | ||
#[serde(default)] | ||
component_id: Patch<String>, | ||
#[serde(default)] | ||
props: Patch<Value>, | ||
#[serde(default)] | ||
priority: Patch<i32>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
mod blok_input; | ||
mod blok_output; | ||
mod blok_patch_input; | ||
|
||
pub use blok_input::BlokInput; | ||
pub use blok_output::BlokOutput; | ||
pub use blok_patch_input::BlokPatchInput; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod serde_json_patch; |
Oops, something went wrong.