-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restructure code and split into multiple files
- Loading branch information
1 parent
569ecbe
commit 3b64270
Showing
3 changed files
with
134 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use crate::{Todo, TodoStatus}; | ||
use anyhow::Result; | ||
use rusqlite::{params, Connection}; | ||
use std::path::Path; | ||
|
||
mod embedded { | ||
use refinery::embed_migrations; | ||
embed_migrations!("./migrations"); | ||
} | ||
|
||
pub struct Store { | ||
conn: Connection, | ||
} | ||
|
||
impl Store { | ||
pub fn open<P>(path: P) -> Result<Self> | ||
where | ||
P: AsRef<Path>, | ||
{ | ||
let mut conn = Connection::open(path).expect("failed to open todo database"); | ||
|
||
embedded::migrations::runner() | ||
.run(&mut conn) | ||
.expect("failed to run migrations"); | ||
|
||
Ok(Self { conn }) | ||
} | ||
|
||
pub fn insert_todo(&self, msg: String) -> Result<()> { | ||
self.conn | ||
.execute("INSERT INTO todos (title) VALUES (?1)", params![msg]) | ||
.expect("failed to insert todo"); | ||
Ok(()) | ||
} | ||
|
||
pub fn find_open_todos(&self) -> Result<Vec<Todo>> { | ||
let mut stmt = self | ||
.conn | ||
.prepare("SELECT id, created_at, title FROM todos WHERE status != 'DONE'") | ||
.expect("failed to prepare query"); | ||
|
||
let todos = stmt | ||
.query_map([], |row| { | ||
Ok(Todo { | ||
id: row.get(0)?, | ||
created_at: row.get(1)?, | ||
title: row.get(2)?, | ||
}) | ||
})? | ||
.filter_map(|todo| todo.ok()) | ||
.collect(); | ||
|
||
Ok(todos) | ||
} | ||
|
||
pub fn update_todo_status(&self, id: u32, status: TodoStatus) -> Result<()> { | ||
self.conn | ||
.execute( | ||
"UPDATE todos SET status = ?1 WHERE id = ?2", | ||
params![status, id], | ||
) | ||
.expect("failed to update todo status"); | ||
Ok(()) | ||
} | ||
|
||
pub fn prune_todos(&self) -> Result<()> { | ||
self.conn | ||
.execute("DELETE FROM todos WHERE status = 'DONE'", ()) | ||
.expect("failed to delete todos"); | ||
|
||
self.conn | ||
.execute( | ||
"UPDATE SQLITE_SEQUENCE SET SEQ = (SELECT MAX(id) FROM todos) WHERE NAME = 'todos'", | ||
(), | ||
) | ||
.expect("failed to reset autoincrement"); | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use std::fmt; | ||
use chrono::{DateTime, Local}; | ||
use clap::ValueEnum; | ||
use rusqlite::ToSql; | ||
|
||
#[derive(Debug, Clone, ValueEnum)] | ||
pub enum TodoStatus { | ||
Ready, | ||
Doing, | ||
Done, | ||
} | ||
|
||
impl fmt::Display for TodoStatus { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
match self { | ||
TodoStatus::Ready => write!(f, "READY"), | ||
TodoStatus::Doing => write!(f, "DOING"), | ||
TodoStatus::Done => write!(f, "DONE"), | ||
} | ||
} | ||
} | ||
|
||
impl ToSql for TodoStatus { | ||
fn to_sql(&self) -> rusqlite::Result<rusqlite::types::ToSqlOutput<'_>> { | ||
Ok(self.to_string().into()) | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct Todo { | ||
pub id: u32, | ||
pub created_at: DateTime<Local>, | ||
pub title: String, | ||
} |