use crate::db::Database;
pub trait Handler {
fn handle(&self, path: &str, body: &str, db: &Database) -> String;
}
pub mod home {
use super::*;
pub struct HomeHandler;
impl Handler for HomeHandler {
fn handle(&self, _path: &str, _body: &str, _db: &Database) -> String {
json_response("home", "Welcome to the API")
}
}
}
pub mod create_user {
use super::*;
use crate::models::User;
pub struct CreateUserHandler;
impl Handler for CreateUserHandler {
fn handle(&self, _path: &str, body: &str, db: &Database) -> String {
let user = db.create_user(body);
json_response("user", &user.name)
}
}
}
pub mod get_user {
use super::*;
use crate::models::User;
pub struct GetUserHandler;
impl Handler for GetUserHandler {
fn handle(&self, _path: &str, _body: &str, db: &Database) -> String {
let user = db.get_user(1);
match user {
Some(u) => json_response("user", &u.name),
None => not_found(),
}
}
}
}
pub mod list_posts {
use super::*;
use crate::models::Post;
pub struct ListPostsHandler;
impl Handler for ListPostsHandler {
fn handle(&self, _path: &str, _body: &str, db: &Database) -> String {
let posts = db.list_posts();
json_response("posts", &posts.len().to_string())
}
}
}
pub mod create_post {
use super::*;
use crate::models::Post;
pub struct CreatePostHandler;
impl Handler for CreatePostHandler {
fn handle(&self, _path: &str, body: &str, db: &Database) -> String {
let post = db.create_post(body);
json_response("post", &post.title)
}
}
}
pub fn json_response(key: &str, value: &str) -> String {
format!("{{\"{}\": \"{}\"}}", key, value)
}
pub fn not_found() -> String {
json_response("error", "not found")
}