2021-07-09 01:19:16 +00:00
|
|
|
use anyhow::{Context, Result};
|
|
|
|
use chrono::NaiveDateTime;
|
|
|
|
use serde::{Deserialize, Serialize};
|
2021-07-11 23:45:26 +00:00
|
|
|
use tracing::instrument;
|
2021-07-09 01:19:16 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
pub struct Cell {
|
|
|
|
pub id: i32,
|
|
|
|
pub form_id: i32,
|
|
|
|
pub x: Option<i32>,
|
|
|
|
pub y: Option<i32>,
|
|
|
|
pub is_persistent: bool,
|
|
|
|
pub updated_at: NaiveDateTime,
|
|
|
|
pub created_at: NaiveDateTime,
|
|
|
|
}
|
|
|
|
|
2021-07-11 23:45:26 +00:00
|
|
|
#[instrument(level = "debug", skip(pool))]
|
2021-07-09 01:19:16 +00:00
|
|
|
pub async fn insert_cell(
|
|
|
|
pool: &sqlx::Pool<sqlx::Postgres>,
|
|
|
|
form_id: i32,
|
|
|
|
x: Option<i32>,
|
|
|
|
y: Option<i32>,
|
|
|
|
is_persistent: bool,
|
|
|
|
) -> Result<Cell> {
|
|
|
|
sqlx::query_as!(
|
|
|
|
Cell,
|
|
|
|
"INSERT INTO cells
|
|
|
|
(form_id, x, y, is_persistent, created_at, updated_at)
|
|
|
|
VALUES ($1, $2, $3, $4, now(), now())
|
|
|
|
ON CONFLICT (form_id) DO UPDATE
|
|
|
|
SET (x, y, is_persistent, updated_at) =
|
|
|
|
(EXCLUDED.x, EXCLUDED.y, EXCLUDED.is_persistent, now())
|
|
|
|
RETURNING *",
|
|
|
|
form_id,
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
is_persistent
|
|
|
|
)
|
|
|
|
.fetch_one(pool)
|
|
|
|
.await
|
|
|
|
.context("Failed to insert cell")
|
|
|
|
}
|