-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
510 additions
and
123 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
use axum::{ | ||
extract::{Query, State}, | ||
http::StatusCode, | ||
response::IntoResponse, | ||
routing::get, | ||
Router, | ||
}; | ||
use sea_orm::TryIntoModel; | ||
|
||
use app::persistence::blog::{create_blog, search_blogs}; | ||
use app::state::AppState; | ||
use models::params::blog::CreateBlogParams; | ||
use models::queries::blog::BlogQuery; | ||
use models::schemas::blog::{BlogListSchema, BlogSchema}; | ||
|
||
use crate::error::ApiError; | ||
use crate::extractor::{Json, Valid}; | ||
|
||
#[utoipa::path( | ||
post, | ||
path = "", | ||
request_body = CreateBlogParams, | ||
responses( | ||
(status = 201, description = "Blog created", body = BlogSchema), | ||
(status = 400, description = "Bad request", body = ApiErrorResponse), | ||
(status = 422, description = "Validation error", body = ParamsErrorResponse), | ||
(status = 500, description = "Internal server error", body = ApiErrorResponse), | ||
) | ||
)] | ||
async fn blogs_post( | ||
state: State<AppState>, | ||
Valid(Json(params)): Valid<Json<CreateBlogParams>>, | ||
) -> Result<impl IntoResponse, ApiError> { | ||
let blog = create_blog(&state.conn, params) | ||
.await | ||
.map_err(ApiError::from)?; | ||
|
||
let blog = blog.try_into_model().unwrap(); | ||
Ok((StatusCode::CREATED, Json(BlogSchema::from(blog)))) | ||
} | ||
|
||
#[utoipa::path( | ||
get, | ||
path = "", | ||
params( | ||
BlogQuery | ||
), | ||
responses( | ||
(status = 200, description = "List blogs", body = BlogListSchema), | ||
(status = 500, description = "Internal server error", body = ApiErrorResponse), | ||
) | ||
)] | ||
async fn blogs_get( | ||
state: State<AppState>, | ||
query: Option<Query<BlogQuery>>, | ||
) -> Result<impl IntoResponse, ApiError> { | ||
let Query(query) = query.unwrap_or_default(); | ||
|
||
let blogs = search_blogs(&state.conn, query) | ||
.await | ||
.map_err(ApiError::from)?; | ||
Ok(Json(BlogListSchema::from(blogs))) | ||
} | ||
|
||
pub fn create_blog_router() -> Router<AppState> { | ||
Router::new().route("/", get(blogs_get).post(blogs_post)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,18 @@ | ||
use axum::Router; | ||
|
||
pub mod blog; | ||
pub mod root; | ||
pub mod user; | ||
|
||
use app::state::AppState; | ||
use blog::create_blog_router; | ||
use root::create_root_router; | ||
use user::create_user_router; | ||
|
||
pub fn create_router(state: AppState) -> Router { | ||
Router::new() | ||
.nest("/users", create_user_router()) | ||
.nest("/blogs", create_blog_router()) | ||
.nest("/", create_root_router()) | ||
.with_state(state) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use sea_orm::{ActiveModelTrait, ColumnTrait, DbConn, DbErr, EntityTrait, QueryFilter, Set}; | ||
|
||
use models::{domains::blog, params::blog::CreateBlogParams, queries::blog::BlogQuery}; | ||
|
||
pub async fn search_blogs(db: &DbConn, query: BlogQuery) -> Result<Vec<blog::Model>, DbErr> { | ||
blog::Entity::find() | ||
.filter(blog::Column::Title.contains(query.title)) | ||
.all(db) | ||
.await | ||
} | ||
|
||
pub async fn create_blog( | ||
db: &DbConn, | ||
params: CreateBlogParams, | ||
) -> Result<blog::ActiveModel, DbErr> { | ||
blog::ActiveModel { | ||
author_id: Set(params.author_id as i32), | ||
title: Set(params.title), | ||
content: Set(params.content), | ||
..Default::default() | ||
} | ||
.save(db) | ||
.await | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod blog; | ||
pub mod user; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use utoipa::OpenApi; | ||
|
||
use models::params::blog::CreateBlogParams; | ||
use models::schemas::blog::{BlogListSchema, BlogSchema}; | ||
|
||
use api::models::{ApiErrorResponse, ParamsErrorResponse}; | ||
use api::routers::blog::*; | ||
|
||
#[derive(OpenApi)] | ||
#[openapi( | ||
paths(blogs_get, blogs_post), | ||
components(schemas( | ||
CreateBlogParams, | ||
BlogListSchema, | ||
BlogSchema, | ||
ApiErrorResponse, | ||
ParamsErrorResponse, | ||
)) | ||
)] | ||
pub(super) struct BlogApi; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,16 @@ | ||
pub use sea_orm_migration::prelude::*; | ||
|
||
mod m20240101_000001_init; | ||
mod m20240816_160144_blog; | ||
|
||
pub struct Migrator; | ||
|
||
#[async_trait::async_trait] | ||
impl MigratorTrait for Migrator { | ||
fn migrations() -> Vec<Box<dyn MigrationTrait>> { | ||
vec![Box::new(m20240101_000001_init::Migration)] | ||
vec![ | ||
Box::new(m20240101_000001_init::Migration), | ||
Box::new(m20240816_160144_blog::Migration), | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use sea_orm_migration::{prelude::*, schema::*}; | ||
|
||
use super::m20240101_000001_init::User; | ||
|
||
#[derive(DeriveMigrationName)] | ||
pub struct Migration; | ||
|
||
#[async_trait::async_trait] | ||
impl MigrationTrait for Migration { | ||
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { | ||
manager | ||
.create_table( | ||
Table::create() | ||
.table(Blog::Table) | ||
.if_not_exists() | ||
.col(pk_auto(Blog::Id)) | ||
.col(integer(Blog::AuthorId).not_null()) | ||
.foreign_key( | ||
ForeignKey::create() | ||
.name("fk_blog_author_id") | ||
.from(Blog::Table, Blog::AuthorId) | ||
.to(User::Table, User::Id) | ||
.on_update(ForeignKeyAction::NoAction) | ||
.on_delete(ForeignKeyAction::Cascade), | ||
) | ||
.col(ColumnDef::new(Blog::Title).string().not_null()) | ||
.col(ColumnDef::new(Blog::Content).string().not_null()) | ||
.to_owned(), | ||
) | ||
.await | ||
} | ||
|
||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { | ||
manager | ||
.drop_table(Table::drop().table(Blog::Table).to_owned()) | ||
.await | ||
} | ||
} | ||
|
||
#[derive(DeriveIden)] | ||
enum Blog { | ||
Table, | ||
Id, | ||
AuthorId, | ||
Title, | ||
Content, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.0.0 | ||
|
||
use sea_orm::entity::prelude::*; | ||
|
||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] | ||
#[sea_orm(table_name = "blog")] | ||
pub struct Model { | ||
#[sea_orm(primary_key)] | ||
pub id: i32, | ||
pub author_id: i32, | ||
pub title: String, | ||
pub content: String, | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] | ||
pub enum Relation { | ||
#[sea_orm( | ||
belongs_to = "super::user::Entity", | ||
from = "Column::AuthorId", | ||
to = "super::user::Column::Id", | ||
on_update = "NoAction", | ||
on_delete = "Cascade" | ||
)] | ||
User, | ||
} | ||
|
||
impl Related<super::user::Entity> for Entity { | ||
fn to() -> RelationDef { | ||
Relation::User.def() | ||
} | ||
} | ||
|
||
impl ActiveModelBehavior for ActiveModel {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.15 | ||
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.0.0 | ||
|
||
pub mod prelude; | ||
|
||
pub mod blog; | ||
pub mod user; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.15 | ||
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.0.0 | ||
|
||
pub use super::blog::Entity as Blog; | ||
pub use super::user::Entity as User; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
use serde::Deserialize; | ||
use utoipa::ToSchema; | ||
use validator::Validate; | ||
|
||
#[derive(Deserialize, Validate, ToSchema)] | ||
pub struct CreateBlogParams { | ||
pub author_id: u32, | ||
|
||
#[validate(length(min = 2))] | ||
pub title: String, | ||
|
||
#[validate(length(min = 2))] | ||
pub content: String, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod blog; | ||
pub mod user; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
use serde::Deserialize; | ||
use utoipa::IntoParams; | ||
|
||
#[derive(Deserialize, Default, IntoParams)] | ||
#[into_params(style = Form, parameter_in = Query)] | ||
pub struct BlogQuery { | ||
#[param(nullable = true)] | ||
pub title: String, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod blog; | ||
pub mod user; |
Oops, something went wrong.