From 6fb20ef6d792d2fe7cdfe8a05938d6268fb985f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=BE=D0=B1=D0=B5=D0=BB=D0=B5=D0=B2=20=D0=90=D0=BD?= =?UTF-8?q?=D0=B4=D1=80=D0=B5=D0=B9=20=D0=90=D0=BD=D0=B4=D1=80=D0=B5=D0=B5?= =?UTF-8?q?=D0=B2=D0=B8=D1=87?= Date: Thu, 13 Oct 2022 10:11:44 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=98=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/{loyalty => cats}/db/mod.rs | 8 ++++---- src/{loyalty => cats}/mod.rs | 12 ++++++------ src/main.rs | 18 ++++++++---------- 3 files changed, 18 insertions(+), 20 deletions(-) rename src/{loyalty => cats}/db/mod.rs (97%) rename src/{loyalty => cats}/mod.rs (84%) diff --git a/src/loyalty/db/mod.rs b/src/cats/db/mod.rs similarity index 97% rename from src/loyalty/db/mod.rs rename to src/cats/db/mod.rs index 21c398e..608a82a 100644 --- a/src/loyalty/db/mod.rs +++ b/src/cats/db/mod.rs @@ -23,12 +23,12 @@ pub type ColorResult = Result; // Сервис базы данных -pub struct DbService { +pub struct Service { db: rusqlite::Connection, } -impl DbService { +impl Service { // Создание сервиса - pub fn new() -> DbService { + pub fn new() -> Service { let conn = Connection::open("cats.db"); let db_con = match conn { Ok(conn) => conn, @@ -73,7 +73,7 @@ impl DbService { .ok(); } } - return DbService { db: db_con }; + return Service { db: db_con }; } // Получение кошек pub fn get_cats(&self) -> CatsResult { diff --git a/src/loyalty/mod.rs b/src/cats/mod.rs similarity index 84% rename from src/loyalty/mod.rs rename to src/cats/mod.rs index 65594e6..2b018f6 100644 --- a/src/loyalty/mod.rs +++ b/src/cats/mod.rs @@ -2,8 +2,8 @@ pub mod db; use actix_web::{get,post, web, HttpResponse, Responder, Result}; use serde::{Deserialize, Serialize}; -pub struct LoyaltyService { - pub db: db::DbService, +pub struct Service { + pub db: db::Service, } #[derive(Serialize)] @@ -18,7 +18,7 @@ pub struct AddColorRequest { } #[get("/cats")] -pub async fn get_cats(ctx: web::Data) -> Result { +pub async fn get_cats(ctx: web::Data) -> Result { let cats = ctx.db.get_cats(); let res = match cats { Ok(v) => ArrayResponse{ result: v }, @@ -28,7 +28,7 @@ pub async fn get_cats(ctx: web::Data) -> Result } #[get("/colors")] -pub async fn get_colors(ctx: web::Data) -> Result { +pub async fn get_colors(ctx: web::Data) -> Result { let colors = ctx.db.get_colors(); let res = match colors { Ok(v) => ArrayResponse { result: v }, @@ -46,7 +46,7 @@ pub struct AddCatRequest { #[post("/add/cat")] pub async fn add_cat( - ctx: web::Data, + ctx: web::Data, cat: web::Json, ) -> Result { let _cat = cat.into_inner(); @@ -60,7 +60,7 @@ pub async fn add_cat( #[post("/add/color")] pub async fn add_color( - ctx: web::Data, + ctx: web::Data, cat: web::Json, ) -> Result { let _color = cat.into_inner(); diff --git a/src/main.rs b/src/main.rs index e767b88..43fb783 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,11 @@ use config::Config; mod cfg; -mod loyalty; -use actix_web::{middleware::Logger, web, App, HttpServer}; +mod cats; use actix_files::Files; - +use actix_web::{web, App, HttpServer}; #[actix_web::main] pub async fn main() -> std::io::Result<()> { - let settings = Config::builder() .add_source(config::File::with_name("settings.yml")) .build() @@ -22,13 +20,13 @@ pub async fn main() -> std::io::Result<()> { HttpServer::new(|| { App::new() - .app_data(web::Data::new(loyalty::LoyaltyService { - db: loyalty::db::DbService::new(), + .app_data(web::Data::new(cats::Service { + db: cats::db::Service::new(), })) - .service(loyalty::get_cats) - .service(loyalty::get_colors) - .service(loyalty::add_cat) - .service(loyalty::add_color) + .service(cats::get_cats) + .service(cats::get_colors) + .service(cats::add_cat) + .service(cats::add_color) .service(Files::new("/", "./static/").index_file("index.html")) }) .bind((app.host, app.port))?