-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce seeding and basic endpoints
- Loading branch information
Showing
12 changed files
with
229 additions
and
10 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
-- Introduce basic property data | ||
CREATE TABLE IF NOT EXISTS properties | ||
( | ||
id INT PRIMARY KEY, | ||
owner_id INT NOT NULL, | ||
product_id INT NOT NULL, | ||
name VARCHAR(255), | ||
media INT[], | ||
created TIMESTAMPTZ NOT NULL DEFAULT NOW(), | ||
modified TIMESTAMPTZ NOT NULL DEFAULT NOW() | ||
); | ||
|
||
CREATE TABLE IF NOT EXISTS products | ||
( | ||
id INT PRIMARY KEY, | ||
name VARCHAR(255) NOT NULL, | ||
media INT[], | ||
tweakers_id INT[], | ||
ean VARCHAR(255)[], | ||
upc VARCHAR(255)[], | ||
sku VARCHAR(255)[], | ||
created TIMESTAMPTZ NOT NULL DEFAULT NOW(), | ||
modified TIMESTAMPTZ NOT NULL DEFAULT NOW() | ||
); | ||
|
||
INSERT INTO properties (id, owner_id, product_id) VALUES (4, 1, 1) ON CONFLICT DO NOTHING; | ||
INSERT INTO products (id, name, media, tweakers_id, ean, sku) VALUES (1, 'Anker 737 (PowerCore 24k)', ARRAY[1], ARRAY[1855004], ARRAY['0194644098728'], ARRAY['a1289', 'A1289011']) ON CONFLICT DO NOTHING; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
CREATE TABLE IF NOT EXISTS media | ||
( | ||
id INT PRIMARY KEY, | ||
description VARCHAR(255), | ||
url VARCHAR(255) NOT NULL, | ||
created TIMESTAMPTZ NOT NULL DEFAULT NOW() | ||
); | ||
|
||
INSERT INTO media (id, description, url, created) VALUES (1, 'My Cool Property', 'https://media.s-bol.com/VZVXoqVokWkv/z700v2/960x1200.jpg', NOW()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use poem_openapi::Object; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::database::Database; | ||
|
||
#[derive(sqlx::FromRow, Debug, Clone, Serialize, Deserialize, Object)] | ||
pub struct Media { | ||
/// Randomly generated ID | ||
pub id: i32, | ||
/// Alt text for the image | ||
pub description: String, | ||
/// URL of the image | ||
pub url: String, | ||
} | ||
|
||
impl Media { | ||
pub async fn get_by_id(id: i32, database: &Database) -> Result<Self, sqlx::Error> { | ||
let media = sqlx::query_as::<_, Self>("SELECT * FROM media WHERE id = $1") | ||
.bind(id) | ||
.fetch_one(&database.pool) | ||
.await?; | ||
|
||
Ok(media) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
pub mod user_data; | ||
pub mod property; | ||
pub mod product; | ||
pub mod media; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
use poem_openapi::Object; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::database::Database; | ||
|
||
#[derive(sqlx::FromRow, Debug, Clone, Serialize, Deserialize, Object, Default)] | ||
pub struct Product { | ||
/// Cherry Picked Identifier | ||
pub id: i32, | ||
/// Name of the product | ||
pub name: String, | ||
/// Media associated with the product | ||
pub media: Vec<i32>, | ||
/// Tweakers ID | ||
pub tweakers_id: Option<Vec<i32>>, | ||
/// EANs | ||
pub ean: Option<Vec<String>>, | ||
/// UPCs | ||
pub upc: Option<Vec<String>>, | ||
/// SKUs | ||
pub sku: Option<Vec<String>>, | ||
} | ||
|
||
impl Product { | ||
pub async fn get_by_id(id: i32, database: &Database) -> Result<Self, sqlx::Error> { | ||
let product = sqlx::query_as::<_, Self>("SELECT * FROM products WHERE id = $1") | ||
.bind(id) | ||
.fetch_one(&database.pool) | ||
.await?; | ||
|
||
Ok(product) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use poem_openapi::Object; | ||
use serde::{Deserialize, Serialize}; | ||
use sqlx::types::chrono; | ||
|
||
use crate::database::Database; | ||
|
||
#[derive(sqlx::FromRow, Debug, Clone, Serialize, Deserialize, Object, Default)] | ||
pub struct Property { | ||
pub id: i32, | ||
pub owner_id: i32, | ||
pub product_id: i32, | ||
pub name: Option<String>, | ||
pub media: Option<Vec<i32>>, | ||
pub created: chrono::DateTime<chrono::Utc>, | ||
pub modified: chrono::DateTime<chrono::Utc>, | ||
} | ||
|
||
impl Property { | ||
pub async fn get_by_owner_id( | ||
owner_id: i32, | ||
database: &Database, | ||
) -> Result<Vec<Self>, sqlx::Error> { | ||
let properties = sqlx::query_as::<_, Self>("SELECT * FROM properties WHERE owner_id = $1") | ||
.bind(owner_id) | ||
.fetch_all(&database.pool) | ||
.await?; | ||
|
||
Ok(properties) | ||
} | ||
|
||
pub async fn get_by_id(id: i32, database: &Database) -> Result<Self, sqlx::Error> { | ||
let property = sqlx::query_as::<_, Self>("SELECT * FROM properties WHERE id = $1") | ||
.bind(id) | ||
.fetch_one(&database.pool) | ||
.await?; | ||
|
||
Ok(property) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters