chore(card_db): Generalize the in-browser database
This commit is contained in:
parent
2bb09cbe54
commit
88da43e208
|
@ -1,2 +1,54 @@
|
|||
use crate::database::{self, models::FrameType, schema::cards};
|
||||
use database::models::CardDetails;
|
||||
use diesel::{prelude::*, Connection};
|
||||
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
|
||||
use thiserror::Error;
|
||||
|
||||
pub mod models;
|
||||
pub mod schema;
|
||||
|
||||
const DB_URL: &str = "cards.db";
|
||||
const MIGRATIONS: EmbeddedMigrations = embed_migrations!("migrations");
|
||||
|
||||
pub struct BrowserDatabase(SqliteConnection);
|
||||
|
||||
impl BrowserDatabase {
|
||||
pub fn open() -> Result<Self> {
|
||||
let mut connection = SqliteConnection::establish(DB_URL)?;
|
||||
connection.run_pending_migrations(MIGRATIONS).unwrap();
|
||||
Ok(Self(connection))
|
||||
}
|
||||
|
||||
pub async fn get_card(&mut self, password: i32) -> Result<Option<CardDetails>> {
|
||||
match cards::dsl::cards.find(password).first(&mut self.0) {
|
||||
Ok(details) => Ok(Some(details)),
|
||||
Err(diesel::result::Error::NotFound) => Ok(None),
|
||||
Err(other) => Err(other.into()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn load_data(&mut self) -> Result<()> {
|
||||
let decode_talker = CardDetails {
|
||||
password: 1861629,
|
||||
name: "Decode Talker".to_owned(),
|
||||
description: "2+ Effect Monsters\r\nGains 500 ATK for each monster it points to. When your opponent activates a card or effect that targets a card(s) you control (Quick Effect): You can Tribute 1 monster this card points to; negate the activation, and if you do, destroy that card.".to_owned(),
|
||||
frame: FrameType::Link,
|
||||
image: "https://images.ygoprodeck.com/images/cards/1861629.jpg".to_owned()
|
||||
};
|
||||
|
||||
diesel::insert_into(cards::table)
|
||||
.values(decode_talker)
|
||||
.execute(&mut self.0)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum BrowserDatabaseError {
|
||||
#[error("failed to connect to database")]
|
||||
DieselConnectionError(#[from] diesel::ConnectionError),
|
||||
#[error("failed to query database")]
|
||||
DieselQueryError(#[from] diesel::result::Error),
|
||||
}
|
||||
type Result<T> = std::result::Result<T, BrowserDatabaseError>;
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
pub mod components;
|
||||
pub mod draft_list;
|
||||
pub mod utils;
|
||||
pub mod database;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use draft_manager::{components::card::Card, utils::card_database::CardDatabase};
|
||||
use draft_manager::{components::card::Card, database::BrowserDatabase};
|
||||
use leptos::prelude::*;
|
||||
|
||||
fn main() {
|
||||
|
@ -10,7 +10,7 @@ fn main() {
|
|||
#[component]
|
||||
fn App() -> impl IntoView {
|
||||
let card = LocalResource::new(|| async move {
|
||||
let mut database = CardDatabase::open()?;
|
||||
let mut database = BrowserDatabase::open()?;
|
||||
database.load_data()?;
|
||||
|
||||
database.get_card(1861629).await
|
||||
|
|
|
@ -1 +0,0 @@
|
|||
pub mod card_database;
|
|
@ -1,51 +0,0 @@
|
|||
use crate::database::{self, models::FrameType, schema::cards};
|
||||
use database::models::CardDetails;
|
||||
use diesel::prelude::*;
|
||||
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
|
||||
use thiserror::Error;
|
||||
|
||||
const DB_URL: &str = "cards.db";
|
||||
const MIGRATIONS: EmbeddedMigrations = embed_migrations!("migrations");
|
||||
|
||||
pub struct CardDatabase(SqliteConnection);
|
||||
|
||||
impl CardDatabase {
|
||||
pub fn open() -> Result<Self> {
|
||||
let mut connection = SqliteConnection::establish(DB_URL)?;
|
||||
connection.run_pending_migrations(MIGRATIONS).unwrap();
|
||||
Ok(Self(connection))
|
||||
}
|
||||
|
||||
pub async fn get_card(&mut self, password: i32) -> Result<Option<CardDetails>> {
|
||||
match cards::dsl::cards.find(password).first(&mut self.0) {
|
||||
Ok(details) => Ok(Some(details)),
|
||||
Err(diesel::result::Error::NotFound) => Ok(None),
|
||||
Err(other) => Err(other.into()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn load_data(&mut self) -> Result<()> {
|
||||
let decode_talker = CardDetails {
|
||||
password: 1861629,
|
||||
name: "Decode Talker".to_owned(),
|
||||
description: "2+ Effect Monsters\r\nGains 500 ATK for each monster it points to. When your opponent activates a card or effect that targets a card(s) you control (Quick Effect): You can Tribute 1 monster this card points to; negate the activation, and if you do, destroy that card.".to_owned(),
|
||||
frame: FrameType::Link,
|
||||
image: "https://images.ygoprodeck.com/images/cards/1861629.jpg".to_owned()
|
||||
};
|
||||
|
||||
diesel::insert_into(cards::table)
|
||||
.values(decode_talker)
|
||||
.execute(&mut self.0)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum CardDatabaseError {
|
||||
#[error("failed to connect to database")]
|
||||
DieselConnectionError(#[from] diesel::ConnectionError),
|
||||
#[error("failed to query database")]
|
||||
DieselQueryError(#[from] diesel::result::Error),
|
||||
}
|
||||
type Result<T> = std::result::Result<T, CardDatabaseError>;
|
Loading…
Reference in a new issue