feat: add example responses for user retrieval and creation endpoints
This commit is contained in:
parent
d654ba0f0f
commit
9baa24cbc7
1 changed files with 31 additions and 8 deletions
|
@ -1,6 +1,6 @@
|
||||||
use crate::{Database, db::entity, error::ApiError};
|
use crate::{Database, db::entity, error::ApiError};
|
||||||
use actix_web::{Responder, delete, get, post, put, web};
|
|
||||||
use actix_web::error::ErrorInternalServerError;
|
use actix_web::error::ErrorInternalServerError;
|
||||||
|
use actix_web::{Responder, delete, get, post, put, web};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use utoipa::ToSchema;
|
use utoipa::ToSchema;
|
||||||
use validator::Validate;
|
use validator::Validate;
|
||||||
|
@ -33,7 +33,19 @@ pub struct CreateUser {
|
||||||
summary = "Get all users",
|
summary = "Get all users",
|
||||||
description = "Retrieve a list of all users",
|
description = "Retrieve a list of all users",
|
||||||
responses(
|
responses(
|
||||||
(status = 200, description = "List of users retrieved successfully", body = Vec<entity::user::Model>, content_type = "application/json"),
|
(status = 200, description = "List of users retrieved successfully", body = Vec<entity::user::Model>, content_type = "application/json",
|
||||||
|
example = json!([
|
||||||
|
{
|
||||||
|
"id": "831195d1-01c4-4029-8284-349f5c41e398",
|
||||||
|
"username": "MyAwesomeUsername",
|
||||||
|
"name": "My Awesome Name",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "0024870c-ea5c-4927-802f-8e44fc57b098",
|
||||||
|
"username": "AnotherUser",
|
||||||
|
"name": "Another User",
|
||||||
|
}
|
||||||
|
])),
|
||||||
(status = 500, description = "Internal server error", body = String, content_type = "application/json")
|
(status = 500, description = "Internal server error", body = String, content_type = "application/json")
|
||||||
)
|
)
|
||||||
)]
|
)]
|
||||||
|
@ -55,7 +67,12 @@ async fn get_users(
|
||||||
("id" = String, Path, description = "User ID")
|
("id" = String, Path, description = "User ID")
|
||||||
),
|
),
|
||||||
responses(
|
responses(
|
||||||
(status = 200, description = "User retrieved successfully", body = entity::user::Model, content_type = "application/json"),
|
(status = 200, description = "User retrieved successfully", body = entity::user::Model, content_type = "application/json",
|
||||||
|
example = json!({
|
||||||
|
"id": "831195d1-01c4-4029-8284-349f5c41e398",
|
||||||
|
"username": "MyAwesomeUsername",
|
||||||
|
"name": "My Awesome Name",
|
||||||
|
})),
|
||||||
(status = 404, description = "User not found", body = String, content_type = "application/json"),
|
(status = 404, description = "User not found", body = String, content_type = "application/json"),
|
||||||
(status = 500, description = "Internal server error", body = String, content_type = "application/json")
|
(status = 500, description = "Internal server error", body = String, content_type = "application/json")
|
||||||
)
|
)
|
||||||
|
@ -78,14 +95,18 @@ async fn get_user(
|
||||||
description = "Create a new user with username, name, and password",
|
description = "Create a new user with username, name, and password",
|
||||||
request_body = CreateUser,
|
request_body = CreateUser,
|
||||||
responses(
|
responses(
|
||||||
(status = 200, description = "User created successfully", body = entity::user::Model, content_type = "application/json"),
|
(status = 200, description = "User created successfully", body = entity::user::Model, content_type = "application/json",
|
||||||
|
example = json!({
|
||||||
|
"id": "831195d1-01c4-4029-8284-349f5c41e398",
|
||||||
|
"username": "MyAwesomeUsername",
|
||||||
|
"name": "My Awesome Name",
|
||||||
|
})),
|
||||||
(status = 400, description = "Invalid request data or validation error", body = String, content_type = "application/json"),
|
(status = 400, description = "Invalid request data or validation error", body = String, content_type = "application/json"),
|
||||||
(status = 409, description = "User already exists", body = String, content_type = "application/json"),
|
(status = 409, description = "User already exists", body = String, content_type = "application/json", example = "User with username - MyAwesomeUsername - already exists"),
|
||||||
(status = 500, description = "Internal server error", body = String, content_type = "application/json")
|
(status = 500, description = "Internal server error", body = String, content_type = "application/json")
|
||||||
)
|
)
|
||||||
)]
|
)]
|
||||||
#[post("")]
|
#[post("")]
|
||||||
// TODO: if a user with the same username already exists, return 409 Conflict
|
|
||||||
async fn create_user(
|
async fn create_user(
|
||||||
db: web::Data<Database>,
|
db: web::Data<Database>,
|
||||||
user: web::Json<CreateUser>,
|
user: web::Json<CreateUser>,
|
||||||
|
@ -105,7 +126,9 @@ async fn create_user(
|
||||||
if e.to_string().contains("user_username_key") {
|
if e.to_string().contains("user_username_key") {
|
||||||
Err(ApiError::UserAlreadyExists(username))
|
Err(ApiError::UserAlreadyExists(username))
|
||||||
} else {
|
} else {
|
||||||
Err(ApiError::InternalServerError("/user/ - create_user - Error: {e}".to_owned()))
|
Err(ApiError::InternalServerError(
|
||||||
|
"/user/ - create_user - Error: {e}".to_owned(),
|
||||||
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue