2022-08-05 23:11:44 +03:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::Write;
|
|
|
|
|
|
|
|
use crate::domain;
|
2022-08-05 23:40:26 +03:00
|
|
|
use crate::repo::{Error, InsertTaskData, Repository, UpdateTaskData};
|
2022-08-05 23:11:44 +03:00
|
|
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use xdg::BaseDirectories;
|
|
|
|
|
|
|
|
#[derive(Deserialize, Serialize, Clone)]
|
|
|
|
pub struct Task {
|
|
|
|
name: String,
|
|
|
|
link: Option<String>,
|
|
|
|
// created_at
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Task> for domain::Task {
|
|
|
|
fn from(repo: Task) -> Self {
|
|
|
|
domain::Task {
|
|
|
|
name: repo.name,
|
|
|
|
link: repo.link,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const DATA_FILE: &str = "data.json";
|
|
|
|
|
|
|
|
pub struct FsRepo {
|
|
|
|
xdg_dirs: BaseDirectories,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FsRepo {
|
|
|
|
pub fn new(xdg_dirs: BaseDirectories) -> Self {
|
|
|
|
Self { xdg_dirs }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Repository for FsRepo {
|
|
|
|
fn get_tasks(&self) -> Result<Vec<domain::Task>, Error> {
|
|
|
|
self.get_tasks_impl()
|
|
|
|
.map(|tasks| tasks.into_iter().map(Task::into).collect())
|
|
|
|
}
|
|
|
|
|
2022-08-05 23:40:26 +03:00
|
|
|
fn update_task(
|
|
|
|
&self,
|
|
|
|
id: domain::TaskId,
|
|
|
|
update_data: UpdateTaskData,
|
|
|
|
) -> Result<domain::Task, Error> {
|
|
|
|
let mut tasks = self.get_tasks_impl()?;
|
|
|
|
if id == 0 || id > tasks.len() {
|
|
|
|
return Err(Error::NotFound);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut task = &mut tasks[id];
|
|
|
|
if let Some(name) = update_data.name {
|
|
|
|
task.name = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(link) = update_data.link {
|
|
|
|
task.link = link;
|
|
|
|
}
|
|
|
|
|
|
|
|
let new_task = task.clone();
|
|
|
|
|
|
|
|
self.save_tasks_impl(tasks)?;
|
|
|
|
|
|
|
|
Ok(new_task.into())
|
|
|
|
}
|
|
|
|
|
2022-08-05 23:11:44 +03:00
|
|
|
fn insert_task(&self, insert_data: InsertTaskData) -> Result<domain::Task, Error> {
|
|
|
|
let new_task = Task {
|
|
|
|
name: insert_data.name,
|
|
|
|
link: insert_data.link,
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut tasks = self.get_tasks_impl()?;
|
|
|
|
tasks.push(new_task.clone());
|
|
|
|
|
2022-08-05 23:40:26 +03:00
|
|
|
self.save_tasks_impl(tasks)?;
|
2022-08-05 23:11:44 +03:00
|
|
|
|
|
|
|
Ok(new_task.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FsRepo {
|
|
|
|
fn get_tasks_impl(&self) -> Result<Vec<Task>, Error> {
|
|
|
|
let file_path = self.xdg_dirs.get_data_file(DATA_FILE);
|
|
|
|
File::open(&file_path)
|
|
|
|
.map_err(|_| Error::NotFound)
|
|
|
|
.and_then(|file| serde_json::from_reader(file).map_err(|_| Error::InvalidData))
|
|
|
|
}
|
2022-08-05 23:40:26 +03:00
|
|
|
|
|
|
|
fn save_tasks_impl(&self, tasks: Vec<Task>) -> Result<(), Error> {
|
|
|
|
let file_path = self
|
|
|
|
.xdg_dirs
|
|
|
|
.place_data_file(DATA_FILE)
|
|
|
|
.map_err(|_| Error::InsertData)?;
|
|
|
|
let mut file = File::create(&file_path).map_err(|_| Error::InsertData)?;
|
|
|
|
let new_data = serde_json::to_vec(&tasks).map_err(|_| Error::InvalidData)?;
|
|
|
|
file.write_all(&new_data).map_err(|_| Error::InsertData)
|
|
|
|
}
|
2022-08-05 23:11:44 +03:00
|
|
|
}
|