Compare commits
3 Commits
main
...
BLOG-56_al
Author | SHA1 | Date | |
---|---|---|---|
![]() |
b1b8fab108 | ||
![]() |
1dc8ca742b | ||
![]() |
9dcefe90c5 |
@ -1,4 +1,4 @@
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
use std::collections::HashMap;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use chrono::{DateTime, Utc};
|
||||
@ -16,11 +16,11 @@ use super::{
|
||||
};
|
||||
|
||||
pub struct PostDbServiceImpl {
|
||||
db_pool: Arc<Pool<Postgres>>,
|
||||
db_pool: Pool<Postgres>,
|
||||
}
|
||||
|
||||
impl PostDbServiceImpl {
|
||||
pub fn new(db_pool: Arc<Pool<Postgres>>) -> Self {
|
||||
pub fn new(db_pool: Pool<Postgres>) -> Self {
|
||||
Self { db_pool }
|
||||
}
|
||||
}
|
||||
@ -58,7 +58,7 @@ impl PostDbService for PostDbServiceImpl {
|
||||
|
||||
let records = query_builder
|
||||
.build_query_as::<PostInfoWithLabelRecord>()
|
||||
.fetch_all(&*self.db_pool)
|
||||
.fetch_all(&self.db_pool)
|
||||
.await
|
||||
.map_err(|err| PostError::DatabaseError(err.to_string()))?;
|
||||
|
||||
@ -121,7 +121,7 @@ impl PostDbService for PostDbServiceImpl {
|
||||
|
||||
let records = query_builder
|
||||
.build_query_as::<PostWithLabelRecord>()
|
||||
.fetch_all(&*self.db_pool)
|
||||
.fetch_all(&self.db_pool)
|
||||
.await
|
||||
.map_err(|err| PostError::DatabaseError(err.to_string()))?;
|
||||
|
||||
|
@ -1,5 +1,3 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use actix_web::{HttpResponse, Responder, web};
|
||||
|
||||
use crate::{
|
||||
@ -8,12 +6,15 @@ use crate::{
|
||||
};
|
||||
|
||||
pub fn configure_post_routes(cfg: &mut web::ServiceConfig) {
|
||||
cfg.service(web::resource("/post_info").route(web::get().to(get_all_post_info)));
|
||||
cfg.service(web::resource("/post/{id}").route(web::get().to(get_full_post)));
|
||||
cfg.service(
|
||||
web::scope("/post")
|
||||
.route("/all", web::get().to(get_all_post_info_handler))
|
||||
.route("/{id}", web::get().to(get_full_post_handler)),
|
||||
);
|
||||
}
|
||||
|
||||
async fn get_all_post_info(
|
||||
post_controller: web::Data<Arc<dyn PostController>>,
|
||||
async fn get_all_post_info_handler(
|
||||
post_controller: web::Data<dyn PostController>,
|
||||
query: web::Query<PostQueryDto>,
|
||||
) -> impl Responder {
|
||||
let is_published_only = query.is_published_only.unwrap_or_else(|| true);
|
||||
@ -28,8 +29,8 @@ async fn get_all_post_info(
|
||||
}
|
||||
}
|
||||
|
||||
async fn get_full_post(
|
||||
post_controller: web::Data<Arc<dyn PostController>>,
|
||||
async fn get_full_post_handler(
|
||||
post_controller: web::Data<dyn PostController>,
|
||||
path: web::Path<i32>,
|
||||
) -> impl Responder {
|
||||
let id = path.into_inner();
|
||||
|
@ -18,8 +18,8 @@ pub struct Container {
|
||||
}
|
||||
|
||||
impl Container {
|
||||
pub fn new(db_pool: Arc<Pool<Postgres>>) -> Self {
|
||||
let post_db_service = Arc::new(PostDbServiceImpl::new(db_pool));
|
||||
pub fn new(db_pool: Pool<Postgres>) -> Self {
|
||||
let post_db_service = Arc::new(PostDbServiceImpl::new(db_pool.clone()));
|
||||
|
||||
let post_repository = Arc::new(PostRepositoryImpl::new(post_db_service.clone()));
|
||||
|
||||
|
@ -7,7 +7,7 @@ use actix_web::{
|
||||
use post::framework::web::post_web_routes::configure_post_routes;
|
||||
use server::container::Container;
|
||||
use sqlx::{Pool, Postgres, postgres::PgPoolOptions};
|
||||
use std::{env, sync::Arc};
|
||||
use std::env;
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
@ -22,7 +22,7 @@ async fn main() -> std::io::Result<()> {
|
||||
.await
|
||||
}
|
||||
|
||||
async fn init_database() -> Arc<Pool<Postgres>> {
|
||||
async fn init_database() -> Pool<Postgres> {
|
||||
let database_url = env::var("DATABASE_URL")
|
||||
.unwrap_or_else(|_| "postgres://postgres@localhost:5432/postgres".to_string());
|
||||
|
||||
@ -37,11 +37,11 @@ async fn init_database() -> Arc<Pool<Postgres>> {
|
||||
.await
|
||||
.expect("Failed to run database migrations");
|
||||
|
||||
Arc::new(db_pool)
|
||||
db_pool
|
||||
}
|
||||
|
||||
fn create_app(
|
||||
db_pool: Arc<Pool<Postgres>>,
|
||||
db_pool: Pool<Postgres>,
|
||||
) -> App<
|
||||
impl ServiceFactory<
|
||||
ServiceRequest,
|
||||
@ -51,10 +51,9 @@ fn create_app(
|
||||
Error = Error,
|
||||
>,
|
||||
> {
|
||||
let container = Container::new(db_pool.clone());
|
||||
let container = Container::new(db_pool);
|
||||
|
||||
App::new()
|
||||
.app_data(web::Data::new(db_pool))
|
||||
.app_data(web::Data::new(container.post_controller))
|
||||
.app_data(web::Data::from(container.post_controller))
|
||||
.configure(configure_post_routes)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user