|
@@ -1,26 +1,16 @@
|
1
|
1
|
use async_std::fs::read_to_string;
|
2
|
2
|
use std::env;
|
3
|
3
|
use std::ffi::OsStr;
|
4
|
|
-use std::fs::read_dir;
|
5
|
|
-use std::io::{Error, ErrorKind};
|
|
4
|
+use std::fs::{read_dir, File};
|
|
5
|
+use std::io::{Error, ErrorKind, Write};
|
6
|
6
|
use std::path::PathBuf;
|
7
|
7
|
|
8
|
8
|
use crate::post::Post;
|
9
|
9
|
|
10
|
|
-pub async fn get_posts_directory_path() -> Result<PathBuf, Error> {
|
11
|
|
- match env::var("POSTS_DIR") {
|
12
|
|
- Ok(dir) => Ok(dir.into()),
|
13
|
|
- Err(_) => Err(Error::new(
|
14
|
|
- ErrorKind::Other,
|
15
|
|
- "Posts directory environment variable not set",
|
16
|
|
- )),
|
17
|
|
- }
|
18
|
|
-}
|
19
|
|
-
|
20
|
10
|
pub async fn get_all_posts() -> Result<Vec<Post>, Error> {
|
21
|
11
|
let mut posts: Vec<Post> = vec![];
|
22
|
12
|
|
23
|
|
- for file in post_directory_contents().await? {
|
|
13
|
+ for file in post_directory_contents()? {
|
24
|
14
|
let file = file?;
|
25
|
15
|
if let Some("json") = file.path().extension().and_then(OsStr::to_str) {
|
26
|
16
|
let contents = read_post_from_disk(&file.path()).await?;
|
|
@@ -33,7 +23,7 @@ pub async fn get_all_posts() -> Result<Vec<Post>, Error> {
|
33
|
23
|
}
|
34
|
24
|
|
35
|
25
|
pub async fn get_one_post(post_id: String) -> Result<Post, Error> {
|
36
|
|
- let posts_dir = get_posts_directory_path().await?;
|
|
26
|
+ let posts_dir = get_posts_directory_path()?;
|
37
|
27
|
let path = posts_dir.join(format!("{}.json", post_id));
|
38
|
28
|
let contents = read_post_from_disk(&path).await?;
|
39
|
29
|
let post = Post::from_str(&contents)?;
|
|
@@ -41,6 +31,13 @@ pub async fn get_one_post(post_id: String) -> Result<Post, Error> {
|
41
|
31
|
Ok(post)
|
42
|
32
|
}
|
43
|
33
|
|
|
34
|
+pub fn delete_post(post_id: String) -> Result<(), Error> {
|
|
35
|
+ let posts_dir = get_posts_directory_path()?;
|
|
36
|
+ let path = posts_dir.join(format!("{}.json", post_id));
|
|
37
|
+ std::fs::remove_file(path)?;
|
|
38
|
+ Ok(())
|
|
39
|
+}
|
|
40
|
+
|
44
|
41
|
async fn read_post_from_disk(path: &PathBuf) -> Result<String, Error> {
|
45
|
42
|
match read_to_string(&path).await {
|
46
|
43
|
Ok(c) => Ok(c),
|
|
@@ -54,8 +51,17 @@ async fn read_post_from_disk(path: &PathBuf) -> Result<String, Error> {
|
54
|
51
|
}
|
55
|
52
|
}
|
56
|
53
|
|
57
|
|
-async fn post_directory_contents() -> Result<std::fs::ReadDir, Error> {
|
58
|
|
- let path = get_posts_directory_path().await?;
|
|
54
|
+pub fn write_post_to_disk(post: &Post) -> Result<(), Error> {
|
|
55
|
+ let mut path: PathBuf = get_posts_directory_path()?;
|
|
56
|
+ let filename = format!("{}.json", post.id);
|
|
57
|
+ path = path.join(&filename);
|
|
58
|
+ let mut file = File::create(&path)?;
|
|
59
|
+ file.write_all(serde_json::to_string(&post)?.as_bytes())?;
|
|
60
|
+ Ok(())
|
|
61
|
+}
|
|
62
|
+
|
|
63
|
+fn post_directory_contents() -> Result<std::fs::ReadDir, Error> {
|
|
64
|
+ let path = get_posts_directory_path()?;
|
59
|
65
|
|
60
|
66
|
match read_dir(path) {
|
61
|
67
|
Ok(f) => Ok(f),
|
|
@@ -65,3 +71,13 @@ async fn post_directory_contents() -> Result<std::fs::ReadDir, Error> {
|
65
|
71
|
)),
|
66
|
72
|
}
|
67
|
73
|
}
|
|
74
|
+
|
|
75
|
+fn get_posts_directory_path() -> Result<PathBuf, Error> {
|
|
76
|
+ match env::var("POSTS_DIR") {
|
|
77
|
+ Ok(dir) => Ok(dir.into()),
|
|
78
|
+ Err(_) => Err(Error::new(
|
|
79
|
+ ErrorKind::Other,
|
|
80
|
+ "Posts directory environment variable not set",
|
|
81
|
+ )),
|
|
82
|
+ }
|
|
83
|
+}
|