feat: create package

This commit is contained in:
Ahmet Kaan GÜMÜŞ 2024-11-20 00:16:31 +03:00
parent 0dacf1f54f
commit 5bdc82a3a0
2 changed files with 16 additions and 3 deletions

View file

@ -6,6 +6,8 @@ use surrealdb::{
}; };
use tokio::time::sleep; use tokio::time::sleep;
use crate::package::Package;
static DB: LazyLock<Surreal<Client>> = LazyLock::new(Surreal::init); static DB: LazyLock<Surreal<Client>> = LazyLock::new(Surreal::init);
pub async fn establish_connection() -> Result<(), surrealdb::Error> { pub async fn establish_connection() -> Result<(), surrealdb::Error> {
@ -21,3 +23,10 @@ pub async fn is_alive() -> bool {
_ = sleep(Duration::from_secs(1)) => false _ = sleep(Duration::from_secs(1)) => false
} }
} }
pub async fn create_package(package: Package) -> Option<Package> {
DB.create::<Option<Package>>(package.get_name())
.content(package)
.await
.unwrap()
}

View file

@ -1,8 +1,10 @@
use std::fmt::Display; use std::fmt::Display;
use surrealdb::Datetime; use serde::{Deserialize, Serialize};
use surrealdb::sql::Datetime;
pub(crate) struct Package { #[derive(Debug, Serialize, Deserialize)]
pub struct Package {
name: String, name: String,
publisher: Publisher, publisher: Publisher,
version: Version, version: Version,
@ -14,7 +16,7 @@ pub(crate) struct Package {
} }
impl Package { impl Package {
fn get_name(&self) -> String { pub fn get_name(&self) -> String {
self.name.to_string() self.name.to_string()
} }
@ -47,6 +49,7 @@ impl Package {
} }
} }
#[derive(Debug, Serialize, Deserialize)]
pub(crate) struct Version { pub(crate) struct Version {
first: u8, first: u8,
second: u8, second: u8,
@ -76,6 +79,7 @@ impl Display for Version {
} }
} }
#[derive(Debug, Serialize, Deserialize)]
pub(crate) struct Publisher { pub(crate) struct Publisher {
name: String, name: String,
} }