use crate::models::{User, Post};
pub struct Database {
users: Vec<User>,
posts: Vec<Post>,
next_user_id: u64,
next_post_id: u64,
}
impl Database {
pub fn new() -> Self {
Database {
users: Vec::new(),
posts: Vec::new(),
next_user_id: 1,
next_post_id: 1,
}
}
pub fn create_user(&self, name: &str) -> User {
let id = self.next_user_id;
User {
id,
name: name.to_string(),
email: format!("{}@example.com", name),
}
}
pub fn get_user(&self, id: u64) -> Option<&User> {
self.users.iter().find(|u| u.id == id)
}
pub fn list_posts(&self) -> &[Post] {
&self.posts
}
pub fn create_post(&self, title: &str) -> Post {
let id = self.next_post_id;
Post {
id,
title: title.to_string(),
body: String::new(),
author_id: 1,
}
}
}