Skip to content

Commit

Permalink
feat: implemented project status (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulobressan authored Jul 23, 2024
1 parent 93ec88a commit 1c48ee4
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 23 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

1 change: 1 addition & 0 deletions src/domain/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub struct ProjectCreated {
pub name: String,
pub namespace: String,
pub owner: String,
pub status: String,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
}
Expand Down
43 changes: 37 additions & 6 deletions src/domain/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use rand::{
Rng,
};
use rdkafka::message::ToBytes;
use std::sync::Arc;
use std::{fmt::Display, str::FromStr, sync::Arc};
use tracing::{error, info};
use uuid::Uuid;

Expand All @@ -35,6 +35,7 @@ pub async fn create(
namespace: cmd.namespace.clone(),
name: cmd.name,
owner: user_id,
status: ProjectStatus::Active.to_string(),
created_at: Utc::now(),
updated_at: Utc::now(),
};
Expand All @@ -46,7 +47,7 @@ pub async fn create(
}

pub async fn create_cache(cache: Arc<dyn ProjectDrivenCache>, evt: ProjectCreated) -> Result<()> {
cache.create(&evt.into()).await?;
cache.create(&evt.try_into()?).await?;

Ok(())
}
Expand Down Expand Up @@ -210,24 +211,52 @@ impl CreateProjectCmd {
}
}

pub enum ProjectStatus {
Active,
Deleted,
}
impl FromStr for ProjectStatus {
type Err = Error;

fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
match s {
"active" => Ok(ProjectStatus::Active),
"deleted" => Ok(ProjectStatus::Deleted),
_ => bail!("project status not supported"),
}
}
}
impl Display for ProjectStatus {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ProjectStatus::Active => write!(f, "active"),
ProjectStatus::Deleted => write!(f, "deleted"),
}
}
}

pub struct ProjectCache {
pub id: String,
pub name: String,
pub namespace: String,
pub owner: String,
pub status: ProjectStatus,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
}
impl From<ProjectCreated> for ProjectCache {
fn from(value: ProjectCreated) -> Self {
Self {
impl TryFrom<ProjectCreated> for ProjectCache {
type Error = Error;

fn try_from(value: ProjectCreated) -> std::result::Result<Self, Self::Error> {
Ok(Self {
id: value.id,
namespace: value.namespace,
name: value.name,
owner: value.owner,
status: value.status.parse()?,
created_at: value.created_at,
updated_at: value.updated_at,
}
})
}
}

Expand Down Expand Up @@ -361,6 +390,7 @@ mod tests {
name: "New Project".into(),
namespace: "sonic-vegas".into(),
owner: "user id".into(),
status: ProjectStatus::Active,
created_at: Utc::now(),
updated_at: Utc::now(),
}
Expand All @@ -373,6 +403,7 @@ mod tests {
name: "New Project".into(),
namespace: "sonic-vegas".into(),
owner: "user id".into(),
status: ProjectStatus::Active.to_string(),
created_at: Utc::now(),
updated_at: Utc::now(),
}
Expand Down
1 change: 1 addition & 0 deletions src/driven/cache/migrations/20240606_tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ CREATE TABLE IF NOT EXISTS project (
namespace TEXT NOT NULL UNIQUE,
name TEXT NOT NULL,
owner TEXT NOT NULL,
status TEXT NOT NULL,
created_at DATETIME NOT NULL,
updated_at DATETIME NOT NULL
);
Expand Down
18 changes: 13 additions & 5 deletions src/driven/cache/project.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::Result;
use anyhow::{Error, Result};
use sqlx::{sqlite::SqliteRow, FromRow, Row};
use std::sync::Arc;

Expand All @@ -21,7 +21,7 @@ impl ProjectDrivenCache for SqliteProjectDrivenCache {
async fn find_by_namespace(&self, namespace: &str) -> Result<Option<ProjectCache>> {
let project = sqlx::query_as::<_, ProjectCache>(
r#"
SELECT id, namespace, name, owner, created_at, updated_at
SELECT id, namespace, name, owner, status, created_at, updated_at
FROM project WHERE namespace = $1;
"#,
)
Expand All @@ -34,7 +34,7 @@ impl ProjectDrivenCache for SqliteProjectDrivenCache {
async fn find_by_id(&self, id: &str) -> Result<Option<ProjectCache>> {
let project = sqlx::query_as::<_, ProjectCache>(
r#"
SELECT id, namespace, name, owner, created_at, updated_at
SELECT id, namespace, name, owner, status, created_at, updated_at
FROM project WHERE id = $1;
"#,
)
Expand All @@ -48,15 +48,18 @@ impl ProjectDrivenCache for SqliteProjectDrivenCache {
async fn create(&self, project: &ProjectCache) -> Result<()> {
let mut tx = self.sqlite.db.begin().await?;

let status = project.status.to_string();

sqlx::query!(
r#"
INSERT INTO project (id, namespace, name, owner, created_at, updated_at)
VALUES ($1, $2, $3, $4, $5, $6)
INSERT INTO project (id, namespace, name, owner, status, created_at, updated_at)
VALUES ($1, $2, $3, $4, $5, $6, $7)
"#,
project.id,
project.namespace,
project.name,
project.owner,
status,
project.created_at,
project.updated_at
)
Expand Down Expand Up @@ -132,11 +135,16 @@ impl ProjectDrivenCache for SqliteProjectDrivenCache {

impl FromRow<'_, SqliteRow> for ProjectCache {
fn from_row(row: &SqliteRow) -> sqlx::Result<Self> {
let status: &str = row.try_get("status")?;

Ok(Self {
id: row.try_get("id")?,
name: row.try_get("name")?,
namespace: row.try_get("namespace")?,
owner: row.try_get("owner")?,
status: status
.parse()
.map_err(|err: Error| sqlx::Error::Decode(err.into()))?,
created_at: row.try_get("created_at")?,
updated_at: row.try_get("updated_at")?,
})
Expand Down

0 comments on commit 1c48ee4

Please sign in to comment.