Skip to content

Commit

Permalink
stop clear table
Browse files Browse the repository at this point in the history
  • Loading branch information
RGGH committed Mar 21, 2024
1 parent 99f6b58 commit dc00f64
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 123 deletions.
92 changes: 91 additions & 1 deletion src/controller.rs
Original file line number Diff line number Diff line change
@@ -1 +1,91 @@
pub mod utils;
use surrealdb::{engine::local::Db, Surreal};
use crate::Colorize;
use crate::{Magazine, Record};

pub async fn add_to(db: &Surreal<Db>, data: Vec<Magazine>) -> surrealdb::Result<()> {
for magazine in data {
let response = db
.query(
"CREATE product SET name=$name,
price=$price, day=$day, month=$month, year=$year",
)
.bind(("name", magazine.name))
.bind(("price", magazine.price))
.bind(("day", magazine.day))
.bind(("month", magazine.month))
.bind(("year", magazine.year))
.await?;

match response.check() {
Ok(_) => {}
Err(err) => {
eprintln!("Could not add entry: '{}'", err);
return Err(err);
}
};
}
Ok(())
}

pub async fn list_all(db: &Surreal<Db>) -> surrealdb::Result<()> {
let mut entries = db
.query(
"SELECT name, price, day, month, year, id
FROM type::table($table) ORDER BY name ASC",
)
.bind(("table", "product"))
.await?;
let entries: Vec<Record> = entries.take(0)?;
println!("----------------------------------------------------------------");
println!(
"{:<12} {:5} {:<2} {:<2} {:<2} {:}",
"Magazine", "price", "day", "month", "year", "table+id"
);
println!("----------------------------------------------------------------");
for entry in entries {
println!(
"{:<12} {:<5.2} {:<3} {:<5} {:<4} {:}",
entry.name.yellow(),
entry.price,
entry.day,
entry.month,
entry.year,
entry.id.to_raw().blue()
);
}

Ok(())
}

pub async fn list_year(db: &Surreal<Db>, year: u32) -> surrealdb::Result<()> {
let mut entries = db
.query("SELECT * FROM type::table($table) WHERE year=$year")
.bind(("table", "product"))
.bind(("year", year))
.await?;
let entries: Vec<Magazine> = entries.take(0)?;
for entry in entries {
println!("{:?} ", entry);
}
Ok(())
}

pub async fn add_relate(db: &Surreal<Db>, topic: String) -> surrealdb::Result<()> {
let _relate = db
.query("RELATE product->featured->($topic)")
.bind(("topic", topic))
.await?;
Ok(())
}

pub async fn list_related(db: &Surreal<Db>) -> surrealdb::Result<()> {
let mut entries = db
.query("SELECT * FROM type::table($table)")
.bind(("table", "featured"))
.await?;
let entries: Vec<Record> = entries.take(0)?;
for entry in entries {
println!("{:?} ", entry);
}
Ok(())
}
91 changes: 0 additions & 91 deletions src/controller/utils.rs

This file was deleted.

61 changes: 30 additions & 31 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![allow(unused)]
use clap::Parser;
use colored::Colorize;
use controller::utils::*;
use controller::*;
use serde::Deserialize;
use std::fmt::format;
use surrealdb::engine::local::Db;
Expand Down Expand Up @@ -73,42 +73,42 @@ async fn main() -> surrealdb::Result<()> {
db.use_ns("test").use_db("test").await?;

// clear old, test data
let _cleanup = db.query("REMOVE TABLE product").await?;
//let _cleanup = db.query("REMOVE TABLE product").await?;

let _response = db
//-- Create an index on the name, month and year fields of the product table
.query("DEFINE INDEX magid ON TABLE product COLUMNS name,month,year UNIQUE")
.await?;

let mut data = vec![
Magazine {
name: "Autosport".to_string(),
price: 1.80,
day: 12,
month: 12,
year: 1987,
},
Magazine {
name: "Autosport".to_string(),
price: 2.10,
day: 1,
month: 9,
year: 1985,
},
Magazine {
name: "Autosport".to_string(),
price: 0.90,
day: 22,
month: 1,
year: 1984,
},
Magazine {
name: "Autosport".to_string(),
price: 1.20,
day: 23,
month: 9,
year: 1984,
},
// Magazine {
// name: "Autosport".to_string(),
// price: 1.80,
// day: 12,
// month: 12,
// year: 1987,
// },
// Magazine {
// name: "Autosport".to_string(),
// price: 2.10,
// day: 1,
// month: 9,
// year: 1985,
// },
// Magazine {
// name: "Autosport".to_string(),
// price: 0.90,
// day: 22,
// month: 1,
// year: 1984,
// },
// Magazine {
// name: "Autosport".to_string(),
// price: 1.20,
// day: 23,
// month: 9,
// year: 1984,
// },
];
data.push(new_mag);

Expand All @@ -134,4 +134,3 @@ async fn main() -> surrealdb::Result<()> {

Ok(())
}

0 comments on commit dc00f64

Please sign in to comment.