Better file org
This commit is contained in:
0
src/api/mod.rs
Normal file
0
src/api/mod.rs
Normal file
0
src/api/product_controller.rs
Normal file
0
src/api/product_controller.rs
Normal file
0
src/config/mod.rs
Normal file
0
src/config/mod.rs
Normal file
0
src/models/mod.rs
Normal file
0
src/models/mod.rs
Normal file
0
src/models/product.rs
Normal file
0
src/models/product.rs
Normal file
@@ -1,5 +0,0 @@
|
|||||||
mod model;
|
|
||||||
mod routes;
|
|
||||||
|
|
||||||
pub use model::Product;
|
|
||||||
pub use routes::init_routes;
|
|
||||||
@@ -1,52 +0,0 @@
|
|||||||
use crate::api_error::ApiError;
|
|
||||||
use crate::db;
|
|
||||||
use crate::schema::products;
|
|
||||||
use chrono::{NaiveDateTime, Utc};
|
|
||||||
use diesel::prelude::*;
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, AsChangeset)]
|
|
||||||
#[table_name = "products"]
|
|
||||||
pub struct ProductMessage {
|
|
||||||
pub productid: i32,
|
|
||||||
pub productname: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Queryable, Insertable)]
|
|
||||||
#[table_name = "products"]
|
|
||||||
pub struct Product {
|
|
||||||
pub productid: i32,
|
|
||||||
pub productname: String,
|
|
||||||
pub created_at: NaiveDateTime
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Product {
|
|
||||||
pub fn find_all() -> Result<Vec<Self>, ApiError> {
|
|
||||||
let conn = db::connection()?;
|
|
||||||
|
|
||||||
let products = products::table
|
|
||||||
.load::<Product>(&conn)?;
|
|
||||||
|
|
||||||
Ok(products)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn find(id: i32) -> Result<Self, ApiError> {
|
|
||||||
let conn = db::connection()?;
|
|
||||||
|
|
||||||
let product = products::table
|
|
||||||
.filter(products::productid.eq(id))
|
|
||||||
.first(&conn)?;
|
|
||||||
|
|
||||||
Ok(product)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<ProductMessage> for Product {
|
|
||||||
fn from(product: ProductMessage) -> Self {
|
|
||||||
Product {
|
|
||||||
productid: product.productid,
|
|
||||||
productname: product.productname,
|
|
||||||
created_at: Utc::now().naive_utc(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
// src/product/routes.rs
|
|
||||||
use crate::product::Product;
|
|
||||||
use actix_web::{get, post, web, HttpResponse, Responder};
|
|
||||||
|
|
||||||
#[get("/products")]
|
|
||||||
async fn find_all() -> impl Responder {
|
|
||||||
let products = Product::find_all().expect("Error fetching all Products");
|
|
||||||
HttpResponse::Ok().json(products)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[get("/product/{id}")]
|
|
||||||
async fn find(id: web::Path<i32>) -> impl Responder {
|
|
||||||
let product = Product::find(id.into_inner()).expect("Error fetching Product");
|
|
||||||
HttpResponse::Ok().json(product)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn init_routes(cfg: &mut web::ServiceConfig) {
|
|
||||||
cfg.service(find_all);
|
|
||||||
cfg.service(find);
|
|
||||||
}
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
table! {
|
|
||||||
order_details (orderid, productid) {
|
|
||||||
orderid -> Int4,
|
|
||||||
productid -> Int4,
|
|
||||||
quantity -> Int4,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
table! {
|
|
||||||
orders (orderid) {
|
|
||||||
customer_email -> Text,
|
|
||||||
orderid -> Int4,
|
|
||||||
created_at -> Timestamp,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
table! {
|
|
||||||
products (productid) {
|
|
||||||
productid -> Int4,
|
|
||||||
productname -> Text,
|
|
||||||
created_at -> Timestamp,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
joinable!(order_details -> orders (orderid));
|
|
||||||
joinable!(order_details -> products (productid));
|
|
||||||
|
|
||||||
allow_tables_to_appear_in_same_query!(
|
|
||||||
order_details,
|
|
||||||
orders,
|
|
||||||
products,
|
|
||||||
);
|
|
||||||
0
src/utils/mod.rs
Normal file
0
src/utils/mod.rs
Normal file
Reference in New Issue
Block a user