update at 2022-03-01 20:01:11 by ehlxr

This commit is contained in:
ehlxr 2022-03-01 20:01:11 +08:00
parent ca43cd5454
commit 0533ca1ca9

View File

@ -2,7 +2,7 @@ use poem::web::Data;
use poem_openapi::{param::Path, payload::Json, OpenApi}; use poem_openapi::{param::Path, payload::Json, OpenApi};
use slab::Slab; use slab::Slab;
use tokio::sync::Mutex; use tokio::sync::RwLock;
use tracing::info; use tracing::info;
use crate::CACHE; use crate::CACHE;
@ -11,7 +11,7 @@ use super::*;
#[derive(Default)] #[derive(Default)]
pub(crate) struct Api { pub(crate) struct Api {
users: Mutex<Slab<User>>, users: RwLock<Slab<User>>,
} }
#[OpenApi] #[OpenApi]
@ -26,7 +26,7 @@ impl Api {
let rt = m_guard.get(&"token".to_string()).unwrap(); let rt = m_guard.get(&"token".to_string()).unwrap();
info!("refresh_token is {}", rt); info!("refresh_token is {}", rt);
let mut users = self.users.lock().await; let mut users = self.users.write().await;
let id = users.insert(user.0) as i64; let id = users.insert(user.0) as i64;
CreateUserResponse::Ok(Json(id)) CreateUserResponse::Ok(Json(id))
} }
@ -34,7 +34,7 @@ impl Api {
/// Find user by id /// Find user by id
#[oai(path = "/users/:user_id", method = "get", tag = "ApiTags::User")] #[oai(path = "/users/:user_id", method = "get", tag = "ApiTags::User")]
async fn find_user(&self, user_id: Path<i64>) -> FindUserResponse { async fn find_user(&self, user_id: Path<i64>) -> FindUserResponse {
let users = self.users.lock().await; let users = self.users.read().await;
match users.get(user_id.0 as usize) { match users.get(user_id.0 as usize) {
Some(user) => FindUserResponse::Ok(Json(user.clone())), Some(user) => FindUserResponse::Ok(Json(user.clone())),
None => FindUserResponse::NotFound, None => FindUserResponse::NotFound,
@ -44,7 +44,7 @@ impl Api {
/// Delete user by id /// Delete user by id
#[oai(path = "/users/:user_id", method = "delete", tag = "ApiTags::User")] #[oai(path = "/users/:user_id", method = "delete", tag = "ApiTags::User")]
async fn delete_user(&self, user_id: Path<i64>) -> DeleteUserResponse { async fn delete_user(&self, user_id: Path<i64>) -> DeleteUserResponse {
let mut users = self.users.lock().await; let mut users = self.users.write().await;
let user_id = user_id.0 as usize; let user_id = user_id.0 as usize;
if users.contains(user_id) { if users.contains(user_id) {
users.remove(user_id); users.remove(user_id);
@ -57,7 +57,7 @@ impl Api {
/// Update user by id /// Update user by id
#[oai(path = "/users/:user_id", method = "put", tag = "ApiTags::User")] #[oai(path = "/users/:user_id", method = "put", tag = "ApiTags::User")]
async fn put_user(&self, user_id: Path<i64>, update: Json<UpdateUser>) -> UpdateUserResponse { async fn put_user(&self, user_id: Path<i64>, update: Json<UpdateUser>) -> UpdateUserResponse {
let mut users = self.users.lock().await; let mut users = self.users.write().await;
match users.get_mut(user_id.0 as usize) { match users.get_mut(user_id.0 as usize) {
Some(user) => { Some(user) => {
if let Some(name) = update.0.name { if let Some(name) = update.0.name {