fix: fixing file_repository for new dyn layout, updating readme, deleting unused From for PrimaryKeyType
This commit is contained in:
parent
75de8c4adb
commit
f57f58818f
14
README.md
14
README.md
@ -69,3 +69,17 @@ Install a postgres:
|
||||
|
||||

|
||||
|
||||
|
||||
### Project Structure
|
||||
|
||||
#### Root Folders
|
||||
|
||||
- docker: all about docker, atm. database docker image for development.
|
||||
- docs: place for all future documentation, including this.
|
||||
- entity: crate containing database models
|
||||
- migration: crate containing database migrations
|
||||
- rear: crate containing the main library for rear
|
||||
- src: crate containing a runnable project implementing rear as a demo project
|
||||
- static: static files used by rear
|
||||
- strinto: experimental crate for an idea.
|
||||
- templates: all templates for rear-admiral.
|
@ -326,16 +326,6 @@ pub mod repository {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for Box<dyn PrimaryKeyType> {
|
||||
fn from(s: String) -> Box<dyn PrimaryKeyType> {
|
||||
if let Ok(i) = s.parse::<i64>() {
|
||||
Box::new(i)
|
||||
} else {
|
||||
Box::new(s)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
pub trait AdminRepository: Send + Sync {
|
||||
type Key: PrimaryKeyType;
|
||||
|
@ -10,7 +10,6 @@ struct FileRepository {
|
||||
|
||||
impl FileRepository {
|
||||
pub fn new(from_path: &str) -> Self {
|
||||
|
||||
let proper_path = file_operations::get_proper_path(from_path);
|
||||
FileRepository {
|
||||
directory_path: proper_path,
|
||||
@ -20,6 +19,12 @@ impl FileRepository {
|
||||
|
||||
#[async_trait]
|
||||
impl AdminRepository for FileRepository {
|
||||
type Key = String;
|
||||
|
||||
fn key_from_string(&self, s: String) -> Option<Self::Key> {
|
||||
Some(s)
|
||||
}
|
||||
|
||||
async fn info(&self, _: &RepositoryContext) -> RepositoryInfo {
|
||||
RepoInfo {
|
||||
name: "File Repository",
|
||||
@ -41,13 +46,16 @@ impl AdminRepository for FileRepository {
|
||||
// GET on item collection.
|
||||
async fn list(&self, model: &RepositoryContext) -> RepositoryList {
|
||||
if let Ok(file_data) = file_operations::directory_info(&self.directory_path) {
|
||||
let items: Vec<RepositoryItem> = file_data.into_iter().map(|fields| {
|
||||
let items: Vec<RepositoryItem> = file_data
|
||||
.into_iter()
|
||||
.map(|fields| {
|
||||
RepositoryItem {
|
||||
fields,
|
||||
detail_url: None, // TODO: change the return type of directory_info into a struct, so we can access key.
|
||||
change_url: None,
|
||||
}
|
||||
}).collect();
|
||||
})
|
||||
.collect();
|
||||
return RepositoryList::List { values: items };
|
||||
}
|
||||
RepositoryList::Empty
|
||||
@ -63,7 +71,7 @@ impl AdminRepository for FileRepository {
|
||||
}
|
||||
|
||||
// GET single item.
|
||||
async fn get(&self, model: &RepositoryContext, id: LookupKey) -> Option<RepositoryItem> {
|
||||
async fn get(&self, model: &RepositoryContext, id: &Self::Key) -> Option<RepositoryItem> {
|
||||
None
|
||||
}
|
||||
|
||||
@ -71,7 +79,7 @@ impl AdminRepository for FileRepository {
|
||||
async fn update(
|
||||
&mut self,
|
||||
model: &RepositoryContext,
|
||||
id: LookupKey,
|
||||
id: &Self::Key,
|
||||
data: Value,
|
||||
) -> Option<RepositoryItem> {
|
||||
None
|
||||
@ -81,14 +89,14 @@ impl AdminRepository for FileRepository {
|
||||
async fn replace(
|
||||
&mut self,
|
||||
model: &RepositoryContext,
|
||||
id: LookupKey,
|
||||
id: &Self::Key,
|
||||
data: Value,
|
||||
) -> Option<RepositoryItem> {
|
||||
None
|
||||
}
|
||||
|
||||
// DELETE single item.
|
||||
async fn delete(&mut self, _: &RepositoryContext, id: LookupKey) -> Option<Value> {
|
||||
async fn delete(&mut self, _: &RepositoryContext, id: &Self::Key) -> Option<Value> {
|
||||
None
|
||||
}
|
||||
}
|
||||
@ -108,13 +116,13 @@ pub fn register(registry: &mut AdminRegistry, path: &str) {
|
||||
}
|
||||
|
||||
mod file_operations {
|
||||
use chrono::{DateTime, TimeZone, Utc};
|
||||
use serde_json::{json, Value};
|
||||
use std::env;
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
use std::env;
|
||||
use std::path::PathBuf;
|
||||
use std::time::SystemTime;
|
||||
use chrono::{DateTime, Utc, TimeZone};
|
||||
|
||||
fn system_time_to_iso_string(system_time: SystemTime) -> String {
|
||||
let datetime: DateTime<Utc> = system_time.into();
|
||||
@ -157,7 +165,9 @@ mod file_operations {
|
||||
get_metadata_as_json(path)
|
||||
}
|
||||
|
||||
pub fn directory_info(directory_path: &PathBuf) -> Result<Vec<Value>, Box<dyn std::error::Error>> {
|
||||
pub fn directory_info(
|
||||
directory_path: &PathBuf,
|
||||
) -> Result<Vec<Value>, Box<dyn std::error::Error>> {
|
||||
let entries = fs::read_dir(directory_path)?;
|
||||
let mut results = Vec::new();
|
||||
|
||||
@ -181,6 +191,5 @@ mod file_operations {
|
||||
let template_path = PathBuf::from(manifest_dir).join(path);
|
||||
return template_path;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user