mirror of
https://github.com/n08i40k/schedule-parser-rusted.git
synced 2025-12-06 17:57:47 +03:00
0.7.0
Добавлена OpenAPI документация эндпоинтов и структур с интерфейсом RapiDoc. Добавлены derive макросы для преобразования структуры в HttpResponse с помощью ResponderJson и IResponse<T> с помощью IntoIResponse. Ревью кода эндпоинтов связанных с авторизацией. Эндпоинт users/me теперь объект пользователя в требуемом виде.
This commit is contained in:
@@ -3,32 +3,68 @@ use actix_web::error::JsonPayloadError;
|
||||
use actix_web::http::StatusCode;
|
||||
use actix_web::{HttpRequest, HttpResponse, Responder};
|
||||
use serde::{Serialize, Serializer};
|
||||
use utoipa::PartialSchema;
|
||||
|
||||
pub struct IResponse<T: Serialize, E: Serialize>(pub Result<T, E>);
|
||||
pub struct IResponse<T, E>(pub Result<T, E>)
|
||||
where
|
||||
T: Serialize + PartialSchema,
|
||||
E: Serialize + PartialSchema + Clone + HttpStatusCode;
|
||||
|
||||
pub trait ErrorToHttpCode {
|
||||
fn to_http_status_code(&self) -> StatusCode;
|
||||
impl<T, E> Into<Result<T, E>> for IResponse<T, E>
|
||||
where
|
||||
T: Serialize + PartialSchema,
|
||||
E: Serialize + PartialSchema + Clone + HttpStatusCode,
|
||||
{
|
||||
fn into(self) -> Result<T, E> {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Serialize, E: Serialize> IResponse<T, E> {
|
||||
impl<T, E> From<E> for IResponse<T, E>
|
||||
where
|
||||
T: Serialize + PartialSchema,
|
||||
E: Serialize + PartialSchema + Clone + HttpStatusCode,
|
||||
{
|
||||
fn from(value: E) -> Self {
|
||||
IResponse(Err(value))
|
||||
}
|
||||
}
|
||||
|
||||
pub trait HttpStatusCode {
|
||||
fn status_code(&self) -> StatusCode;
|
||||
}
|
||||
|
||||
impl<T, E> IResponse<T, E>
|
||||
where
|
||||
T: Serialize + PartialSchema,
|
||||
E: Serialize + PartialSchema + Clone + HttpStatusCode,
|
||||
{
|
||||
pub fn new(result: Result<T, E>) -> Self {
|
||||
IResponse(result)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Serialize, E: Serialize> Serialize for IResponse<T, E> {
|
||||
impl<T, E> Serialize for IResponse<T, E>
|
||||
where
|
||||
T: Serialize + PartialSchema,
|
||||
E: Serialize + PartialSchema + Clone + HttpStatusCode,
|
||||
{
|
||||
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),
|
||||
Err(err) => serializer.serialize_some::<ResponseError<E>>(&ResponseError::new(err)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Serialize, E: Serialize + ErrorToHttpCode> Responder for IResponse<T, E> {
|
||||
impl<T, E> Responder for IResponse<T, E>
|
||||
where
|
||||
T: Serialize + PartialSchema,
|
||||
E: Serialize + PartialSchema + Clone + HttpStatusCode,
|
||||
{
|
||||
type Body = EitherBody<String>;
|
||||
|
||||
fn respond_to(self, _: &HttpRequest) -> HttpResponse<Self::Body> {
|
||||
@@ -36,7 +72,7 @@ impl<T: Serialize, E: Serialize + ErrorToHttpCode> Responder for IResponse<T, E>
|
||||
Ok(body) => {
|
||||
let code = match &self.0 {
|
||||
Ok(_) => StatusCode::OK,
|
||||
Err(e) => e.to_http_status_code(),
|
||||
Err(e) => e.status_code(),
|
||||
};
|
||||
|
||||
match HttpResponse::build(code)
|
||||
@@ -55,24 +91,50 @@ impl<T: Serialize, E: Serialize + ErrorToHttpCode> Responder for IResponse<T, E>
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, utoipa::ToSchema)]
|
||||
pub struct ResponseError<T: Serialize + PartialSchema> {
|
||||
code: T,
|
||||
}
|
||||
|
||||
impl<T: Serialize + PartialSchema + Clone> ResponseError<T> {
|
||||
fn new(status_code: &T) -> Self {
|
||||
ResponseError {
|
||||
code: status_code.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub mod user {
|
||||
use crate::database::models::{User, UserRole};
|
||||
use actix_macros::{IntoIResponse, ResponderJson};
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Serialize)]
|
||||
#[derive(Serialize, utoipa::ToSchema, IntoIResponse, ResponderJson)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct ResponseOk {
|
||||
pub struct UserResponse {
|
||||
#[schema(examples("67dcc9a9507b0000772744a2"))]
|
||||
id: String,
|
||||
|
||||
#[schema(examples("n08i40k"))]
|
||||
username: String,
|
||||
|
||||
#[schema(examples("ИС-214/23"))]
|
||||
group: String,
|
||||
|
||||
role: UserRole,
|
||||
|
||||
#[schema(examples(498094647, json!(null)))]
|
||||
vk_id: Option<i32>,
|
||||
|
||||
#[schema(examples(
|
||||
"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjY3ZGNjOWE5NTA3YjAwMDA3NzI3NDRhMiIsImlhdCI6IjE3NDMxMDgwOTkiLCJleHAiOiIxODY5MjUyMDk5In0.rMgXRb3JbT9AvLK4eiY9HMB5LxgUudkpQyoWKOypZFY"
|
||||
))]
|
||||
access_token: String,
|
||||
}
|
||||
|
||||
impl ResponseOk {
|
||||
pub fn from_user(user: &User) -> Self {
|
||||
ResponseOk {
|
||||
impl From<&User> for UserResponse {
|
||||
fn from(user: &User) -> Self {
|
||||
UserResponse {
|
||||
id: user.id.clone(),
|
||||
username: user.username.clone(),
|
||||
group: user.group.clone(),
|
||||
@@ -82,4 +144,17 @@ pub mod user {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<User> for UserResponse {
|
||||
fn from(user: User) -> Self {
|
||||
UserResponse {
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
group: user.group,
|
||||
role: user.role,
|
||||
vk_id: user.vk_id,
|
||||
access_token: user.access_token,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user