Added functionality to added article.
This commit is contained in:
@@ -14,6 +14,7 @@ common = {path = "../common"}
|
||||
axum = "0.8.8"
|
||||
tokio = { version = "1.49.0", default-features = false, features = ["full"] }
|
||||
http = "1.4.0"
|
||||
tower-http = {version = "0.6.8", features = ["cors"]}
|
||||
tracing = "=0.1.44"
|
||||
tracing-subscriber = "0.3.22"
|
||||
|
||||
|
||||
@@ -1 +1,49 @@
|
||||
-- Add migration script here
|
||||
CREATE TABLE origin
|
||||
(
|
||||
id bigint NOT NULL GENERATED ALWAYS AS IDENTITY ( INCREMENT 1 START 0 MINVALUE 0 ),
|
||||
name text NOT NULL,
|
||||
CONSTRAINT origin_id PRIMARY KEY (id)
|
||||
INCLUDE(id)
|
||||
);
|
||||
|
||||
CREATE TABLE article
|
||||
(
|
||||
id bigint NOT NULL GENERATED ALWAYS AS IDENTITY ( INCREMENT 1 START 1 ),
|
||||
name text NOT NULL,
|
||||
bio boolean NOT NULL,
|
||||
origin bigint NOT NULL,
|
||||
description text,
|
||||
ingredients text,
|
||||
CONSTRAINT article_id PRIMARY KEY (id)
|
||||
INCLUDE(id),
|
||||
FOREIGN KEY (origin)
|
||||
REFERENCES origin (id) MATCH SIMPLE
|
||||
ON UPDATE NO ACTION
|
||||
ON DELETE NO ACTION
|
||||
NOT VALID
|
||||
|
||||
);
|
||||
|
||||
CREATE TABLE variant
|
||||
(
|
||||
id bigint NOT NULL GENERATED ALWAYS AS IDENTITY ( INCREMENT 1 START 1 ),
|
||||
article_id bigint NOT NULL,
|
||||
weight text NOT NULL,
|
||||
ean text NOT NULL,
|
||||
name text NOT NULL,
|
||||
description text NOT NULL,
|
||||
ingredients text NOT NULL,
|
||||
CONSTRAINT variant_id PRIMARY KEY (id)
|
||||
INCLUDE(id),
|
||||
FOREIGN KEY (article_id)
|
||||
REFERENCES article (id) MATCH SIMPLE
|
||||
ON UPDATE NO ACTION
|
||||
ON DELETE NO ACTION
|
||||
NOT VALID
|
||||
);
|
||||
|
||||
-- Insert origin options
|
||||
INSERT INTO origin (name) VALUES ('EU');
|
||||
INSERT INTO origin (name) VALUES ('Nicht EU');
|
||||
INSERT INTO origin (name) VALUES ('EU / Nicht EU');
|
||||
|
||||
+26
-5
@@ -7,9 +7,14 @@ use axum::{
|
||||
use axum::extract::State;
|
||||
use dotenv::dotenv;
|
||||
use env_logger::fmt::style::Reset;
|
||||
use common::models::article::Article;
|
||||
use http::{header, Method};
|
||||
use common::models::article::{insert_article, Article};
|
||||
use common::models::origin::Origin;
|
||||
use sqlx::{ postgres::PgPoolOptions, PgPool };
|
||||
use tower_http::cors::{AllowOrigin, Any, CorsLayer};
|
||||
use tracing::info;
|
||||
use http::header::{AUTHORIZATION, ACCEPT, ACCESS_CONTROL_ALLOW_ORIGIN, CONTENT_TYPE, ACCESS_CONTROL_ALLOW_CREDENTIALS};
|
||||
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
@@ -23,24 +28,36 @@ async fn main() {
|
||||
|
||||
// initialize tracing
|
||||
tracing_subscriber::fmt().init();
|
||||
/*
|
||||
tracing_subscriber::fmt()
|
||||
.with_max_level(tracing::Level::INFO)
|
||||
.init();
|
||||
*/
|
||||
|
||||
let pool = PgPoolOptions::new()
|
||||
.max_connections(50)
|
||||
.connect(&database)
|
||||
.await
|
||||
.expect(&format!("Can't connect to database: {}", &database));
|
||||
|
||||
sqlx::migrate!().run(&pool).await.expect("Migrations failed");
|
||||
|
||||
let cors = CorsLayer::new()
|
||||
.allow_methods([Method::GET, Method::POST])
|
||||
.allow_headers([ACCEPT, ACCESS_CONTROL_ALLOW_ORIGIN, CONTENT_TYPE])
|
||||
.allow_origin(AllowOrigin::any());
|
||||
|
||||
// build our application with a route
|
||||
let app = Router::new()
|
||||
// `GET /` goes to `root`
|
||||
.route("/", get(root))
|
||||
// `POST /users` goes to `create_user`
|
||||
.route("/article", post(create_article))
|
||||
.with_state(pool);
|
||||
.with_state(pool)
|
||||
.layer(cors);
|
||||
|
||||
let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();
|
||||
info!("Server started on: {}", &addr);
|
||||
|
||||
// run our app with hyper, listening globally on port 3000
|
||||
let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
|
||||
axum::serve(listener, app).await.unwrap();
|
||||
}
|
||||
|
||||
@@ -54,6 +71,8 @@ async fn create_article(
|
||||
State(pool): State<PgPool>,
|
||||
Json(payload): Json<Article>,
|
||||
) -> (StatusCode, Json<Article>) {
|
||||
info!("Test");
|
||||
|
||||
let article = Article {
|
||||
id: payload.id,
|
||||
name: payload.name,
|
||||
@@ -64,5 +83,7 @@ async fn create_article(
|
||||
variants: payload.variants,
|
||||
};
|
||||
|
||||
insert_article(&article, pool).await;
|
||||
|
||||
(StatusCode::CREATED, Json(article))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user