Skip to content

Commit

Permalink
Merge branch '2024-editor-server' into EDITOR-NTUEELightDance#523
Browse files Browse the repository at this point in the history
  • Loading branch information
ChenBingWei1201 authored Feb 4, 2024
2 parents 374d373 + 16f47c1 commit 5f54835
Show file tree
Hide file tree
Showing 24 changed files with 1,158 additions and 946 deletions.
14 changes: 9 additions & 5 deletions editor-server/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,10 @@ model PositionData {
}

model PositionFrame {
id Int @id @default(autoincrement())
start Int @unique
id Int @id @default(autoincrement())
start Int @unique
meta_rev Int @default(0)
data_rev Int @default(0)
// relation => not in db
editing EditingPositionFrame?
Expand Down Expand Up @@ -159,9 +161,11 @@ model ControlData {
}

model ControlFrame {
id Int @id @default(autoincrement())
start Int @unique
fade Boolean
id Int @id @default(autoincrement())
start Int @unique
fade Boolean
meta_rev Int @default(0)
data_rev Int @default(0)
// relation => not in db
editing EditingControlFrame?
Expand Down
2 changes: 2 additions & 0 deletions editor-server/src/db/types/control_frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ pub struct ControlFrameData {
pub id: i32,
pub start: i32,
pub fade: bool,
pub meta_rev: i32,
pub data_rev: i32,
}
20 changes: 10 additions & 10 deletions editor-server/src/db/types/dancer.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// ! Dancer data types.

use async_graphql::SimpleObject;
use serde::{Deserialize, Serialize};

#[derive(sqlx::FromRow, SimpleObject, Deserialize, Serialize, Default, Debug, Clone)]
pub struct DancerData {
pub id: i32,
pub name: String,
}
// ! Dancer data types.

use async_graphql::SimpleObject;
use serde::{Deserialize, Serialize};

#[derive(sqlx::FromRow, SimpleObject, Deserialize, Serialize, Default, Debug, Clone)]
pub struct DancerData {
pub id: i32,
pub name: String,
}
6 changes: 3 additions & 3 deletions editor-server/src/db/types/part.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Part data type
use async_graphql::{SimpleObject, Enum};
use serde::{Serialize, Deserialize};
use async_graphql::{Enum, SimpleObject};
use serde::{Deserialize, Serialize};
use sqlx::{FromRow, Type};

#[derive(SimpleObject, FromRow, Debug, Clone)]
Expand All @@ -27,4 +27,4 @@ impl From<String> for PartType {
_ => panic!("Invalid part type."),
}
}
}
}
2 changes: 2 additions & 0 deletions editor-server/src/db/types/position_frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ use serde::{Deserialize, Serialize};
pub struct PositionFrameData {
pub id: i32,
pub start: i32,
pub meta_rev: i32,
pub data_rev: i32,
}
29 changes: 25 additions & 4 deletions editor-server/src/graphql/mutations/control_frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub struct EditControlFrameInput {

#[derive(InputObject, Default)]
pub struct DeleteControlFrameInput {
#[graphql(name = "frameID")]
pub frame_id: i32,
}

Expand Down Expand Up @@ -490,8 +491,12 @@ impl ControlFrameMutation {
let original_frame = sqlx::query_as!(
ControlFrameData,
r#"
SELECT
id, start, fade as "fade: bool"
SELECT
id,
start,
fade as "fade: bool",
meta_rev,
data_rev
FROM ControlFrame
WHERE id = ?
"#,
Expand Down Expand Up @@ -565,6 +570,18 @@ impl ControlFrameMutation {
.execute(mysql)
.await?;

// update revision of the frame
sqlx::query!(
r#"
UPDATE ControlFrame
SET meta_rev = meta_rev + 1
WHERE id = ?;
"#,
frame_id
)
.execute(mysql)
.await?;

// update redis control
update_redis_control(mysql, &clients.redis_client, frame_id).await?;
let redis_control = get_redis_control(&clients.redis_client, frame_id).await?;
Expand Down Expand Up @@ -660,8 +677,12 @@ impl ControlFrameMutation {
let original_frame = sqlx::query_as!(
ControlFrameData,
r#"
SELECT
id, start, fade as "fade: bool"
SELECT
id,
start,
fade as "fade: bool",
meta_rev,
data_rev
FROM ControlFrame
WHERE id = ?
"#,
Expand Down
95 changes: 62 additions & 33 deletions editor-server/src/graphql/mutations/control_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,19 +133,19 @@ impl ControlMapMutation {
let raw_dancer_data: Vec<DancerData> = sqlx::query_as!(
DancerData,
r#"
SELECT
Dancer.id AS "dancer_id",
Part.type AS "part_type: PartType",
Part.id AS "part_id",
ControlData.frame_id AS "control_data_frame_id"
FROM Dancer
INNER JOIN Part
ON Dancer.id = Part.dancer_id
INNER JOIN ControlData
ON Part.id = ControlData.part_id
WHERE ControlData.frame_id = ?
ORDER BY Dancer.id ASC, Part.id ASC;
"#,
SELECT
Dancer.id AS "dancer_id",
Part.type AS "part_type: PartType",
Part.id AS "part_id",
ControlData.frame_id AS "control_data_frame_id"
FROM Dancer
INNER JOIN Part
ON Dancer.id = Part.dancer_id
INNER JOIN ControlData
ON Part.id = ControlData.part_id
WHERE ControlData.frame_id = ?
ORDER BY Dancer.id ASC, Part.id ASC;
"#,
frame_id
)
.fetch_all(mysql)
Expand Down Expand Up @@ -282,7 +282,7 @@ impl ControlMapMutation {
let effect_id = _data[0];

// check if the effect is valid
if !all_led_effect_ids.contains(&effect_id) {
if effect_id > 0 && !all_led_effect_ids.contains(&effect_id) {
let error_message = format!(
"Effect of dancer #{} part #{} is not a valid effect",
index, _index
Expand Down Expand Up @@ -318,9 +318,9 @@ impl ControlMapMutation {

sqlx::query!(
r#"
UPDATE ControlData
SET color_id = ?, alpha = ?
WHERE frame_id = ? AND part_id = ?;
UPDATE ControlData
SET color_id = ?, alpha = ?
WHERE frame_id = ? AND part_id = ?;
"#,
color_id,
alpha,
Expand All @@ -335,19 +335,34 @@ impl ControlMapMutation {
let effect_id = _data[0];
let alpha = _data[1];

sqlx::query!(
r#"
UPDATE ControlData
SET effect_id = ?, alpha = ?
WHERE frame_id = ? AND part_id = ?;
"#,
effect_id,
alpha,
frame_id,
part.part_id,
)
.execute(mysql)
.await?;
if effect_id > 0 {
sqlx::query!(
r#"
UPDATE ControlData
SET effect_id = ?, alpha = ?
WHERE frame_id = ? AND part_id = ?;
"#,
effect_id,
alpha,
frame_id,
part.part_id,
)
.execute(mysql)
.await?;
} else {
sqlx::query!(
r#"
UPDATE ControlData
SET effect_id = NULL, alpha = ?
WHERE frame_id = ? AND part_id = ?;
"#,
alpha,
frame_id,
part.part_id,
)
.execute(mysql)
.await?;
}
}
};
}
Expand All @@ -364,9 +379,11 @@ impl ControlMapMutation {
Some(fade) => {
sqlx::query!(
r#"
UPDATE ControlFrame
SET fade = ?
WHERE id = ?;
UPDATE ControlFrame
SET
fade = ?,
meta_rev = meta_rev + 1
WHERE id = ?;
"#,
fade,
frame_id,
Expand All @@ -392,6 +409,18 @@ impl ControlMapMutation {
.execute(mysql)
.await?;

// update revision of the frame
sqlx::query!(
r#"
UPDATE ControlFrame
SET data_rev = data_rev + 1
WHERE id = ?;
"#,
frame_id,
)
.execute(mysql)
.await?;

// update redis
update_redis_control(mysql, &clients.redis_client, frame_id).await?;
let redis_control = get_redis_control(&clients.redis_client, frame_id).await?;
Expand Down
Loading

0 comments on commit 5f54835

Please sign in to comment.