fix: fixing file_repository for new dyn layout, updating readme, deleting unused From for PrimaryKeyType

This commit is contained in:
Gabor Körber 2024-06-01 20:48:36 +02:00
parent 75de8c4adb
commit f57f58818f
3 changed files with 39 additions and 26 deletions

View File

@ -69,3 +69,17 @@ Install a postgres:
![Mugshot of Admin](./docs/mugshot.png) ![Mugshot of Admin](./docs/mugshot.png)
### 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.

View File

@ -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] #[async_trait]
pub trait AdminRepository: Send + Sync { pub trait AdminRepository: Send + Sync {
type Key: PrimaryKeyType; type Key: PrimaryKeyType;

View File

@ -10,7 +10,6 @@ struct FileRepository {
impl FileRepository { impl FileRepository {
pub fn new(from_path: &str) -> Self { pub fn new(from_path: &str) -> Self {
let proper_path = file_operations::get_proper_path(from_path); let proper_path = file_operations::get_proper_path(from_path);
FileRepository { FileRepository {
directory_path: proper_path, directory_path: proper_path,
@ -20,6 +19,12 @@ impl FileRepository {
#[async_trait] #[async_trait]
impl AdminRepository for FileRepository { 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 { async fn info(&self, _: &RepositoryContext) -> RepositoryInfo {
RepoInfo { RepoInfo {
name: "File Repository", name: "File Repository",
@ -41,13 +46,16 @@ impl AdminRepository for FileRepository {
// GET on item collection. // GET on item collection.
async fn list(&self, model: &RepositoryContext) -> RepositoryList { async fn list(&self, model: &RepositoryContext) -> RepositoryList {
if let Ok(file_data) = file_operations::directory_info(&self.directory_path) { 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
RepositoryItem { .into_iter()
fields, .map(|fields| {
detail_url: None, // TODO: change the return type of directory_info into a struct, so we can access key. RepositoryItem {
change_url: None, fields,
} detail_url: None, // TODO: change the return type of directory_info into a struct, so we can access key.
}).collect(); change_url: None,
}
})
.collect();
return RepositoryList::List { values: items }; return RepositoryList::List { values: items };
} }
RepositoryList::Empty RepositoryList::Empty
@ -63,7 +71,7 @@ impl AdminRepository for FileRepository {
} }
// GET single item. // 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 None
} }
@ -71,7 +79,7 @@ impl AdminRepository for FileRepository {
async fn update( async fn update(
&mut self, &mut self,
model: &RepositoryContext, model: &RepositoryContext,
id: LookupKey, id: &Self::Key,
data: Value, data: Value,
) -> Option<RepositoryItem> { ) -> Option<RepositoryItem> {
None None
@ -81,14 +89,14 @@ impl AdminRepository for FileRepository {
async fn replace( async fn replace(
&mut self, &mut self,
model: &RepositoryContext, model: &RepositoryContext,
id: LookupKey, id: &Self::Key,
data: Value, data: Value,
) -> Option<RepositoryItem> { ) -> Option<RepositoryItem> {
None None
} }
// DELETE single item. // 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 None
} }
} }
@ -108,13 +116,13 @@ pub fn register(registry: &mut AdminRegistry, path: &str) {
} }
mod file_operations { mod file_operations {
use chrono::{DateTime, TimeZone, Utc};
use serde_json::{json, Value}; use serde_json::{json, Value};
use std::env;
use std::fs; use std::fs;
use std::path::Path; use std::path::Path;
use std::env;
use std::path::PathBuf; use std::path::PathBuf;
use std::time::SystemTime; use std::time::SystemTime;
use chrono::{DateTime, Utc, TimeZone};
fn system_time_to_iso_string(system_time: SystemTime) -> String { fn system_time_to_iso_string(system_time: SystemTime) -> String {
let datetime: DateTime<Utc> = system_time.into(); let datetime: DateTime<Utc> = system_time.into();
@ -157,7 +165,9 @@ mod file_operations {
get_metadata_as_json(path) 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 entries = fs::read_dir(directory_path)?;
let mut results = Vec::new(); let mut results = Vec::new();
@ -181,6 +191,5 @@ mod file_operations {
let template_path = PathBuf::from(manifest_dir).join(path); let template_path = PathBuf::from(manifest_dir).join(path);
return template_path; return template_path;
} }
} }
} }