Implement saving a list; make the lists mutable using an RW lock

It currently only saves to memory.  So, if the application restart,
the lists are back to what is in the list files.
This commit is contained in:
Paul van Tilburg 2017-12-27 17:33:28 +01:00
parent f50e4a8e42
commit 167eda78e9
3 changed files with 29 additions and 10 deletions

View File

@ -20,6 +20,10 @@ impl List {
&comrak::ComrakOptions::default())
}
pub fn update_data(&mut self, data : &String) {
self.data = data.clone();
}
pub fn load_all() -> Vec<Self> {
let mut lists : Vec<List> = vec![];
let mut index = 0;
@ -35,7 +39,7 @@ impl List {
file.read_to_string(&mut data)
.expect(&format!("Cannot read list file {}", file_name));
let list = List {
let mut list = List {
id: String::from(name),
index : index,
data: data,

View File

@ -16,6 +16,9 @@ use list::List;
use rocket::{Rocket, State};
use rocket_contrib::{Json, Template};
use std::collections::HashMap;
use std::sync::RwLock;
type ListStore = RwLock<Vec<List>>;
#[derive(Serialize)]
struct IndexTemplateContext<'a> {
@ -23,7 +26,8 @@ struct IndexTemplateContext<'a> {
}
#[get("/")]
fn index(lists: State<Vec<List>>) -> Template {
fn index(lists: State<ListStore>) -> Template {
let lists = lists.read().unwrap();
let mut list_kvs = vec![];
for list in lists.iter() {
let mut list_kv = HashMap::new();
@ -36,22 +40,33 @@ fn index(lists: State<Vec<List>>) -> Template {
}
#[get("/lists/<list_id>", format = "text/html")]
fn list_show_html(list_id: String, lists: State<Vec<List>>) -> Option<String> {
let list = lists.iter().find( |&list| list.id == list_id )?;
fn list_show_html(list_id: String, lists: State<ListStore>) -> Option<String> {
let lists = lists.read().unwrap();
let list = lists.iter().find( |list| list.id == list_id )?;
Some(list.to_html())
}
#[get("/lists/<list_id>", format = "application/json")]
fn list_show_json(list_id: String, lists: State<Vec<List>>) -> Option<Json<List>> {
let list = lists.iter().find( |&list| list.id == list_id )?;
fn list_show_json(list_id: String, lists: State<ListStore>) -> Option<Json<List>> {
let lists = lists.read().unwrap();
let list = lists.iter().find( |list| list.id == list_id )?;
Some(Json(list.clone()))
}
#[put("/lists/<list_id>", format = "application/json", data = "<new_list>")]
fn list_update(list_id: String, new_list: Json<List>, lists: State<ListStore>) -> Option<Json<List>> {
let mut lists = lists.write().unwrap();
let list = lists.iter_mut().find( |list| list.id == list_id )?;
list.update_data(&new_list.data);
println!("update list {} with list data {:?}", list_id, new_list.data);
Some(Json(list.clone()))
}
fn rocket() -> Rocket {
let lists = list::List::load_all();
rocket::ignite()
.manage(lists)
.mount("/", routes![index, list_show_html, list_show_json, static_files::all])
.manage(RwLock::new(lists))
.mount("/", routes![index, list_show_html, list_show_json, list_update, static_files::all])
.attach(Template::fairing())
}

View File

@ -77,12 +77,12 @@ $(function() {
$('#editButton').on('click', function() { enableEditMode(); });
$('#saveButton').on('click', function() {
data = $('#listData').text();
data = $('#listData').val();
saveUpdate(curList.id, data);
});
$('#listData').on('input', function() {
data = $(this).text();
data = this.value;
triggerUpdate(curList.id, data);
});