refactoring
This commit is contained in:
@@ -24,9 +24,8 @@ pub struct TypUpdateInput {
|
||||
|
||||
impl Typ {
|
||||
pub async fn read_one(pool: &PgPool, id: &i32) -> Result<Typ> {
|
||||
let row = sqlx::query_as!(Typ, "SELECT * FROM typen WHERE id = $1", id)
|
||||
.fetch_one(pool)
|
||||
.await?;
|
||||
const QUERY: &str = "SELECT * FROM typen WHERE id = $1";
|
||||
let row: Typ = sqlx::query_as(QUERY).bind(id).fetch_one(pool).await?;
|
||||
|
||||
Ok(row)
|
||||
}
|
||||
@@ -39,37 +38,36 @@ impl Typ {
|
||||
Ok(rows)
|
||||
}
|
||||
|
||||
pub async fn create(pool: &PgPool, input: TypCreateInput) -> Result<Typ> {
|
||||
let row = sqlx::query!(
|
||||
"INSERT INTO typen(name) VALUES ($1) RETURNING id",
|
||||
input.name
|
||||
)
|
||||
.fetch_one(pool)
|
||||
.await?;
|
||||
pub async fn create(pool: &PgPool, input: &TypCreateInput) -> Result<Typ> {
|
||||
const QUERY: &str = "INSERT INTO typen (name) VALUES ($1) RETURNING id, name";
|
||||
|
||||
let result = Self::read_one(pool, &row.id).await?;
|
||||
let result: Typ = sqlx::query_as(QUERY)
|
||||
.bind(&input.name)
|
||||
.fetch_one(pool)
|
||||
.await?;
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub async fn create_many(pool: &PgPool, input: &[TypCreateInput]) -> Result<Typ> {
|
||||
pub async fn create_many(pool: &PgPool, input: &[TypCreateInput]) -> Result<Vec<Typ>> {
|
||||
let mut v1: Vec<&str> = Vec::new();
|
||||
input.iter().for_each(|typ| v1.push(&typ.name));
|
||||
|
||||
let row = sqlx::query_as(
|
||||
let row = sqlx::query_as!(
|
||||
Typ,
|
||||
r#"
|
||||
INSERT INTO typen(name)
|
||||
SELECT * FROM UNNEST($1)
|
||||
SELECT * FROM UNNEST($1::text[])
|
||||
RETURNING id, name"#,
|
||||
&v1 as _
|
||||
)
|
||||
.bind(&v1)
|
||||
.fetch_one(pool)
|
||||
.fetch_all(pool)
|
||||
.await?;
|
||||
|
||||
Ok(row)
|
||||
}
|
||||
|
||||
pub async fn update(pool: &PgPool, input: TypUpdateInput) -> Result<Typ> {
|
||||
pub async fn update(pool: &PgPool, input: &TypUpdateInput) -> Result<Typ> {
|
||||
sqlx::query!(
|
||||
"UPDATE typen SET name=$1 WHERE id = $2",
|
||||
input.name,
|
||||
|
||||
Reference in New Issue
Block a user