mirror of
https://github.com/n08i40k/schedule-parser-rusted.git
synced 2025-12-06 17:57:47 +03:00
0.5.0
Возвращёна реализация сериализации в json для IResponse Добавлены типы для экстракции данных из запросов средствами actix-web Добавлен экстрактор для получения пользователя по токену доступа передаваемому в запросе Добавлен макрос для автоматической реализации ResponseError для ошибок экстракторов Добавлен эндпоинт users/me Из главного проекта исключена зависимость actix-http посредством переноса части тестового функционала в отдельный crate
This commit is contained in:
@@ -132,14 +132,15 @@ mod tests {
|
||||
use crate::database::driver;
|
||||
use crate::database::models::{User, UserRole};
|
||||
use crate::routes::auth::sign_in::sign_in_default;
|
||||
use crate::test_env::tests::{static_app_state, test_app, test_app_state, test_env};
|
||||
use crate::test_env::tests::{static_app_state, test_app_state, test_env};
|
||||
use crate::utility;
|
||||
use actix_http::StatusCode;
|
||||
use actix_web::http::StatusCode;
|
||||
use actix_web::dev::ServiceResponse;
|
||||
use actix_web::http::Method;
|
||||
use actix_web::test;
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::fmt::Write;
|
||||
use actix_test::test_app;
|
||||
|
||||
async fn sign_in_client(data: Request) -> ServiceResponse {
|
||||
let app = test_app(test_app_state(), sign_in_default).await;
|
||||
|
||||
@@ -208,11 +208,12 @@ mod tests {
|
||||
use crate::database::models::UserRole;
|
||||
use crate::routes::auth::sign_up::schema::Request;
|
||||
use crate::routes::auth::sign_up::sign_up_default;
|
||||
use crate::test_env::tests::{static_app_state, test_app, test_app_state, test_env};
|
||||
use actix_http::StatusCode;
|
||||
use crate::test_env::tests::{static_app_state, test_app_state, test_env};
|
||||
use actix_web::http::StatusCode;
|
||||
use actix_web::dev::ServiceResponse;
|
||||
use actix_web::http::Method;
|
||||
use actix_web::test;
|
||||
use actix_test::test_app;
|
||||
|
||||
struct SignUpPartial {
|
||||
username: String,
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
pub mod auth;
|
||||
pub mod users;
|
||||
mod schema;
|
||||
|
||||
@@ -2,7 +2,7 @@ use actix_web::body::EitherBody;
|
||||
use actix_web::error::JsonPayloadError;
|
||||
use actix_web::http::StatusCode;
|
||||
use actix_web::{HttpRequest, HttpResponse, Responder};
|
||||
use serde::Serialize;
|
||||
use serde::{Serialize, Serializer};
|
||||
|
||||
pub struct IResponse<T: Serialize, E: Serialize>(pub Result<T, E>);
|
||||
|
||||
@@ -10,11 +10,29 @@ pub trait ErrorToHttpCode {
|
||||
fn to_http_status_code(&self) -> StatusCode;
|
||||
}
|
||||
|
||||
impl<T: Serialize, E: Serialize> IResponse<T, E> {
|
||||
pub fn new(result: Result<T, E>) -> Self {
|
||||
IResponse(result)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Serialize, E: Serialize> Serialize for IResponse<T, E> {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
match &self.0 {
|
||||
Ok(ok) => serializer.serialize_some::<T>(&ok),
|
||||
Err(err) => serializer.serialize_some::<E>(&err),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Serialize, E: Serialize + ErrorToHttpCode> Responder for IResponse<T, E> {
|
||||
type Body = EitherBody<String>;
|
||||
|
||||
fn respond_to(self, _: &HttpRequest) -> HttpResponse<Self::Body> {
|
||||
match serde_json::to_string(&self.0) {
|
||||
match serde_json::to_string(&self) {
|
||||
Ok(body) => {
|
||||
let code = match &self.0 {
|
||||
Ok(_) => StatusCode::OK,
|
||||
|
||||
11
src/routes/users/me.rs
Normal file
11
src/routes/users/me.rs
Normal file
@@ -0,0 +1,11 @@
|
||||
use crate::app_state::AppState;
|
||||
use crate::database::models::User;
|
||||
use crate::extractors::base::SyncExtractor;
|
||||
use actix_web::{HttpResponse, Responder, get, web};
|
||||
|
||||
#[get("/me")]
|
||||
pub async fn me(user: SyncExtractor<User>, app_state: web::Data<AppState>) -> impl Responder {
|
||||
HttpResponse::Ok().json(user.into_inner())
|
||||
}
|
||||
|
||||
mod schema {}
|
||||
1
src/routes/users/mod.rs
Normal file
1
src/routes/users/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod me;
|
||||
Reference in New Issue
Block a user