65 lines
1.5 KiB
Rust
65 lines
1.5 KiB
Rust
use actix_web::{
|
|
body::{BoxBody, MessageBody},
|
|
HttpResponse,
|
|
};
|
|
|
|
use crate::database::{models::User, UserDao};
|
|
use std::cell::RefCell;
|
|
use std::option::Option;
|
|
|
|
pub struct TestUserDao {
|
|
pub user_map: RefCell<Vec<User>>,
|
|
}
|
|
|
|
impl TestUserDao {
|
|
pub fn new() -> Self {
|
|
Self {
|
|
user_map: RefCell::new(Vec::new()),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl UserDao for TestUserDao {
|
|
fn create_user(&mut self, username: &str, password: &str) -> Option<User> {
|
|
let u = User {
|
|
id: (self.user_map.borrow().len() + 1) as i32,
|
|
username: username.to_string(),
|
|
password: password.to_string(),
|
|
};
|
|
|
|
self.user_map.borrow_mut().push(u.clone());
|
|
|
|
Some(u)
|
|
}
|
|
|
|
fn get_user(&mut self, user: &str, pass: &str) -> Option<User> {
|
|
match self
|
|
.user_map
|
|
.borrow()
|
|
.iter()
|
|
.find(|&u| u.username == user && u.password == pass)
|
|
{
|
|
Some(u) => {
|
|
let copy = (*u).clone();
|
|
Some(copy)
|
|
}
|
|
None => None,
|
|
}
|
|
}
|
|
|
|
fn user_exists(&mut self, user: &str) -> bool {
|
|
self.user_map.borrow().iter().any(|u| u.username == user)
|
|
}
|
|
}
|
|
|
|
pub trait BodyReader {
|
|
fn read_to_str(self) -> String;
|
|
}
|
|
|
|
impl BodyReader for HttpResponse<BoxBody> {
|
|
fn read_to_str(self) -> String {
|
|
let body = self.into_body().try_into_bytes().unwrap();
|
|
std::str::from_utf8(&body).unwrap().to_string()
|
|
}
|
|
}
|