This commit is contained in:
Кобелев Андрей Андреевич 2022-10-13 10:11:44 +03:00
parent 6637e70b03
commit 6fb20ef6d7
3 changed files with 18 additions and 20 deletions

View File

@ -23,12 +23,12 @@ pub type ColorResult = Result<Color, Error>;
// Сервис базы данных // Сервис базы данных
pub struct DbService { pub struct Service {
db: rusqlite::Connection, db: rusqlite::Connection,
} }
impl DbService { impl Service {
// Создание сервиса // Создание сервиса
pub fn new() -> DbService { pub fn new() -> Service {
let conn = Connection::open("cats.db"); let conn = Connection::open("cats.db");
let db_con = match conn { let db_con = match conn {
Ok(conn) => conn, Ok(conn) => conn,
@ -73,7 +73,7 @@ impl DbService {
.ok(); .ok();
} }
} }
return DbService { db: db_con }; return Service { db: db_con };
} }
// Получение кошек // Получение кошек
pub fn get_cats(&self) -> CatsResult { pub fn get_cats(&self) -> CatsResult {

View File

@ -2,8 +2,8 @@ pub mod db;
use actix_web::{get,post, web, HttpResponse, Responder, Result}; use actix_web::{get,post, web, HttpResponse, Responder, Result};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
pub struct LoyaltyService { pub struct Service {
pub db: db::DbService, pub db: db::Service,
} }
#[derive(Serialize)] #[derive(Serialize)]
@ -18,7 +18,7 @@ pub struct AddColorRequest {
} }
#[get("/cats")] #[get("/cats")]
pub async fn get_cats(ctx: web::Data<LoyaltyService>) -> Result<impl Responder> { pub async fn get_cats(ctx: web::Data<Service>) -> Result<impl Responder> {
let cats = ctx.db.get_cats(); let cats = ctx.db.get_cats();
let res = match cats { let res = match cats {
Ok(v) => ArrayResponse{ result: v }, Ok(v) => ArrayResponse{ result: v },
@ -28,7 +28,7 @@ pub async fn get_cats(ctx: web::Data<LoyaltyService>) -> Result<impl Responder>
} }
#[get("/colors")] #[get("/colors")]
pub async fn get_colors(ctx: web::Data<LoyaltyService>) -> Result<impl Responder> { pub async fn get_colors(ctx: web::Data<Service>) -> Result<impl Responder> {
let colors = ctx.db.get_colors(); let colors = ctx.db.get_colors();
let res = match colors { let res = match colors {
Ok(v) => ArrayResponse { result: v }, Ok(v) => ArrayResponse { result: v },
@ -46,7 +46,7 @@ pub struct AddCatRequest {
#[post("/add/cat")] #[post("/add/cat")]
pub async fn add_cat( pub async fn add_cat(
ctx: web::Data<LoyaltyService>, ctx: web::Data<Service>,
cat: web::Json<AddCatRequest>, cat: web::Json<AddCatRequest>,
) -> Result<impl Responder> { ) -> Result<impl Responder> {
let _cat = cat.into_inner(); let _cat = cat.into_inner();
@ -60,7 +60,7 @@ pub async fn add_cat(
#[post("/add/color")] #[post("/add/color")]
pub async fn add_color( pub async fn add_color(
ctx: web::Data<LoyaltyService>, ctx: web::Data<Service>,
cat: web::Json<AddColorRequest>, cat: web::Json<AddColorRequest>,
) -> Result<impl Responder> { ) -> Result<impl Responder> {
let _color = cat.into_inner(); let _color = cat.into_inner();

View File

@ -1,13 +1,11 @@
use config::Config; use config::Config;
mod cfg; mod cfg;
mod loyalty; mod cats;
use actix_web::{middleware::Logger, web, App, HttpServer};
use actix_files::Files; use actix_files::Files;
use actix_web::{web, App, HttpServer};
#[actix_web::main] #[actix_web::main]
pub async fn main() -> std::io::Result<()> { pub async fn main() -> std::io::Result<()> {
let settings = Config::builder() let settings = Config::builder()
.add_source(config::File::with_name("settings.yml")) .add_source(config::File::with_name("settings.yml"))
.build() .build()
@ -22,13 +20,13 @@ pub async fn main() -> std::io::Result<()> {
HttpServer::new(|| { HttpServer::new(|| {
App::new() App::new()
.app_data(web::Data::new(loyalty::LoyaltyService { .app_data(web::Data::new(cats::Service {
db: loyalty::db::DbService::new(), db: cats::db::Service::new(),
})) }))
.service(loyalty::get_cats) .service(cats::get_cats)
.service(loyalty::get_colors) .service(cats::get_colors)
.service(loyalty::add_cat) .service(cats::add_cat)
.service(loyalty::add_color) .service(cats::add_color)
.service(Files::new("/", "./static/").index_file("index.html")) .service(Files::new("/", "./static/").index_file("index.html"))
}) })
.bind((app.host, app.port))? .bind((app.host, app.port))?