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,
}
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 {

View File

@ -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<LoyaltyService>) -> Result<impl Responder> {
pub async fn get_cats(ctx: web::Data<Service>) -> Result<impl Responder> {
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<LoyaltyService>) -> Result<impl Responder>
}
#[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 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<LoyaltyService>,
ctx: web::Data<Service>,
cat: web::Json<AddCatRequest>,
) -> Result<impl Responder> {
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<LoyaltyService>,
ctx: web::Data<Service>,
cat: web::Json<AddColorRequest>,
) -> Result<impl Responder> {
let _color = cat.into_inner();

View File

@ -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))?