temp commit, doesn't compile, will squash later
parent
547d93cbb7
commit
24dfa88d40
@ -0,0 +1,136 @@
|
||||
use crate::{THREADS, POSTS, MEDIA, HEAD};
|
||||
|
||||
/// increments the id stored inside the db
|
||||
fn increment(old: Option<&[u8]>) -> Option<Vec<u8>> {
|
||||
let number = match old {
|
||||
Some(s) => {
|
||||
let buf: [u8; 4] = s.try_into().unwrap();
|
||||
u32::from_be_bytes(buf) + 1
|
||||
// FIXME
|
||||
}
|
||||
None => 0,
|
||||
};
|
||||
Some(number.to_be_bytes().to_vec())
|
||||
}
|
||||
|
||||
// DATABASE ACCESS FUNCTIONS
|
||||
/// returns post from id
|
||||
fn get_post(id: u32) -> Result<Post, DatabaseError> {
|
||||
POSTS
|
||||
.get(id.to_be_bytes())?
|
||||
.ok_or(DatabaseError::NotInDb)
|
||||
.and_then(|x| deserialize::<Post>(&x).map_err(|_| DatabaseError::SerError))
|
||||
}
|
||||
|
||||
/// returns thread from id
|
||||
fn get_thread(id: u32) -> Result<Vec<Post>, DatabaseError> {
|
||||
THREADS
|
||||
.get(id.to_be_bytes())?
|
||||
.ok_or(DatabaseError::NotInDb)
|
||||
.and_then(|x| deserialize::<Thread>(&x).map_err(|_| DatabaseError::SerError))?
|
||||
.into_iter()
|
||||
.map(|x| get_post(x))
|
||||
.filter(|x| x.is_ok())
|
||||
.collect()
|
||||
// let thread = THREADS.get(id.to_be_bytes()).and_then()
|
||||
}
|
||||
|
||||
/// generates next id
|
||||
fn next_id() -> Result<u32, DatabaseError> {
|
||||
DB.update_and_fetch(b"id", increment)
|
||||
.map(|x| match x {
|
||||
Some(s) => {
|
||||
let buf: [u8; 4] = (*s).try_into().unwrap();
|
||||
u32::from_be_bytes(buf)
|
||||
}
|
||||
None => 0u32,
|
||||
})
|
||||
.map_err(|e| DatabaseError::from(e))
|
||||
}
|
||||
|
||||
/// lists all threads
|
||||
fn list_threads() -> Result<Vec<Post>, DatabaseError> {
|
||||
THREADS
|
||||
.iter()
|
||||
.take_while(|x| x.is_ok())
|
||||
.map(|x| x.unwrap().0)
|
||||
.map(|x| {
|
||||
POSTS.get(x).map(|x| match x {
|
||||
Some(s) => s,
|
||||
None => {
|
||||
error!("couldn't find thread op among posts");
|
||||
panic!("unrecoverable error");
|
||||
}
|
||||
})
|
||||
})
|
||||
.map(|x| {
|
||||
x.map_err(|e| DatabaseError::from(e))
|
||||
.and_then(|i| deserialize::<Post>(&i).map_err(|_| DatabaseError::SerError))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
// NOTE worst out of everything, but I guess it's not so bad
|
||||
fn add_post(mut post: Post, thread_id: u32) -> Result<(), DatabaseError> {
|
||||
let id = next_id()?;
|
||||
post.id = id;
|
||||
let mut thread = deserialize::<Thread>(
|
||||
&THREADS
|
||||
.get(thread_id.to_be_bytes())?
|
||||
.ok_or(DatabaseError::NotInDb)?,
|
||||
)
|
||||
.map_err(|_| DatabaseError::SerError)?;
|
||||
thread.push(post.id);
|
||||
let thread = serialize(&thread).map_err(|_| DatabaseError::SerError)?;
|
||||
THREADS.insert(thread_id.to_be_bytes(), thread)?;
|
||||
|
||||
POSTS.insert(
|
||||
id.to_be_bytes(),
|
||||
serialize(&post).map_err(|_| DatabaseError::SerError)?,
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn delete_post(id: u32) -> Result<(), DatabaseError> {
|
||||
POSTS.remove(id.to_be_bytes())?;
|
||||
if THREADS.contains_key(id.to_be_bytes())? {
|
||||
THREADS.remove(id.to_be_bytes())?;
|
||||
} else {
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn add_thread(mut post: Post) -> Result<(), DatabaseError> {
|
||||
let id = next_id()?;
|
||||
let thread = Thread::new(id);
|
||||
post.id = id;
|
||||
THREADS.insert(
|
||||
id.to_be_bytes(),
|
||||
serialize(&thread).map_err(|_| DatabaseError::SerError)?,
|
||||
)?;
|
||||
POSTS.insert(
|
||||
id.to_be_bytes(),
|
||||
serialize(&post).map_err(|_| DatabaseError::SerError)?,
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum DatabaseError {
|
||||
#[error("failed to read from database")]
|
||||
SledError(#[from] sled::Error),
|
||||
#[error("not found in database")]
|
||||
NotInDb,
|
||||
#[error("serialization error")]
|
||||
SerError,
|
||||
}
|
||||
|
||||
impl From<DatabaseError> for Status {
|
||||
fn from(err: DatabaseError) -> Self {
|
||||
match err {
|
||||
DatabaseError::SledError(_) => Status::InternalServerError,
|
||||
DatabaseError::NotInDb => Status::BadRequest,
|
||||
DatabaseError::SerError => Status::InternalServerError,
|
||||
}
|
||||
}
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>kchan - {name}</title>
|
||||
<link rel="icon" type="image/x-icon" href="favicon.ico">
|
||||
<link rel="stylesheet" href="style.css">
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
<div class="wrap">
|
||||
<nav>
|
||||
<span>kchan –</span>
|
||||
<a href="/">index</a>
|
||||
<a href="faq.html">faq</a>
|
||||
<a href="new-thread.html">new thread</a>
|
||||
</nav>
|
||||
</div>
|
||||
</header>
|
||||
<main>
|
||||
<div class="wrap">
|
||||
<h1>FAQ</h1>
|
||||
</div>
|
||||
</main>
|
||||
</head>
|
||||
</html>
|
Loading…
Reference in New Issue